Customer.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. use app\common\model\User;
  6. use fast\Random;
  7. /**
  8. * 客户
  9. */
  10. class Customer extends Apic
  11. {
  12. protected $noNeedLogin = [];
  13. protected $noNeedRight = '*';
  14. //头部统计
  15. public function index(){
  16. $where = [
  17. 'company_id' => $this->auth->company_id,
  18. ];
  19. if($this->auth->type == 2){
  20. $where['staff_id'] = $this->auth->id;
  21. }
  22. //全部
  23. $customer_all = Db::name('user_wallet')->where($where)->count();
  24. //今日
  25. $starttime = strtotime(date('Y-m-d'));
  26. $endtime = $starttime + 86399;
  27. $where['createtime'] = ['BETWEEN',[$starttime,$endtime]];
  28. $customer_today = Db::name('user_wallet')->where($where)->count();
  29. //七日
  30. $starttime = strtotime(date('Y-m-d')) - 518400;
  31. $where['createtime'] = ['BETWEEN',[$starttime,$endtime]];
  32. $customer_week = Db::name('user_wallet')->where($where)->count();
  33. //
  34. $rs = [
  35. 'today'=>$customer_today,
  36. 'week' => $customer_week,
  37. 'all' => $customer_all,
  38. ];
  39. $this->success(1,$rs);
  40. }
  41. //列表
  42. public function lists(){
  43. $keyword = input('keyword','');
  44. $where = [
  45. 'w.company_id' => $this->auth->company_id,
  46. ];
  47. if(!empty($keyword)){
  48. $where['user.nickname|user.mobile'] = ['LIKE','%'.$keyword.'%'];
  49. }
  50. $list = Db::name('user_wallet')->alias('w')
  51. ->field('w.*,user.nickname,user.mobile,user.avatar')
  52. ->join('user','w.user_id = user.id','LEFT')
  53. ->where($where)
  54. ->order('id desc')->autopage()->select();
  55. $list = list_domain_image($list,['avatar']);
  56. //追加车牌
  57. if(!empty($list)){
  58. $user_ids = array_column($list,'user_id');
  59. $user_car = Db::name('user_car')->where('user_id','IN',$user_ids)->select();
  60. foreach($list as $key => &$val){
  61. $val['car_number'] = '';
  62. $car_number = [];
  63. foreach($user_car as $k => $v){
  64. if($val['user_id'] == $v['user_id']){
  65. $car_number[] = $v['car_number'];
  66. }
  67. $val['car_number'] = implode(',',$car_number);
  68. }
  69. }
  70. }
  71. $this->success(1,$list);
  72. }
  73. //新增
  74. public function add(){
  75. $field = ['nickname','mobile','car_number','address','comefrom','remark'];
  76. $data = request_post_hub($field);
  77. $user = Db::name('user')->field('id,nickname,mobile')->where('mobile',$data['mobile'])->find();
  78. //已经存在的用户
  79. if($user){
  80. //已经是我公司的客户
  81. $map = [
  82. 'w.user_id' => $user['id'],
  83. 'w.company_id' => $this->auth->company_id,
  84. ];
  85. $check = Db::name('user_wallet')->alias('w')
  86. ->field('w.*,staff.truename')
  87. ->join('company_staff staff','w.staff_id = staff.id','LEFT')
  88. ->where($map)->find();
  89. if($check){
  90. $this->error('已经是['.$check['truename'].']的客户,无需重复添加');
  91. }
  92. Db::startTrans();
  93. //添加新客户
  94. $new_data = [
  95. 'user_id' => $user['id'],
  96. 'company_id' => $this->auth->company_id,
  97. 'staff_id' => $this->auth->id,
  98. 'money' => 0,
  99. 'address' => $data['address'],
  100. 'createtime' => time(),
  101. 'updatetime' => time(),
  102. 'comefrom' => $data['comefrom'],
  103. 'remark' => $data['remark'],
  104. ];
  105. $rs_customer = Db::name('user_wallet')->insertGetId($new_data);
  106. if(!$rs_customer){
  107. Db::rollback();
  108. $this->error('客户添加失败');
  109. }
  110. //加新车
  111. $car_map = [
  112. 'user_id' => $user['id'],
  113. 'car_number' => $data['car_number'],
  114. ];
  115. $car_info = Db::name('user_car')->where($car_map)->find();
  116. if(empty($car_info)){
  117. $car_map['createtime'] = time();
  118. $car_map['updatetime'] = time();
  119. $rs_car = Db::name('user_car')->insertGetId($car_map);
  120. if(!$rs_car){
  121. Db::rollback();
  122. $this->error('车辆添加失败');
  123. }
  124. }
  125. Db::commit();
  126. $this->success('添加完成');
  127. }else{
  128. //注册新用户
  129. //$introcode = User::column("introcode");
  130. $user_data = [
  131. 'nickname' => $data['nickname'],
  132. 'mobile' => $data['mobile'],
  133. 'avatar' => '/assets/img/avatar.png',
  134. //'introcode' => $this->getUinqueNo(8, $introcode),
  135. 'jointime' => time(),
  136. 'joinip' => request()->ip(),
  137. 'status' => 1,
  138. 'company_id'=> $this->auth->company_id,
  139. ];
  140. Db::startTrans();
  141. $user_id = Db::name('user')->insertGetId($user_data);
  142. if(!$user_id){
  143. Db::rollback();
  144. $this->error('添加客户失败');
  145. }
  146. $username = 'u' . (10000 + $user_id);
  147. Db::name('user')->where('id',$user_id)->update(['username'=>$username]);
  148. //添加新客户
  149. $new_data = [
  150. 'user_id' => $user_id,
  151. 'company_id' => $this->auth->company_id,
  152. 'staff_id' => $this->auth->id,
  153. 'money' => 0,
  154. 'address' => $data['address'],
  155. 'createtime' => time(),
  156. 'updatetime' => time(),
  157. 'comefrom' => $data['comefrom'],
  158. 'remark' => $data['remark'],
  159. ];
  160. $rs_customer = Db::name('user_wallet')->insertGetId($new_data);
  161. if(!$rs_customer){
  162. Db::rollback();
  163. $this->error('客户添加失败');
  164. }
  165. //加新车
  166. $car_map = [
  167. 'user_id' => $user_id,
  168. 'car_number' => $data['car_number'],
  169. 'createtime' => time(),
  170. 'updatetime' => time(),
  171. ];
  172. $rs_car = Db::name('user_car')->insertGetId($car_map);
  173. if(!$rs_car){
  174. Db::rollback();
  175. $this->error('车辆添加失败');
  176. }
  177. Db::commit();
  178. $this->success('添加完成');
  179. }
  180. $this->success('添加成功');
  181. }
  182. /**
  183. * 生成不重复的随机数字字母组合
  184. */
  185. function getUinqueNo($length = 8, $nos = [])
  186. {
  187. $newid = Random::build("alnum", $length);
  188. if (in_array($newid, $nos)) {
  189. $newid = $this->getUinqueNo($length, $nos);
  190. }
  191. return $newid;
  192. }
  193. //检索用户
  194. public function searchuser(){
  195. $mobile = input('mobile','');
  196. $check = Db::name('user')->field('id,nickname,mobile')->where('mobile',$mobile)->find();
  197. $this->success(1,$check);
  198. }
  199. //余额管理
  200. public function changemoney(){
  201. $id = input('id',0);
  202. $user_id = input('user_id',0);
  203. $type = input('type',1); //1增加,2减少
  204. $money = input('money',0);
  205. $number = $type == 1 ? $money : -$money;
  206. $logtype = $type == 1 ? 101 : 102;
  207. //验证
  208. if($this->auth->type != 1){
  209. $this->error('只有门店老板才能操作');
  210. }
  211. //检查
  212. $map = [
  213. 'id' => $id,
  214. 'user_id' => $user_id,
  215. 'company_id' => $this->auth->company_id,
  216. ];
  217. $check = Db::name('user_wallet')->where($map)->find();
  218. if(!$check){
  219. $this->error('错误的客户');
  220. }
  221. Db::startTrans();
  222. $rs = model('wallet')->lockChangeAccountRemain($this->auth->company_id,$user_id,'money',$number,$logtype,$this->auth->mobile.'门店操作余额');
  223. if($rs['status'] === false){
  224. Db::rollback();
  225. $this->error($rs['msg']);
  226. }
  227. Db::commit();
  228. $this->success();
  229. }
  230. //客户详情
  231. public function userinfo(){
  232. $user_id = input('user_id',0);
  233. $map = [
  234. 'w.user_id' => $user_id,
  235. 'w.company_id' => $this->auth->company_id,
  236. ];
  237. $info = Db::name('user_wallet')->alias('w')
  238. ->field('w.*,user.nickname,user.mobile,user.avatar')
  239. ->join('user','w.user_id = user.id','LEFT')
  240. ->where($map)->find();
  241. $info = info_domain_image($info,['avatar']);
  242. $this->success(1,$info);
  243. }
  244. //某客户消费明细
  245. public function moneylog(){
  246. $user_id = input('user_id',0);
  247. $map = [
  248. 'user_id' => $user_id,
  249. 'company_id' => $this->auth->company_id,
  250. ];
  251. $list = Db::name('user_money_log')->where($map)->order('id desc')->autopage()->select();
  252. foreach($list as $key => &$val){
  253. $val['change_value'] = $val['change_value'] > 0 ? '+'.$val['change_value'] : $val['change_value'];
  254. $val['remark'] = '['.$val['remark'].'] '.$val['change_value'].',余额'.$val['remain'];
  255. }
  256. $this->success(1,$list);
  257. }
  258. }