| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <?php
- namespace common\models\user;
- use addons\Agent\common\models\Agent;
- use addons\Agent\common\models\AgentLevel;
- use common\enums\MemberLabelEnum;
- use common\enums\StatusEnum;
- use common\logic\memberLabel\MemberLabelLogic;
- use common\models\backend\Menu;
- use common\models\base\BaseActiveRecord;
- use common\models\mall\Mall;
- use common\models\mall\MallAddons;
- use common\models\rbac\AuthItem;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "qimall_member_label".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $cate_id 分类ID
- * @property string $name 名称
- * @property string $source_table 来源表
- * @property int $source_table_id 来源表id
- * @property int $user_num 会员数量
- * @property string $condition 达标条件
- * @property string $color 颜色
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class MemberLabel extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%member_label}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'cate_id', 'user_num', 'status', 'source_table_id', 'created_at', 'updated_at'], 'integer'],
- [['name'], 'string', 'max' => 45],
- [['condition'], 'string', 'max' => 5000],
- [['color', 'source_table'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'cate_id' => '分类ID',
- 'name' => '名称',
- 'user_num' => '会员数量',
- 'condition' => '达标条件',
- 'color' => '颜色',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public static function setData(array $params)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- //过滤重复标签
- 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'],'cate_id'=>$params['cate_id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if ($find_model) throw new \Exception('标签已经存在:'. $params['name'].':'.$params['cate_id'].':'.$params['mall_id']);
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '') || !$model->save()) {
- throw new \Exception($model->getErrorMessage());
- }
- $t->commit();
- return $model;
- } catch (\Exception $e) {
- $t->rollBack();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- public static function migrateMemberLabel()
- {
- $mall_list = Mall::getEnableMallList();
- $user_level_color = [
- 'C4E6A1',
- 'A4D86C',
- '92D050',
- '72A82C',
- '4D7820',
- ];
- $agent_level_color = [
- '',
- 'CFAFE7',
- '9753CB',
- '8439BD',
- '7030A0',
- '4A206D',
- ];
- foreach ($mall_list as $mall) {
- self::migrateUserLevel($mall['id'], $user_level_color);
- self::migrateAgentLevel($mall['id'], $agent_level_color);
- MemberLabelLogic::changeUserNum($mall['id'], MemberLabelEnum::USER_LEVEL);
- MemberLabelLogic::changeUserNum($mall['id'], MemberLabelEnum::AGENT_LEVEL);
- }
- }
- protected static function migrateUserLevel($mall_id, $user_level_color)
- {
- $user_level_list = UserLevel::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
- ],
- 'order'=>'level asc'
- ]);
- foreach ($user_level_list as $user_level) {
- $color = $user_level_color[$user_level['level']] ?? '4D7820';
- $add_data = [
- 'mall_id' => $mall_id,
- 'cate_id' => MemberLabelEnum::USER_LEVEL,
- 'status' => $user_level['status'],
- 'source_table_id' => $user_level['id'],
- 'source_table' => UserLevel::tableName(),
- 'name' => $user_level['name'],
- 'color' => '#' . $color,
- 'condition' => json_encode(['num' => $user_level['level']]),
- ];
- $res = MemberLabel::setData($add_data);
- if (empty($res)) {
- var_dump(MemberLabel::getStaticError());
- } else {
- var_dump($res['id']);
- }
- }
- }
- protected static function migrateAgentLevel($mall_id, $agent_level_color)
- {
- if (MallAddons::isInstall($mall_id, 'Agent')) {
- $agent_level_list = AgentLevel::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
- ],
- 'order'=>'level asc'
- ]);
- foreach ($agent_level_list as $agent_level) {
- $color = $agent_level_color[$agent_level['level']] ?? '4A206D';
- $add_data = [
- 'mall_id' => $mall_id,
- 'cate_id' => MemberLabelEnum::AGENT_LEVEL,
- 'status' => $agent_level['status'],
- 'source_table_id' => $agent_level['id'],
- 'source_table' => AgentLevel::tableName(),
- 'name' => $agent_level['name'],
- 'color' => '#' . $color,
- 'condition' => json_encode(['num' => $agent_level['level']]),
- ];
- $res = MemberLabel::setData($add_data);
- if (empty($res)) {
- var_dump(MemberLabel::getStaticError());
- } else {
- var_dump($res['id']);
- }
- }
- }
- }
- /**
- * 生成会员标签权限
- * @Author: lun
- * @DateTime: 2023/5/29 0029 10:37
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return int
- * @throws Exception
- */
- public static function createRbacItems()
- {
- $params = [
- 'name' => 'mall/member-label/index',
- 'title' => '会员标签',
- 'app_id' => 'backend',
- 'pid' => '2820',
- 'sort' => '999',
- 'cate_id' => '2',
- ];
- $user_label_model = AuthItem::setData($params);
- $list = [
- ['title' =>'价值标签', 'name' => 'value-label'],
- ['title' =>'会员等级', 'name' => 'user-level'],
- ['title' =>'渠道等级', 'name' => 'agent-level'],
- ['title' =>'复购标签', 'name' => 'repurchase'],
- ['title' =>'活跃标签', 'name' => 'active'],
- ['title' =>'收入标签', 'name' => 'income'],
- ['title' =>'分享标签', 'name' => 'share'],
- ['title' =>'退款标签', 'name' => 'refund'],
- ['title' =>'会员年龄', 'name' => 'user-age'],
- ['title' =>'推广标签', 'name' => 'poppularize'],
- ['title' =>'领导标签', 'name' => 'lead'],
- ['title' =>'智能标签', 'name' => 'index'],
- ['title' =>'智能标签', 'name' => 'index'],
- ];
- foreach ($list as $item) {
- $params = [
- 'name' => $item['title'] == '智能标签' ? 'mall/user-label/' . $item['name'] : 'mall/member-label/' . $item['name'],
- 'title' => $item['title'],
- 'app_id' => 'backend',
- 'pid' => $user_label_model->id,
- 'sort' => '999',
- 'cate_id' => '2',
- ];
- $model = AuthItem::setData($params);
- if ($model) {
- $childs = [
- ['title' =>'添加/编辑', 'name' => 'edit'],
- ['title' =>'禁用/启用', 'name' => 'switch-status'],
- ['title' =>'删除', 'name' => 'del'],
- ];
- foreach ($childs as $child) {
- if ($item['title'] == '智能标签' && $child['name'] == 'switch-status') continue;
- $child_params = [
- 'name' => $item['title'] == '智能标签' ? 'mall/user-label/' . $child['name'] : 'mall/member-label/' . $item['name'] .'-'. $child['name'],
- 'title' => $child['title'],
- 'app_id' => 'backend',
- 'pid' => $model->id,
- 'is_addon' => '0',
- 'sort' => '999',
- 'cate_id' => '2',
- ];
- AuthItem::setData($child_params);
- }
- }
- }
- // 会员分析额外处理
- $params = [
- 'name' => 'mall/user-member-label/index',
- 'title' => '会员分析',
- 'app_id' => 'backend',
- 'pid' => '2820',
- 'sort' => '999',
- 'cate_id' => '2',
- ];
- $analysis = AuthItem::setData($params);
- $child_params = [
- 'name' => 'mall/user-member-label/export',
- 'title' => '批量导出',
- 'app_id' => 'backend',
- 'pid' => $analysis->id,
- 'is_addon' => '0',
- 'sort' => '999',
- 'cate_id' => '2',
- ];
- AuthItem::setData($child_params);
- }
- }
|