DoubleCopyRelationHandle.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace addons\DoubleCopy\common\models;
  3. use common\enums\RabbitMqEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "{{%addons_double_copy_relation_handle}}".
  9. *
  10. * @property int $id 主键
  11. * @property int $mall_id 商城id
  12. * @property int|null $handle_status 执行状态[-1=处理失败,0=未处理,1=处理中,2=已处理]
  13. * @property int $status 状态【1启用 0禁用 -1删除】
  14. * @property int|null $created_at 创建时间
  15. * @property int|null $updated_at 修改时间
  16. */
  17. class DoubleCopyRelationHandle extends \common\models\base\BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return '{{%addons_double_copy_relation_handle}}';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['mall_id'], 'required'],
  33. [['mall_id', 'handle_status', 'status', 'created_at', 'updated_at'], 'integer'],
  34. ];
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function attributeLabels()
  40. {
  41. return [
  42. 'id' => '主键',
  43. 'mall_id' => '商城id',
  44. 'handle_status' => '执行状态[-1=处理失败,0=未处理,1=处理中,2=已处理]',
  45. 'status' => '状态【1启用 0禁用 -1删除】',
  46. 'created_at' => '创建时间',
  47. 'updated_at' => '修改时间',
  48. ];
  49. }
  50. /**
  51. * 检测是否执行过关系链
  52. * @Author:lun
  53. * @Date:2023-11-30 16:35
  54. * @Copyright:copyright(c)2023 广东七件事集团
  55. * @param $mall_id
  56. * @return true|void
  57. */
  58. public static function checkHandle($mall_id)
  59. {
  60. $model = self::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->one();
  61. if ($model) return true;
  62. $model = new self();
  63. $model->mall_id = $mall_id;
  64. $model->handle_status = 1;
  65. $model->loadDefaultValues();
  66. if (!$model->save()) throw new Exception($model->getErrorMessage());
  67. // 队列执行关系链脚本
  68. Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, ['mall_id' => $model->mall_id], self::class, 'handleRelation');
  69. }
  70. /**
  71. * 异步处理关系链
  72. * @Author:lun
  73. * @Date:2023-11-30 16:35
  74. * @Copyright:copyright(c)2023 广东七件事集团
  75. * @param $params
  76. * @return true|void
  77. */
  78. public static function handleRelation($params)
  79. {
  80. if (empty($params['mall_id'])) return true;
  81. $model = self::findOne(['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
  82. if (empty($model)) return true;
  83. try {
  84. if ($model->handle_status == 2) return true;
  85. DoubleCopyRelation::resetMallUser($params['mall_id']);
  86. $model->handle_status = 2;
  87. if (!$model->save()) throw new Exception($model->getErrorMessage());
  88. return true;
  89. } catch (\Exception $e) {echo $e->getMessage();
  90. $model->handle_status = -1;
  91. $model->save();
  92. Yii::error('处理商城id='.$params['mall_id'].'二二复制关系链脚本执行失败');
  93. return true;
  94. }
  95. }
  96. /**
  97. * 查询是否执行过脚本
  98. * @Author:lun
  99. * @Date:2023-11-30 17:00
  100. * @Copyright:copyright(c)2023 广东七件事集团
  101. * @param $mall_id
  102. * @return bool
  103. */
  104. public static function getIsHandle($mall_id)
  105. {
  106. $model = self::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
  107. if (empty($model)) return false;
  108. return $model['handle_status'] != 2 ? false : true;
  109. }
  110. }