Balance.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 app\data\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([
  68. 'uuid.require' => '用户UID不能为空!'
  69. ],$this->request->isGet() ? 'get' : 'post');
  70. $this->user = DataUser::mk()->where(['id' => $data['uuid']])->find();
  71. if (empty($this->user)) $this->error('待充值的用户不存在!');
  72. DataUserBalance::mForm('form');
  73. }
  74. /**
  75. * 表单数据处理
  76. * @param array $data
  77. */
  78. protected function _form_filter(array &$data)
  79. {
  80. if (empty($data['code'])) {
  81. $data['code'] = CodeExtend::uniqidDate('20', 'B');
  82. }
  83. if ($this->request->isGet()) {
  84. $this->upgrades = BaseUserUpgrade::items();
  85. }
  86. if ($this->request->isPost()) {
  87. $data['create_by'] = AdminService::getUserId();
  88. if (empty(floatval($data['amount']))) {
  89. $this->error('金额为零!');
  90. }
  91. userMoneyChange($data['amount'],1,$data['uuid'],'后台余额充值',1,$data['create_by']);
  92. $this->success('成功');
  93. }
  94. }
  95. /**
  96. * 表单结果处理
  97. * @param bool $state
  98. * @param array $data
  99. * @throws \think\db\exception\DbException
  100. */
  101. protected function _form_result(bool $state, array $data)
  102. {
  103. if ($state && isset($data['uuid'])) {
  104. UserBalanceService::amount($data['uuid']);
  105. UserUpgradeService::upgrade($data['uuid']);
  106. }
  107. }
  108. /**
  109. * 删除充值记录
  110. * @auth true
  111. */
  112. public function remove()
  113. {
  114. DataUserBalance::mDelete('', [['code', 'like', 'B%']]);
  115. }
  116. /**
  117. * 删除结果处理
  118. * @param bool $state
  119. * @throws \think\db\exception\DbException
  120. */
  121. protected function _delete_result(bool $state)
  122. {
  123. if ($state) {
  124. $map = [['id', 'in', str2arr(input('id', ''))]];
  125. foreach (DataUserBalance::mk()->where($map)->cursor() as $vo) {
  126. UserBalanceService::amount($vo['uuid']);
  127. UserUpgradeService::upgrade($vo['uuid']);
  128. }
  129. }
  130. }
  131. }