Rule.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace addons\Wechat\common\models;
  3. use Yii;
  4. use common\behaviors\MerchantBehavior;
  5. /**
  6. * This is the model class for table "{{%addon_wechat_rule}}".
  7. *
  8. * @property int $id
  9. * @property int|null $merchant_id 商户id
  10. * @property int|null $store_id 店铺ID
  11. * @property string $name 规则名称
  12. * @property string $module 模块
  13. * @property string|null $data 数据
  14. * @property int $sort 排序
  15. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  16. * @property int|null $created_at 创建时间
  17. * @property int|null $updated_at 修改时间
  18. */
  19. class Rule extends \common\models\base\BaseModel
  20. {
  21. use MerchantBehavior;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%addon_wechat_rule}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. ['name', 'required'],
  36. ['name', 'unique','message' => '规则名称已经被占用', 'filter' => function ($query) {
  37. $query->andWhere(['merchant_id' => Yii::$app->services->merchant->getNotNullId()]);
  38. }],
  39. [['merchant_id', 'store_id', 'sort', 'status', 'created_at', 'updated_at'], 'integer'],
  40. [['data'], 'string'],
  41. [['name', 'module'], 'string', 'max' => 50],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'merchant_id' => '商户id',
  52. 'store_id' => '店铺ID',
  53. 'name' => '规则名称',
  54. 'module' => '模块',
  55. 'data' => '数据',
  56. 'sort' => '排序',
  57. 'status' => '状态',
  58. 'created_at' => '创建时间',
  59. 'updated_at' => '修改时间',
  60. ];
  61. }
  62. /**
  63. * 关联关键字
  64. *
  65. * @return \yii\db\ActiveQuery
  66. */
  67. public function getRuleKeyword()
  68. {
  69. return $this->hasMany(RuleKeyword::class, ['rule_id' => 'id'])->orderBy('type asc');
  70. }
  71. /**
  72. * 关联资源
  73. *
  74. * @return \yii\db\ActiveQuery
  75. */
  76. public function getAttachment()
  77. {
  78. return $this->hasMany(Attachment::class, ['media_id' => 'data']);
  79. }
  80. /**
  81. * 关联图文资源
  82. *
  83. * @return \yii\db\ActiveQuery
  84. */
  85. public function getNews()
  86. {
  87. return $this->hasMany(AttachmentNews::class,['attachment_id' => 'data'])->orderBy('id asc');
  88. }
  89. /**
  90. * 关联图文资源
  91. *
  92. * @return \yii\db\ActiveQuery
  93. */
  94. public function getNewsTop()
  95. {
  96. return $this->hasOne(AttachmentNews::class,['attachment_id' => 'data'])->where(['sort' => 0]);
  97. }
  98. /**
  99. * 删除其他数据
  100. */
  101. public function afterDelete()
  102. {
  103. $id = $this->id;
  104. // 关键字删除
  105. RuleKeyword::deleteAll(['rule_id' => $id]);
  106. // 规则统计
  107. RuleStat::deleteAll(['rule_id' => $id]);
  108. // 关键字规则统计
  109. RuleKeywordStat::deleteAll(['rule_id' => $id]);
  110. parent::afterDelete();
  111. }
  112. /**
  113. * @param bool $insert
  114. * @param array $changedAttributes
  115. */
  116. public function afterSave($insert, $changedAttributes)
  117. {
  118. // 更新状态和排序
  119. RuleKeyword::updateAll(['module' => $this->module, 'sort' => $this->sort, 'status' => $this->status], ['rule_id' => $this->id]);
  120. parent::afterSave($insert, $changedAttributes);
  121. }
  122. }
粤ICP备19079148号