LevelController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. namespace addons\BusinessBonus\backend\controllers;
  3. use addons\BusinessBonus\common\enums\BusinessBonusEnums;
  4. use addons\BusinessBonus\common\models\BusinessBonusLevel;
  5. use addons\BusinessBonus\common\models\BusinessBonusLevelInfo;
  6. use addons\BusinessBonus\common\models\BusinessBonusUpgradeCondition;
  7. use common\enums\StatusEnum;
  8. use common\models\goods\Goods;
  9. use common\models\user\UserLevel;
  10. use Yii;
  11. use yii\db\Exception;
  12. class LevelController extends BaseController
  13. {
  14. /**
  15. * 等级列表
  16. */
  17. public function actionIndex()
  18. {
  19. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  20. $params = Yii::$app->request->post();
  21. $res = Yii::$app->services->validate->validate($params,[
  22. [['page'], 'integer'],
  23. [['bonus_rate'], 'number'],
  24. [['level_name'], 'string'],
  25. [['created_at'], 'safe'],
  26. ]);
  27. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  28. $where = [
  29. ['=', 'mall_id', $this->mall_id],
  30. ['<>', 'status', StatusEnum::DELETE],
  31. ];
  32. if ($params['bonus_rate']) {
  33. $where[] = ['>=', 'bonus_rate', (float)$params['bonus_rate']];
  34. }
  35. if ($params['level_name']) {
  36. $where[] = ['like', 'level_name', '%' . trim($params['level_name']) . '%', false];
  37. }
  38. // 创建时间
  39. $start_time = strtotime($params['created_at'][0] ?? 0);
  40. $end_time = strtotime($params['created_at'][1] ?? 0);
  41. if ($start_time > 0 && $end_time > 0 && $end_time >= $start_time) {
  42. $where[] = ['>=', 'created_at', $start_time];
  43. $where[] = ['<=', 'created_at', $end_time];
  44. }
  45. $condition = ['where' => $where, 'order' => 'level desc'];
  46. $page_data = BusinessBonusLevel::listPage($condition, false, true);
  47. $page_data['list'] = is_array($page_data['list']) ? $page_data['list'] : [];
  48. $levels = array_column($page_data['list'], 'level');
  49. $list = BusinessBonusUpgradeCondition::find()
  50. ->andWhere(['=', 'mall_id', $this->mall_id])
  51. ->andWhere(['=', 'status', StatusEnum::ENABLED])
  52. ->andWhere(['in', 'business_bonus_level', $levels])
  53. ->asArray()
  54. ->all();
  55. $goods_ids = [];
  56. $cond_data = [];
  57. foreach ($list as $cond) {
  58. $cond_data[$cond['business_bonus_level']][] = $cond;
  59. if ($cond['upgrade_type'] == BusinessBonusEnums::UPGRADE_TYPE_SHOPPING) {
  60. $id = explode(',', $cond['goods_id']);
  61. if (!$id) {
  62. continue;
  63. }
  64. $goods_ids = array_merge($goods_ids, $id);
  65. }
  66. }
  67. if ($goods_ids) {
  68. $goods_list = Goods::find()
  69. ->select('id,goods_name')
  70. ->andWhere(['=', 'mall_id', $this->mall_id])
  71. ->andWhere(['in', 'id', $goods_ids])
  72. ->asArray()
  73. ->all();
  74. $goods_data = array_column($goods_list, null, 'id');
  75. } else {
  76. $goods_data = [];
  77. }
  78. $len = 20;
  79. foreach ($page_data['list'] as $index => $item) {
  80. $conditions = $cond_data[$item['level']] ?? [];
  81. $cond_text_arr = [];
  82. foreach ($conditions as $c) {
  83. // if ($c['upgrade_type'] == BusinessBonusEnums::UPGRADE_TYPE_FREE) {
  84. // $cond_text_arr[] = '无条件';
  85. // }
  86. // if ($c['upgrade_type'] == BusinessBonusEnums::UPGRADE_TYPE_APPLY) {
  87. // $cond_text_arr[] = '前端申请, ' . ($c['is_audit'] == 1 ? '需要审核' : '不需要审核');
  88. // }
  89. // if ($c['upgrade_type'] == BusinessBonusEnums::UPGRADE_TYPE_SHOPPING) {
  90. // $ids = explode(',', $c['goods_id']);
  91. // $names = [];
  92. // foreach ($ids as $id) {
  93. // $goods_name = $goods_data[$id]['goods_name'] ?? '';
  94. // $names[] = mb_strlen($goods_name) > $len ? mb_substr($goods_name, 0, $len) . '...' : $goods_name;
  95. // }
  96. // $cond_text_arr[] = '购买指定商品:' . implode(',', $names);
  97. // }
  98. //
  99. // if ($c['upgrade_type'] == BusinessBonusEnums::UPGRADE_TYPE_STORE_SHOPPING) {
  100. // $upgrade_store_goods_list = json_decode($c['upgrade_store_goods_list'], true);
  101. // $names = '任意门店商品';
  102. //
  103. // if ($upgrade_store_goods_list['store_goods_type'] == 2 && $upgrade_store_goods_list['store_goods_list']) {
  104. // $names = array_column($upgrade_store_goods_list['store_goods_list'], 'name');
  105. // $names = implode(',', $names);
  106. // }
  107. //
  108. // $cond_text_arr[] = '购买门店商品:' . $names;
  109. // }
  110. if ($c['is_buy_goods_upgrade']) {
  111. if ($c['buy_goods_upgrade_type'] == 1) {
  112. if ($c['buy_goods_upgrade_method'] == 1) {
  113. $cond_text_arr[] = '购买指定商品:任意商品';
  114. } else {
  115. $ids = explode(',', $c['goods_id']);
  116. $names = [];
  117. foreach ($ids as $id) {
  118. $goods_name = $goods_data[$id]['goods_name'] ?? '';
  119. $names[] = mb_strlen($goods_name) > $len ? mb_substr($goods_name, 0, $len) . '...' : $goods_name;
  120. }
  121. $cond_text_arr[] = '购买指定商品:' . implode(',', $names);
  122. }
  123. } else {
  124. $upgrade_store_goods_list = json_decode($c['upgrade_store_goods_list'], true);
  125. $names = '任意门店商品';
  126. if ($c['buy_goods_upgrade_method'] == 2 && $upgrade_store_goods_list['store_goods_list']) {
  127. $names = array_column($upgrade_store_goods_list['store_goods_list'], 'name');
  128. $names = implode(',', $names);
  129. }
  130. $cond_text_arr[] = '购买门店商品:' . $names;
  131. }
  132. }
  133. if ($c['is_condition_upgrade']) {
  134. $checked_condition_values = json_decode($c['checked_condition_values'], true) ?? [];
  135. $checked_condition_keys = json_decode($c['checked_condition_keys'], true) ?? [];
  136. $level = BusinessBonusLevel::lists([
  137. 'where' => [
  138. ['mall_id' => $this->mall_id],
  139. ['>=', 'status', StatusEnum::DISABLED]
  140. ],
  141. 'index' => 'level',
  142. 'select' => 'id,level,level_name as name'
  143. ]);
  144. foreach ($checked_condition_values as $key => $value) {
  145. if(in_array($key, $checked_condition_keys)) {
  146. if ($key == 0 && $value['is_need_conditional'] == 0) {
  147. $cond_text_arr[] = '无条件';
  148. }
  149. if ($key == 1) {
  150. $cond_text_arr[] = '前端申请, ' . ($value['is_audit'] == 1 ? '需要审核' : '不需要审核');;
  151. }
  152. if ($key == 2) {
  153. $cond_text_arr[] = '直推' . $value['nums'] . '个O2O门店商家';
  154. }
  155. if ($key == 3) {
  156. $level_name = $level[$value['level']]['name'] ?? '';
  157. $cond_text_arr[] = '直推招商分红'. $level_name .'等级数量' . $value['nums'] . '人';
  158. }
  159. }
  160. }
  161. }
  162. }
  163. $item['cond_context'] = $cond_text_arr ? implode(' | ', $cond_text_arr) : '';
  164. $page_data['list'][$index] = $item;
  165. }
  166. $this->success('获取成功', compact('page_data'));
  167. } else {
  168. return $this->render('index');
  169. }
  170. }
  171. /**
  172. * 招商员等级-表格操作
  173. */
  174. public function actionHandle()
  175. {
  176. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  177. $params = Yii::$app->request->post();
  178. $res = Yii::$app->services->validate->validate($params, [
  179. [['id', 'status'], 'integer'],
  180. ]);
  181. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  182. $params['mall_id'] = $this->mall_id;
  183. $res = BusinessBonusLevel::setData($params);
  184. if ($res == false) return $this->error("修改失败: " . BusinessBonusLevel::getStaticError());
  185. return $this->success("修改成功");
  186. }
  187. }
  188. /**
  189. * 编辑、新增招商员等级
  190. */
  191. public function actionEdit()
  192. {
  193. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  194. $params = Yii::$app->request->post();
  195. $res = Yii::$app->services->validate->validate($params, [
  196. [['id', 'level', 'status', 'created_at', 'created_at','is_buy_goods_upgrade','is_condition_upgrade', 'buy_goods_upgrade_type', 'buy_goods_upgrade_method'], 'integer'],
  197. [['level_name'], 'string'],
  198. [['bonus_rate'], 'number'],
  199. [['condition', 'level_info', 'upgrade_store_goods_list','checked_condition_values', 'checked_condition_keys'], 'safe'],
  200. ]);
  201. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  202. if (!$params['created_at']) unset($params['created_at']);
  203. $params['mall_id'] = $this->mall_id;
  204. // 是否重复添加
  205. $where = [
  206. ['=', 'mall_id', $this->mall_id],
  207. ['=', 'level', $params['level']],
  208. ['>=', 'status', StatusEnum::DISABLED],
  209. ];
  210. if (!$params['id']) {
  211. unset($params['id']);
  212. } else {
  213. $where[] = ['<>', 'id', $params['id']];
  214. }
  215. $is_repeact = BusinessBonusLevel::getOne($where, true);
  216. if ($is_repeact) {
  217. return $this->error('等级权重已使用,请勿重复');
  218. }
  219. $tran = Yii::$app->db->beginTransaction();
  220. try {
  221. $condition = $params['condition'];
  222. $condition['checked_condition_values'] = !empty($params['checked_condition_values']) ? json_encode($params['checked_condition_values']) : '[]';
  223. $condition['checked_condition_keys'] = !empty($params['checked_condition_keys']) ? json_encode($params['checked_condition_keys']) : '[]';
  224. $condition['status'] = StatusEnum::ENABLED;
  225. $condition['is_buy_goods_upgrade'] = $params['is_buy_goods_upgrade'];
  226. $condition['is_condition_upgrade'] = $params['is_condition_upgrade'];
  227. $condition['buy_goods_upgrade_type'] = $params['buy_goods_upgrade_type'];
  228. $condition['buy_goods_upgrade_method'] = $params['buy_goods_upgrade_method'];
  229. $condition['mall_id'] = $this->mall_id;
  230. $condition['goods_buy_number'] = 1;
  231. $condition['business_bonus_level'] = $params['level'];
  232. $goods_list = $condition['goods_list'] && is_array($condition['goods_list']) ? $condition['goods_list'] : [];
  233. // if ($goods_list && $condition['upgrade_type'] == 3) {
  234. if ($goods_list && $condition['is_buy_goods_upgrade'] == 1 && $condition['buy_goods_upgrade_type'] == 1) {
  235. $goods_ids = array_column($goods_list, 'goods_id');
  236. $goods_ids = array_unique($goods_ids);
  237. $list = BusinessBonusUpgradeCondition::find()
  238. ->select('goods_id,business_bonus_level')
  239. ->andWhere(['=', 'mall_id', $this->mall_id])
  240. ->andWhere(['=', 'upgrade_type', BusinessBonusEnums::UPGRADE_TYPE_SHOPPING])
  241. ->andWhere(['=', 'status', StatusEnum::ENABLED])
  242. ->andWhere(['<>', 'business_bonus_level', $params['level']])
  243. ->asArray()
  244. ->all();
  245. if ($list) {
  246. $goods_id_array = explode(',', implode(',', array_column($list, 'goods_id')));
  247. $result = array_intersect($goods_ids, $goods_id_array);
  248. if ($result) throw new Exception('该商品已被其他等级指定!请重新选择');
  249. }
  250. $condition['goods_id'] = implode(',', $goods_ids);
  251. } else {
  252. $condition['goods_id'] = '';
  253. }
  254. // if ($condition['upgrade_type'] == 3 && !$condition['goods_id']) {
  255. // throw new Exception('请选择指定商品!');
  256. // }
  257. // if ($condition['upgrade_type'] == 6 || isset($params['upgrade_store_goods_list'])) {
  258. if($condition['buy_goods_upgrade_method'] == 1) {
  259. $params['upgrade_store_goods_list']['store_goods_ids'] = [];
  260. $params['upgrade_store_goods_list']['store_goods_list'] = [];
  261. }
  262. $condition['upgrade_store_goods_list'] = json_encode($params['upgrade_store_goods_list']);
  263. BusinessBonusUpgradeCondition::updateAll(['status' => StatusEnum::DISABLED], ['mall_id' => $this->mall_id, 'business_bonus_level' => $params['level']]);
  264. $res = BusinessBonusUpgradeCondition::setData($condition);
  265. if ($res == false) throw new Exception(BusinessBonusUpgradeCondition::getStaticError());
  266. $level_info = $params['level_info'];
  267. $level_info['status'] = StatusEnum::ENABLED;
  268. $level_info['mall_id'] = $this->mall_id;
  269. $level_info['business_bonus_level'] = $params['level'];
  270. BusinessBonusLevelInfo::updateAll(['status' => StatusEnum::DISABLED], ['mall_id' => $this->mall_id, 'business_bonus_level' => $params['level']]);
  271. $res = BusinessBonusLevelInfo::setData($level_info);
  272. if ($res == false) throw new Exception(BusinessBonusLevelInfo::getStaticError());
  273. unset($params['condition'], $params['level_info']);
  274. $res = BusinessBonusLevel::setData($params);
  275. if ($res == false) throw new Exception(BusinessBonusLevel::getStaticError());
  276. $tran->commit();
  277. } catch (Exception $e) {
  278. $tran->rollBack();
  279. return $this->error("保存失败: " . $e->getMessage());
  280. }
  281. return $this->success("保存成功");
  282. } else {
  283. return $this->render('edit');
  284. }
  285. }
  286. /**
  287. * 获取编辑招商员等级的信息
  288. */
  289. public function actionInfo()
  290. {
  291. if (Yii::$app->request->isAjax) {
  292. $params = Yii::$app->request->get();
  293. $res = Yii::$app->services->validate->validate($params, [
  294. [['inc_id'], 'integer'],
  295. ]);
  296. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  297. $info = BusinessBonusLevel::getOne([
  298. ['id' => $params['inc_id']],
  299. ['mall_id' => $this->mall_id]
  300. ], true);
  301. if ($info) {
  302. $info['condition'] = BusinessBonusUpgradeCondition::getLevelCondition($this->mall_id, $info['level']);
  303. if (!$info['condition']) {
  304. // 默认值,防止前端报错
  305. $info['condition'] = [
  306. 'upgrade_type' => null,
  307. 'is_audit' => 1,
  308. 'goods_list' => [],
  309. 'is_buy_goods_upgrade' => 0,
  310. 'is_condition_upgrade' => 0,
  311. 'checked_condition_values' => [
  312. ['key' => 0, 'value' => [], 'is_need_conditional' => 0, 'is_inviter' => 1],
  313. ['key' => 1, 'value' => [], "is_audit" => 0],
  314. ['key' => 2, 'value' => [], "nums" => 0],
  315. ['key' => 3, 'value' => [], "nums" => 0, 'level' => null]
  316. ],
  317. 'checked_condition_keys' => [],
  318. 'buy_goods_upgrade_type' => 1
  319. ];
  320. }
  321. $info['level_info']['benefit_intro'] = BusinessBonusLevelInfo::getBenefitIntro($this->mall_id, $info['level']);
  322. }
  323. $is_null = $info ? 0 : 1;
  324. $goods_ids = [];
  325. if(!empty($info['condition']['goods_list'])){
  326. $goods_ids = array_column($info['condition']['goods_list'],'goods_id');
  327. }
  328. if ($info['condition']['upgrade_store_goods_list']) {
  329. !empty($info['condition']['upgrade_store_goods_list']) && $info['condition']['upgrade_store_goods_list'] = json_decode($info['condition']['upgrade_store_goods_list'], true);
  330. } else {
  331. $info['condition']['upgrade_store_goods_list'] = [];
  332. }
  333. // $info['condition']['upgrade_store_goods_list']['store_goods_type'] = $info['condition']['upgrade_store_goods_list']['store_goods_type'] ?? 1;
  334. $info['condition']['upgrade_store_goods_list']['store_goods_ids'] = $info['condition']['upgrade_store_goods_list']['store_goods_ids'] ?? [];
  335. $info['condition']['upgrade_store_goods_list']['store_goods_list'] = $info['condition']['upgrade_store_goods_list']['store_goods_list'] ?? [];
  336. if ($info['condition']['checked_condition_values']) {
  337. $info['condition']['checked_condition_values'] = json_decode($info['condition']['checked_condition_values'], true);
  338. }
  339. if ($info['condition']['checked_condition_keys']) {
  340. $info['condition']['checked_condition_keys'] = json_decode($info['condition']['checked_condition_keys'], true);
  341. }
  342. return $this->success("保存成功", compact('info', 'is_null','goods_ids'));
  343. }
  344. }
  345. /**
  346. * 获取权重列表
  347. */
  348. public function actionWeights()
  349. {
  350. if (Yii::$app->request->isAjax) {
  351. $level_weights = UserLevel::getLevelList($this->mall_id);
  352. $level = BusinessBonusLevel::getOne([
  353. ['mall_id' => $this->mall_id],
  354. ['>=', 'status', StatusEnum::DISABLED]
  355. ]);
  356. // 等级是否为空,没有等级时,添加的第一个等级只能是权重1
  357. $is_level_null = $level ? 0 : 1;
  358. return $this->success("保存成功", compact('level_weights', 'is_level_null'));
  359. }
  360. }
  361. public function actionLevels()
  362. {
  363. if (Yii::$app->request->isAjax) {
  364. $level = BusinessBonusLevel::lists([
  365. 'where' => [
  366. ['mall_id' => $this->mall_id],
  367. ['>=', 'status', StatusEnum::DISABLED]
  368. ],
  369. 'select' => 'id,level,level_name as name'
  370. ]);
  371. return $this->success("保存成功", $level);
  372. }
  373. }
  374. }