CustomController.php 20 KB

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