CustomController.php 17 KB

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