CustomController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\admin\classes\AdminRoleClass;
  4. use biz\ghs\classes\GhsClass;
  5. use biz\shop\classes\ShopAdminClass;
  6. use biz\sj\classes\SjClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\custom\services\CustomService;
  9. use bizGhs\merchant\classes\WlClass;
  10. use bizGhs\order\classes\OrderClass;
  11. use bizHd\saas\classes\ApplyClass;
  12. use bizHd\saas\services\ApplyService;
  13. use bizHd\shop\classes\ShopClass;
  14. use bizHd\stat\classes\StatVisitClass;
  15. use common\components\dict;
  16. use common\components\imgUtil;
  17. use common\components\stringUtil;
  18. use common\components\util;
  19. use Yii;
  20. class CustomController extends BaseController
  21. {
  22. //修改散客身份 ssh 20230216
  23. public function actionChangePerson()
  24. {
  25. $get = Yii::$app->request->get();
  26. $id = $get['id'] ?? 0;
  27. $person = $get['person'] ?? 0;
  28. $custom = CustomClass::getById($id, true);
  29. if (empty($custom) || $custom->ownMainId != $this->mainId) {
  30. util::fail('修改失败');
  31. }
  32. $ghsId = $custom->ghsId ?? 0;
  33. $ghs = GhsClass::getById($ghsId, true);
  34. if (empty($ghs)) {
  35. util::fail('修改失败');
  36. }
  37. $ghs->person = $person;
  38. $ghs->save();
  39. $custom->person = $person;
  40. $custom->save();
  41. util::complete('修改成功');
  42. }
  43. //修改物流名称 ssh 20230112
  44. public function actionChangeWlName()
  45. {
  46. $get = Yii::$app->request->get();
  47. $id = $get['id'] ?? 0;
  48. $wlName = $get['wlName'] ?? 0;
  49. $custom = CustomClass::getById($id, true);
  50. if (empty($custom) || $custom->ownMainId != $this->mainId) {
  51. util::fail('修改失败');
  52. }
  53. $ghsId = $custom->ghsId ?? 0;
  54. $ghs = GhsClass::getById($ghsId, true);
  55. if (empty($ghs)) {
  56. util::fail('修改失败');
  57. }
  58. $ghs->customWlName = $wlName;
  59. $ghs->save();
  60. $custom->wlName = $wlName;
  61. $custom->save();
  62. util::complete('已修改');
  63. }
  64. //修改物流信息 ssh 20230105
  65. public function actionChangeWl()
  66. {
  67. util::fail('修改失败');
  68. }
  69. //修改名称 ssh 2022096
  70. public function actionChangeName()
  71. {
  72. $get = Yii::$app->request->get();
  73. $name = $get['name'] ?? '';
  74. $id = $get['id'] ?? 0;
  75. if (empty($name)) {
  76. util::fail('请填写名称');
  77. }
  78. $py = stringUtil::py($name);
  79. $custom = CustomClass::getById($id, true);
  80. CustomClass::valid($custom, $this->shopId);
  81. $custom->name = $name;
  82. $custom->py = $py;
  83. $custom->save();
  84. OrderClass::updateByCondition(['customId' => $id], ['customName' => $name, 'customNamePy' => $py]);
  85. $customShopId = $custom->shopId ?? 0;
  86. //在首店修改客户名称,直营店自动同步
  87. $masterShop = $this->shop;
  88. if (isset($masterShop->default) && $masterShop->default == 1) {
  89. $sjId = $masterShop->sjId ?? 0;
  90. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  91. foreach ($shopList as $shop) {
  92. $shopId = $shop->id ?? 0;
  93. if (isset($shop->join) && $shop->join == 1) {
  94. //加盟店不同步修改
  95. continue;
  96. }
  97. if ($shopId == $masterShop->id) {
  98. //前面已经同步修改过了
  99. continue;
  100. }
  101. $custom = CustomClass::getByCondition(['ownShopId' => $shopId, 'shopId' => $customShopId], true);
  102. if (empty($custom)) {
  103. continue;
  104. }
  105. $custom->name = $name;
  106. $custom->py = $py;
  107. $custom->save();
  108. }
  109. }
  110. util::complete('修改成功');
  111. }
  112. //帮客户添加员工 ssh 20220613
  113. public function actionAddStaff()
  114. {
  115. $post = Yii::$app->request->post();
  116. $customId = $post['id'] ?? 0;
  117. $mobile = $post['mobile'] ?? 0;
  118. if (stringUtil::isMobile($mobile) == false) {
  119. util::fail('手机号填写错误');
  120. }
  121. $info = CustomService::getById($customId, true);
  122. CustomClass::valid($info, $this->shopId);
  123. $connection = Yii::$app->db;
  124. $transaction = $connection->beginTransaction();
  125. try {
  126. $shopId = $info->shopId ?? 0;
  127. $customShop = ShopClass::getById($shopId, true);
  128. if (empty($customShop)) {
  129. util::fail('没有找到客户的门店');
  130. }
  131. $mainId = $customShop->mainId ?? 0;
  132. $ptStyle = dict::getDict('ptStyle', 'hd');
  133. $adminInfo = ['name' => $mobile, 'mobile' => $mobile, 'style' => $ptStyle, 'currentShopId' => $shopId];
  134. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  135. $admin = \bizGhs\admin\classes\AdminClass::replaceAdmin($adminInfo, $fromApp);
  136. $roleList = AdminRoleClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  137. if (empty($roleList)) {
  138. util::fail('客户门店没有员工角色');
  139. }
  140. $currentRoleId = 0;
  141. foreach ($roleList as $role) {
  142. $roleId = $role->id ?? 0;
  143. $currentRoleId = $roleId;
  144. if ($role->roleName == '员工') {
  145. break;
  146. }
  147. }
  148. $params = [
  149. 'shopId' => $shopId,
  150. 'mobile' => $mobile,
  151. 'roleId' => $currentRoleId,
  152. ];
  153. \bizHd\staff\classes\StaffClass::appGenerateStaff($params, $admin);
  154. $transaction->commit();
  155. util::complete('添加成功');
  156. } catch (\Exception $e) {
  157. $transaction->rollBack();
  158. util::fail('添加失败');
  159. }
  160. }
  161. //添加新花店 ssh 2021.1.8
  162. public function actionAdd()
  163. {
  164. $post = Yii::$app->request->post();
  165. $post['style'] = SjClass::STYLE_RETAIL;
  166. $post['introSjId'] = $this->sjId;
  167. $post['introStyle'] = SjClass::STYLE_SUPPLIER;
  168. $mobile = $post['mobile'] ?? '';
  169. $confirmMobile = $post['confirmMobile'];
  170. $address = $post['address'] ?? '';
  171. util::fail('功能已停用');
  172. if (stringUtil::isMobile($mobile) == false) {
  173. util::fail(' 请填写正确的手机号');
  174. }
  175. if ($mobile != $confirmMobile) {
  176. util::fail('二次号码填写不一致');
  177. }
  178. $name = $post['name'] ?? '';
  179. if (empty($name)) {
  180. util::fail('名称不能为空');
  181. }
  182. if (stringUtil::getWordNum($name) > 8) {
  183. util::fail('名称不能超过8个汉字');
  184. }
  185. $has = SjClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_RETAIL]);
  186. if (!empty($has)) {
  187. util::fail('名称已经存在');
  188. }
  189. $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_RETAIL]);
  190. if (!empty($has)) {
  191. util::fail('手机号已经添加过');
  192. }
  193. if (empty($address)) {
  194. util::fail('请填写地址');
  195. }
  196. $connection = Yii::$app->db;
  197. $transaction = $connection->beginTransaction();
  198. try {
  199. $shop = $this->shop;
  200. $sjId = $this->sjId;
  201. $shopId = $this->shopId;
  202. $sjName = $this->sj->name ?? '';
  203. $city = $shop->city ?? '';
  204. $dist = $shop->dist ?? '';
  205. $applyData = [
  206. 'name' => $name,
  207. 'licenseNo' => $mobile,
  208. 'certType' => ApplyClass::CERT_TYPE_MOBILE,
  209. 'mobile' => $mobile,
  210. 'introSjId' => $sjId,
  211. 'introSjName' => $sjName,
  212. 'introShopId' => $shopId,
  213. 'introStyle' => SjClass::STYLE_SUPPLIER,
  214. 'from' => ApplyClass::FROM_GHS,
  215. 'style' => SjClass::STYLE_RETAIL,
  216. 'province' => $shop->province ?? '',
  217. 'city' => $city,
  218. 'dist' => $dist,
  219. 'address' => $address,
  220. 'status' => ApplyClass::STATUS_PASS,
  221. 'replace' => 1,//1表示供货商添加的客户
  222. ];
  223. $apply = ApplyService::replaceRetail($applyData);
  224. $id = $apply['id'] ?? 0;
  225. $respond = ApplyService::pass($id);
  226. $shop = $respond['shop'] ?? [];
  227. $sj = $respond['sj'] ?? [];
  228. $hdSjId = $sj['id'] ?? 0;
  229. $hdShopId = $shop->id ?? 0;
  230. ApplyClass::updateById($id, ['sjId' => $hdSjId, 'shopId' => $hdShopId]);
  231. if (empty($hdShopId)) {
  232. util::fail('客户没有添加成功!');
  233. }
  234. $custom = CustomClass::build($this->shopId, $hdShopId);
  235. $custom['avatar'] = imgUtil::groupImg($custom['avatar']);
  236. //客户欠款额度设置
  237. $debt = isset($post['debt']) && $post['debt'] == CustomClass::IS_DEBT_YES ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
  238. $customId = $custom['id'] ?? 0;
  239. $debtLimit = isset($post['debtLimit']) && is_numeric($post['debtLimit']) ? $post['debtLimit'] : 5000;
  240. CustomClass::updateById($customId, ['debtLimit' => $debtLimit, 'debt' => $debt]);
  241. $transaction->commit();
  242. util::success($custom);
  243. } catch (\Exception $exception) {
  244. $transaction->rollBack();
  245. Yii::info("添加客户没有成功:" . $exception->getMessage());
  246. util::fail('添加客户没有成功');
  247. }
  248. }
  249. //向队列写入新花店数据 shizhongqi 2022.6.8
  250. public function actionCreateCustomers()
  251. {
  252. $post = Yii::$app->request->post();
  253. $arr = [];
  254. for ($i = 1; $i <= 5; $i++) {
  255. $name = $post['name' . $i] ?? '';
  256. $mobile = $post['mobile' . $i] ?? '';
  257. $confirmMobile = $post['confirmMobile' . $i] ?? '';
  258. if (empty($name) && empty($mobile) && empty($confirmMobile)) {
  259. continue;
  260. }
  261. if (empty($name)) {
  262. util::fail('花店名称不能为空');
  263. }
  264. $has = SjClass::getByCondition(['name' => $name]);
  265. if (!empty($has)) {
  266. //util::fail($name . ' 这个名称已经被别人使用了');
  267. }
  268. if (stringUtil::getWordNum($name) > 8) {
  269. util::fail('名称不能超过8个汉字');
  270. }
  271. if (empty($mobile)) {
  272. util::fail($name . " 手机号不能为空");
  273. }
  274. if (stringUtil::isMobile($mobile) == false) {
  275. util::fail($name . " 手机号格式不正确");
  276. }
  277. if ($mobile != $confirmMobile) {
  278. util::fail($name . " 二次手机号不一致");
  279. }
  280. $hasShop = ShopClass::getByCondition(['mobile' => $mobile], true);
  281. if (!empty($hasShop)) {
  282. $shop = ShopClass::getByCondition(['mobile' => $mobile, 'ptStyle' => 1], true);
  283. if (!empty($shop)) {
  284. $ghsShopId = $this->shopId;
  285. $hdShopId = $shop->id;
  286. CustomClass::build($ghsShopId, $hdShopId);
  287. util::complete($shop->merchantName . ' 添加成功');
  288. }
  289. util::fail($mobile . "手机号已经添加过了");
  290. }
  291. $has = ApplyClass::getByCondition(['mobile' => $mobile]);
  292. if (!empty($has)) {
  293. util::fail($mobile . "手机号已经申请添加过了");
  294. }
  295. $arr[] = ['name' => $name, 'mobile' => $mobile];
  296. }
  297. if (empty($arr)) {
  298. util::fail('请填写客户');
  299. }
  300. foreach ($arr as $item) {
  301. $name = $item['name'] ?? '';
  302. $mobile = $item['mobile'] ?? '';
  303. //队列方式去处理
  304. $customerCachedKey = 'batch_create_customer_list';
  305. $value = $name . '_' . $mobile . '_' . $this->sjId . '_' . $this->shop->id . '_' . $this->sj->name . '_' . $this->shopAdminId;
  306. Yii::$app->redis->executeCommand('LPUSH', [$customerCachedKey, $value]);
  307. Yii::info("供货商添加客户:name - " . $name . ' == mobile - ' . $mobile);
  308. Yii::info("shopAdminId: " . $this->shopAdminId);
  309. }
  310. util::complete('已添加,1分钟后生效');
  311. }
  312. //从其它店同步客户 shizhongqi 2022.6.9
  313. public function actionCopyCustomers()
  314. {
  315. $get = Yii::$app->request->get();
  316. $fromShopId = $get['fromShopId'] ?? 0;
  317. $fromShop = ShopClass::getById($fromShopId, true);
  318. if (empty($fromShop)) {
  319. util::fail('门店不存在');
  320. }
  321. $toShopId = $this->shopId;
  322. if ($fromShopId == $toShopId) {
  323. util::fail('本店无需同步');
  324. }
  325. $fromShopName = $fromShop->shopName ?? '';
  326. if ($fromShop->syncCustom == 0) {
  327. util::fail('不允许从' . $fromShopName . '同步客户');
  328. }
  329. $toShop = $this->shop;
  330. $toShopName = $toShop->shopName ?? '';
  331. if ($toShop->syncCustom == 0) {
  332. util::fail($toShopName . '已关闭同步功能');
  333. }
  334. $toSjId = $toShop->sjId ?? 0;
  335. $fromSjId = $fromShop->sjId ?? 0;
  336. if ($toSjId != $fromSjId) {
  337. util::fail('不是您的门店');
  338. }
  339. $cachedKey = 'copy_other_shop_customers';
  340. Yii::$app->redis->executeCommand('LPUSH', [$cachedKey, $toShopId . '_' . $fromShopId]);
  341. Yii::info("从其他门店同步客户:目标门店id = " . $toShopId . ' 来源门店id = ' . $fromShopId);
  342. util::complete('已提交,1分钟后生效');
  343. }
  344. //客户列表 ssh 2021.7.8
  345. public function actionList()
  346. {
  347. $get = Yii::$app->request->get();
  348. $type = isset($get['type']) ? $get['type'] : 0;
  349. $name = isset($get['name']) ? $get['name'] : '';
  350. $where = ['ownMainId' => $this->mainId];
  351. $sort = 'visitTime DESC';
  352. if ($type == 1) {
  353. //消费排行
  354. $sort = 'buyAmount DESC';
  355. } else if ($type == 2) {
  356. //欠款客户
  357. $where['isDebt'] = 1;
  358. $sort = 'debtAmount DESC';
  359. }
  360. $name = trim($name);
  361. if (!empty($name)) {
  362. if (is_numeric($name) && strlen($name) > 4) {
  363. if (stringUtil::isMobile($name)) {
  364. $where['mobile'] = $name;
  365. } else {
  366. $where['mobile'] = ['like', $name];
  367. }
  368. } else if (stringUtil::isLetter($name)) {
  369. $where['py'] = ['like', $name];
  370. } else {
  371. $where['name'] = ['like', $name];
  372. }
  373. }
  374. $list = CustomService::getCustomSortList($where, $sort);
  375. $main = $this->main;
  376. $list['totalUser'] = $main->totalUser ?? 0;
  377. $list['mayGatheringNum'] = $main->mayGatheringNum ?? 0;
  378. util::success($list);
  379. }
  380. //今日访客 ssh 20211022
  381. public function actionGetVisit()
  382. {
  383. $ids = StatVisitClass::getVisitCustomIds($this->shop);
  384. if (empty($ids)) {
  385. util::success(['list' => []]);
  386. }
  387. $sort = 'visitTime DESC';
  388. $where = ['id' => ['in', $ids]];
  389. $respond = CustomService::getCustomSortList($where, $sort);
  390. util::success($respond);
  391. }
  392. //客户详情 ssh 2021.1.8
  393. public function actionDetail()
  394. {
  395. $get = Yii::$app->request->get();
  396. $customId = $get['id'] ?? 0;
  397. $info = CustomService::getCustomInfo($customId);
  398. CustomClass::valid($info, $this->shopId);
  399. //手动添加的客户经纬度不完善不显示地址,引导完善客户地址
  400. if (isset($info['lat']) && empty($info['lat'])) {
  401. $info['address'] = '';
  402. }
  403. if (isset($info['long']) && empty($info['long'])) {
  404. $info['address'] = '';
  405. }
  406. $mainId = $info['mainId'] ?? 0;
  407. //超管的信息
  408. $superInfo = ShopAdminClass::getManager($mainId);
  409. $superName = $superInfo['name'] ?? '';
  410. $currentSuper = ['name' => $superName];
  411. $info['superInfo'] = $currentSuper;
  412. util::success($info);
  413. }
  414. //修改是否显示库存 ssh 20221022
  415. public function actionChangeShowStock()
  416. {
  417. $get = Yii::$app->request->get();
  418. $showStock = isset($get['showStock']) ? $get['showStock'] : 0;
  419. $customId = isset($get['customId']) ? $get['customId'] : 0;
  420. $custom = CustomClass::getById($customId, true);
  421. CustomClass::valid($custom, $this->shopId);
  422. $ghsId = $custom->ghsId ?? 0;
  423. $ghs = GhsClass::getById($ghsId, true);
  424. if (empty($ghs)) {
  425. util::fail('没有找到供货商');
  426. }
  427. $ghs->showStock = $showStock;
  428. $ghs->save();
  429. $custom->showStock = $showStock;
  430. $custom->save();
  431. util::complete();
  432. }
  433. //允许月结开关 ssh 2021.1.8
  434. public function actionDebt()
  435. {
  436. $get = Yii::$app->request->get();
  437. $debt = isset($get['debt']) ? $get['debt'] : 0;
  438. $customId = isset($get['customId']) ? $get['customId'] : 0;
  439. $custom = CustomClass::getById($customId);
  440. CustomClass::valid($custom, $this->shopId);
  441. CustomClass::debt($custom, $debt);
  442. util::complete();
  443. }
  444. //采购查看权限的黑白名单开关 shizhongqi 2021.08.08
  445. public function actionSetBlack()
  446. {
  447. $black = Yii::$app->request->get('black', 2);
  448. $customId = Yii::$app->request->get('customId', 0);
  449. $custom = CustomClass::getById($customId, true);
  450. CustomClass::valid($custom, $this->shopId);
  451. $custom->black = $black;
  452. $custom->save();
  453. $ghsId = $custom->ghsId ?? 0;
  454. $ghs = GhsClass::getById($ghsId, true);
  455. if (empty($ghs)) {
  456. util::fail('出错了');
  457. }
  458. $ghs->black = $black;
  459. $ghs->save();
  460. util::complete();
  461. }
  462. //欠款客户 ssh 2021.2.4
  463. public function actionDebtList()
  464. {
  465. $get = Yii::$app->request->get();
  466. $where = ['ownShopId' => $this->shopId, 'isDebt' => 1];
  467. if (isset($get['name']) && !empty($get['name'])) {
  468. $where['name'] = ['like', $get['name']];
  469. }
  470. $list = CustomService::getDebtList($where);
  471. //共X个客户,合计欠款XXX元
  472. $main = $this->main;
  473. $list['customNum'] = $main->mayGatheringNum ?? 0;
  474. $list['debtAmount'] = $main->mayGathering ?? 0.00;
  475. util::success($list);
  476. }
  477. //修改欠款额度 ssh 20210625
  478. public function actionUpdateDebtLimit()
  479. {
  480. $shopAdmin = $this->shopAdmin;
  481. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  482. util::fail('管理员才能操作');
  483. }
  484. $get = Yii::$app->request->get();
  485. $id = $get['id'] ?? 0;
  486. $debtLimit = $get['debtLimit'] ?? 0;
  487. if (is_numeric($debtLimit) == false || $debtLimit <= 0) {
  488. util::fail('请填写金额');
  489. }
  490. $custom = CustomClass::getById($id, true);
  491. CustomClass::valid($custom, $this->shopId);
  492. $custom->debtLimit = $debtLimit;
  493. $custom->save();
  494. util::complete();
  495. }
  496. //修改折扣 ssh 20210913
  497. public function actionChangeDiscount()
  498. {
  499. $shopAdmin = $this->shopAdmin;
  500. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  501. util::fail('管理员才能操作');
  502. }
  503. $get = Yii::$app->request->get();
  504. $id = $get['id'] ?? 0;
  505. $discount = $get['discount'] ?? 0;
  506. if (is_numeric($discount) == false || $discount > 1 || $discount == 0) {
  507. util::fail('请填写小数值');
  508. }
  509. $custom = CustomClass::getById($id, true);
  510. CustomClass::valid($custom, $this->shopId);
  511. $custom->discount = $discount;
  512. $custom->save();
  513. $ghsId = $custom->ghsId ?? 0;
  514. $ghs = GhsClass::getById($ghsId, true);
  515. $ghs->giveDiscount = $discount;
  516. $ghs->save();
  517. util::complete();
  518. }
  519. //修改客户等级 ssh 20211007
  520. public function actionChangeLevel()
  521. {
  522. $shopAdmin = $this->shopAdmin;
  523. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  524. util::fail('超管才能修改');
  525. }
  526. $get = Yii::$app->request->get();
  527. $customId = $get['customId'] ?? 0;
  528. $level = $get['level'] ?? 1;
  529. $custom = CustomClass::getById($customId, true);
  530. CustomClass::valid($custom, $this->shopId);
  531. $custom->level = $level;
  532. $custom->save();
  533. $ghsId = $custom->ghsId ?? 0;
  534. $ghs = GhsClass::getById($ghsId, true);
  535. if (empty($ghs)) {
  536. util::fail('出错了');
  537. }
  538. $ghs->giveLevel = $level;
  539. $ghs->save();
  540. $map = CustomClass::$levelMap;
  541. $name = $map[$level] ?? '';
  542. util::success(['levelName' => $name]);
  543. }
  544. }