截流自动化的商城平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LevelLogic.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\admin\logic\user;
  3. use app\common\basics\Logic;
  4. use app\common\model\user\User;
  5. use app\common\model\user\UserLevel;
  6. use app\common\server\UrlServer;
  7. use think\facade\Db;
  8. class LevelLogic extends Logic
  9. {
  10. public static function lists($get)
  11. {
  12. $count = UserLevel::where(['del'=>0])->count();
  13. $lists = UserLevel::where(['del'=>0])->order('growth_value', 'asc')->page($get['page'], $get['limit'])->select()->toArray();
  14. foreach ($lists as &$item){
  15. $item['image'] = UrlServer::getFileUrl($item['image']);
  16. $item['background_image'] = UrlServer::getFileUrl($item['background_image']);
  17. }
  18. return ['count' => $count, 'lists' => $lists];
  19. }
  20. public static function add($post)
  21. {
  22. Db::startTrans();
  23. try{
  24. $userLevel = UserLevel::where(['name'=>trim($post['name']), 'del'=>0])->findOrEmpty();
  25. if(!$userLevel->isEmpty()) {
  26. throw new \think\Exception('等级名称已被使用,请更换后重试');
  27. }
  28. $userLevel = UserLevel::where(['growth_value'=>intval($post['growth_value']), 'del'=>0])->findOrEmpty();
  29. if(!$userLevel->isEmpty()) {
  30. throw new \think\Exception('指定成长值的等级已存在');
  31. }
  32. $time = time();
  33. $data = [
  34. 'name' => trim($post['name']),
  35. 'growth_value' => intval($post['growth_value']),
  36. 'image' => clearDomain($post['image']),
  37. 'background_image' => clearDomain($post['background_image']),
  38. 'remark' => trim($post['remark']),
  39. 'discount' => $post['discount'],
  40. 'create_time' => $time,
  41. 'update_time' => $time,
  42. 'del' => 0
  43. ];
  44. UserLevel::create($data);
  45. // 更新会员等级
  46. $userArr = User::field('id,level,user_growth')->where('del', 0)->select()->toArray();
  47. self::updateUserLevel($userArr);
  48. Db::commit();
  49. return true;
  50. }catch(\Exception $e) {
  51. Db::rollback();
  52. self::$error = $e->getMessage();
  53. return false;
  54. }
  55. }
  56. public static function getUserLevel($id){
  57. $detail = UserLevel::where(['id'=>$id,'del'=>0])->findOrEmpty();
  58. if($detail->isEmpty()) {
  59. return [];
  60. }
  61. $detail = $detail->toArray();
  62. $detail['image'] = UrlServer::getFileUrl($detail['image']);
  63. $detail['background_image'] = UrlServer::getFileUrl($detail['background_image']);
  64. return $detail;
  65. }
  66. public static function edit($post)
  67. {
  68. if(empty($post['discount']) || $post['discount'] === ''){
  69. $post['discount'] = 10;
  70. }
  71. Db::startTrans();
  72. try{
  73. $userLevel = UserLevel::where([
  74. ['name', '=', trim($post['name'])],
  75. ['del', '=', 0],
  76. ['id', '<>', $post['id']]
  77. ])->findOrEmpty();
  78. if(!$userLevel->isEmpty()) {
  79. throw new \think\Exception('等级名称已被使用,请更换后重试');
  80. }
  81. $userLevel = UserLevel::where([
  82. ['growth_value', '=', intval($post['growth_value'])],
  83. ['del', '=', 0],
  84. ['id', '<>', $post['id']]
  85. ])->findOrEmpty();
  86. if(!$userLevel->isEmpty()) {
  87. throw new \think\Exception('指定成长值的等级已存在');
  88. }
  89. $time = time();
  90. $data = [
  91. 'id' => $post['id'],
  92. 'name' => trim($post['name']),
  93. 'growth_value' => intval($post['growth_value']),
  94. 'image' => clearDomain($post['image']),
  95. 'background_image' => clearDomain($post['background_image']),
  96. 'discount' => $post['discount'],
  97. 'remark' => trim($post['remark']),
  98. 'update_time' => $time,
  99. 'del' => 0
  100. ];
  101. UserLevel::update($data);
  102. // 更新会员等级
  103. $userArr = User::field('id,level,user_growth')->where('del', 0)->select()->toArray();
  104. self::updateUserLevel($userArr);
  105. Db::commit();
  106. return true;
  107. }catch(\Exception $e) {
  108. Db::rollback();
  109. self::$error = $e->getMessage();
  110. return false;
  111. }
  112. }
  113. public static function del($id)
  114. {
  115. Db::startTrans();
  116. try{
  117. $data = [
  118. 'id' => $id,
  119. 'del' => 1,
  120. 'update_time' => time()
  121. ];
  122. UserLevel::update($data);
  123. // 更新会员等级
  124. $userArr = User::field('id,level,user_growth')->where('del', 0)->select()->toArray();
  125. self::updateUserLevel($userArr);
  126. Db::commit();
  127. return true;
  128. }catch(\Exception $e) {
  129. Db::rollback();
  130. self::$error = $e->getMessage();
  131. return false;
  132. }
  133. }
  134. /**
  135. * 更新会员等级
  136. * 原则:只升不降
  137. * $userArr 需要更新会员等级的用户数组,必须的字段:id,level,user_growth
  138. */
  139. public static function updateUserLevel($userArr)
  140. {
  141. // 所有会员等级
  142. $userLevelArr = UserLevel::field('id,growth_value')->where('del', 0)
  143. ->order('growth_value', 'desc')
  144. ->select()
  145. ->toArray();
  146. if(empty($userLevelArr)) { // 未设置会员等级
  147. // 全部会员等级统一更新为0
  148. User::where('del', 0)->update([
  149. 'level' => 0,
  150. 'update_time' => time()
  151. ]);
  152. }
  153. // 转格式,变为id为数组下标
  154. $levelArr = [];
  155. foreach($userLevelArr as $item) {
  156. $levelArr[$item['id']] = $item;
  157. }
  158. $updateData = [];
  159. foreach($levelArr as $level) {
  160. $filterIds = []; // 本轮结束已处理的会员id
  161. foreach($userArr as $user) {
  162. if($user['user_growth'] >= $level['growth_value']) {
  163. $targetLevelGrwothValue = $level['growth_value']; // 目标会员等级成长值
  164. // 原会员等级成长值
  165. $originLevelGrowthValue = isset($levelArr[$user['level']]) ? $levelArr[$user['level']]['growth_value'] : 0;
  166. // 降级或等级一样,不处理直接下一轮循环
  167. if($originLevelGrowthValue > $targetLevelGrwothValue) {
  168. $filterIds[] = $user['id'];
  169. continue;
  170. }
  171. $updateData[] = ['id'=>$user['id'], 'level'=>$level['id']];
  172. $filterIds[] = $user['id'];
  173. }
  174. }
  175. // 过滤已处理过的用户,避免重复更新
  176. $userArr = array_filter($userArr, function($item) use ($filterIds){
  177. return !in_array($item['id'], $filterIds);
  178. });
  179. }
  180. // 剩余未处理的会员,若原有等级已被删除掉,直接置为0
  181. foreach($userArr as $user) {
  182. if(!isset($levelArr[$user['level']])) {
  183. $updateData[] = ['id'=>$user['id'], 'level'=>0];
  184. }
  185. }
  186. $userModel = new User();
  187. $userModel->saveAll($updateData);
  188. }
  189. public static function getLevelList()
  190. {
  191. $levelArr = UserLevel::field('id,name')
  192. ->where('del', 0)
  193. ->order('growth_value', 'asc')
  194. ->select()
  195. ->toArray();
  196. $levelArr[0] = ['id'=>0, 'name'=>'暂无等级'];
  197. return $levelArr;
  198. }
  199. }