| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- namespace common\models\user;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\enums\UserLabelEnum;
- use common\listeners\user\UserLabelListener;
- use common\models\goods\Goods;
- use console\controllers\UserLabelController;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%user_label}}".
- *
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $user_num 会员数量
- * @property string $name 标签名称
- * @property string $color 颜色
- * @property int $type 标签类型[1=手动,2=自动]
- * @property int $satisfy_type 满足类型:1满足其一,2满足所有
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class UserLabel extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%user_label}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_num', 'type', 'satisfy_type', 'status', 'created_at', 'updated_at'], 'integer'],
- [['name'], 'string', 'max' => 45],
- [['color'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'user_num' => '会员数量',
- 'name' => '标签名称',
- 'color' => '颜色',
- 'type' => '标签类型[1=手动,2=自动]',
- 'satisfy_type' => '满足类型:1满足其一,2满足所有',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getLabelCond()
- {
- return $this->hasMany(UserLabelCond::class, ['label_id' => 'id']);
- }
- /**
- * 关联用户关系表
- * @Author: lun
- * @DateTime: 2022/3/14 0014 10:18
- * @Copyright: copyright (c) 2021 广东七件事集团
- */
- public function getUserLabelRelationship()
- {
- return $this->hasMany(UserLabelRelationship::class, ['label_id' => 'id']);
- }
- /**
- * @Author: ltm
- * @DateTime: 2022/3/10 0010 9:29
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public static function setData(array $params){
- $t = \Yii::$app->db->beginTransaction();
- try{
- $is_insert = false;
- //过滤重复标签
- if (!empty($params['id'])) {
- $model = self::findOne(['id'=>$params['id'],'mall_id'=>$params['mall_id']]);
- if (empty($model)) throw new Exception('记录不存在或已删除');
- }else{
- $find_model = self::findOne(['name'=>$params['name'],'mall_id'=>$params['mall_id'],'status' => StatusEnum::ENABLED]);
- if ($find_model) throw new Exception('标签已经存在');
- $model = new self();
- $model->loadDefaultValues();
- $is_insert = true;
- }
- if(!$model->load($params,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- // 插入条件
- if (!empty($params['condition']) && $params['type'] == UserLabelEnum::AUTO) {
- foreach ($params['condition'] as $k => $v){
- $label_conf['mall_id'] = $params['mall_id'];
- $label_conf['label_id'] = $model->id;
- $label_conf['cond_type'] = $k;
- $label_conf['condition'] = json_encode($v['condition']);
- $label_conf['status'] = $model->status;
- $label_conf['select_status'] = $v['select_status'];
- $res = UserLabelCond::setData($label_conf);
- if ($res == false) throw new \Exception(UserLabelCond::getStaticError());
- }
- }
- //删除会员的标签
- if(!empty($params['id']) && $params['status'] == StatusEnum::DELETE ){
- $res = UserLabelRelationship::updateAll(['status' => StatusEnum::DELETE],['label_id' => $params['id']]);
- $res = UserLabelCond::updateAll(['status' => StatusEnum::DELETE],['label_id' => $params['id']]);
- }
- $t->commit();
- // 新增数据插入队列中进行统计
- if ($is_insert && $model->type == UserLabelEnum::AUTO) {
- Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, ['label_id' => $model->id, 'mall_id' => $model->mall_id], UserLabelListener::class, 'async_handle');
- }
- return $model;
- }catch(\Exception $e){
- $t->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 获取标签列表
- * @Author: lun
- * @DateTime: 2022/3/14 0014 9:54
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $cond
- * @param array $with
- * @param bool $is_array
- * @param string $select
- * @param string $index
- * @return array|\yii\db\ActiveRecord[]
- */
- public static function getUserLabel(array $cond=[],array $with=[],bool $is_array=true,string $select='*',$index='')
- {
- $default_cond = [['<>','status',StatusEnum::DELETE]];
- $cond = array_merge($default_cond,$cond);
- $query = self::find()->select($select);
- self::paramPack(['where'=>$cond,'with'=>$with],$query);
- if(!empty($index)) $query->indexBy($index);
- $list = $query->asArray($is_array)->all();
- return $list;
- }
- /**
- * 达标条件
- * @Author: ltm
- * @DateTime: 2022/3/14 0014 16:48
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public static function getLabelConf($data){
- $cond_data_string = [];
- if($data){
- $cond_enum = UserLabelEnum::getMap();
- foreach ($data as $k => $v){
- $cond_data = json_decode($v['condition']);
- if($v['select_status'] == 1){
- switch ($v['cond_type']){
- case UserLabelEnum::LAST_CONSUME;
- if($cond_data->type == 1){
- $cond_data_string[] = $cond_enum[$v['cond_type']].": ".$cond_data->content->day.'天';
- }else{
- $cond_data_string[] = $cond_enum[$v['cond_type']].": ".$cond_data->content->start."到".$cond_data->content->end;
- }
- break;
- case UserLabelEnum::GOODS;
- if($v['condition']){
- $goods_where[] = ['in','id',explode(',',$cond_data)];
- $select = 'goods_name,price,cover_pic,stock';
- $goods_list = Goods::lists(['where' => $goods_where,'select' => $select],true);
- $goods_name = '';
- foreach ($goods_list as $goods_k => $goods_v){
- $goods_name .= "【".$goods_v['goods_name']."】";
- }
- $msg_goods = '';
- if(count($goods_list) > 1){
- $msg_goods = '其中一样';
- }
- if($goods_name) $cond_data_string[] = '需要购买'.$goods_name.$msg_goods."商品";
- }
- break;
- case UserLabelEnum::FOLLOW;
- if($cond_data->start & $cond_data->end || $cond_data->start == 0 ||$cond_data->end == 0){
- $cond_data_string[] = $cond_enum[$v['cond_type']].": ".$cond_data->start."到".$cond_data->end;
- }
- break;
- case UserLabelEnum::CONSUME_NUM;
- if($cond_data->start && $cond_data->end || $cond_data->start == 0 ||$cond_data->end == 0){
- $cond_data_string[] = $cond_enum[$v['cond_type']].": ".$cond_data->start."-".$cond_data->end."次";
- }
- break;
- default;
- if($cond_data->start && $cond_data->end || $cond_data->start == 0 ||$cond_data->end == 0){
- $cond_data_string[] = $cond_enum[$v['cond_type']].": ".$cond_data->start."-".$cond_data->end.'元';
- }
- }
- }
- }
- }
- return $cond_data_string;
- }
- }
|