Customer.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. namespace addons\qingdong\model;
  3. use addons\qingdong\library\Ku;
  4. use app\common\model\Area;
  5. use think\Db;
  6. use think\Exception;
  7. use think\Model;
  8. use traits\model\SoftDelete;
  9. /**
  10. *客户
  11. */
  12. class Customer Extends Model {
  13. use SoftDelete;
  14. // 表名,不含前缀
  15. protected $name = 'qingdong_customer';
  16. // 开启自动写入时间戳字段
  17. protected $autoWriteTimestamp = 'int';
  18. // 定义时间戳字段名
  19. protected $createTime = 'createtime';
  20. protected $updateTime = 'updatetime';
  21. protected $deleteTime = 'deletetime';
  22. //获取创建时间
  23. public function getCreatetimeAttr($value) {
  24. return date('Y-m-d H:i:s', $value);
  25. }
  26. //创建人
  27. public function createStaff() {
  28. return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name,post');
  29. }
  30. //负责人
  31. public function ownerStaff() {
  32. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img');
  33. }
  34. // 负责人(多对多关系)
  35. public function ownerStaffs() {
  36. return $this->belongsToMany(Staff::class, 'customer_staff', 'staff_id', 'customer_id')->field('id,name,img');
  37. }
  38. public function adminUser(){
  39. return $this->hasOne('app\admin\model\Admin', 'id', 'create_staff_id');
  40. }
  41. //获取联系人
  42. public function contacts() {
  43. return $this->hasOne(Contacts::class, 'customer_id', 'id')->order('is_major desc')->field('id,customer_id,name,mobile,email');
  44. }
  45. //获取联系人
  46. public function contact() {
  47. return $this->belongsTo(Contacts::class,'id','customer_id', [], 'LEFT')->field('id,customer_id,name,mobile,email')->setEagerlyType(0);;
  48. }
  49. //获取客户相关信息
  50. public function customerOther() {
  51. return $this->belongsTo(CustomerOther::class,'id','id');
  52. }
  53. public static function withtrash(){
  54. return self::withTrashed();
  55. }
  56. public static function getList() {
  57. $staff = Staff::info();
  58. $staff_id = $staff->id;
  59. $whereStaff = function ($query) use ($staff_id) {
  60. $query->where(['ro_staff_id' => ['like', "%,{$staff_id},%"]])
  61. ->whereOr('rw_staff_id', 'like', "%,{$staff_id},%")
  62. ->whereOr(['owner_staff_id' => ['in', Staff::getMyStaffIds()]]);
  63. };
  64. return self::where($whereStaff)->field('id,name')->select();
  65. }
  66. //创建客户
  67. public static function createCustomer($params,$leads_id=null,$reminds_id=null) {
  68. //自定义字段
  69. $other = [];
  70. foreach ($params as $name => $val) {
  71. if (strstr($name,'other_') !== false) {
  72. if(is_array($val)){
  73. $other[$name] = implode(',',$val);
  74. }else{
  75. $other[$name] = $val;
  76. }
  77. unset($params[$name]);
  78. }else{
  79. if($params[$name] === ''){
  80. $params[$name]=NULL;
  81. }
  82. }
  83. }
  84. $staff = Staff::info();
  85. if(empty($staff)){
  86. // 验证失败 输出错误信息
  87. throw new Exception('账号不存在');
  88. }
  89. $params['create_staff_id'] = $staff->id;
  90. $params['owner_staff_id'] = $staff->id;
  91. $params['next_time'] = date('Y-m-d H:i:s');
  92. $params['last_time'] = date('Y-m-d H:i:s');
  93. $params['receivetime'] = time();
  94. $customer = new self;
  95. $result = $customer->allowField(true)->save($params);
  96. $lastId=$customer->getLastInsID();
  97. if (false === $result) {
  98. // 验证失败 输出错误信息
  99. throw new Exception($customer->getError());
  100. }
  101. $otherModel = new CustomerOther();
  102. if ($otherModel->save(['id' => $lastId, 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)]) === false) {
  103. // 验证失败 输出错误信息
  104. throw new Exception($otherModel->getError());
  105. }
  106. if(isset($leads_id) && $leads_id){
  107. Leads::where(['id' => $leads_id])->update(['is_transform' => 1, 'customer_id' => $lastId]);
  108. }
  109. if (isset($reminds_id['reminds_id']) && $reminds_id['reminds_id']) {//发送通知
  110. $staff_ids = explode(',', $reminds_id['reminds_id']);
  111. foreach ($staff_ids as $staff_id) {
  112. //发送通知
  113. Message::addMessage(Message::CUSTOMER_TYPE, $lastId, $staff_id, $staff->id);
  114. }
  115. }
  116. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $lastId, '创建客户');
  117. //新增跟进记录
  118. Record::quickCreateRecord(Record::CUSTOMER_TYPE, $lastId, '新增客户:' . $params['name']);
  119. return $lastId;
  120. }
  121. /**
  122. *修改客户信息
  123. */
  124. public static function updateCustomer($params) {
  125. //自定义字段
  126. $other = [];
  127. foreach ($params as $name => $val) {
  128. if (strstr($name,'other_') !== false) {
  129. if(is_array($val)){
  130. $other[$name] = implode(',',$val);
  131. }else{
  132. $other[$name] = $val;
  133. }
  134. unset($params[$name]);
  135. }else{
  136. if($params[$name] === ''){
  137. $params[$name]=NULL;
  138. }
  139. }
  140. }
  141. $customer = new self;
  142. // 调用当前模型对应的User验证器类进行数据验证
  143. $result = $customer->save($params, ['id' => $params['id']]);
  144. if (false === $result) {
  145. // 验证失败 输出错误信息
  146. throw new Exception($customer->getError());
  147. }
  148. $otherModel = new CustomerOther();
  149. $otherFind = $otherModel->where(['id' => $params['id']])->find();
  150. if($otherFind){
  151. $resInfo = $otherModel->save(['otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)],['id' => $params['id']]);
  152. }else{
  153. $resInfo = $otherModel->save(['id' => $params['id'],'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)]);
  154. }
  155. if ( $resInfo === false) {
  156. // 验证失败 输出错误信息
  157. throw new Exception($otherModel->getError());
  158. }
  159. //同步数据
  160. $ku = new Ku();
  161. $ku->editCustomer($params);
  162. return true;
  163. }
  164. /**
  165. * 导入客户
  166. * @param $data
  167. * @return bool
  168. */
  169. public static function importCustomer($data) {
  170. $addCustomers = [];
  171. $addOther = [];
  172. $addcontacts = [];
  173. $addLog=[];
  174. foreach ($data as $params) {
  175. //自定义字段
  176. $other = [];
  177. foreach ($params as $name => $val) {
  178. if (strstr($name, 'other_') !== false) {
  179. if(is_array($val)){
  180. $other[$name] = implode(',',$val);
  181. }else{
  182. $other[$name] = $val;
  183. }
  184. unset($params[$name]);
  185. }else{
  186. if($params[$name] === ''){
  187. $params[$name]=NULL;
  188. }
  189. }
  190. }
  191. $staff = Staff::info();
  192. $other['id'] = $params['id'];
  193. $params['next_time'] = date('Y-m-d H:i:s');
  194. $params['receivetime'] = time();
  195. $params['createtime'] = time();
  196. $addOther[] = ['id' => $params['id'], 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)];
  197. $addcontacts[] = ['customer_id' => $params['id'],'is_major'=>1,'name'=>$params['name'],'mobile'=>$params['mobile'],'createtime'=>time(),'updatetime'=>time()];
  198. $addLog[] = [
  199. 'content' => '导入客户',
  200. 'operation_type' => 2,
  201. 'operation_id' => $staff->id,
  202. 'relation_type' => OperationLog::CUSTOMER_TYPE,
  203. 'relation_id' => $params['id'],
  204. 'createtime' => time()
  205. ];
  206. $addCustomers[] = $params;
  207. }
  208. $customer = new self;
  209. // 调用当前模型对应的User验证器类进行数据验证
  210. $result = $customer->allowField(true)->insertAll($addCustomers);
  211. $otherModel = new CustomerOther();
  212. $otherModel->allowField(true)->insertAll($addOther);
  213. //联系人
  214. $contactsModel = new Contacts();
  215. $contactsModel->allowField(true)->insertAll($addcontacts);
  216. $logModel = new OperationLog();
  217. $logModel->allowField(true)->insertAll($addLog);
  218. return true;
  219. }
  220. /**
  221. *移入公海
  222. */
  223. public static function moveSeas($id) {
  224. $row = Customer::where(['id' => $id])->find();
  225. $row = $row->toArray();
  226. $row = CustomerOther::getOther($row);
  227. $seastype=Seastype::where([])->select();
  228. $seasIds=[];
  229. foreach ($seastype as $r){
  230. $rules=json_decode($r['rules'],true)?:[];
  231. $is_rule=0;//权限是否匹配
  232. foreach ($rules as $n=>$e){
  233. if($n == 'area_ids'){//区域
  234. $area=Area::where(['id'=>['in',$e]])->column('name');
  235. foreach ($area as $a){
  236. $rs=preg_match("/^{$a}/i",$row['address'],$res);
  237. if($rs){
  238. $is_rule=1;
  239. }
  240. }
  241. }else if (isset($row[$n]) && $row[$n]) {
  242. if (is_string($e)) {
  243. $e = explode(',', $e);
  244. }
  245. $rn = explode(',', $row[$n]);
  246. $intersect = array_intersect($e, $rn);
  247. if($intersect){
  248. $is_rule=1;
  249. }
  250. }
  251. }
  252. if ($is_rule == 1) {
  253. $seasIds[] = $r['id'];
  254. }
  255. }
  256. if(empty($seasIds)){
  257. $seasIds[]=1;//默认公海
  258. }
  259. $seas_id = ',' . implode(',', $seasIds) . ',';
  260. if (Customer::where(['id' => $id])->update(['owner_staff_id' => 0,
  261. 'seas_id' => $seas_id,'sea_time'=>time()]) == false) {
  262. throw new Exception('修改失败');
  263. }
  264. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $id, '将客户放入公海');
  265. return true;
  266. }
  267. /**
  268. * 转移客户
  269. */
  270. public static function transfer($id, $staff_id) {
  271. Db::startTrans();
  272. try {
  273. if (Customer::where(['id' => $id])->update([
  274. 'owner_staff_id' => $staff_id,
  275. 'updatetime' => time()
  276. ]) == false) {
  277. throw new Exception('修改失败');
  278. }
  279. if (Contacts::where(['customer_id' => $id])->count()) {//存在联系人 则修改负责人
  280. if (Contacts::where(['customer_id' => $id])->update([
  281. 'owner_staff_id' => $staff_id,
  282. 'updatetime' => time()
  283. ]) == false) {
  284. throw new Exception('修改失败');
  285. }
  286. }
  287. $staff = Staff::get($staff_id);
  288. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $id, '将客户转移给:' . $staff['name']);
  289. Db::commit();
  290. } catch (Exception $e) {
  291. Db::rollback();
  292. throw new Exception($e->getMessage());
  293. }
  294. return true;
  295. }
  296. /**
  297. * 批量转移客户
  298. */
  299. public static function batchTransfer($ids, $staff_id) {
  300. Db::startTrans();
  301. try {
  302. if (Customer::where(['id' => ['in',$ids]])->update([
  303. 'owner_staff_id' => implode(',',$staff_id),
  304. 'updatetime' => time()
  305. ]) == false) {
  306. throw new Exception('修改失败');
  307. }
  308. if (Contacts::where(['customer_id' => ['in',$ids]])->count()) {//存在联系人 则修改负责人
  309. if (Contacts::where(['customer_id' => ['in',$ids]])->update([
  310. 'owner_staff_id' => implode(',',$staff_id),
  311. 'updatetime' => time()
  312. ]) == false) {
  313. throw new Exception('修改失败');
  314. }
  315. }
  316. $staff = Staff::get($staff_id);
  317. foreach ($ids as $id){
  318. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $id, '将客户转移给:' . $staff['name']);
  319. }
  320. Db::commit();
  321. } catch (Exception $e) {
  322. Db::rollback();
  323. throw new Exception($e->getMessage());
  324. }
  325. return true;
  326. }
  327. /**
  328. * 领取客户
  329. */
  330. public static function receive($customer_id) {
  331. $staff = Staff::info();
  332. $where=['owner_staff_id' => 0];
  333. if($customer_id){
  334. $where['id']=$customer_id;
  335. }
  336. $customer = Customer::where($where)->find();
  337. Db::startTrans();
  338. try {
  339. $id = $customer['id'];
  340. $staff_id = $staff->id;
  341. if (Customer::where(['id' => $customer['id']])->update(['owner_staff_id' => $staff->id,'receivetime'=>time()]) == false) {
  342. throw new Exception('修改失败');
  343. }
  344. if (Contacts::where(['customer_id' => $id])->count()) {//存在联系人 则修改负责人
  345. if (Contacts::where(['customer_id' => $id])->update([
  346. 'owner_staff_id' => $staff_id,
  347. 'updatetime' => time()
  348. ]) == false) {
  349. throw new Exception('修改失败');
  350. }
  351. }
  352. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $customer['id'], '领取了客户');
  353. Db::commit();
  354. } catch (Exception $e) {
  355. Db::rollback();
  356. throw new Exception($e->getMessage());
  357. }
  358. return $customer['id'];
  359. }
  360. }