ReplyRule.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace common\models\wechat;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%reply_rule}}".
  7. *
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property string $name 规则名称
  12. * @property string|null $reply_type 回复类型
  13. * @property int|null $is_follow_reply 是否关注自动回复,0/否 1/是
  14. * @property int $type 【0 模糊匹配, 1 精准匹配】
  15. * @property string|null $content
  16. * @property int $status 状态【1启用 0禁用 -1删除】
  17. * @property int $created_at
  18. * @property int $updated_at
  19. */
  20. class ReplyRule extends \common\models\base\BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%wechat_reply_rule}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'name'], 'required'],
  36. [['mall_id', 'status', 'created_at', 'updated_at','is_follow_reply'], 'integer'],
  37. [['name', 'reply_type'], 'string', 'max' => 30],
  38. [['content'], 'string'],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => 'Mall ID',
  49. 'name' => '规则名称',
  50. 'reply_type' => '回复类型',
  51. 'is_follow_reply' => '是否关注自动回复,0/否 1/是',
  52. 'type' => '【0 模糊匹配, 1 精准匹配】',
  53. 'content' => 'Content',
  54. 'status' => '状态【1启用 0禁用 -1删除】',
  55. 'created_at' => 'Created At',
  56. 'updated_at' => 'Updated At',
  57. ];
  58. }
  59. public function getRuleKeyword()
  60. {
  61. return $this->hasMany(RuleKeyword::class, ['rule_id' => 'id'])->andWhere(['<>','status',StatusEnum::DELETE])->orderBy(['created_at' => SORT_DESC]);
  62. }
  63. /**
  64. * 保存数据
  65. * @Author lun
  66. * @DateTime 2021-09-17 14:16:24
  67. * @copyright: Copyright (c) 2020 广东七件事集团
  68. * @param array $params
  69. * @return object|boolean
  70. */
  71. public static function setData($mall_id, array $params){
  72. $trans = Yii::$app->db->beginTransaction();
  73. try{
  74. if(!empty($params['id'])){
  75. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $mall_id],['<>','status',StatusEnum::DELETE]]);
  76. if(empty($model)) throw new \Exception('信息不存在或已删除');
  77. }else{
  78. $model = new self();
  79. $model->mall_id = $mall_id;
  80. $model->type = 0;
  81. }
  82. $model->attributes = $params;
  83. if(!$model->save()){
  84. throw new \Exception($model->getErrorMessage());
  85. }
  86. // 保存匹配关键字
  87. $res = RuleKeyword::setData($mall_id, $model->id, $params);
  88. if ($res == false) throw new \Exception($model->getErrorMessage());
  89. $trans->commit();
  90. return true;
  91. }catch(\Exception $e){
  92. self::$static_error = $e->getMessage();
  93. $trans->rollBack();
  94. return false;
  95. }
  96. }
  97. /**
  98. * 获取回复列表
  99. * @Author lun
  100. * @DateTime 2021-09-17 13:00:11
  101. * @copyright: Copyright (c) 2020 广东七件事集团
  102. * @param array $cond
  103. * @param array $with
  104. * @param integer $is_array
  105. * @return array|null
  106. */
  107. public static function getReplyRule(array $cond,array $with=[],bool $is_array=true,string $select='*')
  108. {
  109. $default_cond = [['<>', 'status', StatusEnum::DELETE]];
  110. $cond = array_merge($cond, $default_cond);
  111. $query = self::find()->select($select);
  112. self::paramPack(['where' => $cond, 'with' => $with],$query);
  113. $list = $query->asArray($is_array)->all();
  114. return $list;
  115. }
  116. }