MCustomerSource.Class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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\System;
  10. use Exception;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use Mall\Framework\Factory;
  15. use JinDouYun\Dao\System\DCustomerSource;
  16. use JinDouYun\Dao\System\DCustomerSourceDeploy;
  17. class MCustomerSource
  18. {
  19. private $objDCustomerSource;
  20. private $objDCustomerSourceDeploy;
  21. private $enterpriseId;
  22. public function __construct($enterpriseId)
  23. {
  24. $this->enterpriseId = $enterpriseId;
  25. $this->objDCustomerSource = new DCustomerSource('default');
  26. $this->objDCustomerSourceDeploy = new DCustomerSourceDeploy('default');
  27. }
  28. /**
  29. * 添加客户渠道(类型)
  30. * @param $params
  31. * @return ResultWrapper
  32. * @throws Exception
  33. */
  34. public function addCustomerSource($params)
  35. {
  36. //添加source名称表
  37. $sourceInsert = [
  38. 'name' => $params['name'],
  39. 'type' => 0,
  40. 'createTime' => time(),
  41. 'updateTime' => time(),
  42. 'modelType' => getArrayItem($params,'modelType',0),
  43. ];
  44. $dbResult = $this->objDCustomerSource->insert($sourceInsert);
  45. if($dbResult === false){
  46. return ResultWrapper::fail($this->objDCustomerSource->error(), ErrorCode::$dberror);
  47. }
  48. //如果新增数据为默认 把表里所有数据改为非默认
  49. if($params['defaultStatus'] == StatusCode::$standard){
  50. $endDbResult = $this->objDCustomerSourceDeploy->update(['defaultStatus' => StatusCode::$delete, 'updateTime' => time()], ['enterpriseId' => $this->enterpriseId]);
  51. if($endDbResult === false){
  52. return ResultWrapper::fail($this->objDCustomerSourceDeploy->error(), ErrorCode::$dberror);
  53. }
  54. }
  55. //添加deploy表
  56. $deployInsert = [
  57. 'sId' => $dbResult,
  58. 'deleteStatus' => StatusCode::$standard,
  59. 'defaultStatus' => $params['defaultStatus'],
  60. 'enableStatus' => $params['enableStatus'],
  61. 'createTime' => time(),
  62. 'updateTime' => time(),
  63. 'enterpriseId' => $this->enterpriseId,
  64. ];
  65. unset($dbResult);
  66. $dbResult = $this->objDCustomerSourceDeploy->insert($deployInsert);
  67. if($dbResult === false){
  68. return ResultWrapper::fail($this->objDCustomerSourceDeploy->error(), ErrorCode::$dberror);
  69. }
  70. return ResultWrapper::success($dbResult);
  71. }
  72. /**
  73. * 获取指定客户渠道(类型)信息,获取联系人信息
  74. * @param $CustomerSourceIds
  75. * @return ResultWrapper
  76. * @throws Exception
  77. */
  78. public function getCustomerSourceInfo($CustomerSourceIds)
  79. {
  80. //获取名称和配置信息
  81. $sql = 'select s.id,s.name,s.type,d.defaultStatus,d.enableStatus from '.$this->objDCustomerSource->get_Table().' s left join '.$this->objDCustomerSourceDeploy->get_Table().' d on s.id = d.sId where s.id = '.$CustomerSourceIds.' and deleteStatus = '.StatusCode::$standard;
  82. $dbResult = $this->objDCustomerSource->query($sql);
  83. if ($dbResult === false) {
  84. return ResultWrapper::fail($this->objDCustomerSource->error(), ErrorCode::$dberror);
  85. }
  86. $dbResult = array_shift($dbResult);
  87. return ResultWrapper::success($dbResult);
  88. }
  89. /**
  90. * 编辑客户渠道(类型)
  91. * 事务修改联系人信息
  92. * @param int|array $params 修改客户渠道(类型)的数据
  93. *
  94. * @return ResultWrapper
  95. * @throws Exception
  96. */
  97. public function editCustomerSource($params)
  98. {
  99. //传来的是source表的id
  100. //查询source表
  101. $dbResult = $this->objDCustomerSource->get(['id' => $params['id']]);
  102. if($dbResult === false){
  103. return ResultWrapper::fail($this->objDCustomerSourceDeploy->error(), ErrorCode::$dberror);
  104. }
  105. //type = 1 不可编辑
  106. if($dbResult['type'] == 1){
  107. return ResultWrapper::fail('系统默认不可编辑', ErrorCode::$dberror);
  108. }
  109. unset($dbResult);
  110. //查询配置表deploy里有没有配置
  111. $dbResult = $this->objDCustomerSourceDeploy->get(['sId' => $params['id']]);
  112. if($dbResult === false){
  113. return ResultWrapper::fail($this->objDCustomerSourceDeploy->error(), ErrorCode::$dberror);
  114. }
  115. //修改source表的名称
  116. $midDbResult = $this->objDCustomerSource->update(['modelType' => $params['modelType'],'name' => $params['name'], 'updateTime' => time()], ['id' => $params['id']]);
  117. if($midDbResult === false){
  118. return ResultWrapper::fail($this->objDCustomerSource->error(), ErrorCode::$dberror);
  119. }
  120. //如果新增数据为默认 把表里所有数据改为非默认
  121. if($params['defaultStatus'] == StatusCode::$standard){
  122. $endDbResult = $this->objDCustomerSourceDeploy->update(['defaultStatus' => StatusCode::$delete, 'updateTime' => time()], ['enterpriseId' => $this->enterpriseId]);
  123. if($endDbResult === false){
  124. return ResultWrapper::fail($this->objDCustomerSourceDeploy->error(), ErrorCode::$dberror);
  125. }
  126. }
  127. $data = [
  128. 'sId' => $params['id'],
  129. 'deleteStatus' => StatusCode::$standard,
  130. 'defaultStatus' => $params['defaultStatus'],
  131. 'enableStatus' => $params['enableStatus'],
  132. 'updateTime' => time(),
  133. 'enterpriseId' => $this->enterpriseId,
  134. ];
  135. if(empty($dbResult)){
  136. //没有配置
  137. //如果没有 新增一条
  138. $data['createTime'] = time();
  139. $data['enterpriseId'] = $this->enterpriseId;
  140. $finalDbResult = $this->objDCustomerSourceDeploy->insert($data);
  141. }else{
  142. //如果有 则编辑
  143. $finalDbResult = $this->objDCustomerSourceDeploy->update(['defaultStatus' => StatusCode::$standard, 'updateTime' => time()], ['id' => $dbResult['id']]);
  144. }
  145. if ($finalDbResult === false) {
  146. return ResultWrapper::fail($this->objDCustomerSourceDeploy->error(), ErrorCode::$dberror);
  147. }
  148. return ResultWrapper::success($dbResult);
  149. }
  150. /**
  151. * 删除客户渠道(类型)
  152. *
  153. * @param array $delCustomerSourceIds 要删除的客户渠道(类型)id
  154. * @return ResultWrapper
  155. * @throws Exception
  156. */
  157. public function delCustomerSource($delCustomerSourceIds)
  158. {
  159. //传来的是source表的id
  160. if (empty($delCustomerSourceIds)) {
  161. return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
  162. }
  163. $dbResult = $this->objDCustomerSourceDeploy->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()], ['sId' => $delCustomerSourceIds]);
  164. if ($dbResult === false) {
  165. return ResultWrapper::fail($this->objDCustomerSourceDeploy->error(), ErrorCode::$dberror);
  166. } else {
  167. return ResultWrapper::success($dbResult);
  168. }
  169. }
  170. /**
  171. * 客户渠道(类型)启用和禁用
  172. * @param array $params
  173. * @return ResultWrapper
  174. * @throws Exception
  175. */
  176. public function updateCustomerSourceStatus($params)
  177. {
  178. //传来的是source表的id
  179. //查询配置表deploy里有没有配置
  180. $dbResult = $this->objDCustomerSourceDeploy->get(['enterpriseId' => $this->enterpriseId, 'sId' => $params['id']]);
  181. if($dbResult === false){
  182. return ResultWrapper::fail($this->objDCustomerSource->error(), ErrorCode::$dberror);
  183. }
  184. if(empty($dbResult)){
  185. //没有配置
  186. //如果没有 新增一条 启用状态为传参
  187. $insert = [
  188. 'sId' => $params['id'],
  189. 'deleteStatus' => StatusCode::$standard,
  190. 'defaultStatus' => StatusCode::$delete,
  191. 'enableStatus' => $params['enableStatus'],
  192. 'createTime' => time(),
  193. 'updateTime' => time(),
  194. 'enterpriseId' => $this->enterpriseId,
  195. ];
  196. $finalDbResult = $this->objDCustomerSourceDeploy->insert($insert);
  197. }else{
  198. //把这条数据启用状态改为启用
  199. $finalDbResult = $this->objDCustomerSourceDeploy->update(['enableStatus' => $params['enableStatus'], 'updateTime' => time()], ['id' => $dbResult['id']]);
  200. }
  201. if ($finalDbResult === false) {
  202. return ResultWrapper::fail($this->objDCustomerSourceDeploy->error(), ErrorCode::$dberror);
  203. }
  204. return ResultWrapper::success($dbResult);
  205. }
  206. /**
  207. * 客户渠道(类型)默认
  208. * @param array $params
  209. * @return ResultWrapper
  210. * @throws Exception
  211. */
  212. public function updateDefaultStatus($params)
  213. {
  214. //传来的是source表的id
  215. //查询配置表deploy里有没有配置
  216. $dbResult = $this->objDCustomerSourceDeploy->get(['enterpriseId' => $this->enterpriseId, 'sId' => $params['id']]);
  217. if($dbResult === false){
  218. return ResultWrapper::fail($this->objDCustomerSourceDeploy->error(), ErrorCode::$dberror);
  219. }
  220. //把该企业其他配置改为非默认
  221. $midDbResult = $this->objDCustomerSourceDeploy->update(['defaultStatus' => StatusCode::$delete, 'updateTime' => time()], ['enterpriseId' => $this->enterpriseId]);
  222. if($midDbResult === false){
  223. return ResultWrapper::fail($this->objDCustomerSourceDeploy->error(), ErrorCode::$dberror);
  224. }
  225. if(empty($dbResult)){
  226. //没有配置
  227. //如果没有 新增一条 默认状态为默认
  228. $insert = [
  229. 'sId' => $params['id'],
  230. 'deleteStatus' => StatusCode::$standard,
  231. 'defaultStatus' => StatusCode::$standard,
  232. 'enableStatus' => StatusCode::$standard,
  233. 'createTime' => time(),
  234. 'updateTime' => time(),
  235. 'enterpriseId' => $this->enterpriseId,
  236. ];
  237. $finalDbResult = $this->objDCustomerSourceDeploy->insert($insert);
  238. }else{
  239. //如果有 把这条数据默认状态改为默认
  240. $finalDbResult = $this->objDCustomerSourceDeploy->update(['defaultStatus' => StatusCode::$standard, 'updateTime' => time()], ['id' => $dbResult['id']]);
  241. }
  242. if ($finalDbResult === false) {
  243. return ResultWrapper::fail($this->objDCustomerSourceDeploy->error(), ErrorCode::$dberror);
  244. }
  245. return ResultWrapper::success($dbResult);
  246. }
  247. /**
  248. * 根据ids获取分类名称
  249. * @param $customerSourceIds
  250. * @return ResultWrapper
  251. */
  252. public function getCustomerSourceNameByIds($customerSourceIds)
  253. {
  254. $dbResult = $this->objDCustomerSource->select(['id' => $customerSourceIds]);
  255. if($dbResult === false){
  256. return ResultWrapper::fail($this->objDCustomerSource->error(), ErrorCode::$dberror);
  257. }
  258. return ResultWrapper::success($dbResult);
  259. }
  260. /**
  261. * 获取客户类型(不分页)
  262. * @param array $selectParams
  263. * @return ResultWrapper
  264. */
  265. public function getCustomerSourceList($selectParams = [])
  266. {
  267. $selectParams['enterpriseId'] = $this->enterpriseId;
  268. $selectParams['deleteStatus'] = StatusCode::$standard;
  269. $selectParams['enableStatus'] = StatusCode::$standard;
  270. $sql = 'select s.id,s.`name`,s.type,if(d.enterpriseId='.$this->enterpriseId.',d.defaultStatus,'.StatusCode::$delete.') as defaultStatus,if(d.enterpriseId='.$this->enterpriseId.',d.enableStatus,'.StatusCode::$standard.') as enableStatus ,d.enterpriseId,s.isEvidence from '.$this->objDCustomerSource->get_Table().' s left join '.$this->objDCustomerSourceDeploy->get_Table().' d on d.sId = s.id where d.enterpriseId = '.$selectParams["enterpriseId"].' and d.deleteStatus = '.$selectParams['deleteStatus'].' and d.enableStatus = '.$selectParams['enableStatus'].' or (s.type = 1 and s.id not in(select sId from '.$this->objDCustomerSourceDeploy->get_Table().' where enterpriseId = '.$this->enterpriseId.' and deleteStatus = '.StatusCode::$standard.')) GROUP BY s.id order by s.createTime,s.id asc';
  271. $dbResult = $this->objDCustomerSource->query($sql);
  272. if ($dbResult === false) {
  273. return ResultWrapper::fail($this->objDCustomerSource->error(), ErrorCode::$dberror);
  274. }
  275. $returnData = [];
  276. foreach($dbResult as $value){
  277. $array = [
  278. 'id' => $value['id'],
  279. 'name' => $value['name'],
  280. 'type' => $value['type'],
  281. 'isEvidence' => $value['isEvidence'],
  282. 'defaultStatus' => empty($value['defaultStatus']) ? StatusCode::$delete : $value['defaultStatus'],
  283. 'enableStatus' => empty($value['enableStatus']) ? StatusCode::$standard : $value['enableStatus'],
  284. ];
  285. $returnData[] = $array;
  286. }
  287. return ResultWrapper::success($returnData);
  288. }
  289. /**
  290. * 获取企业下的所有客户渠道(类型)数据
  291. *
  292. * @param array $selectParams 过滤条件
  293. *
  294. * @return ResultWrapper
  295. * @throws Exception
  296. */
  297. public function getAllCustomerSource($selectParams = [])
  298. {
  299. $limit = $selectParams['limit'];
  300. unset($selectParams['limit']);
  301. $offset = $selectParams['offset'];
  302. unset($selectParams['offset']);
  303. $selectParams['deleteStatus'] = StatusCode::$standard;
  304. $selectParams['enterpriseId'] = $this->enterpriseId;
  305. $sql = 'select s.id,s.`name`,s.modelType,s.type,s.isEvidence,d.defaultStatus,d.enableStatus,d.enterpriseId from '.$this->objDCustomerSource->get_Table().' s left join '.$this->objDCustomerSourceDeploy->get_Table().' d on d.sId = s.id and d.enterpriseId = '.$selectParams["enterpriseId"].' where s.type = 1 or s.id in(select sId from '.$this->objDCustomerSourceDeploy->get_Table().' where enterpriseId = '.$selectParams['enterpriseId'].' and deleteStatus = '.$selectParams['deleteStatus'].') order by s.createTime,s.id asc limit '.$offset.','.$limit;
  306. $dbResult = $this->objDCustomerSource->query($sql);
  307. if ($dbResult === false) {
  308. return ResultWrapper::fail($this->objDCustomerSource->error(), ErrorCode::$dberror);
  309. }
  310. $returnData = [];
  311. foreach($dbResult as $value){
  312. $array = [
  313. 'id' => $value['id'],
  314. 'name' => $value['name'],
  315. 'type' => $value['type'],
  316. 'modelType' => $value['modelType'],
  317. 'defaultStatus' => empty($value['defaultStatus']) ? StatusCode::$delete : $value['defaultStatus'],
  318. 'enableStatus' => empty($value['enableStatus']) ? StatusCode::$standard : $value['enableStatus'],
  319. ];
  320. $returnData[] = $array;
  321. }
  322. unset($dbResult);
  323. $countSql = 'select count(*) as count from '.$this->objDCustomerSource->get_Table().' where type = 1 or id in(select sId from '.$this->objDCustomerSourceDeploy->get_Table().' where enterpriseId = '.$selectParams['enterpriseId'].' and deleteStatus = '.$selectParams['deleteStatus'].')';
  324. $dbResult = $this->objDCustomerSource->query($countSql);
  325. if ($dbResult === false) {
  326. return ResultWrapper::fail($this->objDCustomerSource->error(), ErrorCode::$dberror);
  327. }
  328. $countArray = array_shift($dbResult);
  329. $total = $countArray['count'];
  330. unset($dbResult);
  331. $return = [
  332. 'data' => $returnData,
  333. 'total' => ($total) ? intval($total) : 0,
  334. ];
  335. return ResultWrapper::success($return);
  336. }
  337. /**
  338. * @param $selectParams
  339. * @return ResultWrapper
  340. */
  341. public function getCustomer($selectParams)
  342. {
  343. $dbResult=$this->objDCustomerSource->select($selectParams,'name');
  344. if ($dbResult===false){
  345. return ResultWrapper::fail($this->objDCustomerSource->error(), ErrorCode::$dberror);
  346. }
  347. return ResultWrapper::success($dbResult);
  348. }
  349. }