Upgrade.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\data\controller\base;
  3. use app\data\model\BaseUserUpgrade;
  4. use app\data\service\RebateService;
  5. use think\admin\Controller;
  6. /**
  7. * 用户等级管理
  8. * Class Upgrade
  9. * @package app\data\controller\base
  10. */
  11. class Upgrade extends Controller
  12. {
  13. /**
  14. * 用户等级管理
  15. * @auth true
  16. * @menu true
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\DbException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. */
  21. public function index()
  22. {
  23. $this->title = '用户等级管理';
  24. BaseUserUpgrade::mQuery()->like('name')->equal('status')->dateBetween('create_at')->layTable();
  25. }
  26. /**
  27. * 添加用户等级
  28. * @auth true
  29. * @return void
  30. * @throws \think\db\exception\DbException
  31. */
  32. public function add()
  33. {
  34. $this->max = BaseUserUpgrade::maxNumber() + 1;
  35. BaseUserUpgrade::mForm('form');
  36. }
  37. /**
  38. * 编辑用户等级
  39. * @auth true
  40. * @return void
  41. * @throws \think\db\exception\DbException
  42. */
  43. public function edit()
  44. {
  45. $this->max = BaseUserUpgrade::maxNumber();
  46. BaseUserUpgrade::mForm('form');
  47. }
  48. /**
  49. * 表单数据处理
  50. * @param array $vo
  51. * @throws \think\db\exception\DbException
  52. */
  53. protected function _form_filter(array &$vo)
  54. {
  55. if ($this->request->isGet()) {
  56. $this->prizes = RebateService::PRIZES;
  57. $vo['number'] = $vo['number'] ?? BaseUserUpgrade::maxNumber();
  58. } else {
  59. $vo['utime'] = time();
  60. // 用户升级条件开关
  61. $vo['goods_vip_status'] = isset($vo['goods_vip_status']) ? 1 : 0;
  62. $vo['teams_users_status'] = isset($vo['teams_users_status']) ? 1 : 0;
  63. $vo['teams_direct_status'] = isset($vo['teams_direct_status']) ? 1 : 0;
  64. $vo['teams_indirect_status'] = isset($vo['teams_indirect_status']) ? 1 : 0;
  65. $vo['order_amount_status'] = isset($vo['order_amount_status']) ? 1 : 0;
  66. // 默认等级去除条件
  67. if (empty($vo['number'])) {
  68. $vo['rebate_rule'] = [];
  69. foreach ($vo as $k => &$v) if (is_numeric(stripos($k, '_status'))) $v = 0;
  70. }
  71. // 根据数量判断状态
  72. $vo['teams_users_status'] = intval($vo['teams_users_status'] && $vo['teams_users_number'] > 0);
  73. $vo['teams_direct_status'] = intval($vo['teams_direct_status'] && $vo['teams_direct_number'] > 0);
  74. $vo['teams_indirect_status'] = intval($vo['teams_indirect_status'] && $vo['teams_indirect_number'] > 0);
  75. $vo['order_amount_status'] = intval($vo['order_amount_status'] && $vo['order_amount_number'] > 0);
  76. // 检查升级条件配置
  77. $count = 0;
  78. foreach ($vo as $k => $v) if (is_numeric(stripos($k, '_status'))) $count += $v;
  79. if (empty($count) && $vo['number'] > 0) $this->error('升级条件不能为空!');
  80. }
  81. }
  82. /**
  83. * 表单结果处理
  84. * @param boolean $state
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function _form_result(bool $state)
  90. {
  91. if ($state) {
  92. $isasc = input('old_number', 0) <= input('number', 0);
  93. $order = $isasc ? 'number asc,utime asc' : 'number asc,utime desc';
  94. foreach (BaseUserUpgrade::mk()->order($order)->select() as $number => $upgrade) {
  95. $upgrade->save(['number' => $number]);
  96. }
  97. }
  98. }
  99. /**
  100. * 重算用户等级
  101. * @auth true
  102. */
  103. public function sync()
  104. {
  105. $this->_queue('重新计算所有用户等级', 'xdata:UserUpgrade');
  106. }
  107. /**
  108. * 修改等级状态
  109. * @auth true
  110. */
  111. public function state()
  112. {
  113. BaseUserUpgrade::mSave();
  114. }
  115. /**
  116. * 删除用户等级
  117. * @auth true
  118. */
  119. public function remove()
  120. {
  121. BaseUserUpgrade::mDelete();
  122. }
  123. /**
  124. * 状态变更处理
  125. * @auth true
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\DbException
  128. * @throws \think\db\exception\ModelNotFoundException
  129. */
  130. protected function _save_result()
  131. {
  132. $this->_form_result(true);
  133. }
  134. /**
  135. * 删除结果处理
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\DbException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. */
  140. protected function _delete_result()
  141. {
  142. $this->_form_result(true);
  143. }
  144. }