CustomController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. $toShop = $this->shop;
  278. $toSjId = $toShop->sjId ?? 0;
  279. $fromSjId = $fromShop->sjId ?? 0;
  280. if ($toSjId != $fromSjId) {
  281. util::fail('不是您的门店');
  282. }
  283. $cachedKey = 'copy_other_shop_customers';
  284. Yii::$app->redis->executeCommand('LPUSH', [$cachedKey, $toShopId . '_' . $fromShopId]);
  285. Yii::info("从其他门店同步客户:目标门店id = " . $toShopId . ' 来源门店id = ' . $fromShopId);
  286. util::complete('已提交,1分钟后生效');
  287. }
  288. //客户列表 ssh 2021.7.8
  289. public function actionList()
  290. {
  291. $get = Yii::$app->request->get();
  292. $type = isset($get['type']) ? $get['type'] : 0;
  293. $name = isset($get['name']) ? $get['name'] : '';
  294. $where = ['ownMainId' => $this->mainId];
  295. $sort = 'visitTime DESC';
  296. if ($type == 1) {
  297. //消费排行
  298. $sort = 'buyAmount DESC';
  299. } else if ($type == 2) {
  300. //欠款客户
  301. $where['isDebt'] = 1;
  302. $sort = 'debtAmount DESC';
  303. }
  304. if (!empty($name)) {
  305. if (stringUtil::isLetter($name)) {
  306. $where['py'] = ['like', $name];
  307. } else {
  308. $where['name'] = ['like', $name];
  309. }
  310. }
  311. $list = CustomService::getCustomSortList($where, $sort);
  312. $main = $this->main;
  313. $list['totalUser'] = $main->totalUser ?? 0;
  314. $list['mayGatheringNum'] = $main->mayGatheringNum ?? 0;
  315. util::success($list);
  316. }
  317. //今日访客 ssh 20211022
  318. public function actionGetVisit()
  319. {
  320. $ids = StatVisitClass::getVisitCustomIds($this->shop);
  321. if (empty($ids)) {
  322. util::success(['list' => []]);
  323. }
  324. $sort = 'visitTime DESC';
  325. $where = ['id' => ['in', $ids]];
  326. $respond = CustomService::getCustomSortList($where, $sort);
  327. util::success($respond);
  328. }
  329. //客户详情 ssh 2021.1.8
  330. public function actionDetail()
  331. {
  332. $get = Yii::$app->request->get();
  333. $customId = $get['id'] ?? 0;
  334. $info = CustomService::getCustomInfo($customId);
  335. CustomClass::valid($info, $this->shopId);
  336. //手动添加的客户经纬度不完善不显示地址,引导完善客户地址
  337. if (isset($info['lat']) && empty($info['lat'])) {
  338. $info['address'] = '';
  339. }
  340. if (isset($info['long']) && empty($info['long'])) {
  341. $info['address'] = '';
  342. }
  343. $mainId = $info['mainId'] ?? 0;
  344. //超管的信息
  345. $superInfo = ShopAdminClass::getManager($mainId);
  346. $superName = $superInfo['name'] ?? '';
  347. $currentSuper = ['name' => $superName];
  348. $info['superInfo'] = $currentSuper;
  349. util::success($info);
  350. }
  351. //允许月结开关 ssh 2021.1.8
  352. public function actionDebt()
  353. {
  354. $get = Yii::$app->request->get();
  355. $debt = isset($get['debt']) ? $get['debt'] : 0;
  356. $customId = isset($get['customId']) ? $get['customId'] : 0;
  357. $custom = CustomClass::getById($customId);
  358. CustomClass::valid($custom, $this->shopId);
  359. CustomClass::debt($custom, $debt);
  360. util::complete();
  361. }
  362. //采购查看权限的黑白名单开关 shizhongqi 2021.08.08
  363. public function actionSetBlack()
  364. {
  365. $black = Yii::$app->request->get('black', 2);
  366. $customId = Yii::$app->request->get('customId', 0);
  367. $custom = CustomClass::getById($customId, true);
  368. CustomClass::valid($custom, $this->shopId);
  369. $custom->black = $black;
  370. $custom->save();
  371. $ghsId = $custom->ghsId ?? 0;
  372. $ghs = GhsClass::getById($ghsId, true);
  373. if (empty($ghs)) {
  374. util::fail('出错了');
  375. }
  376. $ghs->black = $black;
  377. $ghs->save();
  378. util::complete();
  379. }
  380. //欠款客户 ssh 2021.2.4
  381. public function actionDebtList()
  382. {
  383. $get = Yii::$app->request->get();
  384. $where = ['ownShopId' => $this->shopId, 'isDebt' => 1];
  385. if (isset($get['name']) && !empty($get['name'])) {
  386. $where['name'] = ['like', $get['name']];
  387. }
  388. $list = CustomService::getDebtList($where);
  389. //共X个客户,合计欠款XXX元
  390. $main = $this->main;
  391. $list['customNum'] = $main->mayGatheringNum ?? 0;
  392. $list['debtAmount'] = $main->mayGathering ?? 0.00;
  393. util::success($list);
  394. }
  395. //修改欠款额度 ssh 20210625
  396. public function actionUpdateDebtLimit()
  397. {
  398. $shopAdmin = $this->shopAdmin;
  399. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  400. util::fail('管理员才能操作');
  401. }
  402. $get = Yii::$app->request->get();
  403. $id = $get['id'] ?? 0;
  404. $debtLimit = $get['debtLimit'] ?? 0;
  405. if (is_numeric($debtLimit) == false || $debtLimit <= 0) {
  406. util::fail('请填写金额');
  407. }
  408. $custom = CustomClass::getById($id, true);
  409. CustomClass::valid($custom, $this->shopId);
  410. $custom->debtLimit = $debtLimit;
  411. $custom->save();
  412. util::complete();
  413. }
  414. //修改折扣 ssh 20210913
  415. public function actionChangeDiscount()
  416. {
  417. $shopAdmin = $this->shopAdmin;
  418. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  419. util::fail('管理员才能操作');
  420. }
  421. $get = Yii::$app->request->get();
  422. $id = $get['id'] ?? 0;
  423. $discount = $get['discount'] ?? 0;
  424. if (is_numeric($discount) == false || $discount > 1 || $discount == 0) {
  425. util::fail('请填写小数值');
  426. }
  427. $custom = CustomClass::getById($id, true);
  428. CustomClass::valid($custom, $this->shopId);
  429. $custom->discount = $discount;
  430. $custom->save();
  431. $ghsId = $custom->ghsId ?? 0;
  432. $ghs = GhsClass::getById($ghsId, true);
  433. $ghs->giveDiscount = $discount;
  434. $ghs->save();
  435. util::complete();
  436. }
  437. //修改客户等级 ssh 20211007
  438. public function actionChangeLevel()
  439. {
  440. $shopAdmin = $this->shopAdmin;
  441. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  442. util::fail('超管才能修改');
  443. }
  444. $get = Yii::$app->request->get();
  445. $customId = $get['customId'] ?? 0;
  446. $level = $get['level'] ?? 1;
  447. $custom = CustomClass::getById($customId, true);
  448. CustomClass::valid($custom, $this->shopId);
  449. $custom->level = $level;
  450. $custom->save();
  451. $ghsId = $custom->ghsId ?? 0;
  452. $ghs = GhsClass::getById($ghsId, true);
  453. if (empty($ghs)) {
  454. util::fail('出错了');
  455. }
  456. $ghs->giveLevel = $level;
  457. $ghs->save();
  458. $map = CustomClass::$levelMap;
  459. $name = $map[$level] ?? '';
  460. util::success(['levelName' => $name]);
  461. }
  462. }