| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- namespace addons\CloudStockTwo\common\models;
- use addons\CloudStockTwo\common\events\order\FillOrderPickEvent;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use common\models\user\User;
- use addons\CloudStockTwo\common\enums\CloudStockTwoEnum;
- use addons\Coupon\common\models\AddonsCouponUserRelate;
- use common\enums\AddonsEnum;
- use common\enums\RabbitMqEnum;
- use common\models\mall\MallAddons;
- use common\models\user\UserAddress;
- use Yii;
- /**
- * This is the model class for table "{{%addons_cloud_stock_agent_order".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $user_id 用户id
- * @property int $goods_id 商品ID
- * @property int $num 商品数量
- * @property string $address 收货人地址
- * @property string $express_no 发货单号
- * @property string $express_code 物流公司编号
- * @property int $send_status 0待发货;1已发货;2已完成;
- * @property string $mobile 收货人手机号码
- * @property string $name 收货人名称
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- * @property int $province_id
- * @property int $city_id
- * @property int $district_id
- * @property int $town_id
- */
- class CloudStockTwoAgentOrder extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_cloud_stock_two_agent_order}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'goods_id', 'num', 'send_status', 'status', 'created_at', 'updated_at',
- 'province_id','city_id','district_id','town_id'], 'integer'],
- [['goods_id', 'name'], 'required'],
- [['address', 'express_no', 'express_code'], 'string', 'max' => 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;
- }
- }
- }
|