MoneyLog.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. $params['create_month'] = date('Ym', strtotime($params['create_date']));
  94. $result = $row->save($params);
  95. if($this->callback){
  96. $callback=$this->callback;
  97. $callback($row);
  98. }
  99. Db::commit();
  100. } catch (\Exception $e) {
  101. Db::rollback();
  102. $this->error($e->getMessage());
  103. }
  104. if (false === $result) {
  105. $this->error(__('没有数据被更新'));
  106. }
  107. $this->success();
  108. }
  109. //删除
  110. #[Route("GET,POST","del")]
  111. public function del()
  112. {
  113. $ids = $this->request->param("ids");
  114. if (empty($ids)) {
  115. $this->error(__('参数%s不能为空', ['s'=>'ids']));
  116. }
  117. $pk = $this->model->getPk();
  118. $list = $this->model->where($pk, 'in', $ids)->select();
  119. $count = 0;
  120. Db::startTrans();
  121. try {
  122. foreach ($list as $item) {
  123. if(count($this->volidateFields)>0){
  124. foreach ($this->volidateFields as $field=>$value){
  125. if($item[$field]!=$value){
  126. $this->error(__('没有操作权限'));
  127. }
  128. }
  129. }
  130. $item->status=0;
  131. $count += $item->save();
  132. }
  133. if($this->callback){
  134. $callback=$this->callback;
  135. $callback($ids);
  136. }
  137. Db::commit();
  138. } catch (\Exception $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. }
  142. if ($count) {
  143. $this->success();
  144. }
  145. $this->error(__('没有记录被删除'));
  146. }
  147. //更新
  148. #[Route("GET,POST","multi")]
  149. public function multi()
  150. {
  151. //通过定义callback回调函数来执行更新后的操作
  152. $this->callback=function ($ids,$field,$value){};
  153. return $this->_multi();
  154. }
  155. //导入
  156. #[Route("GET,POST","import")]
  157. public function import()
  158. {
  159. //通过定义callback回调函数来处理导入的数据
  160. $this->callback=function ($inserData){
  161. return $inserData;
  162. };
  163. return $this->_import();
  164. }
  165. //获取分类
  166. private static function getTypeAllByType(int $type)
  167. {
  168. return ($type== 1)?site_config('addonsd.money_in_type'):site_config('addonsd.money_out_type');
  169. }
  170. }