123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use think\Db;
- class UserWallet extends Backend
- {
-
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\UserWallet;
- }
-
-
- public function index()
- {
-
- $this->relationSearch = true;
-
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
-
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $list = $this->model
- ->with(['company','user','staff'])
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
- foreach ($list as $row) {
-
- $row->getRelation('company')->visible(['name']);
- $row->getRelation('user')->visible(['nickname']);
- $row->getRelation('staff')->visible(['truename']);
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
-
- public function newone(){
- if (!$this->request->post()) {
- $data = [
- '线下新客','老带新','平台引流','自然进店'
- ];
- $this->assign('comefrom',$data);
- return $this->view->fetch();
- }
- $field = ['nickname','mobile','car_number','address','comefrom','remark'];
- $data = request_post_hub($field);
- $user = Db::name('user')->field('id,nickname,mobile')->where('mobile',$data['mobile'])->find();
-
- if($user){
-
- $map = [
- 'w.user_id' => $user['id'],
- 'w.company_id' => $this->auth->company_id,
- ];
- $check = Db::name('user_wallet')->alias('w')
- ->field('w.*,staff.truename')
- ->join('company_staff staff','w.staff_id = staff.id','LEFT')
- ->where($map)->find();
- if($check){
- $this->error('已经是['.$check['truename'].']的客户,无需重复添加');
- }
- Db::startTrans();
-
- $new_data = [
- 'user_id' => $user['id'],
- 'company_id' => $this->auth->company_id,
- 'staff_id' => $this->auth->staff_id,
- 'money' => 0,
- 'address' => $data['address'],
- 'createtime' => time(),
- 'updatetime' => time(),
- 'comefrom' => $data['comefrom'],
- 'remark' => $data['remark'],
- ];
- $rs_customer = Db::name('user_wallet')->insertGetId($new_data);
- if(!$rs_customer){
- Db::rollback();
- $this->error('客户添加失败');
- }
-
- $car_map = [
- 'user_id' => $user['id'],
- 'car_number' => $data['car_number'],
- ];
- $car_info = Db::name('user_car')->where($car_map)->find();
- if(empty($car_info)){
- $car_map['createtime'] = time();
- $car_map['updatetime'] = time();
- $rs_car = Db::name('user_car')->insertGetId($car_map);
- if(!$rs_car){
- Db::rollback();
- $this->error('车辆添加失败');
- }
- }
- Db::commit();
- $this->success('添加完成');
- }else{
-
-
- $user_data = [
- 'nickname' => $data['nickname'],
- 'mobile' => $data['mobile'],
- 'avatar' => '/assets/img/avatar.png',
-
- 'jointime' => time(),
- 'joinip' => request()->ip(),
- 'status' => 1,
- 'company_id'=> $this->auth->company_id,
- ];
- Db::startTrans();
- $user_id = Db::name('user')->insertGetId($user_data);
- if(!$user_id){
- Db::rollback();
- $this->error('添加客户失败');
- }
- $username = 'u' . (10000 + $user_id);
- Db::name('user')->where('id',$user_id)->update(['username'=>$username]);
-
- $new_data = [
- 'user_id' => $user_id,
- 'company_id' => $this->auth->company_id,
- 'staff_id' => $this->auth->staff_id,
- 'money' => 0,
- 'address' => $data['address'],
- 'createtime' => time(),
- 'updatetime' => time(),
- 'comefrom' => $data['comefrom'],
- 'remark' => $data['remark'],
- ];
- $rs_customer = Db::name('user_wallet')->insertGetId($new_data);
- if(!$rs_customer){
- Db::rollback();
- $this->error('客户添加失败');
- }
-
- $car_map = [
- 'user_id' => $user_id,
- 'car_number' => $data['car_number'],
- 'createtime' => time(),
- 'updatetime' => time(),
- ];
- $rs_car = Db::name('user_car')->insertGetId($car_map);
- if(!$rs_car){
- Db::rollback();
- $this->error('车辆添加失败');
- }
- Db::commit();
- $this->success('添加完成');
- }
- $this->success('添加成功');
- }
- }
|