AgentsWork.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. <?php
  2. namespace app\api\controller;
  3. use app\logics\AgentsLogic;
  4. use app\model\Customer;
  5. use app\model\Employee;
  6. use Firebase\JWT\JWT;
  7. use openssl\Aes;
  8. use app\model\AgentUser;
  9. use app\model\AgentIntegralLog;
  10. use app\model\AgentIntegral;
  11. use app\model\CreditsSetting;
  12. use app\model\Activity;
  13. use think\facade\Db;
  14. use think\facade\Console;
  15. use app\model\CustomerTop;
  16. use app\model\CustomerVisitLog;
  17. use app\model\User;
  18. use app\model\Org;
  19. use app\model\Miniprogram;
  20. use app\model\Company;
  21. use app\model\ActivityView;
  22. use app\model\UserCollect;
  23. use app\model\AgentCustomerLog;
  24. use app\model\AgentCustomerTop;
  25. use app\model\AgentArticle;
  26. use app\model\AgentShareLog;
  27. use app\model\ShareLog;
  28. use app\model\CustomerClue;
  29. use app\model\Footprints;
  30. use app\model\CustomerSubscribe;
  31. use app\model\AgentShareContent;
  32. use app\model\MaterialCase;
  33. use app\model\Article;
  34. use app\model\Video;
  35. use app\model\MaterialEvidence;
  36. use app\model\CompanyStrength;
  37. use app\model\Building;
  38. use app\model\AgentApplyIntegral;
  39. use app\event\Msg;
  40. use app\model\AgentPrize;
  41. use app\model\AgentType;
  42. use app\model\Setting;
  43. use app\model\VrGroup;
  44. use Exception;
  45. use xiaohongwu\Vr;
  46. class AgentsWork extends Base
  47. {
  48. /**
  49. * 经纪人端首页头部统计
  50. */
  51. public function agt_crm_count()
  52. {
  53. $token = $this->request->token;
  54. $agtdata = $this->request->agtdata;
  55. $integral = AgentIntegralLog::where('agt_id', '=', $agtdata['id'])->order('id desc')->value('now_integral');
  56. //查询出属于我的客户
  57. $customer_id = Customer::where('agents_id', '=', $agtdata['id'])->column('id');
  58. // 待确认数量统计
  59. $visit = Customer::where(array_merge([CustomerVisitLog::changeState(['state', '=', '待确认'])], [['id', 'in', $customer_id]]))->count();
  60. // 交定数量统计
  61. $ding = CustomerVisitLog::where(array_merge([CustomerVisitLog::changeState(['state', '=', '交定'])], [['customer_id', 'in', $customer_id]]))->count();
  62. // 签单数量统计
  63. $sign = CustomerVisitLog::where(array_merge([CustomerVisitLog::changeState(['state', '=', '签单'])], [['customer_id', 'in', $customer_id]]))->count();
  64. // 正在进行的活动查询
  65. $acting = Activity::where(['del' => 0, 'show' => 1, 'root_id' => $token['root_org']])
  66. ->whereBetweenTimeField('start_date', 'end_date')->count();
  67. //线索量
  68. $clue_count = CustomerClue::where(['agent_id' => $agtdata['id']])->count();
  69. // 企业名获取
  70. $companyName = Company::where(['root_id'=>$agtdata['root_id']])->value('company_name');
  71. $data = [
  72. 'agttype' => $agtdata['type'],
  73. 'acting' => $acting,
  74. 'integral' => $integral,
  75. 'visit' => $visit,
  76. 'ding' => $ding,
  77. 'sign' => $sign,
  78. 'count' => $customer_id ? count($customer_id) : 0,
  79. 'clue_count' => $clue_count,
  80. 'company_name'=>$companyName
  81. ];
  82. return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']);
  83. }
  84. //经济人报备客户
  85. public function add_customer()
  86. {
  87. $token = $this->request->token;
  88. $agtdata = $this->request->agtdata;
  89. $data = $this->request->only(['name', 'phone', 'sex' => 1, 'community_name', 'last_contact_date' => date('Y-m-d'), 'source_id', 'deposit_money' => null, 'signed_money' => null, 'external_userid', 'remark']);
  90. // 设计师能否报备、获取客户
  91. $root_id = !empty($token['root_org']) ? $token['root_org'] : $agtdata['root_id'];
  92. $de_where[] = ['root_id', '=', $root_id];
  93. $de_where[] = ['name', '=', 'designer_get_customer'];
  94. $info_de_where = Setting::where($de_where)->findOrEmpty();
  95. $org_type = Org::where('id', '=', $agtdata['org_id'])->value('org_type');
  96. if (!$info_de_where->isEmpty() && $org_type == 2) {
  97. if ((int)$info_de_where['content'] != 1) {
  98. return json(['code' => 1, 'msg' => '请联系业务员进行报备客户']);
  99. }
  100. }
  101. $data['employee_id'] = $agtdata['agent_employee_id'];
  102. $data['org_id'] = $agtdata['org_id'];
  103. $data['state'] = '待确认';
  104. $data['level'] = 'C';
  105. $data['agents_id'] = $agtdata['id'];
  106. $orgids = orgSubIds(!empty($token['root_org']) ? $token['root_org'] : $agtdata['root_id']);
  107. // 新建档,检测手机号是否重复
  108. $pharr = [$data['phone']];
  109. $tips = (new AgentsLogic())->checkPepeat($pharr, $orgids, $data['employee_id']);
  110. if (!empty($tips)) return $tips;
  111. //插入经济人提报的客户
  112. $data['phone'] = trim($data['phone']);
  113. Db::startTrans();
  114. try {
  115. $ms = Customer::create($data);
  116. $empuid = Employee::where('id', $agtdata['agent_employee_id'])->value('uid');
  117. $visitlog = CustomerVisitLog::where([['customer_id', '=', $ms->id], ['state', 'in', CustomerVisitLog::changeState('待确认', 'chaos')]])->find();
  118. if (!$visitlog) {
  119. $save = [];
  120. $save['customer_id'] = $ms->id;
  121. $save['type'] = '';
  122. $save['remark'] = !empty($data['remark']) ? $data['remark'] : '经纪人报备客户';
  123. $save['employee_id'] = $agtdata['agent_employee_id'];
  124. $save['user_id'] = $empuid;
  125. $save['state'] = 1;
  126. //$save['aid'] = $param['aid'];
  127. CustomerVisitLog::create($save);
  128. }
  129. //报备增加积分
  130. $rule_type = 'putong_integral';
  131. $rule_data = AgentType::where([['root_id', '=', $agtdata['root_id']], ['id', '=', $agtdata['type']]])->value('count');
  132. $rule_list = !empty($rule_data) ? json_decode($rule_data, true) : '';
  133. $itg_rule = !empty($rule_list) ? $rule_list['putong_integral'] : 0;
  134. //$itg_rule = CreditsSetting::where([['code', '=', $rule_type], ['root_id', '=', $agtdata['root_id']]])->value('value');
  135. $state = '经纪人手动录入客户';
  136. $add = array(
  137. 'agent_id' => $agtdata['id'],
  138. 'type' => 4,
  139. 'integral' => $itg_rule,
  140. 'addtime' => time(),
  141. 'state' => $state,
  142. 'customer_id' => $ms->id
  143. );
  144. AgentIntegral::insert($add);
  145. Db::commit();
  146. } catch (\Exception $e) {
  147. Db::rollback();
  148. return json(['code' => 1, 'msg' => '录入数据失败.']);
  149. }
  150. if ($ms) {
  151. return json(['code' => 0, 'msg' => '录入成功.']);
  152. } else {
  153. return json(['code' => 1, 'msg' => '录入失败.']);
  154. }
  155. }
  156. /**
  157. * 经纪人客户列表
  158. */
  159. public function customer_list()
  160. {
  161. $token = $this->request->token;
  162. $agtdata = $this->request->agtdata;
  163. $param = $this->request->only([
  164. 'state' => '',
  165. 'page' => 1,
  166. 'limit' => 20,
  167. 'keyword' => '',
  168. 'source' => '',
  169. 'date' => '',
  170. 'visit' => '',
  171. 'addtime' => '',
  172. 'agent_source_type' => ''
  173. ]);
  174. $agt_id = [$agtdata['id']];
  175. // 1)查询条件
  176. $condition = [['agents_id', 'in', $agt_id]];
  177. empty($param['agent_source_type']) ?: $condition[] = ['agent_source_type', '=', $param['agent_source_type']];
  178. // 可回收列表
  179. if (!empty($param['protected'])) {
  180. $orgids = orgSubIds($this->request->token['org_id']);
  181. $condition[] = ['org_id', 'in', $orgids];
  182. }
  183. setCondition($param, 'level', '=', $condition);
  184. // 3)来源筛选
  185. $source = [
  186. 'ownadd' => [
  187. ['crm_res_id', '=', null],
  188. ['is_resource', '=', 0],
  189. ['remark', '=', '']
  190. ],
  191. 'crmres' => [
  192. ['crm_res_id', 'not null', ''],
  193. ['is_resource', '=', 0]
  194. ],
  195. 'public' => [
  196. ['remark', '=', '公海获取']
  197. ],
  198. ];
  199. // 来源
  200. (empty($param['source']) || !isset($source[$param['source']])) ?: $condition = array_merge_recursive($condition, $source[$param['source']]);
  201. // 5)待回访
  202. empty($param['visit']) ?: $condition[] = empty($param['date']) ? ['revisit_time', '=', date('Y-m-d') . ' 00:00:00'] : ['revisit_time', '=', $param['date'] . ' 00:00:00'];
  203. // 6)录入时间筛选
  204. empty($param['addtime']) ?: $condition[] = ['addtime', 'like', date('Y-m-d', strtotime($param['addtime'])) . '%'];
  205. //增加按跟进时间排序
  206. $order = 'addtime desc';
  207. empty($param['following']) ?: $order = 'last_contact_date ' . $param['following'] . ',' . $order;
  208. //关键词搜索
  209. if (!empty($param['keyword'])) {
  210. $list = Customer::where($condition)->order($order)->field('id,name,community_name,phone')->select()->toArray();
  211. $customersIdList = [];
  212. foreach ($list as $k => $v) {
  213. $str = $v['name'] . $v['community_name'] . $v['phone'];
  214. if (strpos($str, trim($param['keyword'])) !== false) $customersIdList[] = $v['id'];
  215. }
  216. } else {
  217. $customersIdList = Customer::where($condition)->order($order)->column('id');
  218. }
  219. // 剔除掉未经历该状态的客户id
  220. if (!empty($param['state'])) {
  221. if (CustomerVisitLog::changeState($param['state']) == '回访' || $param['state'] == '有效') {
  222. // 查询经历过回访以外所有状态的客户id(有效)
  223. $expectCustomersIdList = CustomerVisitLog::where([['customer_id', 'in', $customersIdList], ['state', 'not in', CustomerVisitLog::changeState('回访', 'chaos')], ['employee_id', '=', $agtdata['agent_employee_id']]])->group('customer_id')->column('customer_id');
  224. if ($param['state'] == '有效') {
  225. $customersIdList = $expectCustomersIdList;
  226. } else {
  227. $customersIdList = array_diff($customersIdList, $expectCustomersIdList);
  228. }
  229. } else {
  230. $customersIdList = CustomerVisitLog::where([['customer_id', 'in', $customersIdList], ['state', 'in', CustomerVisitLog::changeState($param['state'], 'chaos')]])->group('customer_id')->column('customer_id');
  231. }
  232. }
  233. // 置顶数据获取
  234. $top = AgentCustomerTop::where([['root_id', '=', $token['root_org']], ['agent_id', '=', $agtdata['id']]])->where([['customer_id', 'in', $customersIdList]])->order('addtoptime asc')->column('customer_id');
  235. //var_dump($top);
  236. //exit;
  237. // 新数据获取
  238. //$new = Customer::where([['id', 'in', $customersIdList], ['fresh', '=', 1]])->order($order)->column('id');
  239. $new = [];
  240. // 剔除不应该出现的客户
  241. $top = array_diff($top, array_diff($top, $customersIdList));
  242. // 剔除不应该出现的新客户
  243. $new = array_diff($new, array_diff($new, $customersIdList));
  244. // 剔除置顶的客户
  245. $notTop = array_diff($customersIdList, $top);
  246. // 剔除新客户
  247. $notNew = array_diff($notTop, $new);
  248. // 将置顶客户放置到前面
  249. $customersIdList = array_unique(array_merge($top, $new, $notNew));
  250. // 总数获取
  251. $count = Customer::where($condition)->count();
  252. //返回到特定位置
  253. $id = input('id', 0);
  254. if ($id) {
  255. $ids = array_chunk($customersIdList, $param['limit']);
  256. foreach ($ids as $k => $v) {
  257. $a = false;
  258. foreach ($v as $k2 => $v2) {
  259. if ($id == $v2) {
  260. $a = true;
  261. $param['page'] = $k + 1;
  262. break;
  263. }
  264. }
  265. if ($a) break;
  266. }
  267. }
  268. // 分页
  269. $pageIdList = array_slice($customersIdList, ($param['page'] - 1) * $param['limit'], $param['limit']);
  270. $condition[] = ['id', 'in', $pageIdList];
  271. $customers = Customer::with(['designer', 'designerOrg'])->where($condition)->select();
  272. // 总数获取
  273. $count = $customers->count();
  274. $customers = $customers->visible(['id', 'employee_id', 'name', 'sex', 'level', 'community_name', 'state', 'addtime', 'revisit_time', 'designer.name', 'designerOrg.name', 'fresh', 'square', 'last_contact_date', 'updatetime', 'agent_source_type'])->toArray();
  275. if (!empty($setting)) {
  276. $setting_content = json_decode($setting['content'], true);
  277. } else {
  278. $setting_content = [];
  279. }
  280. foreach ($customers as $k => $v) {
  281. $customers[$k]['un_protected'] = 0;
  282. $customers[$k]['protected_tips'] = '';
  283. if (!empty($v['protected_to']) && time() > strtotime($v['protected_to'])) {
  284. $state_n = Customer::changeState($v['state'], 'n');
  285. if (!empty($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) {
  286. $customers[$k]['un_protected'] = 1;
  287. $customers[$k]['protected_tips'] = '已过期';
  288. }
  289. }
  290. if (!empty($v['protected_to']) && time() < strtotime($v['protected_to'])) {
  291. $day = floor((strtotime($v['protected_to']) - time()) / (24 * 3600));
  292. $customers[$k]['protected_tips'] = $day ? '保护期' . $day . '天' : '即将过期';
  293. }
  294. }
  295. $customers = array_combine(array_column($customers, 'id'), $customers);
  296. // 状态统计获取
  297. $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $pageIdList]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  298. $customersState = [];
  299. foreach ($customersStateList as $s) {
  300. if (!isset($customersState[$s['customer_id']]))
  301. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  302. $customersState[$s['customer_id']]['count'] += $s['num'];
  303. // 到店,量房,活动,定金,签单
  304. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  305. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  306. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  307. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  308. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  309. }
  310. // 排序(按每页id顺序取值)
  311. $listData = [];
  312. foreach ($pageIdList as $customerId) {
  313. $customers[$customerId]['top'] = 0;
  314. $customers[$customerId]['is_new'] = $customers[$customerId]['fresh'] == 1 ? 1 : 0;
  315. $customers[$customerId]['addtoptime'] = null;
  316. if (in_array($customerId, $top)) $customers[$customerId]['addtoptime'] = 1;
  317. $customers[$customerId]['stateNum'] = $customersState[$customerId] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  318. $listData[] = $customers[$customerId];
  319. }
  320. $data = [
  321. 'list' => $listData,
  322. 'count' => $count
  323. ];
  324. return json(['code' => 0, 'data' => $data, 'page' => $param['page']]);
  325. }
  326. //经济人核销记录
  327. public function already_list()
  328. {
  329. $agtdata = $this->request->agtdata;
  330. $data = $this->request->only(['page' => 1, 'limit' => 15]);
  331. //查询申请记录
  332. $rule_type = 'reduce_integral';
  333. $already_rule = CreditsSetting::where([['code', '=', $rule_type], ['root_id', '=', $agtdata['root_id']]])->value('value');
  334. $already_rule = $already_rule ? json_decode($already_rule, true) : ['jifen' => 1, 'money' => 1];
  335. $list = AgentApplyIntegral::where([['root_id', '=', $agtdata['root_id']], ['agent_id', '=', $agtdata['id']]])->page($data['page'], $data['limit'])
  336. ->field('id,addtime,status,integral,good_id')->order('id desc')->select()->toArray();
  337. foreach ($list as $key => $val) {
  338. if (in_array($val['status'], [1, 2])) {
  339. $status = '核销中';
  340. } elseif ($val['status'] == 3) {
  341. $status = '兑换成功';
  342. } elseif ($val['status'] == 4) {
  343. $status = '核销驳回';
  344. }
  345. $list[$key]['status'] = $status;
  346. $list[$key]['good_name'] = !empty($val['good_id']) ? AgentPrize::where([['root_id', '=', $agtdata['root_id']], ['id', '=', $val['good_id']]])->value('good_name') : '';
  347. $list[$key]['money'] = !empty($val['integral']) ? bcdiv($val['integral'], $already_rule['jifen'], 1) * $already_rule['money'] : 0;
  348. }
  349. return json(['code' => 0, 'data' => $list, 'msg' => '请求成功.']);
  350. }
  351. //经纪人获得积分统计
  352. public function intregral_total()
  353. {
  354. $agtdata = $this->request->agtdata;
  355. $all_integral = $all_already = 0;
  356. $rule_type = 'reduce_integral';
  357. $already_rule = CreditsSetting::where([['code', '=', $rule_type], ['root_id', '=', $agtdata['root_id']]])->value('value');
  358. $already_rule = $already_rule ? json_decode($already_rule, true) : ['jifen' => 1, 'money' => 0];
  359. $integral = AgentIntegralLog::where([['agt_id', '=', $agtdata['id']]])->fieldRaw('sum(integral) as at,type')->group('type')->select()->toArray();
  360. if (!empty($integral)) {
  361. foreach ($integral as $val) {
  362. if ($val['type'] == 1) {
  363. $all_integral = $val['at'] ? $val['at'] : 0;
  364. }
  365. if ($val['type'] == 2) {
  366. $all_already = $val['at'] ? $val['at'] : 0;
  367. }
  368. }
  369. }
  370. $no_already = AgentIntegralLog::where('agt_id', '=', $agtdata['id'])->order('id desc')->value('now_integral');
  371. $no_already = $no_already ? $no_already : 0;
  372. $no_already_money = !empty($no_already) ? bcdiv($no_already, $already_rule['jifen'], 1) * $already_rule['money'] : 0;
  373. $data = array('all_intregral' => $all_integral, 'all_already' => $all_already, 'ke_already' => $no_already, 'no_already' => $no_already, 'no_already_money' => $no_already_money);
  374. return json(['code' => 0, 'data' => $data, 'msg' => '请求成功.']);
  375. }
  376. /**
  377. * 经纪人积分明细
  378. */
  379. public function integral_log()
  380. {
  381. $agtdata = $this->request->agtdata;
  382. $data = $this->request->only(['page' => 1, 'limit' => 10]);
  383. //先进行积分到账期限查询并添加到账
  384. //$this->sel_call_integral($agtdata['id']);
  385. //添加到账end
  386. $list = AgentIntegral::with(['customer' => function ($query) {
  387. $query->field('id,name,sex');
  388. }])->where([['agent_id', '=', $agtdata['id']]])->page($data['page'], $data['limit'])->order('addtime desc')->select()->toArray();
  389. foreach ($list as $key => $val) {
  390. $list[$key]['addtime'] = date('Y-m-d H:i:s', $val['addtime']);
  391. $list[$key]['status'] = $val['status'] == 1 ? '待确认' : '到账';
  392. //type:1客户变更状态,2分享获取,4网红录入
  393. //$list[$key]['from'] = $val['type'] == 1 || $val['type'] == 4 ? 'customer' : 'share';
  394. $list[$key]['from'] = in_array($val['type'], [1, 2, 3, 4]) ? 'customer' : 'share';
  395. }
  396. return json(['code' => 0, 'data' => $list, 'msg' => '请求成功.']);
  397. }
  398. //活动列表接口
  399. public function activity_list()
  400. {
  401. $token = $this->request->token;
  402. $agtdata = $this->request->agtdata;
  403. $param = $this->request->only(['page' => 1, 'limit' => 10, 'type']);
  404. $condition = ['root_id' => $token['root_org']];
  405. if (isset($param['type']) && is_numeric($param['type']) && in_array($param['type'], [0, 1, 2])) $condition['cate'] = $param['type'];
  406. $data = Activity::where($condition)
  407. ->field('id,cover,title,start_date,end_date,IF(start_date>curdate(),1,IF(end_date<curdate(),2,0)) as timestate,cate,address')
  408. ->order('timestate asc,addtime desc')
  409. ->page($param['page'], $param['limit'])
  410. ->select()->toArray();
  411. foreach ($data as $key => $val) {
  412. $data[$key]['cover'] = str_replace('https://' . config('app.ali_oss_bindurl') . '/', '', $val['cover']);
  413. }
  414. return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']);
  415. }
  416. //活动详情
  417. public function activity_detail($id)
  418. {
  419. $token = request()->token;
  420. $agtdata = $this->request->agtdata;
  421. $data = Activity::where(['id' => $id, 'del' => 0, 'root_id' => $token['root_org']])->field('id,cover,title,content,type,start_date,end_date,end_date as timestate,view,money,cate,address,poster')->find();
  422. //是否收藏
  423. $collectedIds = UserCollect::where(['user_id' => $token['uid'], 'content_type' => 'activity', 'content_id' => $id])->count();
  424. $data['collected'] = $collectedIds > 0 ? true : false;
  425. Activity::where('id', $id)->inc('view')->update();
  426. $data['employee_id'] = $agtdata['agent_employee_id'];
  427. $data['agent_id'] = $agtdata['id'];
  428. return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']);
  429. }
  430. //经纪人申请核销积分
  431. public function apply_wipen()
  432. {
  433. $agtdata = $this->request->agtdata;
  434. $param = $this->request->only(['wiped_integral']);
  435. if (empty($param['wiped_integral'])) return json(['code' => 1, 'msg' => '请填写大于1的核销积分']);
  436. $agt_integral = AgentIntegralLog::where('agt_id', $agtdata['id'])->order('id desc')->value('now_integral');
  437. if (empty($agt_integral) || $param['wiped_integral'] > $agt_integral) {
  438. return json(['code' => 1, 'msg' => '积分为零或者大于现有积分']);
  439. }
  440. $add = [
  441. 'agent_id' => $agtdata['id'],
  442. 'empid' => $agtdata['agent_employee_id'],
  443. 'integral' => $param['wiped_integral'],
  444. 'root_id' => $agtdata['root_id']
  445. ];
  446. $bili = Db::table('fl_credits_setting')->where([['code', '=', 'reduce_integral'], ['root_id', '=', $agtdata['root_id']]])->value('value');
  447. $bili = !empty($bili) ? json_decode($bili, true) : ['jifen' => 1, 'money' => 1];
  448. $money = bcdiv($param['wiped_integral'], $bili['jifen'], 1) * $bili['money'];
  449. Db::startTrans();
  450. try {
  451. $ms = AgentApplyIntegral::insertGetId($add);
  452. // 添加记录
  453. AgentIntegralLog::create([
  454. 'agt_id' => $agtdata['id'],
  455. 'addtime' => time(),
  456. 'integral' => $param['wiped_integral'],
  457. 'type' => 2,
  458. 'money' => $money,
  459. 'now_integral' => $agt_integral - $param['wiped_integral']
  460. ]);
  461. } catch (Exception $e) {
  462. // 回滚事务
  463. Db::rollback();
  464. return json(['code' => 1, 'msg' => '兑换失败']);
  465. }
  466. Db::commit();
  467. $msg = '经纪人【' . $agtdata['agent_name'] . '】提交核销积分申请';
  468. if ($ms) {
  469. event(new Msg($agtdata['agent_employee_id'], $msg, 'agentWipen', $ms));
  470. return json(['code' => 0, 'msg' => '申请成功']);
  471. } else {
  472. return json(['code' => 1, 'msg' => '申请失败']);
  473. }
  474. }
  475. //获取加入的企业
  476. public function company()
  477. {
  478. $token = $this->request->token;
  479. // 用token中的openid获取用户的uid列表
  480. $uidList = User::where(['mini_openid' => $token['openid']])->column('id');
  481. $employees = AgentUser::where([['uid', 'in', $uidList], ['status', '=', 1], ['is_review', '=', 2]])->column('root_id,org_id', 'root_id');
  482. $orgs = Org::where([['id', 'in', array_column($employees, 'org_id')]])->column('id,name', 'id');
  483. $notifyList = Miniprogram::where([['root_id', 'in', array_column($employees, 'root_id')]])->column('root_id,notify', 'root_id');
  484. $companyList = Company::with(['brand'])->where([['root_id', 'in', array_column($employees, 'root_id')]])->select()->visible(['company_name', 'root_id', 'status', 'end_date'])->toArray();
  485. foreach ($companyList as &$item) {
  486. $item['client_type'] = isset($notifyList[$item['root_id']]) ? $notifyList[$item['root_id']]['notify'] : '';
  487. //$item['is_manager'] = isset($employees[$item['root_id']]) ? $employees[$item['root_id']]['is_manager'] : 0;
  488. $item['org_name'] = (isset($employees[$item['root_id']]) && isset($orgs[$employees[$item['root_id']]['org_id']])) ? $orgs[$employees[$item['root_id']]['org_id']]['name'] : '';
  489. if ($item['status'] == 2) {
  490. $item['off'] = 1;
  491. $item['off_remark'] = '账号被禁用';
  492. } else {
  493. $item['off'] = 0;
  494. $item['off_remark'] = '账号正常';
  495. }
  496. }
  497. return json(['code' => 0, 'data' => $companyList]);
  498. }
  499. //设置经纪人客户置顶
  500. public function set_top()
  501. {
  502. $token = $this->request->token;
  503. $agtdata = $this->request->agtdata;
  504. $param = $this->request->only(['customer_id']);
  505. $you = AgentCustomerTop::where([['root_id', '=', $agtdata['root_id']], ['agent_id', '=', $agtdata['id']], ['customer_id', '=', $param['customer_id']]])->find();
  506. if (!empty($you)) {
  507. $ms = AgentCustomerTop::where([['root_id', '=', $agtdata['root_id']], ['agent_id', '=', $agtdata['id']], ['customer_id', '=', $param['customer_id']]])->delete();
  508. if ($ms) {
  509. return json(['code' => 0, 'msg' => '取消成功']);
  510. } else {
  511. return json(['code' => 1, 'msg' => '取消失败']);
  512. }
  513. } else {
  514. $add = ['agent_id' => $agtdata['id'], 'root_id' => $agtdata['root_id'], 'customer_id' => $param['customer_id']];
  515. $ms = AgentCustomerTop::insert($add);
  516. if ($ms) {
  517. return json(['code' => 0, 'msg' => '设置成功']);
  518. } else {
  519. return json(['code' => 1, 'msg' => '设置失败']);
  520. }
  521. }
  522. }
  523. //核销规则
  524. public function wiped_rule()
  525. {
  526. $token = $this->request->token;
  527. $agtdata = $this->request->agtdata;
  528. $rule_type = 'agent_hx_time';
  529. $daoday = CreditsSetting::where([['code', '=', $rule_type], ['root_id', '=', $agtdata['root_id']]])->value('value');
  530. if (!empty($daoday)) {
  531. $data1 = '经纪人推荐的客户在到店,交定,签单所产生的积分,会在' . $daoday . '天后自动到达积分账户';
  532. $data2 = '经纪人推荐的客户所产生的积分,会在' . $daoday . '天后自动到达积分账户';
  533. $data = $agtdata['type'] == 1 ? $data1 : $data2;
  534. } else {
  535. $data = '暂未设置';
  536. }
  537. return json(['code' => 0, 'data' => $data, 'msg' => '请求成功']);
  538. }
  539. /**
  540. * 经纪人端首页排行榜
  541. */
  542. public function agent_rank_list()
  543. {
  544. $param = $this->request->only(['page' => 1, 'limit' => 10]);
  545. $agtdata = $this->request->agtdata;
  546. $list = AgentUser::with(['user' => function ($query) {
  547. $query->field('id,nickname,headimgurl');
  548. }])->withCount(['customer' => function ($query, &$alias) {
  549. $alias = 'reccont';
  550. }])->where([['root_id', '=', $agtdata['root_id']], ['is_review', '=', 2], ['status', '=', 1]])->field('id,agent_name,type,uid')->select()->toArray();
  551. foreach ($list as &$val) {
  552. $val['type'] = $val['type'] == 1 ? '普通' : '网红';
  553. $parr[] = $val['reccont'];
  554. }
  555. if (!empty($list)) {
  556. array_multisort($parr, SORT_DESC, $list);
  557. }
  558. $count = count($list);
  559. $list = array_slice($list, ($param['page'] - 1) * $param['limit'], $param['limit']);
  560. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  561. }
  562. /**
  563. * 分享任务、分享内容(展示我分享过的)
  564. */
  565. // public function share_articles()
  566. // {
  567. // $param = $this->request->only(['page' => 1, 'limit' => 10, 'from' => '']);
  568. // $token = $this->request->token;
  569. // $agtdata = $this->request->agtdata;
  570. // $where = [
  571. // ['root_id', '=', $token['root_org']],
  572. // ['employee_id', '=', $agtdata['agent_employee_id']],
  573. // ['disable', '=', 0],
  574. // ];
  575. // //分享记录
  576. // $log = ShareLog::where(['agent_id' => $agtdata['id'], 'type' => 'AgentArticle'])->group('data_id')->column('data_id');
  577. // if (isset($param['from']) && $param['from'] == 'shareLog') {
  578. // //分享内容只展示我分享过的文章
  579. // $where[] = ['id', 'in', $log];
  580. // }
  581. // $data = AgentArticle::where($where)->withCount(['clue' => function($query, &$alias){
  582. // $query->where([['pipe_type', '=', 'agentArticle']]);
  583. // $alias = 'clue_num';
  584. // }])->field('id,title,file,share_num,addtime,type')->page($param['page'], $param['limit'])->order('addtime desc')->select()->toArray();
  585. // foreach ($data as &$item) {
  586. // $item['new'] = in_array($item['id'], $log) ? false : true;
  587. // }
  588. // return json(['code' => 0, 'data' => $data]);
  589. // }
  590. /**
  591. * 分享任务、分享内容(展示我分享过的)
  592. */
  593. public function share_article()
  594. {
  595. $param = $this->request->only(['page' => 1, 'limit' => 10, 'from' => '', 'type' => 1]);
  596. $token = $this->request->token;
  597. $agtdata = $this->request->agtdata;
  598. $where = [
  599. ['root_id', '=', $token['root_org']],
  600. //['employee_id', '=', $agtdata['agent_employee_id']],
  601. ['disable', '=', 0],
  602. ];
  603. $or1 = ['employee_id', '=', $agtdata['agent_employee_id']];
  604. $or2 = ['from', '=', 2];
  605. //分享内容只展示我分享过的文章
  606. if (isset($param['from']) && $param['from'] == 'shareLog') {
  607. $content_id = [];
  608. $content = AgentShareContent::where($where)->where(function($query) use ($or1,$or2){
  609. $query->whereOr([$or1,$or2]);
  610. })->select();
  611. $log = ShareLog::where(['agent_id' => $agtdata['id']])->select();
  612. foreach($content as $val) {
  613. foreach($log as $item) {
  614. if($val['data_id'] == $item['data_id'] && $val['type'] == $item['type']) {
  615. $content_id[] = $val['id'];
  616. }
  617. }
  618. }
  619. $where[] = ['id', 'in', $content_id];
  620. $data = AgentShareContent::where($where)->where(function($query) use ($or1,$or2){
  621. $query->whereOr([$or1,$or2]);
  622. })->order('addtime desc')->order('addtime desc')->page($param['page'], $param['limit'])->select()->toArray();
  623. }else{
  624. $data = AgentShareContent::where($where)->where('employee_id', '=', $agtdata['agent_employee_id'])->order('addtime desc')->order('addtime desc')->page($param['page'], $param['limit'])->select()->toArray();
  625. }
  626. foreach ($data as &$item) {
  627. //分享量
  628. $item['share_num'] = ShareLog::where([['type', '=', $item['type']], ['data_id', '=', $item['data_id']], ['agent_id', '<>', 'NULL']])->count();
  629. //线索
  630. $item['clue_num'] = CustomerClue::where([['pipe_type', '=', $item['type']], ['pipe_id', '=', $item['data_id']], ['agent_id', '<>', 0]])->count();
  631. //是否未分享
  632. //$item['new'] = in_array($item['id'], $content_id) ? false : true;
  633. switch ($item['type']) {
  634. case 'Article':
  635. $find = Article::where([
  636. ['root_id', '=', $token['root_org']],
  637. ['delete_time', '=', 0],
  638. ['publish', '=', 1],
  639. ['id', '=', $item['data_id']]
  640. ])->field('id,title,cover_img')->find();
  641. $item['content_id'] = $find['id'];
  642. $item['title'] = $find['title'];
  643. $item['cover'] = $find['cover_img'];
  644. break;
  645. case 'MaterialCase':
  646. $find = MaterialCase::where([
  647. ['root_id', '=', $token['root_org']],
  648. ['del', '=', 0],
  649. ['publish', '=', 1],
  650. ['id', '=', $item['data_id']]
  651. ])->field('id,title,cover_img')->find();
  652. $item['content_id'] = $find['id'];
  653. $item['title'] = $find['title'];
  654. $item['cover'] = $find['cover_img'];
  655. break;
  656. case 'Video':
  657. $find = Video::where([
  658. ['root_id', '=', $token['root_org']],
  659. ['delete_time', '=', 0],
  660. ['publish', '=', 1],
  661. ['id', '=', $item['data_id']]
  662. ])->field('id,title,video_url')->find();
  663. $item['content_id'] = $find['id'];
  664. $item['title'] = $find['title'];
  665. $item['cover'] = $find['video_url'];
  666. break;
  667. case 'CompanyStrength':
  668. $find = CompanyStrength::where([
  669. ['root_id', '=', $token['root_org']],
  670. ['del', '=', 0],
  671. ['publish', '=', 1],
  672. ['id', '=', $item['data_id']]
  673. ])->field('id,title,cover,pics,difference')->find();
  674. $item['content_id'] = $find['id'];
  675. $item['title'] = $find['title'];
  676. $item['cover'] = $find['cover'];
  677. $item['pics'] = $find['pics'];
  678. $item['difference'] = $find['difference'];
  679. break;
  680. case 'MaterialEvidence':
  681. $find = MaterialEvidence::where([
  682. ['root_id', '=', $token['root_org']],
  683. ['del', '=', 0],
  684. ['publish', '=', 1],
  685. ['id', '=', $item['data_id']]
  686. ])->field('id,title,pics,difference')->find();
  687. $item['content_id'] = $find['id'];
  688. $item['title'] = $find['title'];
  689. $item['cover'] = $find['pics'];
  690. $item['difference'] = $find['difference'];
  691. break;
  692. case 'Building':
  693. $find = Building::where([
  694. ['root_id', '=', $token['root_org']],
  695. ['del', '=', 0],
  696. ['id', '=', $item['data_id']]
  697. ])->field('id,community_id,name,cover')->find();
  698. $item['content_id'] = $find['id'];
  699. $item['title'] = $find['name'];
  700. $item['cover'] = $find['cover'];
  701. break;
  702. case 'AgentArticle':
  703. $find = AgentArticle::where([
  704. ['root_id', '=', $token['root_org']],
  705. ['id', '=', $item['data_id']]
  706. ])->field('id,title,file,type')->find();
  707. $item['content_id'] = $find['id'];
  708. $item['title'] = $find['title'];
  709. $item['cover'] = $find['file'];
  710. $item['file_type'] = $find['type'];
  711. break;
  712. default:
  713. break;
  714. }
  715. }
  716. //查询分享的奖励规则
  717. $rule_data = AgentType::where([['root_id', '=', $agtdata['root_id']], ['id', '=', $agtdata['type']]])->value('count');
  718. $rule_list = !empty($rule_data) ? json_decode($rule_data, true) : '';
  719. $reward_rules = [];
  720. if (!empty($rule_list)) {
  721. $reward_rules[] = '1 推荐一个客户' . $rule_list['putong_integral'] . '积分';
  722. $reward_rules[] = '2 分享一条内容' . $rule_list['ptagentshare_integral'] . '积分';
  723. $reward_rules[] = '3 分享内容获得一条线索' . $rule_list['produceclue_integral'] . '积分';
  724. $reward_rules[] = '4 客户到店' . $rule_list['daodian_integral'] . '积分';
  725. $reward_rules[] = '5 客户签单' . $rule_list['jiaoding_integral'] . '积分';
  726. $reward_rules[] = '6 客户转单' . $rule_list['qiandan_integral'] . '积分';
  727. }
  728. //$reward_rules = !empty($rule_list) ? '分享一条内容'.$rule_list['ptagentshare_integral'].'积分' : '';
  729. $list = ['list' => $data, 'reward_rules' => $reward_rules];
  730. return json(['code' => 0, 'data' => $list]);
  731. }
  732. /**
  733. * 公司上传内容列表
  734. */
  735. public function company_artlist()
  736. {
  737. $agtdata = $this->request->agtdata;
  738. $token = $this->request->token;
  739. $param = $this->request->only(['page' => 1, 'limit' => 10]);
  740. $where = [
  741. ['root_id', '=', $token['root_org']],
  742. ['from', '=', 2],
  743. ['disable', '=', 0]
  744. ];
  745. $vrObj = new Vr();
  746. $list = AgentArticle::where($where)->field('id,title,file,type,content,addtime,vr_group_ids')->page($param['page'], $param['limit'])->select()->toArray();
  747. $show_url = config('app.vr_show_domain');
  748. foreach ($list as $key => $val) {
  749. //分享量
  750. $list[$key]['share_num'] = ShareLog::where([['type', '=', 'AgentArticle'], ['data_id', '=', $val['id']], ['agent_id', '<>', 'NULL']])->count();
  751. //线索
  752. $list[$key]['clue_num'] = CustomerClue::where([['pipe_type', '=', 'AgentArticle'], ['pipe_id', '=', $val['id']], ['agent_id', '<>', 0]])->count();
  753. $list[$key]['files'] = '';
  754. if ($val['type'] == 2) $list[$key]['cover'] = !empty($val['file'][0]) ? $val['file'][0] : '';
  755. if ($val['type'] == 3) {
  756. $list[$key]['files'] = !empty($val['file'][0]) ? str_replace('https://' . config('app.ali_oss_bindurl') . '/', '', $val['file'][0]) : '';
  757. $list[$key]['cover'] = !empty($val['file'][0]) ? $vrObj->getFirstImg(str_replace('https://' . config('app.ali_oss_bindurl') . '/', '', $val['file'][0])) : '';
  758. }
  759. if ($val['type'] == 4) {
  760. $group = VrGroup::where('id', $val['vr_group_ids'])->field('pic_path')->findOrEmpty();
  761. $list[$key]['cover'] = $group->isEmpty() ? '' : $group->pic_path;
  762. }
  763. $list[$key]['vr_group_url'] = '';
  764. if ($val['type'] == 4) {
  765. $group = VrGroup::where('id', $val['vr_group_ids'])->field('pic_path,sid')->findOrEmpty();
  766. if (!$group->isEmpty()) {
  767. $list[$key]['vr_group_url'] = $show_url . $group->sid;
  768. $list[$key]['file_img'] = $group->pic_path;
  769. }
  770. $data['file_img'] = $group->isEmpty() ? '' : $group->pic_path;
  771. }
  772. }
  773. //查询分享的奖励规则
  774. $rule_data = AgentType::where([['root_id', '=', $agtdata['root_id']], ['id', '=', $agtdata['type']]])->value('count');
  775. $rule_list = !empty($rule_data) ? json_decode($rule_data, true) : '';
  776. $reward_rules = [];
  777. if (!empty($rule_list)) {
  778. $reward_rules[] = '1 推荐一个客户' . $rule_list['putong_integral'] . '积分';
  779. $reward_rules[] = '2 分享一条内容' . $rule_list['ptagentshare_integral'] . '积分';
  780. $reward_rules[] = '3 分享内容获得一条线索' . $rule_list['produceclue_integral'] . '积分';
  781. $reward_rules[] = '4 客户到店' . $rule_list['daodian_integral'] . '积分';
  782. $reward_rules[] = '5 客户签单' . $rule_list['jiaoding_integral'] . '积分';
  783. $reward_rules[] = '6 客户转单' . $rule_list['qiandan_integral'] . '积分';
  784. }
  785. $list = ['list' => $list, 'reward_rules' => $reward_rules];
  786. return json(['code' => 0, 'data' => $list, 'msg' => '获取成功']);
  787. }
  788. /**
  789. * 查询我是否还有未分享的任务(用于显示红点图标)
  790. */
  791. public function newShareArticle()
  792. {
  793. $agtdata = $this->request->agtdata;
  794. $token = $this->request->token;
  795. $where = [
  796. ['root_id', '=', $token['root_org']],
  797. ['employee_id', '=', $agtdata['agent_employee_id']],
  798. ['disable', '=', 0],
  799. ];
  800. $data = AgentShareContent::where($where)->select();
  801. $count = 0;
  802. foreach ($data as $item) {
  803. $log = ShareLog::where(['agent_id' => $agtdata['id'], 'type' => $item['type'], 'data_id' => $item['data_id']])->count();
  804. !empty($log) ? $count += $log : 0;
  805. }
  806. return json(['code' => 0, 'new' => empty($count) ? false : true]);
  807. }
  808. /**
  809. * 分享任务详情
  810. */
  811. public function share_article_details()
  812. {
  813. $id = $this->request->param('id');
  814. $from = $this->request->param('from');
  815. $token = $this->request->token;
  816. $agtdata = $this->request->agtdata;
  817. $where = [
  818. ['root_id', '=', $token['root_org']],
  819. ['id', '=', $id],
  820. ['disable', '=', 0]
  821. ];
  822. if (empty($from)) $where[] = ['employee_id', '=', $agtdata['agent_employee_id']];
  823. $data = AgentArticle::where($where)->field('id,title,file,content,talkskill,type,vr_group_ids')->find();
  824. //var_dump($data);exit;
  825. $vrObj = new Vr();
  826. $data['file_img'] = $data['files'] = '';
  827. $show_url = config('app.vr_show_domain');
  828. if ($data['type'] == 3) {
  829. $data['files'] = !empty($data['file'][0]) ? str_replace('https://' . config('app.ali_oss_bindurl') . '/', '', $data['file'][0]) : '';
  830. $data['file_img'] = !empty($data['file'][0]) ? $vrObj->getFirstImg(str_replace('https://' . config('app.ali_oss_bindurl') . '/', '', $data['file'][0])) : '';
  831. }
  832. $data['vr_group_url'] = '';
  833. if ($data['type'] == 4) {
  834. $group = VrGroup::where('id', $data['vr_group_ids'])->field('pic_path,sid')->findOrEmpty();
  835. if (!$group->isEmpty()) {
  836. $data['vr_group_url'] = $show_url . $group->sid;
  837. $data['file_img'] = $group->pic_path;
  838. }
  839. $data['file_img'] = $group->isEmpty() ? '' : $group->pic_path;
  840. }
  841. return json(['code' => 0, 'data' => $data]);
  842. }
  843. /**
  844. * 上传分享记录
  845. * type:Article文章、Video视频、MaterialCase案例、CompanyStrength实力、MaterialEvidence客户好评、Building楼盘、AgentArticle业务员上传
  846. */
  847. public function upload_share_log()
  848. {
  849. $param = $this->request->only(['article_id' => 0, 'file_media_id', 'remarks', 'type' => '']);
  850. $token = $this->request->token;
  851. $agtdata = $this->request->agtdata;
  852. $param['root_id'] = $token['root_org'];
  853. $param['employee_id'] = $agtdata['agent_employee_id'];
  854. $param['uid'] = $agtdata['uid'];
  855. $param['addtime'] = time();
  856. //上传文件
  857. if (!empty($param['file_media_id'])) {
  858. $file_media_id = [];
  859. foreach ($param['file_media_id'] as $k => $v) {
  860. $file_media_id[$k] = [
  861. 'serverId' => !empty($v['serverId']) ? $v['serverId'] : '',
  862. 'img' => !empty($v['img']) ? str_replace('https://' . config('app.ali_oss_bindurl') . '/', '', $v['img']) : ''
  863. ];
  864. }
  865. $param['down_status'] = 1;
  866. $param['file_media_id'] = json_encode($file_media_id);
  867. }
  868. $add = AgentShareLog::insert($param);
  869. Console::call('download', ['agent_share_log']);
  870. if ($add) {
  871. return json(['code' => 0, 'msg' => '保存成功']);
  872. } else {
  873. return json(['code' => 1, 'msg' => '保存失败']);
  874. }
  875. }
  876. /**
  877. * 经纪人分享文章成功回调
  878. * type:Article文章、Video视频、MaterialCase案例、CompanyStrength实力、MaterialEvidence客户好评、Building楼盘、AgentArticle上传分享内容
  879. */
  880. public function agent_sharelog($article_id, $type)
  881. {
  882. if (!isset($article_id) || empty($article_id)) return json(['code' => self::error_msg, 'msg' => '缺少分享参数']);
  883. $token = $this->request->token;
  884. $agtdata = $this->request->agtdata;
  885. ShareLog::create([
  886. 'employee_id' => $agtdata['agent_employee_id'],
  887. 'type' => $type,
  888. 'data_id' => $article_id,
  889. 'agent_id' => $agtdata['id']
  890. ]);
  891. return json(['code' => self::success, 'msg' => '记录添加成功']);
  892. }
  893. /**
  894. * 经纪人上传截图列表
  895. */
  896. public function uploadlog_list()
  897. {
  898. $param = $this->request->only(['page' => 1, 'limit' => 10]);
  899. $token = $this->request->token;
  900. $agtdata = $this->request->agtdata;
  901. $agt_typename = !empty($agtdata['type']) ? AgentType::where([['root_id', '=', $agtdata['root_id']], ['id', '=', $agtdata['type']]])->value('type_name') : '';
  902. $list = AgentShareLog::with(['user', 'agentUser'])->where([['root_id', '=', $agtdata['root_id']], ['uid', '=', $agtdata['uid']]])
  903. ->field('id,employee_id,uid,img,addtime,remarks,status')->order('id desc')->page($param['page'], $param['limit'])->select()->toArray();
  904. foreach ($list as $key => $val) {
  905. $list[$key]['type_name'] = $agt_typename;
  906. }
  907. return json(['code' => 0, 'data' => $list, 'msg' => '获取成功']);
  908. }
  909. /**
  910. * 经纪人线索列表
  911. * state:0、1、2
  912. * phone:has、hasno
  913. * getDate:Y-m-d
  914. */
  915. public function clue_list($page, $limit, $getDate = '', $state = '', $phone = '')
  916. {
  917. $token = $this->request->token;
  918. $agtdata = $this->request->agtdata;
  919. $count = [
  920. 'count_clue' => 0,
  921. 'creat_clue' => 0,
  922. 'day_clue' => 0
  923. ];
  924. //总线索
  925. $count['count_clue'] = CustomerClue::where([['agent_id', '=', $agtdata['id']]])->count();
  926. //已建档
  927. $count['creat_clue'] = CustomerClue::where([['agent_id', '=', $agtdata['id']], ['state', '=', 1]])->count();
  928. //今日线索
  929. $count['day_clue'] = CustomerClue::where([['agent_id', '=', $agtdata['id']], ['addtime', 'between', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')]]])->count();
  930. $condition = [
  931. ['agent_id', '=', $agtdata['id']],
  932. //['pipe_type', '=', 'agentArticle']
  933. ];
  934. if (isset($state)) $condition[] = ['state', '=', $state];
  935. if (!empty($getDate)) $condition[] = ['addtime', 'like', $getDate . '%'];
  936. if ($phone == 'has') $condition[] = ['phone', 'not null', ''];
  937. elseif ($phone == 'hasno') $condition[] = ['phone', 'null', ''];
  938. $data = CustomerClue::field('id,uid,phone,addtime,updatetime,state')->with(['user'])->withCount(['subscribe' => function ($query) use ($token) {
  939. $query->where('root_id', $token['root_org']);
  940. }, 'footprints'])->where($condition)->order('updatetime desc')->page($page, $limit)->select();
  941. foreach ($data as $k => $v) {
  942. $data[$k]['updatetime'] = $v['updatetime'] ? explode(' ', $v['updatetime']) : explode(' ', $v['addtime']);
  943. }
  944. return json(['code' => 0, 'msg' => '获取成功', 'data' => ['count' => $count, 'data' => $data]]);
  945. }
  946. /**
  947. * 经纪人查看客户访问记录(👣)
  948. */
  949. public function footprints($id)
  950. {
  951. $token = $this->request->token;
  952. $agtdata = $this->request->agtdata;
  953. $where = [
  954. ['id', '=', $id],
  955. ['agent_id', '=', $agtdata['id']]
  956. ];
  957. $clue = CustomerClue::where($where)->find();
  958. if (empty($clue)) return json(['code' => self::success, 'data' => [], 'msg' => '获取成功']);
  959. // 查询条件设置
  960. $condition = [
  961. ['addtime', '>=', $clue->addtime],
  962. ['uid', '=', $clue->uid],
  963. ['org_id', 'in', orgSubIds($token['root_org'])]
  964. ];
  965. $data = Footprints::where($condition)->field('id,pipe_type,reg_info,visit_due_time,addtime,uid')->order('addtime desc')->select();
  966. if ($data) {
  967. //装修案例预约记录
  968. $w2[] = ['uid', 'in', array_column($data->toArray(), 'uid')];
  969. $w2[] = ['root_id', '=', $token['root_org']];
  970. $sub = CustomerSubscribe::where($w2)->field('sucai_id,uid')->select()->toArray();
  971. foreach ($data as $k => $v) {
  972. if ($v['pipe_type'] == 'materialCase') {
  973. $data[$k]['subscribe'] = 0;
  974. foreach ($sub as $k2 => $v2) {
  975. $json = json_decode($v->getData('reg_info'), true);
  976. if ($v2['uid'] == $v['uid'] && $v2['sucai_id'] == $json['id']) {
  977. $data[$k]['subscribe'] = 1;
  978. break;
  979. }
  980. }
  981. } else {
  982. $data[$k]['subscribe'] = 0;
  983. }
  984. }
  985. }
  986. return json(['code' => self::success, 'data' => $data, 'msg' => '获取成功']);
  987. }
  988. /**
  989. * 兑换奖品列表
  990. */
  991. public function prize_list()
  992. {
  993. $token = $this->request->token;
  994. $agtdata = $this->request->agtdata;
  995. $agt_integral = AgentIntegralLog::where([['agt_id', '=', $agtdata['id']]])->order('id desc')->value('now_integral');
  996. $list = AgentPrize::where([['root_id', '=', $token['root_org']], ['status', '=', 1]])->select()->toArray();
  997. $agtemp = Employee::where([['root_id', '=', $token['root_org']], ['id', '=', $agtdata['agent_employee_id']], ['state', '=', '在职']])->field('id,phone,wx')->find();
  998. $data = ['list' => $list, 'agt_integral' => $agt_integral ? $agt_integral : 0, 'agtemp' => $agtemp];
  999. return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']);
  1000. }
  1001. /**
  1002. * 兑换奖品
  1003. */
  1004. public function exchange_prize()
  1005. {
  1006. $token = $this->request->token;
  1007. $agtdata = $this->request->agtdata;
  1008. $param = $this->request->only(['id']);
  1009. // 积分兑换的商品
  1010. $prize_data = AgentPrize::where([['root_id', '=', $token['root_org']], ['id', '=', $param['id']]])->find();
  1011. // 经纪人积分获取
  1012. $agtIntegral = AgentIntegralLog::where([['agt_id', '=', $agtdata['id']]])->order('id desc')->value('now_integral');
  1013. if ($prize_data['integral'] > $agtIntegral) return json(['code' => 1, 'msg' => '当前积分不能兑换此奖品']);
  1014. $bili = Db::table('fl_credits_setting')->where([['code', '=', 'reduce_integral'], ['root_id', '=', $token['root_org']]])->value('value');
  1015. $bili = !empty($bili) ? json_decode($bili, true) : ['jifen' => 1, 'money' => 1];
  1016. Db::startTrans();
  1017. try {
  1018. $add = [
  1019. 'agent_id' => $agtdata['id'],
  1020. 'empid' => $agtdata['agent_employee_id'],
  1021. 'integral' => $prize_data['integral'],
  1022. 'root_id' => $agtdata['root_id'],
  1023. 'good_id' => $param['id']
  1024. ];
  1025. $ms = AgentApplyIntegral::insertGetId($add);
  1026. $money = bcdiv($prize_data['integral'], $bili['jifen'], 1) * $bili['money'];
  1027. // 添加积分兑换记录
  1028. AgentIntegralLog::create([
  1029. 'agt_id' => $agtdata['id'],
  1030. 'addtime' => time(),
  1031. 'integral' => $prize_data['integral'],
  1032. 'type' => 2,
  1033. 'money' => $money,
  1034. 'now_integral' => $agtIntegral - $prize_data['integral']
  1035. ]);
  1036. } catch (\Exception $e) {
  1037. // 回滚事务
  1038. Db::rollback();
  1039. return json(['code' => 1, 'msg' => '兑换失败']);
  1040. }
  1041. Db::commit();
  1042. $msg = '经纪人【' . $agtdata['agent_name'] . '】提交兑换奖品申请';
  1043. event(new Msg($agtdata['agent_employee_id'], $msg, 'agentWipen', $ms));
  1044. return json(['code' => 0, 'msg' => '兑换申请已提交']);
  1045. }
  1046. /**
  1047. * 经纪人端是否展示【推荐分享】板块(后台设置开关)
  1048. */
  1049. public function show_recommend_share()
  1050. {
  1051. $token = $this->request->token;
  1052. $setdata = Setting::where([['root_id', '=', $token['root_org']], ['name', '=', 'agentEmployeeAdd']])->value('content');
  1053. $setdata = $setdata ?: 'true';
  1054. return json(['code' => 0, 'data' => $setdata]);
  1055. }
  1056. }