CustomController.php 16 KB

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