CustomController.php 20 KB

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