UserLevelLogic.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\user;
  20. use app\common\enum\PayEnum;
  21. use app\common\model\
  22. {
  23. User,
  24. Order,
  25. UserLevel
  26. };
  27. /**
  28. * 会员等级逻辑层
  29. * Class UserLevelLogic
  30. * @package app\adminapi\logic\user
  31. */
  32. class UserLevelLogic
  33. {
  34. /**
  35. * @notes 添加会员等级
  36. * @param array $params
  37. * @return bool
  38. * @author cjhao
  39. * @date 2021/7/28 15:08
  40. */
  41. public function add(array $params)
  42. {
  43. $userLevel = new UserLevel();
  44. //等级条件
  45. $condition = $this->disposeCondition($params['condition']);
  46. $userLevel->name = $params['name'];
  47. $userLevel->rank = $params['rank'];
  48. $userLevel->image = $params['image'];
  49. $userLevel->background_image = $params['background_image'];
  50. $userLevel->remark = $params['remark'];
  51. $userLevel->discount = $params['level_discount'] ? $params['discount'] : '';
  52. $userLevel->condition = json_encode($condition,JSON_UNESCAPED_UNICODE);
  53. $userLevel->save();
  54. return true;
  55. }
  56. /**
  57. * @notes 获取用户等级
  58. * @param $id
  59. * @return array
  60. * @author cjhao
  61. * @date 2021/7/29 17:14
  62. */
  63. public function detail($id){
  64. $userLevel = UserLevel::find($id);
  65. $detail = [
  66. 'id' => $userLevel->id,
  67. 'name' => $userLevel->name,
  68. 'rank' => $userLevel->rank,
  69. 'image' => $userLevel->image,
  70. 'background_image' => $userLevel->background_image,
  71. 'remark' => $userLevel->remark,
  72. 'level_discount' => $userLevel->discount > 0 ? 1 :0,
  73. 'discount' => $userLevel->discount,
  74. 'condition' => \app\common\logic\UserLogic::formatLevelCondition($userLevel->condition),
  75. ];
  76. return $detail;
  77. }
  78. /**
  79. * @notes 编辑会员等级
  80. * @param array $params
  81. * @author cjhao
  82. * @date 2021/7/28 15:15
  83. */
  84. public function edit(array $params){
  85. $userlevel = UserLevel::find($params['id']);
  86. $userlevel->name = $params['name'];
  87. $userlevel->image = $params['image'];
  88. $userlevel->background_image = $params['background_image'];
  89. $userlevel->remark = $params['remark'];
  90. $userlevel->discount = $params['level_discount'] ? $params['discount'] : '';
  91. //非系统默认,可设置等级条件
  92. if(1 != $userlevel->rank){
  93. $userlevel->rank = $params['rank'];
  94. //等级条件
  95. $condition =$this->disposeCondition($params['condition']);
  96. $userlevel->condition = json_encode($condition,JSON_UNESCAPED_UNICODE);
  97. }
  98. $userlevel->save();
  99. return true;
  100. }
  101. /**
  102. * @notes 删除会员等级
  103. * @param int $id
  104. * @return bool
  105. * @author cjhao
  106. * @date 2021/7/28 16:59
  107. */
  108. public function del(int $id){
  109. $res = UserLevel::destroy($id);
  110. //todo 将该等级的用户全部降到系统默认等级
  111. if($res){
  112. $level = UserLevel::where(['rank'=>1])->find();
  113. if($level){
  114. User::where(['level'=>$id])->update(['level'=>$level->id]);
  115. }
  116. }
  117. return true;
  118. }
  119. /**
  120. * @notes 处理前端传过来的等级数据
  121. * @param $condition
  122. * @author cjhao
  123. * @date 2022/4/28 17:05
  124. */
  125. public function disposeCondition($condition){
  126. //默认满足任意条件
  127. $condition_type = $condition['condition_type'] ?? 0;
  128. //默认不勾选
  129. $isSingleMoney = $condition['is_single_money'];
  130. //单笔消费金额
  131. $singleMoney =$condition['single_money'] ?? '';
  132. //默认不勾选
  133. $isTotalMoney = $condition['is_total_money'];
  134. //累计消费金额
  135. $totalMoney = $condition['total_money'] ?? '';
  136. //默认不勾选
  137. $isTotalNum = $condition['is_total_num'];
  138. //累计消费次数
  139. $totalNum = $condition['total_num'] ?? '';
  140. return [
  141. 'condition_type' => (int)$condition_type, //默认满足任意条件
  142. 'is_single_money' => (int)$isSingleMoney, //默认不勾选
  143. 'single_money' => $singleMoney ? round($singleMoney,2) : '',
  144. 'is_total_money' => (int)$isTotalMoney,
  145. 'total_money' => $totalMoney ? round($totalMoney,2) : '',
  146. 'is_total_num' => (int)$isTotalNum,
  147. 'total_num' => $totalNum ? (int)$totalNum : '',
  148. ];
  149. }
  150. }