Balance.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\data\controller\user;
  3. use app\data\model\BaseUserUpgrade;
  4. use app\data\model\DataUser;
  5. use app\data\model\DataUserBalance;
  6. use app\data\service\UserAdminService;
  7. use app\data\service\UserBalanceService;
  8. use app\data\service\UserUpgradeService;
  9. use think\admin\Controller;
  10. use think\admin\extend\CodeExtend;
  11. use think\admin\model\SystemUser;
  12. use think\admin\service\AdminService;
  13. /**
  14. * 余额充值记录
  15. * Class Balance
  16. * @package app\data\controller\user
  17. */
  18. class Balance extends Controller
  19. {
  20. /**
  21. * 余额充值管理
  22. * @auth true
  23. * @menu true
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\DbException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. */
  28. public function index()
  29. {
  30. $this->title = '余额充值记录';
  31. // 统计用户余额
  32. $this->balance = UserBalanceService::amount(0);
  33. // 现有余额类型
  34. $this->names = DataUserBalance::mk()->group('name')->column('name');
  35. // 创建查询对象
  36. $query = DataUserBalance::mQuery()->equal('name,upgrade');
  37. // 用户搜索查询
  38. $db = DataUser::mQuery()->like('phone|nickname#user_keys')->db();
  39. if ($db->getOptions('where')) $query->whereRaw("uuid in {$db->field('id')->buildSql()}");
  40. // 数据查询分页
  41. $query->where(['deleted' => 0])->like('code,remark')->dateBetween('create_at')->order('id desc')->page();
  42. }
  43. /**
  44. * 数据列表处理
  45. * @param array $data
  46. */
  47. protected function _index_page_filter(array &$data)
  48. {
  49. UserAdminService::buildByUid($data);
  50. $uids = array_unique(array_column($data, 'create_by'));
  51. $users = SystemUser::mk()->whereIn('id', $uids)->column('username', 'id');
  52. $this->upgrades = BaseUserUpgrade::items();
  53. foreach ($data as &$vo) {
  54. $vo['upgradeinfo'] = $this->upgrades[$vo['upgrade']] ?? [];
  55. $vo['create_byname'] = $users[$vo['create_by']] ?? '';
  56. }
  57. }
  58. /**
  59. * 添加余额充值
  60. * @auth true
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public function add()
  66. {
  67. $data = $this->_vali(['uuid.require' => '用户UID不能为空!']);
  68. $this->user = DataUser::mk()->where(['id' => $data['uuid']])->find();
  69. if (empty($this->user)) $this->error('待充值的用户不存在!');
  70. DataUserBalance::mForm('form');
  71. }
  72. /**
  73. * 表单数据处理
  74. * @param array $data
  75. */
  76. protected function _form_filter(array &$data)
  77. {
  78. if (empty($data['code'])) {
  79. $data['code'] = CodeExtend::uniqidDate('20', 'B');
  80. }
  81. if ($this->request->isGet()) {
  82. $this->upgrades = BaseUserUpgrade::items();
  83. }
  84. if ($this->request->isPost()) {
  85. $data['create_by'] = AdminService::getUserId();
  86. if (empty(floatval($data['amount'])) && empty($data['upgrade'])) {
  87. $this->error('金额为零并且没有升级行为!');
  88. }
  89. }
  90. }
  91. /**
  92. * 表单结果处理
  93. * @param bool $state
  94. * @param array $data
  95. * @throws \think\db\exception\DbException
  96. */
  97. protected function _form_result(bool $state, array $data)
  98. {
  99. if ($state && isset($data['uuid'])) {
  100. UserBalanceService::amount($data['uuid']);
  101. UserUpgradeService::upgrade($data['uuid']);
  102. }
  103. }
  104. /**
  105. * 删除充值记录
  106. * @auth true
  107. */
  108. public function remove()
  109. {
  110. DataUserBalance::mDelete('', [['code', 'like', 'B%']]);
  111. }
  112. /**
  113. * 删除结果处理
  114. * @param bool $state
  115. * @throws \think\db\exception\DbException
  116. */
  117. protected function _delete_result(bool $state)
  118. {
  119. if ($state) {
  120. $map = [['id', 'in', str2arr(input('id', ''))]];
  121. foreach (DataUserBalance::mk()->where($map)->cursor() as $vo) {
  122. UserBalanceService::amount($vo['uuid']);
  123. UserUpgradeService::upgrade($vo['uuid']);
  124. }
  125. }
  126. }
  127. }