| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace addons\DoubleCopy\common\models;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_double_copy_relation_handle}}".
- *
- * @property int $id 主键
- * @property int $mall_id 商城id
- * @property int|null $handle_status 执行状态[-1=处理失败,0=未处理,1=处理中,2=已处理]
- * @property int $status 状态【1启用 0禁用 -1删除】
- * @property int|null $created_at 创建时间
- * @property int|null $updated_at 修改时间
- */
- class DoubleCopyRelationHandle extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_double_copy_relation_handle}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id'], 'required'],
- [['mall_id', 'handle_status', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- 'mall_id' => '商城id',
- 'handle_status' => '执行状态[-1=处理失败,0=未处理,1=处理中,2=已处理]',
- 'status' => '状态【1启用 0禁用 -1删除】',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- /**
- * 检测是否执行过关系链
- * @Author:lun
- * @Date:2023-11-30 16:35
- * @Copyright:copyright(c)2023 广东七件事集团
- * @param $mall_id
- * @return true|void
- */
- public static function checkHandle($mall_id)
- {
- $model = self::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->one();
- if ($model) return true;
- $model = new self();
- $model->mall_id = $mall_id;
- $model->handle_status = 1;
- $model->loadDefaultValues();
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- // 队列执行关系链脚本
- Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, ['mall_id' => $model->mall_id], self::class, 'handleRelation');
- }
- /**
- * 异步处理关系链
- * @Author:lun
- * @Date:2023-11-30 16:35
- * @Copyright:copyright(c)2023 广东七件事集团
- * @param $params
- * @return true|void
- */
- public static function handleRelation($params)
- {
- if (empty($params['mall_id'])) return true;
- $model = self::findOne(['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
- if (empty($model)) return true;
- try {
- if ($model->handle_status == 2) return true;
- DoubleCopyRelation::resetMallUser($params['mall_id']);
- $model->handle_status = 2;
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- return true;
- } catch (\Exception $e) {echo $e->getMessage();
- $model->handle_status = -1;
- $model->save();
- Yii::error('处理商城id='.$params['mall_id'].'二二复制关系链脚本执行失败');
- return true;
- }
- }
- /**
- * 查询是否执行过脚本
- * @Author:lun
- * @Date:2023-11-30 17:00
- * @Copyright:copyright(c)2023 广东七件事集团
- * @param $mall_id
- * @return bool
- */
- public static function getIsHandle($mall_id)
- {
- $model = self::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
- if (empty($model)) return false;
- return $model['handle_status'] != 2 ? false : true;
- }
- }
|