MCustomerCommunication.Class.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. <?php
  2. /**
  3. * 客户沟通记录表
  4. * Created by PhpStorm.
  5. * User: haoren
  6. * Date: 2021/02/23
  7. * Time: 14:00
  8. */
  9. namespace JinDouYun\Model\Customer;
  10. use http\Message\Parser;
  11. use JinDouYun\Controller\Customer\Customer;
  12. use JinDouYun\Model\Department\MDepartment;
  13. use JinDouYun\Model\Department\MStaff;
  14. use JinDouYun\Model\SysAreaChina\MSysAreaChina;
  15. use JinDouYun\Model\System\MCustomerSource;
  16. use JinDouYun\Model\MBaseModel;
  17. use Mall\Framework\Core\ErrorCode;
  18. use Mall\Framework\Core\StatusCode;
  19. use Mall\Framework\Core\ResultWrapper;
  20. use JinDouYun\Dao\Order\DOrder;
  21. use JinDouYun\Dao\Customer\DCustomerCommunication;
  22. class MCustomerCommunication
  23. {
  24. private $objDCustomerCommunication;
  25. private $enterpriseId;
  26. private $userCenterId;
  27. public function __construct($enterpriseId = 0,$userCenterId = 0)
  28. {
  29. $this->enterpriseId = $enterpriseId;
  30. $this->userCenterId = $userCenterId;
  31. $this->objDCustomerCommunication = new DCustomerCommunication('default');
  32. $this->objDCustomerCommunication->setTable('qianniao_customer_communication_'.$enterpriseId);
  33. }
  34. /**
  35. * 客户沟通添加
  36. * @param $params
  37. * @return ResultWrapper
  38. */
  39. public function addCustomerCommunication($params)
  40. {
  41. $params['createTime'] = time();
  42. $params['updateTime'] = time();
  43. $dbResult = $this->objDCustomerCommunication->insert($params);
  44. if($dbResult === false){
  45. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  46. }else{
  47. return ResultWrapper::success($dbResult);
  48. }
  49. }
  50. /**
  51. * 客户沟通删除
  52. * @param $id
  53. * @return ResultWrapper
  54. */
  55. public function delCustomerCommunication($id)
  56. {
  57. $update = [
  58. 'deleteStatus' => StatusCode::$delete,
  59. 'updateTime' => time(),
  60. ];
  61. $dbResult = $this->objDCustomerCommunication->update($update, $id);
  62. if($dbResult === false){
  63. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  64. }else{
  65. return ResultWrapper::success($dbResult);
  66. }
  67. }
  68. /**
  69. * 客户沟通修改
  70. * @param $params
  71. * @param $id
  72. * @return ResultWrapper
  73. */
  74. public function updateCustomerCommunication($params, $id)
  75. {
  76. $params['updateTime'] = time();
  77. $dbResult = $this->objDCustomerCommunication->update($params,['id' => $id]);
  78. if($dbResult === false){
  79. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  80. }else{
  81. return ResultWrapper::success($dbResult);
  82. }
  83. }
  84. /**
  85. * 客户沟通列表
  86. * @return ResultWrapper
  87. */
  88. public function getAllCustomerCommunication($params)
  89. {
  90. $limit = $params['limit'];
  91. unset($params['limit']);
  92. $offset = $params['offset'];
  93. unset($params['offset']);
  94. $whereSql = ' deleteStatus = '.StatusCode::$standard;
  95. if(isset($params['customerId']) && !empty($params['customerId'])){
  96. $whereSql .= ' and customerId = '.$params['customerId'];
  97. }
  98. // if(isset($params['staffId']) && !empty($params['staffId'])){
  99. // $whereSql .= ' and staffId = '.$params['staffId'];
  100. // }
  101. if(isset($params['start']) && !empty($params['start']) && isset($params['end']) && !empty($params['end'])){
  102. $whereSql .= ' and `time` between '.$params['start'].' and '.$params['end'];
  103. }
  104. //数据域查询
  105. $objMBaseModel = new MBaseModel($this->enterpriseId,$this->userCenterId);
  106. $whereSql = $objMBaseModel->getSalesManQueryParams($whereSql, 'staffId');
  107. $dbResult = $this->objDCustomerCommunication->select($whereSql, '*', 'createTime desc', $limit, $offset);
  108. if ($dbResult === false) {
  109. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  110. }
  111. $modelResult = self::format($dbResult);
  112. if(!$modelResult->isSuccess()){
  113. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  114. }
  115. $data = $modelResult->getData();
  116. $sql = 'select count(*) as count from '.$this->objDCustomerCommunication->get_Table().' where '.$whereSql;
  117. $dbResult = $this->objDCustomerCommunication->query($sql);
  118. if ($dbResult === false) {
  119. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  120. }
  121. $return = [
  122. 'data' => $data,
  123. 'total' => $dbResult[0]['count']?$dbResult[0]['count']:0,
  124. ];
  125. if($return === false){
  126. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  127. }else{
  128. return ResultWrapper::success($return);
  129. }
  130. }
  131. /**
  132. * 客户沟通详情
  133. * @param $params
  134. * @return ResultWrapper
  135. */
  136. public function getCustomerCommunication($params)
  137. {
  138. $params['deleteStatus'] = StatusCode::$standard;
  139. $dbResult = $this->objDCustomerCommunication->get($params);
  140. if($dbResult === false){
  141. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  142. }
  143. $modelResult = self::format($dbResult);
  144. if(!$modelResult->isSuccess()){
  145. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  146. }
  147. $data = $modelResult->getData();
  148. return ResultWrapper::success($data);
  149. }
  150. /**
  151. * 格式化
  152. * @param $params
  153. */
  154. public function format($params)
  155. {
  156. if (empty($params)) {
  157. return ResultWrapper::success($params);
  158. }
  159. if (!isset($params['id'])) {
  160. $data = $params;
  161. } else {
  162. $data = [$params];
  163. }
  164. $customerData = [];
  165. $customerIds = array_column($data, 'customerId');
  166. $staffData = [];
  167. $staffIds = array_column($data, 'staffId');
  168. if(!empty($customerIds)){
  169. $objMCustomer = new MCustomer($this->enterpriseId,false);
  170. $modelResult = $objMCustomer->getCustomerData(['id' => $customerIds], '*', false);
  171. if(!$modelResult->isSuccess()){
  172. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  173. }
  174. $customerResult = $modelResult->getData();
  175. foreach($customerResult as $value){
  176. $customerData[$value['id']] = $value;
  177. $customerData[$value['managerMobile']] = $value;
  178. }
  179. unset($modelResult);
  180. }
  181. if(!empty($staffIds)){
  182. $objMStaff = new MStaff($this->enterpriseId,false);
  183. $modelResult = $objMStaff->getAllStaffData(['id' => $staffIds]);
  184. if(!$modelResult->isSuccess()){
  185. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  186. }
  187. $staffResult = $modelResult->getData();
  188. foreach($staffResult as $value){
  189. $staffData[$value['id']] = $value;
  190. }
  191. }
  192. foreach ($data as &$value) {
  193. $value['customerName'] = isset($customerData[$value['customerId']]) ? $customerData[$value['customerId']]['name'] : '';
  194. $value['staffName'] = isset($staffData[$value['staffId']]) ? $staffData[$value['staffId']]['staffName'] : '';
  195. if(isset($value['managerMobile'])){
  196. $value['managerMobile'] = isset($customerData[$value['managerMobile']]) && !empty($customerData[$value['managerMobile']]) ? $customerData[$value['managerMobile']]['managerMobile'] : '';
  197. }
  198. }
  199. unset($value);
  200. if (!isset($params['id'])) {
  201. $return = $data;
  202. } else {
  203. $return = array_shift($data);
  204. }
  205. return ResultWrapper::success($return);
  206. }
  207. /**
  208. * 客户拜访报表
  209. * @return ResultWrapper
  210. */
  211. public function getCustomerCallOnReportForm($selectParams)
  212. {
  213. $limit = $selectParams['limit'];
  214. unset($selectParams['limit']);
  215. $offset = $selectParams['offset'];
  216. unset($selectParams['offset']);
  217. //筛选部门
  218. $whereSql = 'c.deleteStatus ='.StatusCode::$standard ;
  219. if(!empty($selectParams['departmentId']) ){
  220. $whereSql .= ' and s.departmentId in(' .implode (',',$selectParams['departmentId']).')';
  221. }
  222. //筛选时间
  223. if(isset($selectParams['start']) && !empty($selectParams['start']) && isset($selectParams['end']) && !empty($selectParams['end'])){
  224. $whereSql .= ' and `time` between '.$selectParams['start'].' and '.$selectParams['end'];
  225. }
  226. //数据域查询
  227. $objMBaseModel = new MBaseModel($this->enterpriseId,$this->userCenterId);
  228. $whereSql = $objMBaseModel->getSalesManQueryParams($whereSql, ' staffId ');
  229. //查询员工 姓名,部门,客户数,跟进次数
  230. $sql = 'select c.staffId,s.staffName,s.departmentId,count(DISTINCT c.customerid) as customerNum,count(c.id) as num from qianniao_customer_communication_'.$this->enterpriseId.' as c left join qianniao_staff_'.$this->enterpriseId.' s on c.staffId = s.id where '. $whereSql.' GROUP BY c.staffId limit '.$offset.','.$limit;
  231. $dbResultStaff = $this->objDCustomerCommunication->query($sql);
  232. if ($dbResultStaff === false) {
  233. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  234. }
  235. //统计条数
  236. $sql = 'SELECT count(*) as total FROM (select c.staffId from qianniao_customer_communication_'.$this->enterpriseId.' as c left join qianniao_staff_'.$this->enterpriseId.' s on c.staffId = s.id where '.$whereSql.' GROUP BY c.staffId ) as a';
  237. $dbResult = $this->objDCustomerCommunication->query($sql);
  238. if ($dbResult === false) {
  239. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  240. }
  241. //查询单个客户的订单金额和订单数量
  242. $sql = 'select customerId,count(id) as number ,sum(payAmount) as amount from qianniao_order_'.$this->enterpriseId.'_1 where customerId in(select c.customerId from qianniao_customer_communication_'.$this->enterpriseId.' as c left join qianniao_staff_'.$this->enterpriseId.' s on c.staffId = s.id group by c.customerId) group by customerId';
  243. $dbResultOrder = $this->objDCustomerCommunication->query($sql);
  244. if ($dbResultOrder === false) {
  245. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  246. }
  247. //用客户id映射客户数据
  248. $customerNum = [];
  249. foreach ($dbResultOrder as $key =>$value){
  250. $customerNum [$value['customerId']] = $value ;
  251. }
  252. $departmentIds = [];
  253. //把查寻记录表中的员工循环出来
  254. foreach ($dbResultStaff as &$v){
  255. $departmentIds[] = $v['departmentId'];
  256. //根据员工id查询客户id;
  257. $sql = 'select customerId from qianniao_customer_communication_'.$this->enterpriseId.' where staffId = '.$v['staffId'].' group by customerId';
  258. $dbResultCustomer = $this->objDCustomerCommunication->query($sql);
  259. if ($dbResultCustomer === false) {
  260. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  261. }
  262. $v['customerOrderNum'] = 0;
  263. $v['customerOrderAmount'] = 0.00;
  264. //循环累加客户数据
  265. foreach ($dbResultCustomer as $value){
  266. if( isset($customerNum[$value['customerId']]) ){
  267. $v['customerOrderNum'] += $customerNum[$value['customerId']]['number'];
  268. $v['customerOrderAmount'] += $customerNum[$value['customerId']]['amount'];
  269. }
  270. }
  271. }
  272. $objDepartment =new MDepartment($this->enterpriseId);
  273. $modelResult = $objDepartment->getNameByIds($departmentIds);
  274. if(!$modelResult->isSuccess()){
  275. return ResultWrapper::fail($modelResult->getData(),$modelResult->getErrorCode());
  276. }
  277. $departmentData = $modelResult->getData();
  278. $departmentName = [];
  279. foreach ($departmentData as $value){
  280. $departmentName[$value['id']] = $value['departmentName'];
  281. }
  282. foreach ($dbResultStaff as &$value){
  283. $value['departmentName'] = $departmentName[$value['departmentId']];
  284. }
  285. $return = [
  286. 'data' => $dbResultStaff,
  287. 'total' => $dbResult[0]['total']
  288. ];
  289. return ResultWrapper::success($return);
  290. }
  291. /**
  292. * 客户拜访记录详情
  293. * @return ResultWrapper
  294. */
  295. public function getCustomerVisitInfo($selectParams)
  296. {
  297. $limit = $selectParams['limit'];
  298. unset($selectParams['limit']);
  299. $offset = $selectParams['offset'];
  300. unset($selectParams['offset']);
  301. $selectParams['deleteStatus'] = StatusCode::$standard;
  302. $sql = 'select a.customerId,b.`name`,a.time,a.location,a.content,a.picture from qianniao_customer_communication_'.$this->enterpriseId.' as a LEFT JOIN qianniao_customer_'.$this->enterpriseId.' as b on a.customerId = b.id where staffId ='.$selectParams['staffId'].' and a.deleteStatus ='.$selectParams['deleteStatus']. ' limit '.$offset .',' .$limit;
  303. $dbResult = $this->objDCustomerCommunication->query($sql);
  304. if($dbResult === false){
  305. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  306. }
  307. $total = $this->objDCustomerCommunication->count($selectParams);
  308. $return = [
  309. 'data' => $dbResult,
  310. 'total' => ($total)?intval($total):0,
  311. ];
  312. return ResultWrapper::success($return);
  313. }
  314. /**
  315. * 未拜访客户统计列表
  316. * @return ResultWrapper
  317. */
  318. public function getCustomerNoVisit($selectParams)
  319. {
  320. $limit = $selectParams['limit'];
  321. unset($selectParams['limit']);
  322. $offset = $selectParams['offset'];
  323. unset($selectParams['offset']);
  324. $selectParams['deleteStatus'] = StatusCode::$standard;
  325. $whereSql = ' where deleteStatus='.$selectParams['deleteStatus'];
  326. if( !empty($selectParams['staffId']) ){
  327. $whereSql .= ' and staffId ='.$selectParams['staffId'];
  328. }
  329. $where ='';
  330. if( !empty($selectParams['province']) ){
  331. $where .= ' and c.provinceCode ='.$selectParams['province'];
  332. }
  333. if( !empty($selectParams['city']) ){
  334. $where .= ' and c.cityCode ='.$selectParams['city'];
  335. }
  336. if( !empty($selectParams['district']) ){
  337. $where .= ' and c.districtCode ='.$selectParams['district'];
  338. }
  339. //`name` LIKE '%测试%' and salesManName LIKE '%根据%'
  340. if( !empty($selectParams['customerName']) ){
  341. $where .= ' and c.`name` LIKE "%' .$selectParams['customerName']. '%"';
  342. }
  343. //根据天数查询所对应的未拜访记录
  344. $daysSql = 'select b.customerId,b.intervalDay,c.* from (select *,TIMESTAMPDIFF(day,FROM_UNIXTIME(createTime,"%Y%m%d"),CURRENT_DATE()) as intervalDay from (select customerId,createTime from qianniao_customer_communication_'.$this->enterpriseId.$whereSql.' order by createTime desc limit 99999)a GROUP BY a.customerId)b LEFT JOIN qianniao_customer_'.$this->enterpriseId.' c on b.customerId = c.id WHERE b.intervalDay < '.$selectParams['days'].$where.' limit '.$offset.' , '.$limit;
  345. $dayDbResult = $this->objDCustomerCommunication->query($daysSql);
  346. if($dayDbResult === false){
  347. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  348. }
  349. if(empty($dayDbResult)){
  350. $return = [
  351. 'data' => [],
  352. 'total' => 0
  353. ];
  354. return ResultWrapper::success($return);
  355. }
  356. //拿到范围内的客户
  357. $customer = [];
  358. foreach ($dayDbResult as $value){
  359. $customer[] = $value['customerId'];
  360. }
  361. $customerIds = implode(',',$customer);
  362. //查询每个客户本年拜访次数
  363. $dbYearNum = $this->getCustomerYearVisitNumber($customerIds);
  364. if(!$dbYearNum->isSuccess()){
  365. return ResultWrapper::fail($dbYearNum->getData(),$dbYearNum->getErrorCode());
  366. }
  367. $yearNumDbResult = $dbYearNum->getData();
  368. $yearNum = [];
  369. foreach ($yearNumDbResult as $v){
  370. $yearNum[$v['customerId']]= $v['yearNum'];//客户本年拜访次数
  371. }
  372. //根据查出天数范围内的客户查出所对应的日期并归组
  373. $result = $this->getCustomerDate($customerIds);
  374. if(!$result->isSuccess()){
  375. return ResultWrapper::fail($result->getData(),$result->getErrorCode());
  376. }
  377. $dbResult = $result->getData();
  378. //查询客户数据以及注册时间
  379. $objMCustomer = new MCustomer($this->enterpriseId,false);
  380. $modelResult = $objMCustomer->getCustomerData(['id'=> $customer],'*',false);
  381. if(!$modelResult->isSuccess()){
  382. return ResultWrapper::fail($modelResult->getData(),$modelResult->getErrorCode());
  383. }
  384. $customerResult = $modelResult->getData();
  385. //处理客户地址
  386. $objMSysAreaChina = new MSysAreaChina();
  387. //处理客户类型
  388. $objMCustomerSource = new MCustomerSource($this->enterpriseId);
  389. $customerSourceIds = array_column($customerResult,'type');//客户类型
  390. //查询客户类型
  391. if ($customerSourceIds) {
  392. $customerSourceData = $objMCustomerSource->getCustomerSourceNameByIds($customerSourceIds);
  393. if ($customerSourceData->isSuccess()) {
  394. $customerSourceArray = $customerSourceData->getData();
  395. foreach ($customerSourceArray as $customerSource){
  396. $customerSourceArr[$customerSource['id']] = $customerSource['name'];
  397. }
  398. }
  399. }
  400. $customerData = [];
  401. foreach ($customerResult as $value){
  402. $customerData[$value['id']]['customerDays'] = intval((time() - $value['createTime']) / 86400);//注册天数
  403. $customerData[$value['id']]['name'] = $value['name'];//客户名
  404. $customerData[$value['id']]['code'] = $value['code'];//客户编码
  405. $customerData[$value['id']]['provinceCode'] = $value['provinceCode'];//地区
  406. $customerData[$value['id']]['cityCode'] = $value['cityCode'];//地区
  407. $customerData[$value['id']]['districtCode'] = $value['districtCode'];//地区
  408. $customerData[$value['id']]['address'] = $value['address'];//地区
  409. $customerData[$value['id']]['type'] = $value['type'];//客户类型
  410. // $customerSourceIds[] = $value['type'];
  411. $customerData[$value['id']]['salesManName'] = $value['salesManName'];//业务员
  412. }
  413. //按照查出的年 本月 上月进行分组
  414. $data = [];
  415. $year = strtotime(date('Y-1-1'));
  416. $thisMonth = mktime(0,0,0,date('m'),1,date('Y'));
  417. $lastMonth = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
  418. foreach ( $dbResult as $value ){
  419. //年
  420. $time = strtotime($value['time']);
  421. if( $time>$year ){
  422. if( isset($data[$value['customerId']]['year']) ){
  423. $data[$value['customerId']]['year']['orderNum'] += $value['orderNum'];
  424. $data[$value['customerId']]['year']['orderMoney'] += $value['orderMoney'];
  425. } else {
  426. $data[$value['customerId']]['year']['orderNum'] = $value['orderNum'];
  427. $data[$value['customerId']]['year']['orderMoney'] = $value['orderMoney'];
  428. }
  429. }
  430. //月
  431. if( $time>$thisMonth ){
  432. if( isset($data[$value['customerId']]['thisMonth']) ){
  433. $data[$value['customerId']]['thisMonth']['orderNum'] += $value['orderNum'];
  434. $data[$value['customerId']]['thisMonth']['orderMoney'] += $value['orderMoney'];
  435. } else {
  436. $data[$value['customerId']]['thisMonth']['orderNum'] = $value['orderNum'];
  437. $data[$value['customerId']]['thisMonth']['orderMoney'] = $value['orderMoney'];
  438. }
  439. }
  440. //上月
  441. if( $time>$lastMonth && $time < $thisMonth ){
  442. if( isset($data[$value['customerId']]['lastMonth']) ){
  443. $data[$value['customerId']]['lastMonth']['orderNum'] += $value['orderNum'];
  444. $data[$value['customerId']]['lastMonth']['orderMoney'] += $value['orderMoney'];
  445. } else {
  446. $data[$value['customerId']]['lastMonth']['orderNum'] = $value['orderNum'];
  447. $data[$value['customerId']]['lastMonth']['orderMoney'] = $value['orderMoney'];
  448. }
  449. }
  450. }
  451. foreach($dayDbResult as &$value) {
  452. //年
  453. if ( isset($data[$value['customerId']]['year']) ) {
  454. $value['year']['orderNum'] = $data[$value['customerId']]['year']['orderNum'];
  455. $value['year']['orderMoney'] = $data[$value['customerId']]['year']['orderMoney'];
  456. } else {
  457. $value['year']['orderNum'] = 0;
  458. $value['year']['orderMoney'] = 0;
  459. }
  460. //月
  461. if ( isset($data[$value['customerId']]['thisMonth']) ) {
  462. $value['thisMonth']['orderNum'] = $data[$value['customerId']]['thisMonth']['orderNum'];
  463. $value['thisMonth']['orderMoney'] = $data[$value['customerId']]['thisMonth']['orderMoney'];
  464. } else {
  465. $value['thisMonth']['orderNum'] = 0;
  466. $value['thisMonth']['orderMoney'] = 0;
  467. }
  468. //上月
  469. if ( isset($data[$value['customerId']]['lastMonth']) ) {
  470. $value['lastMonth']['orderNum'] = $data[$value['customerId']]['lastMonth']['orderNum'];
  471. $value['lastMonth']['orderMoney'] = $data[$value['customerId']]['lastMonth']['orderMoney'];
  472. } else {
  473. $value['lastMonth']['orderNum'] = 0;
  474. $value['lastMonth']['orderMoney'] = 0;
  475. }
  476. $value['customerDays'] = isset($customerData[$value['customerId']]) ? $customerData[$value['customerId']]['customerDays'] : '';
  477. $value['customerName'] = isset($customerData[$value['customerId']]) ? $customerData[$value['customerId']]['name'] : '';
  478. $value['customerCode'] = isset($customerData[$value['customerId']]) ? $customerData[$value['customerId']]['code'] : '';
  479. $value['customerSalesManName'] = isset($customerData[$value['customerId']]) ? $customerData[$value['customerId']]['salesManName'] : '';
  480. //处理客户地址
  481. if (isset($customerData[$value['customerId']]['provinceCode'] ) && isset($customerData[$value['customerId']]['cityCode']) && isset($customerData[$value['customerId']]['districtCode']) ) {
  482. $areaName = $objMSysAreaChina->getNameByCode([
  483. $customerData[$value['customerId']]['provinceCode'],
  484. $customerData[$value['customerId']]['cityCode'],
  485. $customerData[$value['customerId']]['districtCode']
  486. ]);
  487. $value['area']['provinceName'] = isset($areaName[ $customerData[$value['customerId']]['provinceCode']]) ? $areaName[ $customerData[$value['customerId']]['provinceCode']] : '';
  488. $value['area']['cityName'] = isset($areaName[ $customerData[$value['customerId']]['cityCode']]) ? $areaName[ $customerData[$value['customerId']]['cityCode']] : '';
  489. $value['area']['districtName'] = isset($areaName[ $customerData[$value['customerId']]['districtCode']]) ? $areaName[ $customerData[$value['customerId']]['districtCode']] : '';
  490. $value['area']['address'] = isset($customerData[$value['customerId']]['address']) ? $customerData[$value['customerId']]['address'] :'';
  491. }
  492. if(isset($customerData[$value['customerId']])){
  493. $value['typeName'] = isset($customerSourceArr[$customerData[$value['customerId']]['type']]) ? $customerSourceArr[$customerData[$value['customerId']]['type']] :'';
  494. } else {
  495. $value['typeName'] = '';
  496. }
  497. if(isset($yearNum[$value['customerId']])){
  498. $value['yearNum'] = isset( $yearNum[$value['customerId']]) ? $yearNum[$value['customerId']] : 0;
  499. } else {
  500. $value['yearNum'] = 0;
  501. }
  502. }
  503. $totalSql = 'select count(*) as total from (select *,TIMESTAMPDIFF(day,FROM_UNIXTIME(createTime,"%Y%m%d"),CURRENT_DATE()) as intervalDay from (select customerId,createTime from qianniao_customer_communication_'.$this->enterpriseId.$whereSql.' order by createTime desc limit 99999)a GROUP BY a.customerId)b LEFT JOIN qianniao_customer_'.$this->enterpriseId.' c on b.customerId = c.id WHERE b.intervalDay < '.$selectParams['days'].$where;
  504. $total = $this->objDCustomerCommunication->query($totalSql);
  505. if($total === false){
  506. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  507. }
  508. $return = [
  509. 'data' => $dayDbResult,
  510. 'total' => $total[0]['total'] ? intval($total[0]['total']):0,
  511. ];
  512. return ResultWrapper::success($return);
  513. }
  514. /**
  515. * 获取客户本年拜访次数统计
  516. * @return ResultWrapper
  517. * @params string
  518. */
  519. public function getCustomerYearVisitNumber($customerIds)
  520. {
  521. if(empty($customerIds)){
  522. return ResultWrapper::success([]);
  523. }
  524. $year = strtotime(date('Y-1-1')); //本年开始时间
  525. //根据客户id查询客户本年拜访次数
  526. $yearNumSql = 'SELECT customerId,count(customerId) as yearNum from qianniao_customer_communication_'.$this->enterpriseId.' WHERE customerId in('.$customerIds.') and time >= '.$year.' GROUP BY customerId';
  527. $yearNumDbResult = $this->objDCustomerCommunication->query($yearNumSql);
  528. if($yearNumDbResult === false){
  529. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  530. }
  531. return ResultWrapper::success($yearNumDbResult);
  532. }
  533. /**
  534. * 获取客户距离上次拜访多少天
  535. * @return ResultWrapper
  536. * @params string
  537. */
  538. public function getCustomerlastTimeDay($customerIds)
  539. {
  540. if(empty($customerIds)){
  541. return ResultWrapper::success([]);
  542. }
  543. //根据客户id查询客户距离上次拜访多少天
  544. $daySql = 'SELECT DISTINCT customerId,`time` from qianniao_customer_communication_'.$this->enterpriseId.' WHERE customerId in('.$customerIds.') order by `time` desc';
  545. $dayNumDbResult = $this->objDCustomerCommunication->query($daySql);
  546. foreach ($dayNumDbResult as $key =>$value){
  547. $dayNumDbResult[$key]['time'] = intval((time() - $value['time']) / 86400);
  548. }
  549. if($dayNumDbResult === false){
  550. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  551. }
  552. return ResultWrapper::success($dayNumDbResult);
  553. }
  554. /**
  555. * 根据客户id查出客户所对应的日期
  556. * @return ResultWrapper
  557. * @params string
  558. */
  559. public function getCustomerDate($customerIds)
  560. {
  561. if(empty($customerIds)){
  562. return ResultWrapper::success([]);
  563. }
  564. $sql = 'select customerId,count(id) as orderNum,sum(payAmount) as orderMoney,FROM_UNIXTIME(createTime,"%Y-%m") as time,createTime from qianniao_order_'.$this->enterpriseId.'_1 where customerId in('.$customerIds.') GROUP BY customerId,time';
  565. $dbResult = $this->objDCustomerCommunication->query($sql);
  566. if($dbResult === false){
  567. return ResultWrapper::fail($this->objDCustomerCommunication->error(), ErrorCode::$dberror);
  568. }
  569. return ResultWrapper::success($dbResult);
  570. }
  571. }