UserWallet.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 用户余额管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class UserWallet extends Backend
  11. {
  12. /**
  13. * UserWallet模型对象
  14. * @var \app\admin\model\UserWallet
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\UserWallet;
  21. }
  22. /**
  23. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  24. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  25. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  26. */
  27. /**
  28. * 查看
  29. */
  30. public function index()
  31. {
  32. //当前是否为关联查询
  33. $this->relationSearch = true;
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags', 'trim']);
  36. if ($this->request->isAjax()) {
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('keyField')) {
  39. return $this->selectpage();
  40. }
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $list = $this->model
  43. ->with(['company','user','staff'])
  44. ->where($where)
  45. ->order($sort, $order)
  46. ->paginate($limit);
  47. foreach ($list as $row) {
  48. $row->getRelation('company')->visible(['name']);
  49. $row->getRelation('user')->visible(['nickname']);
  50. $row->getRelation('staff')->visible(['truename']);
  51. }
  52. $result = array("total" => $list->total(), "rows" => $list->items());
  53. return json($result);
  54. }
  55. return $this->view->fetch();
  56. }
  57. /**
  58. * 添加客户
  59. */
  60. public function newone(){
  61. if (!$this->request->post()) {
  62. $data = [
  63. '线下新客','老带新','平台引流','自然进店'
  64. ];
  65. $this->assign('comefrom',$data);
  66. return $this->view->fetch();
  67. }
  68. $field = ['nickname','mobile','car_number','address','comefrom','remark'];
  69. $data = request_post_hub($field);
  70. $user = Db::name('user')->field('id,nickname,mobile')->where('mobile',$data['mobile'])->find();
  71. //已经存在的用户
  72. if($user){
  73. //已经是我公司的客户
  74. $map = [
  75. 'w.user_id' => $user['id'],
  76. 'w.company_id' => $this->auth->company_id,
  77. ];
  78. $check = Db::name('user_wallet')->alias('w')
  79. ->field('w.*,staff.truename')
  80. ->join('company_staff staff','w.staff_id = staff.id','LEFT')
  81. ->where($map)->find();
  82. if($check){
  83. $this->error('已经是['.$check['truename'].']的客户,无需重复添加');
  84. }
  85. Db::startTrans();
  86. //添加新客户
  87. $new_data = [
  88. 'user_id' => $user['id'],
  89. 'company_id' => $this->auth->company_id,
  90. 'staff_id' => $this->auth->staff_id,
  91. 'money' => 0,
  92. 'address' => $data['address'],
  93. 'createtime' => time(),
  94. 'updatetime' => time(),
  95. 'comefrom' => $data['comefrom'],
  96. 'remark' => $data['remark'],
  97. ];
  98. $rs_customer = Db::name('user_wallet')->insertGetId($new_data);
  99. if(!$rs_customer){
  100. Db::rollback();
  101. $this->error('客户添加失败');
  102. }
  103. //加新车
  104. $car_map = [
  105. 'user_id' => $user['id'],
  106. 'car_number' => $data['car_number'],
  107. ];
  108. $car_info = Db::name('user_car')->where($car_map)->find();
  109. if(empty($car_info)){
  110. $car_map['createtime'] = time();
  111. $car_map['updatetime'] = time();
  112. $rs_car = Db::name('user_car')->insertGetId($car_map);
  113. if(!$rs_car){
  114. Db::rollback();
  115. $this->error('车辆添加失败');
  116. }
  117. }
  118. Db::commit();
  119. $this->success('添加完成');
  120. }else{
  121. //注册新用户
  122. //$introcode = User::column("introcode");
  123. $user_data = [
  124. 'nickname' => $data['nickname'],
  125. 'mobile' => $data['mobile'],
  126. 'avatar' => '/assets/img/avatar.png',
  127. //'introcode' => $this->getUinqueNo(8, $introcode),
  128. 'jointime' => time(),
  129. 'joinip' => request()->ip(),
  130. 'status' => 1,
  131. 'company_id'=> $this->auth->company_id,
  132. ];
  133. Db::startTrans();
  134. $user_id = Db::name('user')->insertGetId($user_data);
  135. if(!$user_id){
  136. Db::rollback();
  137. $this->error('添加客户失败');
  138. }
  139. $username = 'u' . (10000 + $user_id);
  140. Db::name('user')->where('id',$user_id)->update(['username'=>$username]);
  141. //添加新客户
  142. $new_data = [
  143. 'user_id' => $user_id,
  144. 'company_id' => $this->auth->company_id,
  145. 'staff_id' => $this->auth->staff_id,
  146. 'money' => 0,
  147. 'address' => $data['address'],
  148. 'createtime' => time(),
  149. 'updatetime' => time(),
  150. 'comefrom' => $data['comefrom'],
  151. 'remark' => $data['remark'],
  152. ];
  153. $rs_customer = Db::name('user_wallet')->insertGetId($new_data);
  154. if(!$rs_customer){
  155. Db::rollback();
  156. $this->error('客户添加失败');
  157. }
  158. //加新车
  159. $car_map = [
  160. 'user_id' => $user_id,
  161. 'car_number' => $data['car_number'],
  162. 'createtime' => time(),
  163. 'updatetime' => time(),
  164. ];
  165. $rs_car = Db::name('user_car')->insertGetId($car_map);
  166. if(!$rs_car){
  167. Db::rollback();
  168. $this->error('车辆添加失败');
  169. }
  170. Db::commit();
  171. $this->success('添加完成');
  172. }
  173. $this->success('添加成功');
  174. }
  175. }