Customer.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. /**
  6. * 客户
  7. */
  8. class Customer extends Apic
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = '*';
  12. //头部统计
  13. public function index(){
  14. $where = [
  15. 'company_id' => $this->auth->company_id,
  16. ];
  17. if($this->auth->type == 2){
  18. $where['staff_id'] = $this->auth->id;
  19. }
  20. //全部
  21. $customer_all = Db::name('user_wallet')->where($where)->count();
  22. //今日
  23. $starttime = strtotime(date('Y-m-d'));
  24. $endtime = $starttime + 86399;
  25. $where['createtime'] = ['BETWEEN',[$starttime,$endtime]];
  26. $customer_today = Db::name('user_wallet')->where($where)->count();
  27. //七日
  28. $starttime = strtotime(date('Y-m-d')) - 518400;
  29. $where['createtime'] = ['BETWEEN',[$starttime,$endtime]];
  30. $customer_week = Db::name('user_wallet')->where($where)->count();
  31. //
  32. $rs = [
  33. 'today'=>$customer_today,
  34. 'week' => $customer_week,
  35. 'all' => $customer_all,
  36. ];
  37. $this->success(1,$rs);
  38. }
  39. //列表
  40. public function lists(){
  41. $keyword = input('keyword','');
  42. $where = [
  43. 'w.company_id' => $this->auth->company_id,
  44. ];
  45. if(!empty($keyword)){
  46. $where['user.nickname|user.mobile'] = ['LIKE','%'.$keyword.'%'];
  47. }
  48. $list = Db::name('user_wallet')->alias('w')
  49. ->field('w.*,user.nickname,user.mobile,user.avatar')
  50. ->join('user','w.user_id = user.id','LEFT')
  51. ->where($where)
  52. ->order('id desc')->autopage()->select();
  53. $list = list_domain_image($list,['avatar']);
  54. //追加车牌
  55. if(!empty($list)){
  56. $user_ids = array_column($list,'user_id');
  57. $user_car = Db::name('user_car')->where('user_id','IN',$user_ids)->select();
  58. foreach($list as $key => &$val){
  59. $val['car_number'] = '';
  60. $car_number = [];
  61. foreach($user_car as $k => $v){
  62. if($val['user_id'] == $v['user_id']){
  63. $car_number[] = $v['car_number'];
  64. }
  65. $val['car_number'] = implode(',',$car_number);
  66. }
  67. }
  68. }
  69. $this->success(1,$list);
  70. }
  71. //新增
  72. public function add(){
  73. $this->success('添加成功');
  74. }
  75. //余额管理
  76. public function changemoney(){
  77. $id = input('id',0);
  78. $user_id = input('user_id',0);
  79. $type = input('type',1); //1增加,2减少
  80. $money = input('money',0);
  81. $number = $type == 1 ? $money : -$money;
  82. $logtype = $type == 1 ? 101 : 102;
  83. //检查
  84. $map = [
  85. 'id' => $id,
  86. 'user_id' => $user_id,
  87. 'company_id' => $this->auth->company_id,
  88. ];
  89. $check = Db::name('user_wallet')->where($map)->find();
  90. if(!$check){
  91. $this->error('错误的客户');
  92. }
  93. Db::startTrans();
  94. $rs = model('wallet')->lockChangeAccountRemain($this->auth->company_id,$user_id,'money',$number,$logtype,$remark='门店操作余额');
  95. if($rs['status'] === false){
  96. Db::rollback();
  97. $this->error($rs['msg']);
  98. }
  99. Db::commit();
  100. $this->success();
  101. }
  102. //客户详情
  103. public function userinfo(){
  104. $user_id = input('user_id',0);
  105. $map = [
  106. 'w.user_id' => $user_id,
  107. 'w.company_id' => $this->auth->company_id,
  108. ];
  109. $info = Db::name('user_wallet')->alias('w')
  110. ->field('w.*,user.nickname,user.mobile,user.avatar')
  111. ->join('user','w.user_id = user.id','LEFT')
  112. ->where($map)->find();
  113. $info = info_domain_image($info,['avatar']);
  114. $this->success(1,$info);
  115. }
  116. //某客户消费明细
  117. public function moneylog(){
  118. $user_id = input('user_id',0);
  119. $map = [
  120. 'user_id' => $user_id,
  121. 'company_id' => $this->auth->company_id,
  122. ];
  123. $list = Db::name('user_money_log')->where($map)->order('id desc')->autopage()->select();
  124. foreach($list as $key => &$val){
  125. $val['change_value'] = $val['change_value'] > 0 ? '+'.$val['change_value'] : $val['change_value'];
  126. $val['remark'] = '['.$val['remark'].'] '.$val['change_value'].',余额'.$val['remain'];
  127. }
  128. $this->success(1,$list);
  129. }
  130. }