MCustomerVisitsLog.Class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: kang
  5. * Date: 2021/3/10
  6. * Time: 10:11
  7. */
  8. namespace JinDouYun\Model\Customer;
  9. use JinDouYun\Dao\UserCenter\DUserCenter;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\StatusCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use JinDouYun\Model\MBaseModel;
  14. use JinDouYun\Dao\Customer\DCustomer;
  15. use JinDouYun\Dao\Customer\DCustomerVisitsLog;
  16. use JinDouYun\Dao\GoodsManage\DGoodsBasic;
  17. class MCustomerVisitsLog extends MBaseModel
  18. {
  19. private $objDCustomerVisitsLog;
  20. private $enterpriseId;
  21. private $userCenterId;
  22. private $objDCustomer;
  23. public function __construct($enterpriseId, $userCenterId)
  24. {
  25. $this->enterpriseId = $enterpriseId;
  26. $this->userCenterId = $userCenterId;
  27. parent::__construct($this->enterpriseId, $this->userCenterId);
  28. $this->objDCustomerVisitsLog = new DCustomerVisitsLog('default');
  29. $this->objDCustomerVisitsLog->setTable('qianniao_customer_visits_log_'.$enterpriseId);
  30. $this->objDCustomer = new DCustomer();
  31. $this->objDCustomer->setTable('qianniao_customer_'.$enterpriseId);
  32. }
  33. /**
  34. * 添加客户访问记录
  35. * @param $params
  36. * @return ResultWrapper
  37. * @throws Exception
  38. */
  39. public function addCustomerVisitsLog($visitsLogData)
  40. {
  41. $accountTypeId = $this->objDCustomerVisitsLog->insert($visitsLogData);
  42. if($accountTypeId === false){
  43. return ResultWrapper::fail($this->objDCustomerVisitsLog->error(), ErrorCode::$dberror);
  44. }
  45. return ResultWrapper::success($accountTypeId);
  46. }
  47. /**
  48. * 获取指定客户访问记录
  49. */
  50. public function getCustomerVisitsLogInfo($customerVisitsLogId)
  51. {
  52. $dbResult = $this->objDCustomerVisitsLog->select($customerVisitsLogId);
  53. if($dbResult === false){
  54. return ResultWrapper::fail($this->objDCustomerVisitsLog->error(), ErrorCode::$dberror);
  55. }
  56. if( empty($dbResult) ){
  57. return ResultWrapper::success([]);
  58. }
  59. return ResultWrapper::success( self::format($dbResult) );
  60. }
  61. /**
  62. * 格式化
  63. * @return array
  64. */
  65. public function format($data)
  66. {
  67. // 映射客户id对应的名字
  68. $customerIdBindName = [];
  69. // 映射客户id对应的手机号
  70. $userCenterIdBindMobile = [];
  71. // 映射商品id和商品名称
  72. $goodsIdBindName = [];
  73. // 映射商品id和商品图片
  74. $goodsIdBindImg = [];
  75. $customerIds = array_column($data, 'customerId');
  76. $goodsIds = array_column($data, 'goodsId');
  77. $userCenterIds = array_column($data, 'usercenterId');
  78. // 客户数据映射
  79. if(!empty($customerIds)){
  80. $customerData = $this->objDCustomer->select($customerIds, 'id,name');
  81. if($customerData !== false){
  82. if( !empty($customerData) ){
  83. foreach ($customerData as $key => $value){
  84. $customerIdBindName[$value['id']] = $value['name'];
  85. }
  86. }
  87. }
  88. }
  89. // 客户电话数据映射
  90. if(!empty($customerIds)){
  91. $objDUserCenter = new DUserCenter();
  92. $userCenterData = $objDUserCenter->select($userCenterIds, 'id,mobile');
  93. if($userCenterData !== false){
  94. if( !empty($userCenterData) ){
  95. foreach ($userCenterData as $key => $value){
  96. $userCenterIdBindMobile[$value['id']] = $value['mobile'];
  97. }
  98. }
  99. }
  100. }
  101. // 根据商品ids查询商品名称和图片进行映射
  102. $objDGoodsBasic = new DGoodsBasic();
  103. $objDGoodsBasic->setTable('qianniao_goods_basic_'.$this->enterpriseId);
  104. if(!empty($goodsIds)){
  105. $dbResult = $objDGoodsBasic->select($goodsIds,'id,title,images');
  106. if($dbResult !== false){
  107. if( !empty($dbResult) ){
  108. foreach ($dbResult as $key => $value){
  109. $goodsIdBindName[$value['id']] = $value['title'];
  110. $goodsIdBindImg[$value['id']] = $value['images'];
  111. }
  112. }
  113. }
  114. }
  115. // 循环主数据,进行对应的字段映射操作
  116. foreach ($data as $key => $value){
  117. $data[$key]['customerName'] = '';
  118. if( isset($customerIdBindName[$value['customerId']]) ){
  119. $data[$key]['customerName'] = $customerIdBindName[$value['customerId']];
  120. }
  121. $data[$key]['mobile'] = '';
  122. if( isset($userCenterIdBindMobile[$value['usercenterId']]) ){
  123. $data[$key]['mobile'] = $userCenterIdBindMobile[$value['usercenterId']];
  124. }
  125. $data[$key]['goodsName'] = '';
  126. if( isset($goodsIdBindName[$value['goodsId']]) ){
  127. $data[$key]['goodsName'] = $goodsIdBindName[$value['goodsId']];
  128. }
  129. }
  130. return $data;
  131. }
  132. /**
  133. * 获取所有的客户的浏览记录
  134. */
  135. public function getAllCustomerVisitsLog($selectParams)
  136. {
  137. $limit = $selectParams['limit'];
  138. unset($selectParams['limit']);
  139. $offset = $selectParams['offset'];
  140. unset($selectParams['offset']);
  141. $returnData = [
  142. 'data' => [],
  143. 'total' => 0,
  144. ];
  145. $whereSql = '';
  146. if (isset($selectParams['startTime']) && !empty($selectParams['startTime'])) {
  147. $where = empty($whereSql) ? ' where ' : ' AND ';
  148. $whereSql .= $where . ' a.createTime >=' . $selectParams['startTime'];
  149. }
  150. if (isset($selectParams['endTime']) && !empty($selectParams['endTime'])) {
  151. $where = empty($whereSql) ? ' where ' : ' AND ';
  152. $whereSql .= $where . ' a.createTime <= '. $selectParams['endTime'];
  153. }
  154. if (isset($selectParams['categoryId']) && !empty($selectParams['categoryId'])) {
  155. $where = empty($whereSql) ? ' where ' : ' AND ';
  156. $whereSql .= $where .' FIND_IN_SET('.$selectParams['categoryId'].',b.categoryPath)';
  157. }
  158. if (isset($selectParams['customerId']) && !empty($selectParams['customerId'])) {
  159. $where = empty($whereSql) ? ' where ' : ' AND ';
  160. $whereSql .= $where . ' a.customerId = '. $selectParams['customerId'];
  161. }
  162. //数据域查询
  163. $objMBaseModel = new MBaseModel($this->enterpriseId,$this->userCenterId);
  164. $whereSql = $objMBaseModel->getSalesManQueryParams($whereSql, ' c.salesManId ');
  165. if(!isset($where) && !empty($whereSql)){
  166. $whereSql = ' where '. $whereSql;
  167. }
  168. $sql = 'select a.* from qianniao_customer_visits_log_'.$this->enterpriseId.' as a LEFT JOIN qianniao_goods_basic_'.$this->enterpriseId.' as b on a.goodsId = b.id LEFT JOIN qianniao_customer_'.$this->enterpriseId.' as c on a.customerId = c.id '.$whereSql.' order by createTime desc limit '.$offset.','.$limit;
  169. $dbResult = $this->objDCustomerVisitsLog->query($sql);
  170. if ($dbResult === false) {
  171. return ResultWrapper::fail($this->objDCustomerVisitsLog->error(), ErrorCode::$dberror);
  172. }
  173. if(empty($dbResult)){
  174. return ResultWrapper::success($returnData);
  175. }
  176. $sql = 'select count(*)as total from qianniao_customer_visits_log_'.$this->enterpriseId.' as a LEFT JOIN qianniao_goods_basic_'.$this->enterpriseId.' as b on a.goodsId = b.id LEFT JOIN qianniao_customer_'.$this->enterpriseId.' as c on a.customerId = c.id'.$whereSql;
  177. $dbTotalResult = $this->objDCustomerVisitsLog->query($sql);
  178. if ($dbTotalResult === false) {
  179. return ResultWrapper::fail($this->objDCustomerVisitsLog->error(), ErrorCode::$dberror);
  180. }
  181. $return = [
  182. 'data' => $this->format($dbResult),
  183. 'total' => $dbTotalResult[0]['total'],
  184. ];
  185. return ResultWrapper::success($return);
  186. }
  187. }