Balance.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\WorkerBalance;
  4. use think\App;
  5. use think\Db;
  6. use think\Exception;
  7. class Balance extends Auth {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->model = new \app\common\model\Worker();
  11. }
  12. public function index() {
  13. if (request()->isAjax()) {
  14. //分页参数
  15. $length = input('rows', 10, 'intval'); //每页条数
  16. $page = input('page', 1, 'intval'); //第几页
  17. $title = input('title', '', 'trim');
  18. $map = [];
  19. if ($title) {
  20. $map['name'] = $title;
  21. }
  22. $recommend = input('recommend', '', 'trim');
  23. if ($recommend != '') {
  24. $map['recommend'] = $recommend;
  25. }
  26. $enable = input('enable', '', 'trim');
  27. if ($enable != '') {
  28. $map['enable'] = $enable;
  29. }
  30. $gender = input('gender', '', 'trim');
  31. if ($gender != '') {
  32. $map['gender'] = $gender;
  33. }
  34. $map['balance'] = 1;
  35. //数据查询
  36. $result = $this->model->lists($this->orgId, $map, $page, $length);
  37. return json($result);
  38. }
  39. else {
  40. return $this->fetch();
  41. }
  42. }
  43. public function js(){
  44. $data = request()->post();
  45. $ids = $data['ids'];
  46. $balanceModel = new WorkerBalance();
  47. $model = new \app\common\model\Worker();
  48. $succ = 0;
  49. $err = 0;
  50. $er = [];
  51. $logId = Db::name('settlement_log')
  52. ->insertGetId([
  53. 'user_id'=>$this->userId,
  54. 'org_id'=>$this->orgId,
  55. 'create_time'=>date('Y-m-d H:i:s'),
  56. ]);
  57. if(!$logId){
  58. $this->error('结算记录保存失败');
  59. }
  60. foreach ($ids as $k=>$id){
  61. $user_id = Db::name('worker')
  62. ->where('id',$id)
  63. ->value('user_id');
  64. $info = $model->getUserInfo($user_id);
  65. $res = $balanceModel->saveWorkerBalance($this->orgId,
  66. -$info['balance'],$this->userId,$id,2,'',0,$logId);
  67. if($res){
  68. $succ++;
  69. }else{
  70. $err++;
  71. $er[] = $info['real_name'].':'.$balanceModel->getError();
  72. }
  73. }
  74. if(empty($er)){
  75. $msg = '成功:'.$succ.'失败:'.$err;
  76. $a = ['code'=>0,'msg'=>$msg];
  77. }else{
  78. $msg = '成功:'.$succ.'失败:'.$err.' 失败原因:'.json_encode($er,JSON_UNESCAPED_UNICODE);
  79. $a = ['code'=>1,'msg'=>$msg];
  80. }
  81. $this->success('','',$a);
  82. }
  83. }