CloudStockTwoAgentOrder.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace addons\CloudStockTwo\common\models;
  3. use addons\CloudStockTwo\common\events\order\FillOrderPickEvent;
  4. use common\enums\StatusEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use common\models\goods\Goods;
  7. use common\models\user\User;
  8. use addons\CloudStockTwo\common\enums\CloudStockTwoEnum;
  9. use addons\Coupon\common\models\AddonsCouponUserRelate;
  10. use common\enums\AddonsEnum;
  11. use common\enums\RabbitMqEnum;
  12. use common\models\mall\MallAddons;
  13. use common\models\user\UserAddress;
  14. use Yii;
  15. /**
  16. * This is the model class for table "{{%addons_cloud_stock_agent_order".
  17. *
  18. * @property int $id
  19. * @property int $mall_id 商城id
  20. * @property int $user_id 用户id
  21. * @property int $goods_id 商品ID
  22. * @property int $num 商品数量
  23. * @property string $address 收货人地址
  24. * @property string $express_no 发货单号
  25. * @property string $express_code 物流公司编号
  26. * @property int $send_status 0待发货;1已发货;2已完成;
  27. * @property string $mobile 收货人手机号码
  28. * @property string $name 收货人名称
  29. * @property int $status 状态[-1:删除;0:禁用;1启用]
  30. * @property int $created_at
  31. * @property int $updated_at
  32. * @property int $province_id
  33. * @property int $city_id
  34. * @property int $district_id
  35. * @property int $town_id
  36. */
  37. class CloudStockTwoAgentOrder extends BaseActiveRecord
  38. {
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public static function tableName()
  43. {
  44. return '{{%addons_cloud_stock_two_agent_order}}';
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function rules()
  50. {
  51. return [
  52. [['mall_id', 'user_id', 'goods_id', 'num', 'send_status', 'status', 'created_at', 'updated_at',
  53. 'province_id','city_id','district_id','town_id'], 'integer'],
  54. [['goods_id', 'name'], 'required'],
  55. [['address', 'express_no', 'express_code'], 'string', 'max' => 128],
  56. [['mobile'], 'string', 'max' => 45],
  57. [['name'], 'string', 'max' => 125],
  58. ];
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function attributeLabels()
  64. {
  65. return [
  66. 'id' => 'ID',
  67. 'mall_id' => '商城id',
  68. 'user_id' => '用户id',
  69. 'goods_id' => '商品ID',
  70. 'num' => '商品数量',
  71. 'address' => '收货人地址',
  72. 'express_no' => '发货单号',
  73. 'express_code' => '物流公司编号',
  74. 'send_status' => '0待发货;1已发货;2已完成;',
  75. 'mobile' => '收货人手机号码',
  76. 'name' => '收货人名称',
  77. 'status' => '状态[-1:删除;0:禁用;1启用]',
  78. 'created_at' => 'Created At',
  79. 'updated_at' => 'Updated At',
  80. 'province_id' => '省',
  81. 'city_id' => '市',
  82. 'district_id' => '区',
  83. 'town_id' => '镇',
  84. ];
  85. }
  86. public function getUser()
  87. {
  88. return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id', 'nickname', 'avatar_url', 'mobile']);
  89. }
  90. public function getGoods()
  91. {
  92. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'goods_name', 'cover_pic']);
  93. }
  94. public static function setData(int $mall_id, array $data)
  95. {
  96. if (!empty($data['id'])) {
  97. $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
  98. if (empty($model)) {
  99. self::$static_error = '数据异常';
  100. return false;
  101. }
  102. } else {
  103. $model = new self();
  104. $model->loadDefaultValues();
  105. $model->mall_id = $mall_id;
  106. }
  107. foreach ($data as $key => $val) {
  108. $model->$key = $val;
  109. }
  110. // dd($model);
  111. if (!$model->save()) {
  112. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  113. return false;
  114. }
  115. return $model;
  116. }
  117. /**
  118. * @name:
  119. * @msg: 提货到线下
  120. * @param {int} $mall_id
  121. * @param {array} $data user_id:会员id;addr_id:收货地址id;goods_id:商品id;number:提货数量
  122. * @return {*}
  123. */
  124. public static function pickOffline(int $mall_id, array $data)
  125. {
  126. $addr = UserAddress::findOne(['user_id' => $data['user_id'], 'id' => $data['addr_id'], 'status' => StatusEnum::ENABLED]);
  127. if (empty($addr)) {
  128. self::$static_error = '收货地址不存在';
  129. return false;
  130. }
  131. $addr_full = $addr->province . $addr->city . $addr->district . $addr->town . $addr->detail;
  132. $order_params = [
  133. 'mall_id' => $mall_id,
  134. 'user_id' => $data['user_id'],
  135. 'goods_id' => $data['goods_id'],
  136. 'num' => $data['number'],
  137. 'address' => $addr_full,
  138. 'express_no' => '',
  139. 'express_code' => '',
  140. 'send_status' => CloudStockTwoEnum::PICK_ORDER_STATUS0,
  141. 'mobile' => $addr->mobile,
  142. 'name' => $addr->name,
  143. 'province_id' => $addr->province_id,
  144. 'city_id' => $addr->city_id,
  145. 'district_id' => $addr->district_id,
  146. 'town_id' => $addr->town_id,
  147. 'status' => StatusEnum::ENABLED,
  148. ];
  149. $transaction = \Yii::$app->db->beginTransaction();
  150. $res_order = self::setData($mall_id, $order_params);
  151. if (false === $res_order) {
  152. $transaction->rollBack();
  153. return false;
  154. }
  155. $stock_params = [
  156. 'goods_id' => $data['goods_id'],
  157. 'number' => $data['number'],
  158. 'user_id' => $data['user_id'],
  159. 'order_id' => $res_order->id,
  160. 'order_type' => CloudStockTwoEnum::STOCK_ORDER_TYPE_REPLENISH,
  161. ];
  162. $res_handle_stock = CloudStockTwoGoodsStockLog::stockHandle($mall_id, $stock_params, CloudStockTwoEnum::STOCK_CHANGE_TYPE203);
  163. if (false === $res_handle_stock) {
  164. $transaction->rollBack();
  165. self::$static_error = CloudStockTwoGoodsStockLog::getStaticError();
  166. return false;
  167. }
  168. $agent_user = CloudStockTwoAgent::find()
  169. ->where(['mall_id' => $mall_id, 'user_id' => $data['user_id'], 'status' => StatusEnum::ENABLED])
  170. ->one();
  171. $agent_user->total_order += 1;
  172. $res_agent_user = $agent_user->save();
  173. if (false === $res_agent_user) {
  174. $transaction->rollBack();
  175. self::$static_error = $agent_user->getErrorMessage();
  176. return false;
  177. }
  178. $sync_params = [
  179. 'mall_id' => $mall_id,
  180. 'user_id' => $data['user_id'],
  181. 'goods_id' => $data['goods_id'],
  182. 'num' => $data['number'],
  183. ];
  184. $transaction->commit();
  185. // 发起自提事件
  186. $event = new FillOrderPickEvent($mall_id);
  187. $data = ['id' => $res_order->id];
  188. \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
  189. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $sync_params, self::class, 'handleSync');
  190. return true;
  191. }
  192. /**
  193. * @name:
  194. * @msg: 提货到线下赠送优惠券,异步任务处理
  195. * @param {array} $params
  196. * @return {*}
  197. */
  198. public static function handleSync(array $params = null)
  199. {
  200. try {
  201. $is_install = MallAddons::isInstall($params['mall_id'], AddonsEnum::ADDONS_NAME_WECHATLIVE);
  202. if (!$is_install) {
  203. \Yii::error(AddonsEnum::ADDONS_NAME_WECHATLIVE . ' 应用未安装');
  204. \Yii::getLogger()->flush(true);
  205. return null;
  206. }
  207. $res = AddonsCouponUserRelate::buyGoodsGiveCoupon($params['mall_id'], $params['goods_id'], $params['user_id']);
  208. return $res;
  209. } catch (\Exception $e) {
  210. \Yii::warning("giveCoupon:" . $e->getMessage());
  211. return false;
  212. }
  213. }
  214. }