MemberLabel.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace common\models\user;
  3. use addons\Agent\common\models\Agent;
  4. use addons\Agent\common\models\AgentLevel;
  5. use common\enums\MemberLabelEnum;
  6. use common\enums\StatusEnum;
  7. use common\logic\memberLabel\MemberLabelLogic;
  8. use common\models\backend\Menu;
  9. use common\models\base\BaseActiveRecord;
  10. use common\models\mall\Mall;
  11. use common\models\mall\MallAddons;
  12. use common\models\rbac\AuthItem;
  13. use Yii;
  14. use yii\db\Exception;
  15. /**
  16. * This is the model class for table "qimall_member_label".
  17. *
  18. * @property int $id
  19. * @property int $mall_id 商城ID
  20. * @property int $cate_id 分类ID
  21. * @property string $name 名称
  22. * @property string $source_table 来源表
  23. * @property int $source_table_id 来源表id
  24. * @property int $user_num 会员数量
  25. * @property string $condition 达标条件
  26. * @property string $color 颜色
  27. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  28. * @property int $created_at
  29. * @property int $updated_at
  30. */
  31. class MemberLabel extends BaseActiveRecord
  32. {
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public static function tableName()
  37. {
  38. return '{{%member_label}}';
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function rules()
  44. {
  45. return [
  46. [['mall_id', 'cate_id', 'user_num', 'status', 'source_table_id', 'created_at', 'updated_at'], 'integer'],
  47. [['name'], 'string', 'max' => 45],
  48. [['condition'], 'string', 'max' => 5000],
  49. [['color', 'source_table'], 'string', 'max' => 255],
  50. ];
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function attributeLabels()
  56. {
  57. return [
  58. 'id' => 'ID',
  59. 'mall_id' => '商城ID',
  60. 'cate_id' => '分类ID',
  61. 'name' => '名称',
  62. 'user_num' => '会员数量',
  63. 'condition' => '达标条件',
  64. 'color' => '颜色',
  65. 'status' => '状态[-1:删除;0:禁用;1启用]',
  66. 'created_at' => 'Created At',
  67. 'updated_at' => 'Updated At',
  68. ];
  69. }
  70. public static function setData(array $params)
  71. {
  72. $t = \Yii::$app->db->beginTransaction();
  73. try {
  74. //过滤重复标签
  75. if (!empty($params['id'])) {
  76. $model = self::findOne(['id' => $params['id'], 'mall_id' => $params['mall_id']]);
  77. if (empty($model)) throw new \Exception('记录不存在或已删除');
  78. } else {
  79. $find_model = self::findOne(['name' => $params['name'],'cate_id'=>$params['cate_id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  80. if ($find_model) throw new \Exception('标签已经存在:'. $params['name'].':'.$params['cate_id'].':'.$params['mall_id']);
  81. $model = new self();
  82. $model->loadDefaultValues();
  83. }
  84. if (!$model->load($params, '') || !$model->save()) {
  85. throw new \Exception($model->getErrorMessage());
  86. }
  87. $t->commit();
  88. return $model;
  89. } catch (\Exception $e) {
  90. $t->rollBack();
  91. self::setStaticError($e->getMessage());
  92. return false;
  93. }
  94. }
  95. public static function migrateMemberLabel()
  96. {
  97. $mall_list = Mall::getEnableMallList();
  98. $user_level_color = [
  99. 'C4E6A1',
  100. 'A4D86C',
  101. '92D050',
  102. '72A82C',
  103. '4D7820',
  104. ];
  105. $agent_level_color = [
  106. '',
  107. 'CFAFE7',
  108. '9753CB',
  109. '8439BD',
  110. '7030A0',
  111. '4A206D',
  112. ];
  113. foreach ($mall_list as $mall) {
  114. self::migrateUserLevel($mall['id'], $user_level_color);
  115. self::migrateAgentLevel($mall['id'], $agent_level_color);
  116. MemberLabelLogic::changeUserNum($mall['id'], MemberLabelEnum::USER_LEVEL);
  117. MemberLabelLogic::changeUserNum($mall['id'], MemberLabelEnum::AGENT_LEVEL);
  118. }
  119. }
  120. protected static function migrateUserLevel($mall_id, $user_level_color)
  121. {
  122. $user_level_list = UserLevel::lists([
  123. 'where' => [
  124. ['mall_id' => $mall_id],
  125. ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
  126. ],
  127. 'order'=>'level asc'
  128. ]);
  129. foreach ($user_level_list as $user_level) {
  130. $color = $user_level_color[$user_level['level']] ?? '4D7820';
  131. $add_data = [
  132. 'mall_id' => $mall_id,
  133. 'cate_id' => MemberLabelEnum::USER_LEVEL,
  134. 'status' => $user_level['status'],
  135. 'source_table_id' => $user_level['id'],
  136. 'source_table' => UserLevel::tableName(),
  137. 'name' => $user_level['name'],
  138. 'color' => '#' . $color,
  139. 'condition' => json_encode(['num' => $user_level['level']]),
  140. ];
  141. $res = MemberLabel::setData($add_data);
  142. if (empty($res)) {
  143. var_dump(MemberLabel::getStaticError());
  144. } else {
  145. var_dump($res['id']);
  146. }
  147. }
  148. }
  149. protected static function migrateAgentLevel($mall_id, $agent_level_color)
  150. {
  151. if (MallAddons::isInstall($mall_id, 'Agent')) {
  152. $agent_level_list = AgentLevel::lists([
  153. 'where' => [
  154. ['mall_id' => $mall_id],
  155. ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
  156. ],
  157. 'order'=>'level asc'
  158. ]);
  159. foreach ($agent_level_list as $agent_level) {
  160. $color = $agent_level_color[$agent_level['level']] ?? '4A206D';
  161. $add_data = [
  162. 'mall_id' => $mall_id,
  163. 'cate_id' => MemberLabelEnum::AGENT_LEVEL,
  164. 'status' => $agent_level['status'],
  165. 'source_table_id' => $agent_level['id'],
  166. 'source_table' => AgentLevel::tableName(),
  167. 'name' => $agent_level['name'],
  168. 'color' => '#' . $color,
  169. 'condition' => json_encode(['num' => $agent_level['level']]),
  170. ];
  171. $res = MemberLabel::setData($add_data);
  172. if (empty($res)) {
  173. var_dump(MemberLabel::getStaticError());
  174. } else {
  175. var_dump($res['id']);
  176. }
  177. }
  178. }
  179. }
  180. /**
  181. * 生成会员标签权限
  182. * @Author: lun
  183. * @DateTime: 2023/5/29 0029 10:37
  184. * @Copyright: copyright (c) 2021 广东七件事集团
  185. * @return int
  186. * @throws Exception
  187. */
  188. public static function createRbacItems()
  189. {
  190. $params = [
  191. 'name' => 'mall/member-label/index',
  192. 'title' => '会员标签',
  193. 'app_id' => 'backend',
  194. 'pid' => '2820',
  195. 'sort' => '999',
  196. 'cate_id' => '2',
  197. ];
  198. $user_label_model = AuthItem::setData($params);
  199. $list = [
  200. ['title' =>'价值标签', 'name' => 'value-label'],
  201. ['title' =>'会员等级', 'name' => 'user-level'],
  202. ['title' =>'渠道等级', 'name' => 'agent-level'],
  203. ['title' =>'复购标签', 'name' => 'repurchase'],
  204. ['title' =>'活跃标签', 'name' => 'active'],
  205. ['title' =>'收入标签', 'name' => 'income'],
  206. ['title' =>'分享标签', 'name' => 'share'],
  207. ['title' =>'退款标签', 'name' => 'refund'],
  208. ['title' =>'会员年龄', 'name' => 'user-age'],
  209. ['title' =>'推广标签', 'name' => 'poppularize'],
  210. ['title' =>'领导标签', 'name' => 'lead'],
  211. ['title' =>'智能标签', 'name' => 'index'],
  212. ['title' =>'智能标签', 'name' => 'index'],
  213. ];
  214. foreach ($list as $item) {
  215. $params = [
  216. 'name' => $item['title'] == '智能标签' ? 'mall/user-label/' . $item['name'] : 'mall/member-label/' . $item['name'],
  217. 'title' => $item['title'],
  218. 'app_id' => 'backend',
  219. 'pid' => $user_label_model->id,
  220. 'sort' => '999',
  221. 'cate_id' => '2',
  222. ];
  223. $model = AuthItem::setData($params);
  224. if ($model) {
  225. $childs = [
  226. ['title' =>'添加/编辑', 'name' => 'edit'],
  227. ['title' =>'禁用/启用', 'name' => 'switch-status'],
  228. ['title' =>'删除', 'name' => 'del'],
  229. ];
  230. foreach ($childs as $child) {
  231. if ($item['title'] == '智能标签' && $child['name'] == 'switch-status') continue;
  232. $child_params = [
  233. 'name' => $item['title'] == '智能标签' ? 'mall/user-label/' . $child['name'] : 'mall/member-label/' . $item['name'] .'-'. $child['name'],
  234. 'title' => $child['title'],
  235. 'app_id' => 'backend',
  236. 'pid' => $model->id,
  237. 'is_addon' => '0',
  238. 'sort' => '999',
  239. 'cate_id' => '2',
  240. ];
  241. AuthItem::setData($child_params);
  242. }
  243. }
  244. }
  245. // 会员分析额外处理
  246. $params = [
  247. 'name' => 'mall/user-member-label/index',
  248. 'title' => '会员分析',
  249. 'app_id' => 'backend',
  250. 'pid' => '2820',
  251. 'sort' => '999',
  252. 'cate_id' => '2',
  253. ];
  254. $analysis = AuthItem::setData($params);
  255. $child_params = [
  256. 'name' => 'mall/user-member-label/export',
  257. 'title' => '批量导出',
  258. 'app_id' => 'backend',
  259. 'pid' => $analysis->id,
  260. 'is_addon' => '0',
  261. 'sort' => '999',
  262. 'cate_id' => '2',
  263. ];
  264. AuthItem::setData($child_params);
  265. }
  266. }