Customer.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. $carnumber = '';
  63. foreach($user_car as $k => $v){
  64. if($val['user_id'] == $v['user_id']){
  65. $carnumber .= $v['car_number'];
  66. if(!empty($v['car_model'])){
  67. $carnumber .= '('.$v['car_model'].')';
  68. }
  69. $carnumber .= ',';
  70. }
  71. }
  72. if($carnumber != ''){
  73. $carnumber = substr($carnumber,0,-1);
  74. }
  75. $val['car_number'] = $carnumber;
  76. }
  77. }
  78. $this->success(1,$list);
  79. }
  80. //新增
  81. public function add(){
  82. $field = ['nickname','mobile','car_number','address','comefrom','remark'];
  83. $data = request_post_hub($field);
  84. $user = Db::name('user')->field('id,nickname,mobile')->where('mobile',$data['mobile'])->find();
  85. //已经存在的用户
  86. if($user){
  87. //已经是我公司的客户
  88. $map = [
  89. 'w.user_id' => $user['id'],
  90. 'w.company_id' => $this->auth->company_id,
  91. ];
  92. $check = Db::name('user_wallet')->alias('w')
  93. ->field('w.*,staff.truename')
  94. ->join('company_staff staff','w.staff_id = staff.id','LEFT')
  95. ->where($map)->find();
  96. if($check){
  97. $this->error('已经是['.$check['truename'].']的客户,无需重复添加');
  98. }
  99. Db::startTrans();
  100. //添加新客户
  101. $new_data = [
  102. 'user_id' => $user['id'],
  103. 'company_id' => $this->auth->company_id,
  104. 'staff_id' => $this->auth->id,
  105. 'money' => 0,
  106. 'address' => $data['address'],
  107. 'createtime' => time(),
  108. 'updatetime' => time(),
  109. 'comefrom' => $data['comefrom'],
  110. 'remark' => $data['remark'],
  111. ];
  112. $rs_customer = Db::name('user_wallet')->insertGetId($new_data);
  113. if(!$rs_customer){
  114. Db::rollback();
  115. $this->error('客户添加失败');
  116. }
  117. //加新车
  118. $car_map = [
  119. 'user_id' => $user['id'],
  120. 'car_number' => $data['car_number'],
  121. ];
  122. $car_info = Db::name('user_car')->where($car_map)->find();
  123. if(empty($car_info)){
  124. $car_map['createtime'] = time();
  125. $car_map['updatetime'] = time();
  126. $rs_car = Db::name('user_car')->insertGetId($car_map);
  127. if(!$rs_car){
  128. Db::rollback();
  129. $this->error('车辆添加失败');
  130. }
  131. }
  132. Db::commit();
  133. $this->success('添加完成');
  134. }else{
  135. //注册新用户
  136. //$introcode = User::column("introcode");
  137. $user_data = [
  138. 'nickname' => $data['nickname'],
  139. 'mobile' => $data['mobile'],
  140. 'avatar' => '/assets/img/avatar.png',
  141. //'introcode' => $this->getUinqueNo(8, $introcode),
  142. 'jointime' => time(),
  143. 'joinip' => request()->ip(),
  144. 'status' => 1,
  145. 'company_id'=> $this->auth->company_id,
  146. ];
  147. Db::startTrans();
  148. $user_id = Db::name('user')->insertGetId($user_data);
  149. if(!$user_id){
  150. Db::rollback();
  151. $this->error('添加客户失败');
  152. }
  153. $username = 'u' . (10000 + $user_id);
  154. Db::name('user')->where('id',$user_id)->update(['username'=>$username]);
  155. //添加新客户
  156. $new_data = [
  157. 'user_id' => $user_id,
  158. 'company_id' => $this->auth->company_id,
  159. 'staff_id' => $this->auth->id,
  160. 'money' => 0,
  161. 'address' => $data['address'],
  162. 'createtime' => time(),
  163. 'updatetime' => time(),
  164. 'comefrom' => $data['comefrom'],
  165. 'remark' => $data['remark'],
  166. ];
  167. $rs_customer = Db::name('user_wallet')->insertGetId($new_data);
  168. if(!$rs_customer){
  169. Db::rollback();
  170. $this->error('客户添加失败');
  171. }
  172. //加新车
  173. $car_map = [
  174. 'user_id' => $user_id,
  175. 'car_number' => $data['car_number'],
  176. 'createtime' => time(),
  177. 'updatetime' => time(),
  178. ];
  179. $rs_car = Db::name('user_car')->insertGetId($car_map);
  180. if(!$rs_car){
  181. Db::rollback();
  182. $this->error('车辆添加失败');
  183. }
  184. Db::commit();
  185. $this->success('添加完成');
  186. }
  187. $this->success('添加成功');
  188. }
  189. /**
  190. * 生成不重复的随机数字字母组合
  191. */
  192. function getUinqueNo($length = 8, $nos = [])
  193. {
  194. $newid = Random::build("alnum", $length);
  195. if (in_array($newid, $nos)) {
  196. $newid = $this->getUinqueNo($length, $nos);
  197. }
  198. return $newid;
  199. }
  200. //检索用户
  201. public function searchuser(){
  202. $mobile = input('mobile','');
  203. $check = Db::name('user')->field('id,nickname,mobile')->where('mobile',$mobile)->find();
  204. $this->success(1,$check);
  205. }
  206. //余额管理
  207. public function changemoney(){
  208. $id = input('id',0);
  209. $user_id = input('user_id',0);
  210. $type = input('type',1); //1增加,2减少
  211. $money = input('money',0);
  212. $number = $type == 1 ? $money : -$money;
  213. $logtype = $type == 1 ? 101 : 102;
  214. //验证
  215. if($this->auth->type != 1){
  216. $this->error('只有门店老板才能操作');
  217. }
  218. //检查
  219. $map = [
  220. 'id' => $id,
  221. 'user_id' => $user_id,
  222. 'company_id' => $this->auth->company_id,
  223. ];
  224. $check = Db::name('user_wallet')->where($map)->find();
  225. if(!$check){
  226. $this->error('错误的客户');
  227. }
  228. Db::startTrans();
  229. $rs = model('wallet')->lockChangeAccountRemain($this->auth->company_id,$user_id,'money',$number,$logtype,'门店操作余额('.$this->auth->truename.')');
  230. if($rs['status'] === false){
  231. Db::rollback();
  232. $this->error($rs['msg']);
  233. }
  234. Db::commit();
  235. $this->success();
  236. }
  237. //客户详情
  238. public function userinfo(){
  239. $user_id = input('user_id',0);
  240. $map = [
  241. 'w.user_id' => $user_id,
  242. 'w.company_id' => $this->auth->company_id,
  243. ];
  244. $info = Db::name('user_wallet')->alias('w')
  245. ->field('w.*,user.nickname,user.mobile,user.avatar')
  246. ->join('user','w.user_id = user.id','LEFT')
  247. ->where($map)->find();
  248. $info = info_domain_image($info,['avatar']);
  249. $this->success(1,$info);
  250. }
  251. //某客户消费明细
  252. public function moneylog(){
  253. $user_id = input('user_id',0);
  254. $map = [
  255. 'user_id' => $user_id,
  256. 'company_id' => $this->auth->company_id,
  257. ];
  258. $list = Db::name('user_money_log')->where($map)->order('id desc')->autopage()->select();
  259. foreach($list as $key => &$val){
  260. $val['change_value'] = $val['change_value'] > 0 ? '+'.$val['change_value'] : $val['change_value'];
  261. $val['remark'] = '['.$val['remark'].'] '.$val['change_value'].',余额'.$val['remain'];
  262. }
  263. $this->success(1,$list);
  264. }
  265. }