CustomController.php 19 KB

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