MoneyLog.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller\user;
  4. use app\common\controller\Backend;
  5. use app\admin\traits\Actions;
  6. use think\annotation\route\Group;
  7. use think\annotation\route\Route;
  8. use think\facade\Db;
  9. use app\common\model\MoneyLog as MoneyLogModel;
  10. #[Group("user/money_log")]
  11. class MoneyLog extends Backend
  12. {
  13. use Actions{
  14. edit as private _edit;
  15. multi as private _multi;
  16. import as private _import;
  17. }
  18. protected function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new MoneyLogModel();
  22. $this->assign('typeInList', site_config('addonsd.money_in_type')); //
  23. $this->assign('typeOutList', site_config('addonsd.money_out_type'));
  24. $this->assign('bankList', site_config('addonsd.bank_account'));
  25. $this->relationField=['users'];
  26. }
  27. //查看
  28. #[Route('GET,JSON','index')]
  29. public function index()
  30. {
  31. if (false === $this->request->isAjax()) {
  32. return $this->fetch();
  33. }
  34. if($this->request->post('selectpage')){
  35. return $this->selectpage();
  36. }
  37. [$where, $order, $limit, $with] = $this->buildparams();
  38. $list = $this->model
  39. ->withJoin($with,'left')
  40. //如果没有使用operate filter过滤的情况下,推荐使用with关联,可以提高查询效率
  41. ->where('money_log.status', 1)
  42. ->where($where)
  43. ->order($order)
  44. ->paginate($limit);
  45. $result = ['total' => $list->total(), 'rows' => $list->items()];
  46. return json($result);
  47. }
  48. //修改
  49. #[Route("GET,POST","edit")]
  50. public function edit(mixed $row=null)
  51. {
  52. $ids = $this->request->get('ids');
  53. if(!$row || is_array($row)){
  54. $row = $this->model->find($ids);
  55. }
  56. if (!$row) {
  57. $this->error(__('没有找到记录'));
  58. }
  59. if(count($this->volidateFields)>0){
  60. foreach ($this->volidateFields as $field=>$value){
  61. if($row[$field]!=$value){
  62. $this->error(__('没有操作权限'));
  63. }
  64. }
  65. }
  66. if (false === $this->request->isPost()) {
  67. $this->assign('parentList', self::getTypeAllByType($row->type));
  68. $this->assign('row', $row);
  69. return $this->fetch();
  70. }
  71. $params = array_merge($this->request->post("row/a"),$this->postParams);
  72. if (empty($params)) {
  73. $this->error(__('提交的参数不能为空'));
  74. }
  75. if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){
  76. $this->error(__('token错误,请刷新页面重试'));
  77. }
  78. foreach ($params as &$value){
  79. if(is_array($value)){
  80. $value=implode(',',$value);
  81. }
  82. if($value===''){
  83. $value=null;
  84. }
  85. }
  86. $result = false;
  87. Db::startTrans();
  88. try {
  89. if(isEnglish($params['type_name'])){
  90. $arr = self::getTypeAllByType((int)$params['type']);
  91. $params['type_name'] = $arr[$params['type_name']];
  92. }
  93. $result = $row->save($params);
  94. if($this->callback){
  95. $callback=$this->callback;
  96. $callback($row);
  97. }
  98. Db::commit();
  99. } catch (\Exception $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. }
  103. if (false === $result) {
  104. $this->error(__('没有数据被更新'));
  105. }
  106. $this->success();
  107. }
  108. //删除
  109. #[Route("GET,POST","del")]
  110. public function del()
  111. {
  112. $ids = $this->request->param("ids");
  113. if (empty($ids)) {
  114. $this->error(__('参数%s不能为空', ['s'=>'ids']));
  115. }
  116. $pk = $this->model->getPk();
  117. $list = $this->model->where($pk, 'in', $ids)->select();
  118. $count = 0;
  119. Db::startTrans();
  120. try {
  121. foreach ($list as $item) {
  122. if(count($this->volidateFields)>0){
  123. foreach ($this->volidateFields as $field=>$value){
  124. if($item[$field]!=$value){
  125. $this->error(__('没有操作权限'));
  126. }
  127. }
  128. }
  129. $item->status=0;
  130. $count += $item->save();
  131. }
  132. if($this->callback){
  133. $callback=$this->callback;
  134. $callback($ids);
  135. }
  136. Db::commit();
  137. } catch (\Exception $e) {
  138. Db::rollback();
  139. $this->error($e->getMessage());
  140. }
  141. if ($count) {
  142. $this->success();
  143. }
  144. $this->error(__('没有记录被删除'));
  145. }
  146. //更新
  147. #[Route("GET,POST","multi")]
  148. public function multi()
  149. {
  150. //通过定义callback回调函数来执行更新后的操作
  151. $this->callback=function ($ids,$field,$value){};
  152. return $this->_multi();
  153. }
  154. //导入
  155. #[Route("GET,POST","import")]
  156. public function import()
  157. {
  158. //通过定义callback回调函数来处理导入的数据
  159. $this->callback=function ($inserData){
  160. return $inserData;
  161. };
  162. return $this->_import();
  163. }
  164. //获取分类
  165. private static function getTypeAllByType(int $type)
  166. {
  167. return ($type== 1)?site_config('addonsd.money_in_type'):site_config('addonsd.money_out_type');
  168. }
  169. }