JewelLog.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 用户钻石流水记录
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class JewelLog extends Backend
  13. {
  14. protected $searchFields = 'user.u_id,user.nickname';
  15. /**
  16. * JewelLog模型对象
  17. * @var \app\admin\model\user\JewelLog
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\user\JewelLog;
  24. $this->view->assign("typeList", $this->model->getTypeList());
  25. }
  26. public function import()
  27. {
  28. parent::import();
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 列表
  37. */
  38. public function index()
  39. {
  40. //当前是否为关联查询
  41. $this->relationSearch = true;
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if ($this->request->isAjax()) {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $list = $this->model
  51. ->with(['user'])
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. foreach ($list as $row) {
  56. $row->getRelation('user')->visible(['u_id','nickname']);
  57. }
  58. $result = array("total" => $list->total(), "rows" => $list->items());
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 添加
  65. */
  66. public function add()
  67. {
  68. if ($this->request->isPost()) {
  69. $params = $this->request->post("row/a");
  70. if ($params) {
  71. $params = $this->preExcludeFields($params);
  72. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  73. $params[$this->dataLimitField] = $this->auth->id;
  74. }
  75. Db::startTrans();
  76. try {
  77. //是否采用模型验证
  78. if ($this->modelValidate) {
  79. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  80. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  81. $this->model->validateFailException(true)->validate($validate);
  82. }
  83. // 获取用户的变更前余额
  84. $userJewel = \app\admin\model\User::where('id', $params['user_id'])->value('jewel');
  85. $params['before'] = $userJewel;
  86. $adjustmentValue = intval($params['value']);
  87. switch ($params['mode']) {
  88. case '+':
  89. $params['balance'] = $userJewel + $adjustmentValue;
  90. break;
  91. case '-':
  92. if ($adjustmentValue > $userJewel) {
  93. $this->error('调整砖石余额大于现有砖石余额');
  94. }
  95. $params['balance'] = $userJewel - $adjustmentValue;
  96. break;
  97. default:
  98. $this->error('请输入正确的变更方式');
  99. }
  100. $res1 = $this->model->allowField(true)->save($params);
  101. // 调整用户声币余额
  102. $res2 = \app\admin\model\User::where(["id" => $params['user_id']])->update([
  103. 'jewel' => Db::raw("jewel{$params['mode']}{$adjustmentValue}"),
  104. ]);
  105. if ($res1 && $res2) {
  106. Db::commit();
  107. $this->success();
  108. } else {
  109. $this->error(__('Operation failed'));
  110. }
  111. Db::commit();
  112. } catch (ValidateException $e) {
  113. Db::rollback();
  114. $this->error($e->getMessage());
  115. } catch (PDOException $e) {
  116. Db::rollback();
  117. $this->error($e->getMessage());
  118. } catch (Exception $e) {
  119. Db::rollback();
  120. $this->error($e->getMessage());
  121. }
  122. }
  123. $this->error(__('Parameter %s can not be empty', ''));
  124. }
  125. return $this->view->fetch();
  126. }
  127. }