1
0

AgentsLogic.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\logics;
  3. use app\model\Customer;
  4. use think\facade\Db;
  5. use app\model\CustomerRecycle;
  6. use app\model\AgentIntegralLog;
  7. use app\model\AgentIntegral;
  8. use app\model\AgentUser;
  9. use app\model\CreditsSetting;
  10. use app\model\AgentType;
  11. use app\model\Employee;
  12. use app\model\Org;
  13. use app\model\Setting;
  14. class AgentsLogic
  15. {
  16. //自动计算经济人积分到账户
  17. public function call_agt_integral($daoday,$agentid,$root_id)
  18. {
  19. $agent = AgentUser::where([['root_id','=',$root_id],['id','=',$agentid]])->field('id,type')->find();
  20. $rule_data = AgentType::where([['root_id','=',$root_id],['id','=',$agent['type']]])->value('count');
  21. $daoday = !empty($rule_data) ? json_decode($rule_data,true) : '';
  22. //$daoday=CreditsSetting::where([['code','in',$rule_type],['root_id','=',$agt_root]])->column('value', 'code');
  23. $deposit_time = isset($daoday['agent_hx_time']) ? $daoday['agent_hx_time'] : 0;
  24. $sign_time = isset($daoday['agent_hxtime_sign']) ? $daoday['agent_hxtime_sign'] : 0;
  25. $adddeposit_time=time()-$deposit_time*86400;
  26. $addsign_time=time()-$sign_time*86400;
  27. Db::startTrans();
  28. try {
  29. $list=AgentIntegral::where([
  30. //['addtime','<',$addtime],
  31. ['status','=',1],
  32. ['agent_id','=',$agentid]
  33. ])->lock(true)->select()->toArray();
  34. if(!empty($list)){
  35. $xin=0;
  36. $xinid=null;
  37. foreach($list as $key=>$val){
  38. if(in_array($val['type'],[1,4,5,6])){
  39. $xin+=$val['integral'];
  40. $xinid.=$val['id'].',';
  41. }
  42. if($val['type']==2 && $val['addtime'] < $adddeposit_time){
  43. $xin+=$val['integral'];
  44. $xinid.=$val['id'].',';
  45. }
  46. if($val['type']==3 && $val['addtime'] < $addsign_time){
  47. $xin+=$val['integral'];
  48. $xinid.=$val['id'].',';
  49. }
  50. }
  51. $agt_data=AgentIntegralLog::where('agt_id',$agentid)->order('id desc')->find();
  52. if(empty($agt_data)){
  53. $agt_data['now_integral']=0;
  54. }
  55. $now_integral=$xin+$agt_data['now_integral'];
  56. $add=array(
  57. 'agt_id'=>$agentid,
  58. 'addtime'=>time(),
  59. 'integral_id'=>trim($xinid,','),
  60. 'integral'=>$xin,
  61. 'now_integral'=>$now_integral,
  62. 'type'=>1
  63. );
  64. $ms=AgentIntegralLog::insert($add);
  65. AgentIntegral::where('id','in',$xinid)->update(['status'=>2]);
  66. }
  67. Db::commit();
  68. }catch (\Exception $e) {
  69. Db::rollback();
  70. }
  71. }
  72. /**
  73. * 客户检测手机号的重复
  74. * $from sign 活动 add 添加
  75. */
  76. public function checkPepeat($pharr, $orgids, $employee_id, $id = null, $from = 'add')
  77. {
  78. $emp = Employee::where('id', $employee_id)->find();
  79. if (empty($emp)) {
  80. return '';
  81. }
  82. $root_id = $emp['root_id'];
  83. $emp_org = $emp['org_id'];
  84. // 同部门能否重复录入开关设置
  85. $empcrm_repeat[] = ['root_id', '=', $root_id];
  86. $empcrm_repeat[] = ['name', '=', 'empcrm_customer_repeat'];
  87. $repeat_setting = Setting::where($empcrm_repeat)->findOrEmpty();
  88. foreach($pharr as $key=>$val){
  89. if(empty($val)) continue;
  90. // 先判断公海 只有添加时候判断 且不是活动报名
  91. if (empty($id) && $from != 'sign') {
  92. $poolExist = Customer::where([
  93. ['phone|phone1|phone2', '=', cypherphone(trim($val))],
  94. ['org_id', '=', $emp_org],
  95. ['employee_id', 'null', null],
  96. ['is_resource', '=', 0]
  97. ])->column('id');
  98. if (!empty($poolExist)) {
  99. $poolCrmIdStr = implode(',', $poolExist);
  100. return json(['code' => 3, 'data' => $poolCrmIdStr, 'msg' => '录入失败:客户信息已在公海中。']);
  101. }
  102. }
  103. // 新建档,检测手机号是否重复
  104. $customerExist = Customer::where([
  105. ['phone|phone1|phone2', '=', cypherphone(trim($val))],
  106. ['org_id', 'in', $orgids],
  107. ['employee_id', '>', 0] // 员工客户
  108. ])->where(
  109. function($query) {
  110. $not_sure = Customer::changeState('待确认', 'chaos');
  111. $or1[] = ['crm_res_id', 'null', null];
  112. $or2[] = ['crm_res_id', '>', 0];
  113. $or2[] = ['state', 'not in', $not_sure];
  114. $query->whereOr([$or1, $or2]);
  115. }
  116. )->field('id,employee_id,is_resource,org_id')->select()->toArray();
  117. $id_arr = array_column($customerExist, 'id');
  118. $employee_id_arr = array_column($customerExist, 'employee_id');
  119. if (empty($id)) {
  120. // 对非当前业务员名下重复客户不在判断
  121. if (in_array($employee_id, $employee_id_arr)) {
  122. // 是否已存在为自己的客户
  123. if ($from == 'sign') { // 活动报名
  124. return json(['code' => 1, 'msg' => '您已是' . $emp['name'] . '的客户,请联系他预约']);
  125. } else {
  126. return json(['code' => 2, 'msg' => '录入失败。' . $val . '已经是您的客户。']);
  127. }
  128. }
  129. } else {
  130. if (!in_array($id, $id_arr) && in_array($employee_id, $employee_id_arr)) {
  131. // 是否已存在为自己的客户
  132. if ($from == 'sign') {
  133. return json(['code' => 1, 'msg' => '您已是' . $emp['name'] . '的客户,请联系他预约']);
  134. } else {
  135. return json(['code' => 2, 'msg' => '录入失败。' . $val . '已经是您的客户。']);
  136. }
  137. }
  138. }
  139. //判断同部门
  140. if (!$repeat_setting->isEmpty()) {
  141. $repeat_org = explode(',', $repeat_setting['content']);
  142. if (in_array($emp_org, $repeat_org)) {
  143. // 需要验证
  144. if (!empty($customerExist)) {
  145. foreach ($customerExist as $ex) {
  146. if (in_array($ex['org_id'], $repeat_org) && $ex['employee_id'] != $employee_id) {
  147. $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
  148. if ($from == 'sign') {
  149. return json(['code' => 1, 'msg' => '您已是' . $have_emp . '的客户,请联系他预约']);
  150. } else {
  151. return json(['code' => 2, 'msg' => '该客户与员工' . $have_emp . '撞单,无法录入']);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }
  161. ?>