MCustomerTagLib.Class.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * 客户标签管理模块
  4. * Created by PhpStorm.
  5. * User: kyl
  6. * Date: 2021/03/05
  7. */
  8. namespace JinDouYun\Model\Customer;
  9. use JinDouYun\Dao\Customer\DCustomer;
  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\DCustomerTagLib;
  15. class MCustomerTagLib extends MBaseModel
  16. {
  17. private $objDCustomerTagLib;
  18. private $objDCustomer;
  19. private $enterpriseId;
  20. private $userCenterId;
  21. public function __construct($enterpriseId, $userCenterId)
  22. {
  23. $this->enterpriseId = $enterpriseId;
  24. $this->userCenterId = $userCenterId;
  25. parent::__construct($this->enterpriseId, $this->userCenterId);
  26. $this->objDCustomerTagLib = new DCustomerTagLib('default');
  27. $this->objDCustomer = new DCustomer('default');
  28. $this->objDCustomer->setTable('qianniao_customer_'.$enterpriseId);
  29. }
  30. /**
  31. * 添加客户标签
  32. * @param $params
  33. * @return ResultWrapper
  34. * @throws Exception
  35. */
  36. public function addCustomerTagLib($params)
  37. {
  38. $dbResult = $this->objDCustomerTagLib->get(['name' => $params['name']]);
  39. if($dbResult === false){
  40. return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
  41. }
  42. if(!empty($dbResult)){
  43. return ResultWrapper::fail('该标签组已存在', ErrorCode::$paramError);
  44. }
  45. unset($dbResult);
  46. $tags = $params['tags'];
  47. unset($params['tags']);
  48. $beginTransactionStatus = $this->objDCustomerTagLib->beginTransaction();
  49. $customerTagLibId = $this->objDCustomerTagLib->insert($params);
  50. if($customerTagLibId === false){
  51. return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
  52. }
  53. $tmpCustomerTags = [];
  54. foreach ($tags as $key => $value){
  55. $tmpCustomerTags[] = [
  56. 'enterpriseId' => $this->enterpriseId,
  57. 'name' => $value,
  58. 'pid' => $customerTagLibId,
  59. 'createTime' => time(),
  60. 'updateTime' => time(),
  61. ];
  62. }
  63. $dbResult = $this->objDCustomerTagLib->insert($tmpCustomerTags, true);
  64. if($dbResult === false){
  65. return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
  66. }
  67. if($beginTransactionStatus){
  68. $this->objDCustomerTagLib->commit();
  69. }
  70. return ResultWrapper::success($customerTagLibId);
  71. }
  72. /**
  73. * 获取指定客户标签
  74. */
  75. public function getCustomerTagLibInfo($customerTagLibId)
  76. {
  77. $where = 'id = '.$customerTagLibId .' or pid = '.$customerTagLibId.' and deleteStatus = '.StatusCode::$standard;
  78. $dbResult = $this->objDCustomerTagLib->select($where,'*');
  79. if($dbResult === false){
  80. return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
  81. }else{
  82. return ResultWrapper::success( arr2tree($dbResult) );
  83. }
  84. }
  85. /**
  86. * 编辑客户标签
  87. *
  88. * @param int|array $params 修改客户标签的数据
  89. *
  90. * @return ResultWrapper
  91. */
  92. public function editCustomerTagLib($params)
  93. {
  94. // 区分是修改标签组还是新增标签
  95. if( isset($params['update']) && !empty($params['update']) ){
  96. foreach ($params['update'] as $key=>$value){
  97. $id = $value['id'];
  98. unset($value['id']);
  99. $params['update'][$key]['updateTime'] = time();
  100. $dbResult = $this->objDCustomerTagLib->update($value, $id);
  101. }
  102. }
  103. if( isset($params['add']) && !empty($params['add'])){
  104. foreach ($params['add'] as $key => $value){
  105. $params['add'][$key]['enterpriseId'] = $this->enterpriseId;
  106. $params['add'][$key]['updateTime'] = time();
  107. }
  108. $dbResult = $this->objDCustomerTagLib->insert($params['add'], true);
  109. }
  110. if($dbResult === false){
  111. return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
  112. }else{
  113. return ResultWrapper::success($dbResult);
  114. }
  115. }
  116. /**
  117. * 删除客户标签
  118. *
  119. * @param array $params 要删除的客户标签
  120. *
  121. * @return ResultWrapper
  122. */
  123. public function delCustomerTagLib($params)
  124. {
  125. // 区分是删除标签组还是删除标签值
  126. if( isset($params['pid']) && !empty($params['pid']) ){
  127. $where = 'id = '.$params['id'] .' or pid = '.$params['id'].' and deleteStatus = '.StatusCode::$standard;
  128. }else{
  129. $where = 'id = '.$params['id'] .' and deleteStatus = '.StatusCode::$standard;
  130. }
  131. $dbResult = $this->objDCustomerTagLib->update( ['deleteStatus'=>StatusCode::$delete], $where );
  132. if($dbResult === false){
  133. return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
  134. }else{
  135. return ResultWrapper::success($dbResult);
  136. }
  137. }
  138. /**
  139. * 获取所有客户标签数据
  140. *
  141. * @param array $selectParams 过滤条件
  142. *
  143. * @return ResultWrapper
  144. */
  145. public function getAllCustomerTagLib()
  146. {
  147. $selectParams['deleteStatus'] = StatusCode::$standard;
  148. $dbResult = $this->objDCustomerTagLib->select('', '*', 'createTime desc');
  149. if($dbResult === false){
  150. return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
  151. }
  152. return ResultWrapper::success($dbResult?arr2tree($dbResult):[]);
  153. }
  154. /**
  155. * 编辑客户指定标签
  156. *
  157. * @param int|array $params 修改客户指定标签的数据
  158. *
  159. * @return ResultWrapper
  160. */
  161. public function updateCustomerTagLibById($updateCustomerLibData)
  162. {
  163. $customerid = $updateCustomerLibData['id'];
  164. unset($updateCustomerLibData['id']);
  165. $updateCustomerLibData['taglib'] = json_encode($updateCustomerLibData['taglib']);
  166. $dbResult = $this->objDCustomer->update($updateCustomerLibData,$customerid);
  167. if($dbResult === false){
  168. return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror);
  169. }else{
  170. return ResultWrapper::success($dbResult);
  171. }
  172. }
  173. }