Blame view

application/admin/controller/antis/customer/Cnki.php 14.4 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  <?php

  

  namespace app\admin\controller\antis\customer;

  

  use addons\antis\model\ContactsFile;

  use addons\antis\model\ReceivablesPlan;

  use app\admin\controller\antis\Base;

  use addons\antis\model\Staff;

  use think\Db;

  use think\Exception;

  use addons\antis\model\Customer;

  use addons\antis\model\Contacts;

  use addons\antis\model\Form;

  use addons\antis\model\CustomerOther;

  use addons\antis\model\Receivables;

  use addons\antis\model\Record;

  use addons\antis\model\Contract;

  use addons\antis\model\Comment;

  use addons\antis\model\Consume;

  /**

   * 客户查重管理

   *  操作文档:https://doc.fastadmin.net/antis

   * 软件介绍:https://www.fastadmin.net/store/antis.html

   * 售后微信:qingdong_crm

   */

  class Cnki extends Base

  {

      protected $relationSearch = true;

      /**

       * @var \addons\antis\model\Customer

       */

      protected $model = null;

  

  

      public function _initialize()

      {

          parent::_initialize();

          $this->model = new \addons\antis\model\Customer;

      }

  

  

      /**

       * 查看

       */

      public function index()

      {

          //设置过滤方法

          $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();

              $customer_ids_name=Customer::where('name in '.Customer::where([])->having('count(*) > 1')->group('name')->field('name')->buildSql(true))->column('id');

              $customer_ids_mobile= Contacts::where('mobile in '. Contacts::where([])->having('count(*) > 1')->group('mobile')->where(['mobile'=>['neq','']])->field('mobile')->buildSql(true))->column('customer_id');

              $customer_ids_email = Contacts::where('email in '.Contacts::where([])->having('count(*) > 1')->group('email,customer_id')->where(['email'=>['neq','']])->field('email')->buildSql(true))->column('customer_id');

              $customer_ids=array_merge($customer_ids_name?:[],$customer_ids_mobile?:[],$customer_ids_email?:[]);

              $customer_ids=array_values(array_unique($customer_ids));

              $list = $this->model->where($where)

                  ->where(['id'=>['in',$customer_ids]])

                  ->order($sort, $order)->paginate($limit);

              $rows = $list->items();

              foreach($rows as $k=>$v){

                  $contacts = Contacts::where(array('customer_id'=>$v['id']))->select();

                  foreach($contacts as $ks=>$vs){

                      $emailinfo='';

                      $mobileinfo ='';

                      if($vs['mobile']){

                          $mobileinfo = Contacts::where(array('id'=>['neq',$vs['id']],'customer_id'=>$v['id'],'mobile'=>$vs['mobile']))->value('id');

                      }

                      if($vs['email']){

                          $emailinfo = Contacts::where(array('id'=>['neq',$vs['id']],'customer_id'=>$v['id'],'email'=>$vs['email']))->value('id');

                      }

                      if($mobileinfo || $emailinfo){

                              unset($rows[$k]);

                      }

                  }

  

              }

              $result = array("total" => $list->total(), "rows" => array_values($rows));

  

              return json($result);

          }

  

          return $this->view->fetch();

      }

  

      /**

       * 合并客户

       */

      public function edit($ids=null)

      {

          if ($this->request->isPost()) {

              $params = $this->request->post("row/a");

              if ($params) {

  

                  $check = isset($params['check']) ? $params['check'] : '';

                  $ids = $params['id'];

                  $customer_ids = $params['customer_ids'];

                  unset($params['check']);

                  unset($params['customer_ids']);

                  $params = $this->preExcludeFields($params);

                  $result = false;

                  Db::startTrans();

                  try {

                      $params['id'] = $ids;

                      $result = $this->model::updateCustomer($params);

                      //合并后删除被合并客户

                      $rescustomer = true;

                      $rescontacts = true;

                      $resrecord = true;

                      $rescontract = true;

                      $resconsume = true;

                      $resreceivables = true;

                      $resreceivables_plan = true;

                      $rescomment = true;

                      if($check == 1){

                          //客户

                          $hecustomer = $this->model->where(array('id'=>$customer_ids))->find();

                          if($hecustomer){

                              $rescustomer = $this->model->where(array('id'=>$customer_ids))->update(array('deletetime'=>time()));

                          }

                          //联系人

                          $hecontacts =Contacts::where(array('customer_id'=>$customer_ids))->find();

                          if($hecontacts){

                              $rescontacts = Contacts::where(array('customer_id'=>$customer_ids))->update(array('customer_id'=>$ids,'updatetime'=>time()));

                          }

                          //跟进记录

                          $herecord = Record::where(array('relation_type'=>1,'relation_id'=>$customer_ids))->find();

                          if($herecord){

                              $resrecord = Record::where(array('relation_type'=>1,'relation_id'=>$customer_ids))->update(array('relation_id'=>$ids,'updatetime'=>time()));

                          }

                          //合同

                          $hecontract=Contract::where(array('customer_id'=>$customer_ids))->find();

                          if($hecontract){

                              $rescontract=Contract::where(array('customer_id'=>$customer_ids))->update(array('customer_id'=>$ids,'updatetime'=>time()));

                          }

                          //费用

                          $heconsume = Consume::where(array('customer_id'=>$customer_ids))->find();

                          if($heconsume){

                              $resconsume = Consume::where(array('customer_id'=>$customer_ids))->update(array('customer_id'=>$ids,'updatetime'=>time()));

                          }

                          //回款

                          $hereceivables = Receivables::where(array('customer_id'=>$customer_ids))->find();

                          if($hereceivables){

                              $resreceivables = Receivables::where(array('customer_id'=>$customer_ids))->update(array('customer_id'=>$ids,'updatetime'=>time()));

                          }

                          //回款计划

                          $hereceivables_plan = ReceivablesPlan::where(array('customer_id'=>$customer_ids))->find();

                          if($hereceivables_plan){

                              $resreceivables_plan = ReceivablesPlan::where(array('customer_id'=>$customer_ids))->update(array('customer_id'=>$ids,'updatetime'=>time()));

                          }

                          //评论

                          $hecomment = Comment::where(array('relation_type'=>1,'relation_id'=>$customer_ids))->find();

                          if($hecomment){

                              $rescomment = Comment::where(array('relation_type'=>1,'relation_id'=>$customer_ids))->update(array('relation_id'=>$ids,'updatetime'=>time()));

  

                          }

                          if(!$rescustomer || !$rescontacts || !$resrecord  || !$rescontract || !$resconsume || !$resreceivables || !$resreceivables_plan || !$rescomment){

                              $result = false;

                              throw new Exception('合并失败');

                          }

                      }else{

                          //联系人

                          $hecontacts = collection(Contacts::where(array('customer_id'=>$customer_ids))->select())->toArray();

                          foreach($hecontacts as $k=>$v){

                              unset($v['id']);

                              $mobileinfo = Contacts::where(array('customer_id'=>$customer_ids,'mobile'=>$v['mobile']))->value('id');

                              $emailinfo = Contacts::where(array('customer_id'=>$customer_ids,'email'=>$v['email']))->value('id');

                              if($emailinfo){

                                  $v['email'] = '';

                              }

                              if(($mobileinfo && $emailinfo) || $mobileinfo){

                                  continue;

                              }

                              $v['customer_id'] = $ids;

                              $rescontacts = Contacts::create($v);

                          }

  

                          //跟进记录

                          $herecords = collection(Record::where(array('relation_type'=>1,'relation_id'=>$customer_ids))->select())->toArray();

                          foreach($herecords as $k=>$vs){

                              unset($vs['id']);

                              $vs['relation_id'] = $ids;

                              $resrecord = Record::create($vs);

                          }

                          //合同

                          $hecontract=collection(Contract::where(array('customer_id'=>$customer_ids))->select())->toArray();

                          foreach($hecontract as $k=>$v){

                              unset($v['id']);

                              $v['customer_id'] = $ids;

                              $rescontract = Contract::create($v);

                          }

                          //费用

                          $heconsume = collection(Consume::where(array('customer_id'=>$customer_ids))->select())->toArray();

                          foreach($heconsume as $k=>$v){

                              unset($v['id']);

                              $v['customer_id'] = $ids;

                              $resconsume = Consume::create($v);

                          }

                          //回款

                          $hereceivables = collection(Receivables::where(array('customer_id'=>$customer_ids))->select())->toArray();

                          foreach($hereceivables as $k=>$v){

                              unset($v['id']);

                              $v['customer_id'] = $ids;

                              $resreceivables = Receivables::create($v);

                          }

                          //回款计划

                          $hereceivables_plan = collection(ReceivablesPlan::where(array('customer_id'=>$customer_ids))->select())->toArray();

                          foreach($hereceivables_plan as $k=>$v){

                              unset($v['id']);

                              $v['customer_id'] = $ids;

                              $resreceivables_plan = ReceivablesPlan::create($v);

                          }

                          //评论

                          $hecomment = collection(Comment::where(array('relation_type'=>1,'relation_id'=>$customer_ids))->select())->toArray();

                          foreach($hecomment as $k=>$v){

                              unset($v['id']);

                              $v['relation_id'] = $ids;

                              $rescomment = Comment::create($v);

                          }

  

                          if(!$rescontacts || !$resrecord  || !$rescontract || !$resconsume || !$resreceivables || !$resreceivables_plan || !$rescomment){

                              $result = false;

                              throw new Exception('合并失败');

                          }

                      }

                      Db::commit();

                  } catch (Exception $e) {

                      Db::rollback();

                      $this->error($e->getMessage());

                  }

                  if ($result !== false) {

                      $this->success();

                  } else {

                      $this->error('合并失败');

                  }

              }

              $this->error('参数不能为空');

          }

          //客户

          $row = Customer::where(array('id'=>$ids))->find();

          if(!$row){

              $this->error('客户不存在');

          }

          $row=$row->toArray();

          $row = CustomerOther::getOther($row);

          $fieldlist = Form::getDataValue('customer');

  

          //被合并客户信息

          $customer_ids = Customer::where(array('name'=>$row['name'],'id'=>['neq',$ids]))->value('id');

          if(!$customer_ids){

              //手机号

              $wheres['customer_id'] = array('neq',$ids);

              $customer_ids_mobile = Contacts::where(array('customer_id'=>$ids))->field('mobile')->group('mobile')->column('mobile');

              if($customer_ids_mobile){

                  $customer_ids_mobile = Contacts::where($wheres)->where('mobile', 'in', $customer_ids_mobile)->value('customer_id');

                  if($customer_ids_mobile){

                      $customer_ids = $customer_ids_mobile;

                  }

              }

              $customer_ids_email = Contacts::where(array('customer_id'=>$ids))->field('email')->group('email')->column('email');

              if($customer_ids_email){

                  $customer_ids_email = Contacts::where($wheres)->where('email', 'in', $customer_ids_mobile)->value('customer_id');

                  if($customer_ids_email){

                      $customer_ids = $customer_ids_email;

                  }

              }

          }

          if(!$customer_ids){

              $this->error('被合并客户不存在');

          }

          $rowinfo = Customer::where(array('id' => $customer_ids))->find();

          $rowinfo = $rowinfo->toArray();

          $rowinfo = CustomerOther::getOther($rowinfo);

          $hecustomer = $fieldlist;

          $hecustomerInfo = [];

          foreach($hecustomer as $k=>$v){

              $hecustomerInfo[$v['id']] = $v;

              $hecustomerInfo[$v['id']]['value']= $rowinfo[$v['id']]??'';

          }

  

          $zucustomer = $fieldlist;

          //主客户信息

          foreach($zucustomer as $k=>$v){

              $zucustomer[$k]['value']= $row[$v['id']]??'';

  

              if(key_exists($v['id'],$hecustomerInfo)){

                  $zucustomer[$k]['chilend'] = $hecustomerInfo[$v['id']];;

              }

          }

  

          $check = array('1'=>'合并后删除被合并客户');

          $this->assign('check',$check);

          $this->assign('form_data', $zucustomer);

          $this->assign('ids',$ids);

          $this->assign('customer_ids',$customer_ids);

          return $this->view->fetch();

      }

  

  }