MCustomerContact.Class.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * 客户联系人管理模块
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/10/30
  7. * Time: 14:02
  8. */
  9. namespace JinDouYun\Model\Customer;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\StatusCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use JinDouYun\Cache\CustomerCache;
  14. use JinDouYun\Model\MBaseModel;
  15. use JinDouYun\Model\SysAreaChina\MSysAreaChina;
  16. use JinDouYun\Model\Customer\MCustomer;
  17. use JinDouYun\Dao\Customer\DCustomerContact;
  18. class MCustomerContact extends MBaseModel {
  19. private $objDCustomerContact;
  20. private $enterpriseId;
  21. private $userCenterId;
  22. private $cutTable = 1;//客户联系人按照企业id分表
  23. public function __construct($enterpriseId, $userCenterId)
  24. {
  25. $this->enterpriseId = $enterpriseId;
  26. $this->userCenterId = $userCenterId;
  27. parent::__construct($this->enterpriseId, $this->userCenterId);
  28. $this->objDCustomerContact = new DCustomerContact('default');
  29. $tableName = $this->objDCustomerContact->getTableName('qianniao_customer_contact_', $enterpriseId, $this->cutTable);
  30. $this->objDCustomerContact->setTable($tableName);
  31. }
  32. /**
  33. * 添加客户联系人
  34. * @param $params
  35. * @return ResultWrapper
  36. * @throws Exception
  37. */
  38. public function addCustomerContact($params)
  39. {
  40. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  41. $customerUserCenterId = $objMCustomer->getUserCenterIdByCustomerId($params['customerId']);
  42. if(empty($customerUserCenterId)){
  43. return ResultWrapper::fail('当前客户不存在', ErrorCode::$contentNotExists);
  44. }
  45. $dbResult = $this->objDCustomerContact->get(['customerId' => $params['customerId'], 'mobile' => $params['mobile']]);
  46. if($dbResult === false){
  47. return ResultWrapper::fail($this->objDCustomerContact->error(), ErrorCode::$dberror);
  48. }
  49. if(!empty($dbResult)){
  50. return ResultWrapper::fail('手机号已存在', ErrorCode::$paramError);
  51. }
  52. unset($dbResult);
  53. $CustomerContactId = $this->objDCustomerContact->insert($params);
  54. if($CustomerContactId === false){
  55. return ResultWrapper::fail($this->objDCustomerContact->error(), ErrorCode::$dberror);
  56. }
  57. //$objMCustomer->updateEsData($params['customerId']);
  58. $objCustomerContactCache = new CustomerCache();
  59. $objCustomerContactCache->delCustomerData($this->enterpriseId, $params['customerId']);
  60. $objCustomerContactCache->delCustomerUserData($this->enterpriseId, $customerUserCenterId);
  61. return ResultWrapper::success($CustomerContactId);
  62. }
  63. /**
  64. * 获取指定客户联系人信息
  65. */
  66. public function getCustomerContactInfo($CustomerContactIds)
  67. {
  68. $dbResult = $this->objDCustomerContact->select($CustomerContactIds);
  69. if($dbResult === false){
  70. return ResultWrapper::fail($this->objDCustomerContact->error(), ErrorCode::$dberror);
  71. }else{
  72. return ResultWrapper::success($dbResult);
  73. }
  74. }
  75. /**
  76. * 编辑客户联系人
  77. *
  78. * @param int|array $params 修改客户联系人的数据
  79. *
  80. * @return ResultWrapper
  81. */
  82. public function editCustomerContact($params)
  83. {
  84. if( empty($params['id']) ){
  85. return ResultWrapper::fail('没有指定要修改的客户联系人id', ErrorCode::$paramError);
  86. }
  87. $updateCustomerContactId = $params['id'];
  88. unset($params['id']);
  89. if(isset($params['area'])) unset($params['area']);
  90. $dbResult = $this->objDCustomerContact->update($params, $updateCustomerContactId);
  91. if($dbResult === false){
  92. return ResultWrapper::fail($this->objDCustomerContact->error(), ErrorCode::$dberror);
  93. }else{
  94. return ResultWrapper::success($dbResult);
  95. }
  96. }
  97. /**
  98. * 删除客户联系人
  99. *
  100. * @param array $delCustomerContactIds 要删除的客户联系人id
  101. *
  102. * @return ResultWrapper
  103. */
  104. public function delCustomerContact($delCustomerContactIds)
  105. {
  106. if (empty($delCustomerContactIds)) {
  107. return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
  108. }
  109. $dbResult = $this->objDCustomerContact->update( ['deleteStatus'=>StatusCode::$delete], $delCustomerContactIds );
  110. if($dbResult === false){
  111. return ResultWrapper::fail($this->objDCustomerContact->error(), ErrorCode::$dberror);
  112. }else{
  113. return ResultWrapper::success($dbResult);
  114. }
  115. }
  116. /**
  117. * 客户联系人启用和禁用
  118. * @param array $params
  119. * @return ResultWrapper
  120. */
  121. public function updateCustomerContactStatus($params)
  122. {
  123. $dbResult = $this->objDCustomerContact->update( ['status'=>$params['status']], $params['CustomerContactId'] );
  124. if($dbResult === false){
  125. return ResultWrapper::fail($this->objDCustomerContact->error(), ErrorCode::$dberror);
  126. }else{
  127. return ResultWrapper::success($dbResult);
  128. }
  129. }
  130. /**
  131. * 获取所有客户联系人数据
  132. *
  133. * @param array $selectParams 过滤条件
  134. *
  135. * @return ResultWrapper
  136. */
  137. public function getAllCustomerContact($selectParams)
  138. {
  139. $limit = $selectParams['limit'];
  140. unset($selectParams['limit']);
  141. $offset = $selectParams['offset'];
  142. unset($selectParams['offset']);
  143. $selectParams['deleteStatus'] = StatusCode::$standard;
  144. $dbResult = $this->objDCustomerContact->select($selectParams, '*', 'createTime desc', $limit, $offset);
  145. if($dbResult === false){
  146. return ResultWrapper::fail($this->objDCustomerContact->error(), ErrorCode::$dberror);
  147. }
  148. $total = $this->objDCustomerContact->count($selectParams);
  149. $return = [
  150. 'data' => $dbResult,
  151. 'total' => ($total)?intval($total):0,
  152. ];
  153. return ResultWrapper::success($return);
  154. }
  155. /**
  156. * 获取客户id对应的联系人信息
  157. * @param $customerIds mixed 客户id
  158. * @return array|bool
  159. * @throws Exception
  160. */
  161. public function getContactInfoById($customerIds) {
  162. if(is_array($customerIds)) {
  163. $where = "customerId in (". implode(',',$customerIds) .")";
  164. }else {
  165. $where = "customerId = $customerIds";
  166. }
  167. $where .= ' AND deleteStatus = ' . StatusCode::$standard;
  168. $dbResult = $this->objDCustomerContact->select($where, '*');
  169. if($dbResult === false) {
  170. return [];
  171. }
  172. $objMSysAreaChina = new MSysAreaChina();
  173. foreach ($dbResult as &$contact) {
  174. $areaName = $objMSysAreaChina->getNameByCode([
  175. $contact['provinceCode'],
  176. $contact['cityCode'],
  177. $contact['districtCode']
  178. ]);
  179. $contact['area']['provinceName'] = isset($areaName[$contact['provinceCode']]) ? $areaName[$contact['provinceCode']] : '';
  180. $contact['area']['cityName'] = isset($areaName[$contact['cityCode']]) ? $areaName[$contact['cityCode']] : '';
  181. $contact['area']['districtName'] = isset($areaName[$contact['districtCode']]) ? $areaName[$contact['districtCode']] : '';
  182. $contact['area']['address'] = $contact['address'];
  183. }
  184. return $dbResult;
  185. }
  186. }