CustomController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. util::fail($name . "这个客户已经添加过了");
  191. }
  192. $has = ApplyClass::getByCondition(['mobile' => $mobile]);
  193. if (!empty($has)) {
  194. util::fail($name . "这个客户已经添加过了哦");
  195. }
  196. $arr[] = ['name' => $name, 'mobile' => $mobile];
  197. }
  198. if (empty($arr)) {
  199. util::fail('请填写客户');
  200. }
  201. foreach ($arr as $item) {
  202. $name = $item['name'] ?? '';
  203. $mobile = $item['mobile'] ?? '';
  204. //队列方式去处理
  205. $customerCachedKey = 'batch_create_customer_list';
  206. $value = $name . '_' . $mobile . '_' . $this->sjId . '_' . $this->shop->id . '_' . $this->sj->name . '_' . $this->shopAdminId;
  207. Yii::$app->redis->executeCommand('LPUSH', [$customerCachedKey, $value]);
  208. Yii::info("供应商添加客户:name - " . $name . ' == mobile - ' . $mobile);
  209. Yii::info("shopAdminId: " . $this->shopAdminId);
  210. }
  211. util::complete('已添加,1分钟后生效');
  212. }
  213. //从首店同步客户 shizhongqi 2022.6.9
  214. public function actionCopyCustomers()
  215. {
  216. $shopId = $this->shopId;
  217. // 检测首店存在
  218. $defaultShopId = ShopClass::getDefaultShopId($this->shop);
  219. if (empty($defaultShopId)) {
  220. util::fail('没有找到首店');
  221. }
  222. $shop = ShopClass::getById($defaultShopId, true);
  223. if (empty($shop)) {
  224. util::fail('首店不存在');
  225. }
  226. $cachedKey = 'copy_firstShop_customers';
  227. Yii::$app->redis->executeCommand('LPUSH', [$cachedKey, $shopId . '_' . $defaultShopId]);
  228. Yii::info("从首店同步客户:分店id = " . $shopId . ' 首店id = ' . $defaultShopId);
  229. util::complete('已申请,1分钟后同步完成');
  230. }
  231. //客户列表 ssh 2021.7.8
  232. public function actionList()
  233. {
  234. $get = Yii::$app->request->get();
  235. $type = isset($get['type']) ? $get['type'] : 0;
  236. $name = isset($get['name']) ? $get['name'] : '';
  237. $where = ['ownMainId' => $this->mainId];
  238. $sort = 'visitTime DESC';
  239. if ($type == 1) {
  240. //消费排行
  241. $sort = 'buyAmount DESC';
  242. } else if ($type == 2) {
  243. //欠款客户
  244. $where['isDebt'] = 1;
  245. $sort = 'debtAmount DESC';
  246. }
  247. if (!empty($name)) {
  248. if (stringUtil::isLetter($name)) {
  249. $where['py'] = ['like', $name];
  250. } else {
  251. $where['name'] = ['like', $name];
  252. }
  253. }
  254. $list = CustomService::getCustomSortList($where, $sort);
  255. $main = $this->main;
  256. $list['totalUser'] = $main->totalUser ?? 0;
  257. $list['mayGatheringNum'] = $main->mayGatheringNum ?? 0;
  258. util::success($list);
  259. }
  260. //今日访客 ssh 20211022
  261. public function actionGetVisit()
  262. {
  263. $ids = StatVisitClass::getVisitCustomIds($this->shop);
  264. if (empty($ids)) {
  265. util::success(['list' => []]);
  266. }
  267. $sort = 'visitTime DESC';
  268. $where = ['id' => ['in', $ids]];
  269. $respond = CustomService::getCustomSortList($where, $sort);
  270. util::success($respond);
  271. }
  272. //客户详情 ssh 2021.1.8
  273. public function actionDetail()
  274. {
  275. $get = Yii::$app->request->get();
  276. $customId = $get['id'] ?? 0;
  277. $info = CustomService::getCustomInfo($customId);
  278. CustomClass::valid($info, $this->shopId);
  279. //手动添加的客户经纬度不完善不显示地址,引导完善客户地址
  280. if (isset($info['lat']) && empty($info['lat'])) {
  281. $info['address'] = '';
  282. }
  283. if (isset($info['long']) && empty($info['long'])) {
  284. $info['address'] = '';
  285. }
  286. $mainId = $info['mainId'] ?? 0;
  287. //超管的信息
  288. $superInfo = ShopAdminClass::getManager($mainId);
  289. $superName = $superInfo['name'] ?? '';
  290. $currentSuper = ['name' => $superName];
  291. $info['superInfo'] = $currentSuper;
  292. util::success($info);
  293. }
  294. //允许月结开关 ssh 2021.1.8
  295. public function actionDebt()
  296. {
  297. $get = Yii::$app->request->get();
  298. $debt = isset($get['debt']) ? $get['debt'] : 0;
  299. $customId = isset($get['customId']) ? $get['customId'] : 0;
  300. $custom = CustomClass::getById($customId);
  301. CustomClass::valid($custom, $this->shopId);
  302. CustomClass::debt($custom, $debt);
  303. util::complete();
  304. }
  305. //采购查看权限的黑白名单开关 shizhongqi 2021.08.08
  306. public function actionSetBlack()
  307. {
  308. $black = Yii::$app->request->get('black', 2);
  309. $customId = Yii::$app->request->get('customId', 0);
  310. $custom = CustomClass::getById($customId, true);
  311. CustomClass::valid($custom, $this->shopId);
  312. $custom->black = $black;
  313. $custom->save();
  314. $ghsId = $custom->ghsId ?? 0;
  315. $ghs = GhsClass::getById($ghsId, true);
  316. if (empty($ghs)) {
  317. util::fail('出错了');
  318. }
  319. $ghs->black = $black;
  320. $ghs->save();
  321. util::complete();
  322. }
  323. //欠款客户 ssh 2021.2.4
  324. public function actionDebtList()
  325. {
  326. $get = Yii::$app->request->get();
  327. $where = ['ownShopId' => $this->shopId, 'isDebt' => 1];
  328. if (isset($get['name']) && !empty($get['name'])) {
  329. $where['name'] = ['like', $get['name']];
  330. }
  331. $list = CustomService::getDebtList($where);
  332. //共X个客户,合计欠款XXX元
  333. $main = $this->main;
  334. $list['customNum'] = $main->mayGatheringNum ?? 0;
  335. $list['debtAmount'] = $main->mayGathering ?? 0.00;
  336. util::success($list);
  337. }
  338. //修改欠款额度 ssh 20210625
  339. public function actionUpdateDebtLimit()
  340. {
  341. $shopAdmin = $this->shopAdmin;
  342. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  343. util::fail('管理员才能操作');
  344. }
  345. $get = Yii::$app->request->get();
  346. $id = $get['id'] ?? 0;
  347. $debtLimit = $get['debtLimit'] ?? 0;
  348. if (is_numeric($debtLimit) == false || $debtLimit <= 0) {
  349. util::fail('请填写金额');
  350. }
  351. $custom = CustomClass::getById($id, true);
  352. CustomClass::valid($custom, $this->shopId);
  353. $custom->debtLimit = $debtLimit;
  354. $custom->save();
  355. util::complete();
  356. }
  357. //修改折扣 ssh 20210913
  358. public function actionChangeDiscount()
  359. {
  360. $shopAdmin = $this->shopAdmin;
  361. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  362. util::fail('管理员才能操作');
  363. }
  364. $get = Yii::$app->request->get();
  365. $id = $get['id'] ?? 0;
  366. $discount = $get['discount'] ?? 0;
  367. if (is_numeric($discount) == false || $discount > 1 || $discount == 0) {
  368. util::fail('请填写小数值');
  369. }
  370. $custom = CustomClass::getById($id, true);
  371. CustomClass::valid($custom, $this->shopId);
  372. $custom->discount = $discount;
  373. $custom->save();
  374. $ghsId = $custom->ghsId ?? 0;
  375. $ghs = GhsClass::getById($ghsId, true);
  376. $ghs->giveDiscount = $discount;
  377. $ghs->save();
  378. util::complete();
  379. }
  380. //修改客户等级 ssh 20211007
  381. public function actionChangeLevel()
  382. {
  383. $shopAdmin = $this->shopAdmin;
  384. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  385. util::fail('超管才能修改');
  386. }
  387. $get = Yii::$app->request->get();
  388. $customId = $get['customId'] ?? 0;
  389. $level = $get['level'] ?? 1;
  390. $custom = CustomClass::getById($customId, true);
  391. CustomClass::valid($custom, $this->shopId);
  392. $custom->level = $level;
  393. $custom->save();
  394. $ghsId = $custom->ghsId ?? 0;
  395. $ghs = GhsClass::getById($ghsId, true);
  396. if (empty($ghs)) {
  397. util::fail('出错了');
  398. }
  399. $ghs->giveLevel = $level;
  400. $ghs->save();
  401. $map = CustomClass::$levelMap;
  402. $name = $map[$level] ?? '';
  403. util::success(['levelName' => $name]);
  404. }
  405. }