Balance.php 4.4 KB

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