Upgrade.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. * @var string
  16. */
  17. private $table = 'BaseUserUpgrade';
  18. /**
  19. * 用户等级管理
  20. * @auth true
  21. * @menu true
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\DbException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. */
  26. public function index()
  27. {
  28. $this->title = '用户等级管理';
  29. $this->_query(BaseUserUpgrade::class)->order('number asc')->page();
  30. }
  31. /**
  32. * 数据列表处理
  33. * @param array $data
  34. */
  35. protected function _page_filter(array &$data)
  36. {
  37. foreach ($data as &$vo) {
  38. $vo['rebate_rule'] = str2arr($vo['rebate_rule']);
  39. foreach ($vo['rebate_rule'] as &$v) {
  40. $v = RebateService::instance()->name($v);
  41. }
  42. }
  43. }
  44. /**
  45. * 添加用户等级
  46. * @auth true
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function add()
  52. {
  53. $this->_form(BaseUserUpgrade::class, 'form');
  54. }
  55. /**
  56. * 编辑用户等级
  57. * @auth true
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function edit()
  63. {
  64. $this->_form(BaseUserUpgrade::class, 'form');
  65. }
  66. /**
  67. * 表单数据处理
  68. * @param array $vo
  69. */
  70. protected function _form_filter(array &$vo)
  71. {
  72. if ($this->request->isGet()) {
  73. $this->prizes = RebateService::PRIZES;
  74. if (!isset($vo['number'])) {
  75. $vo['number'] = BaseUserUpgrade::mk()->order('number desc')->value('number', -1) + 1;
  76. }
  77. $vo['rebate_rule'] = str2arr($vo['rebate_rule'] ?? '');
  78. } else {
  79. $vo['utime'] = time();
  80. $vo['rebate_rule'] = arr2str($vo['rebate_rule'] ?? []);
  81. // 用户升级条件开关
  82. $vo['goods_vip_status'] = isset($vo['goods_vip_status']) ? 1 : 0;
  83. $vo['teams_users_status'] = isset($vo['teams_users_status']) ? 1 : 0;
  84. $vo['teams_direct_status'] = isset($vo['teams_direct_status']) ? 1 : 0;
  85. $vo['teams_indirect_status'] = isset($vo['teams_indirect_status']) ? 1 : 0;
  86. $vo['order_amount_status'] = isset($vo['order_amount_status']) ? 1 : 0;
  87. // 根据数量判断状态
  88. $vo['teams_users_status'] = intval($vo['teams_users_status'] && $vo['teams_users_number'] > 0);
  89. $vo['teams_direct_status'] = intval($vo['teams_direct_status'] && $vo['teams_direct_number'] > 0);
  90. $vo['teams_indirect_status'] = intval($vo['teams_indirect_status'] && $vo['teams_indirect_number'] > 0);
  91. $vo['order_amount_status'] = intval($vo['order_amount_status'] && $vo['order_amount_number'] > 0);
  92. // 检查升级条件配置
  93. $count = 0;
  94. foreach ($vo as $k => $v) if (is_numeric(stripos($k, '_status'))) $count += $v;
  95. if (empty($count) && $vo['number'] > 0) $this->error('升级条件不能为空!');
  96. }
  97. }
  98. /**
  99. * 表单结果处理
  100. * @param boolean $state
  101. * @throws \think\db\exception\DbException
  102. */
  103. public function _form_result(bool $state)
  104. {
  105. if ($state) {
  106. $order = 'number asc,utime desc';
  107. if (input('old_number', 100) <= input('number', 0)) $order = 'number asc,utime asc';
  108. foreach (BaseUserUpgrade::mk()->order($order)->cursor() as $k => $vo) {
  109. BaseUserUpgrade::mk()->where(['id' => $vo['id']])->update(['number' => $k]);
  110. }
  111. }
  112. }
  113. /**
  114. * 重算用户等级
  115. * @auth true
  116. */
  117. public function sync()
  118. {
  119. $this->_queue('重新计算所有用户等级', 'xdata:UserUpgrade');
  120. }
  121. /**
  122. * 修改等级状态
  123. * @auth true
  124. * @throws \think\db\exception\DbException
  125. */
  126. public function state()
  127. {
  128. $this->_save(BaseUserUpgrade::class);
  129. }
  130. /**
  131. * 删除用户等级
  132. * @auth true
  133. * @throws \think\db\exception\DbException
  134. */
  135. public function remove()
  136. {
  137. $this->_delete(BaseUserUpgrade::class);
  138. }
  139. /**
  140. * 状态变更处理
  141. * @auth true
  142. * @throws \think\db\exception\DbException
  143. */
  144. protected function _save_result()
  145. {
  146. $this->_form_result(true);
  147. }
  148. /**
  149. * 删除结果处理
  150. * @throws \think\db\exception\DbException
  151. */
  152. protected function _delete_result()
  153. {
  154. $this->_form_result(true);
  155. }
  156. }