CustomController.php 21 KB

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