128], [['mobile'], 'string', 'max' => 45], [['name'], 'string', 'max' => 125], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => '商城id', 'user_id' => '用户id', 'goods_id' => '商品ID', 'num' => '商品数量', 'address' => '收货人地址', 'express_no' => '发货单号', 'express_code' => '物流公司编号', 'send_status' => '0待发货;1已发货;2已完成;', 'mobile' => '收货人手机号码', 'name' => '收货人名称', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'province_id' => '省', 'city_id' => '市', 'district_id' => '区', 'town_id' => '镇', ]; } public function getUser() { return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id', 'nickname', 'avatar_url', 'mobile']); } public function getGoods() { return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'goods_name', 'cover_pic']); } public static function setData(int $mall_id, array $data) { if (!empty($data['id'])) { $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]); if (empty($model)) { self::$static_error = '数据异常'; return false; } } else { $model = new self(); $model->loadDefaultValues(); $model->mall_id = $mall_id; } foreach ($data as $key => $val) { $model->$key = $val; } // dd($model); if (!$model->save()) { self::$static_error = '保存数据失败:' . $model->getErrorMessage(); return false; } return $model; } /** * @name: * @msg: 提货到线下 * @param {int} $mall_id * @param {array} $data user_id:会员id;addr_id:收货地址id;goods_id:商品id;number:提货数量 * @return {*} */ public static function pickOffline(int $mall_id, array $data) { $addr = UserAddress::findOne(['user_id' => $data['user_id'], 'id' => $data['addr_id'], 'status' => StatusEnum::ENABLED]); if (empty($addr)) { self::$static_error = '收货地址不存在'; return false; } $addr_full = $addr->province . $addr->city . $addr->district . $addr->town . $addr->detail; $order_params = [ 'mall_id' => $mall_id, 'user_id' => $data['user_id'], 'goods_id' => $data['goods_id'], 'num' => $data['number'], 'address' => $addr_full, 'express_no' => '', 'express_code' => '', 'send_status' => CloudStockTwoEnum::PICK_ORDER_STATUS0, 'mobile' => $addr->mobile, 'name' => $addr->name, 'province_id' => $addr->province_id, 'city_id' => $addr->city_id, 'district_id' => $addr->district_id, 'town_id' => $addr->town_id, 'status' => StatusEnum::ENABLED, ]; $transaction = \Yii::$app->db->beginTransaction(); $res_order = self::setData($mall_id, $order_params); if (false === $res_order) { $transaction->rollBack(); return false; } $stock_params = [ 'goods_id' => $data['goods_id'], 'number' => $data['number'], 'user_id' => $data['user_id'], 'order_id' => $res_order->id, 'order_type' => CloudStockTwoEnum::STOCK_ORDER_TYPE_REPLENISH, ]; $res_handle_stock = CloudStockTwoGoodsStockLog::stockHandle($mall_id, $stock_params, CloudStockTwoEnum::STOCK_CHANGE_TYPE203); if (false === $res_handle_stock) { $transaction->rollBack(); self::$static_error = CloudStockTwoGoodsStockLog::getStaticError(); return false; } $agent_user = CloudStockTwoAgent::find() ->where(['mall_id' => $mall_id, 'user_id' => $data['user_id'], 'status' => StatusEnum::ENABLED]) ->one(); $agent_user->total_order += 1; $res_agent_user = $agent_user->save(); if (false === $res_agent_user) { $transaction->rollBack(); self::$static_error = $agent_user->getErrorMessage(); return false; } $sync_params = [ 'mall_id' => $mall_id, 'user_id' => $data['user_id'], 'goods_id' => $data['goods_id'], 'num' => $data['number'], ]; $transaction->commit(); // 发起自提事件 $event = new FillOrderPickEvent($mall_id); $data = ['id' => $res_order->id]; \Yii::$app->services->events->dispatch($event, $data, $event->listeners); \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $sync_params, self::class, 'handleSync'); return true; } /** * @name: * @msg: 提货到线下赠送优惠券,异步任务处理 * @param {array} $params * @return {*} */ public static function handleSync(array $params = null) { try { $is_install = MallAddons::isInstall($params['mall_id'], AddonsEnum::ADDONS_NAME_WECHATLIVE); if (!$is_install) { \Yii::error(AddonsEnum::ADDONS_NAME_WECHATLIVE . ' 应用未安装'); \Yii::getLogger()->flush(true); return null; } $res = AddonsCouponUserRelate::buyGoodsGiveCoupon($params['mall_id'], $params['goods_id'], $params['user_id']); return $res; } catch (\Exception $e) { \Yii::warning("giveCoupon:" . $e->getMessage()); return false; } } }