UserLabel.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace common\models\user;
  3. use common\enums\RabbitMqEnum;
  4. use common\enums\StatusEnum;
  5. use common\enums\UserLabelEnum;
  6. use common\listeners\user\UserLabelListener;
  7. use common\models\goods\Goods;
  8. use console\controllers\UserLabelController;
  9. use Yii;
  10. use yii\db\Exception;
  11. /**
  12. * This is the model class for table "{{%user_label}}".
  13. *
  14. *
  15. * @property int $id
  16. * @property int $mall_id 商城ID
  17. * @property int $user_num 会员数量
  18. * @property string $name 标签名称
  19. * @property string $color 颜色
  20. * @property int $type 标签类型[1=手动,2=自动]
  21. * @property int $satisfy_type 满足类型:1满足其一,2满足所有
  22. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  23. * @property int $created_at
  24. * @property int $updated_at
  25. */
  26. class UserLabel extends \common\models\base\BaseActiveRecord
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%user_label}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['mall_id', 'user_num', 'type', 'satisfy_type', 'status', 'created_at', 'updated_at'], 'integer'],
  42. [['name'], 'string', 'max' => 45],
  43. [['color'], 'string', 'max' => 255],
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'mall_id' => '商城ID',
  54. 'user_num' => '会员数量',
  55. 'name' => '标签名称',
  56. 'color' => '颜色',
  57. 'type' => '标签类型[1=手动,2=自动]',
  58. 'satisfy_type' => '满足类型:1满足其一,2满足所有',
  59. 'status' => '状态[-1:删除;0:禁用;1启用]',
  60. 'created_at' => 'Created At',
  61. 'updated_at' => 'Updated At',
  62. ];
  63. }
  64. public function getLabelCond()
  65. {
  66. return $this->hasMany(UserLabelCond::class, ['label_id' => 'id']);
  67. }
  68. /**
  69. * 关联用户关系表
  70. * @Author: lun
  71. * @DateTime: 2022/3/14 0014 10:18
  72. * @Copyright: copyright (c) 2021 广东七件事集团
  73. */
  74. public function getUserLabelRelationship()
  75. {
  76. return $this->hasMany(UserLabelRelationship::class, ['label_id' => 'id']);
  77. }
  78. /**
  79. * @Author: ltm
  80. * @DateTime: 2022/3/10 0010 9:29
  81. * @Copyright: copyright (c) 2021 广东七件事集团
  82. * @param $data
  83. * @return string
  84. */
  85. public static function setData(array $params){
  86. $t = \Yii::$app->db->beginTransaction();
  87. try{
  88. $is_insert = false;
  89. //过滤重复标签
  90. if (!empty($params['id'])) {
  91. $model = self::findOne(['id'=>$params['id'],'mall_id'=>$params['mall_id']]);
  92. if (empty($model)) throw new Exception('记录不存在或已删除');
  93. }else{
  94. $find_model = self::findOne(['name'=>$params['name'],'mall_id'=>$params['mall_id'],'status' => StatusEnum::ENABLED]);
  95. if ($find_model) throw new Exception('标签已经存在');
  96. $model = new self();
  97. $model->loadDefaultValues();
  98. $is_insert = true;
  99. }
  100. if(!$model->load($params,'') || !$model->save()){
  101. throw new Exception($model->getErrorMessage());
  102. }
  103. // 插入条件
  104. if (!empty($params['condition']) && $params['type'] == UserLabelEnum::AUTO) {
  105. foreach ($params['condition'] as $k => $v){
  106. $label_conf['mall_id'] = $params['mall_id'];
  107. $label_conf['label_id'] = $model->id;
  108. $label_conf['cond_type'] = $k;
  109. $label_conf['condition'] = json_encode($v['condition']);
  110. $label_conf['status'] = $model->status;
  111. $label_conf['select_status'] = $v['select_status'];
  112. $res = UserLabelCond::setData($label_conf);
  113. if ($res == false) throw new \Exception(UserLabelCond::getStaticError());
  114. }
  115. }
  116. //删除会员的标签
  117. if(!empty($params['id']) && $params['status'] == StatusEnum::DELETE ){
  118. $res = UserLabelRelationship::updateAll(['status' => StatusEnum::DELETE],['label_id' => $params['id']]);
  119. $res = UserLabelCond::updateAll(['status' => StatusEnum::DELETE],['label_id' => $params['id']]);
  120. }
  121. $t->commit();
  122. // 新增数据插入队列中进行统计
  123. if ($is_insert && $model->type == UserLabelEnum::AUTO) {
  124. Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, ['label_id' => $model->id, 'mall_id' => $model->mall_id], UserLabelListener::class, 'async_handle');
  125. }
  126. return $model;
  127. }catch(\Exception $e){
  128. $t->rollBack();
  129. self::setStaticError($e->getMessage());
  130. return false;
  131. }
  132. }
  133. /**
  134. * 获取标签列表
  135. * @Author: lun
  136. * @DateTime: 2022/3/14 0014 9:54
  137. * @Copyright: copyright (c) 2021 广东七件事集团
  138. * @param array $cond
  139. * @param array $with
  140. * @param bool $is_array
  141. * @param string $select
  142. * @param string $index
  143. * @return array|\yii\db\ActiveRecord[]
  144. */
  145. public static function getUserLabel(array $cond=[],array $with=[],bool $is_array=true,string $select='*',$index='')
  146. {
  147. $default_cond = [['<>','status',StatusEnum::DELETE]];
  148. $cond = array_merge($default_cond,$cond);
  149. $query = self::find()->select($select);
  150. self::paramPack(['where'=>$cond,'with'=>$with],$query);
  151. if(!empty($index)) $query->indexBy($index);
  152. $list = $query->asArray($is_array)->all();
  153. return $list;
  154. }
  155. /**
  156. * 达标条件
  157. * @Author: ltm
  158. * @DateTime: 2022/3/14 0014 16:48
  159. * @Copyright: copyright (c) 2021 广东七件事集团
  160. * @param $data
  161. * @return string
  162. */
  163. public static function getLabelConf($data){
  164. $cond_data_string = [];
  165. if($data){
  166. $cond_enum = UserLabelEnum::getMap();
  167. foreach ($data as $k => $v){
  168. $cond_data = json_decode($v['condition']);
  169. if($v['select_status'] == 1){
  170. switch ($v['cond_type']){
  171. case UserLabelEnum::LAST_CONSUME;
  172. if($cond_data->type == 1){
  173. $cond_data_string[] = $cond_enum[$v['cond_type']].": ".$cond_data->content->day.'天';
  174. }else{
  175. $cond_data_string[] = $cond_enum[$v['cond_type']].": ".$cond_data->content->start."到".$cond_data->content->end;
  176. }
  177. break;
  178. case UserLabelEnum::GOODS;
  179. if($v['condition']){
  180. $goods_where[] = ['in','id',explode(',',$cond_data)];
  181. $select = 'goods_name,price,cover_pic,stock';
  182. $goods_list = Goods::lists(['where' => $goods_where,'select' => $select],true);
  183. $goods_name = '';
  184. foreach ($goods_list as $goods_k => $goods_v){
  185. $goods_name .= "【".$goods_v['goods_name']."】";
  186. }
  187. $msg_goods = '';
  188. if(count($goods_list) > 1){
  189. $msg_goods = '其中一样';
  190. }
  191. if($goods_name) $cond_data_string[] = '需要购买'.$goods_name.$msg_goods."商品";
  192. }
  193. break;
  194. case UserLabelEnum::FOLLOW;
  195. if($cond_data->start & $cond_data->end || $cond_data->start == 0 ||$cond_data->end == 0){
  196. $cond_data_string[] = $cond_enum[$v['cond_type']].": ".$cond_data->start."到".$cond_data->end;
  197. }
  198. break;
  199. case UserLabelEnum::CONSUME_NUM;
  200. if($cond_data->start && $cond_data->end || $cond_data->start == 0 ||$cond_data->end == 0){
  201. $cond_data_string[] = $cond_enum[$v['cond_type']].": ".$cond_data->start."-".$cond_data->end."次";
  202. }
  203. break;
  204. default;
  205. if($cond_data->start && $cond_data->end || $cond_data->start == 0 ||$cond_data->end == 0){
  206. $cond_data_string[] = $cond_enum[$v['cond_type']].": ".$cond_data->start."-".$cond_data->end.'元';
  207. }
  208. }
  209. }
  210. }
  211. }
  212. return $cond_data_string;
  213. }
  214. }