CustomController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\shop\classes\ShopAdminClass;
  5. use biz\sj\classes\SjClass;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizGhs\custom\services\CustomService;
  8. use bizHd\saas\classes\ApplyClass;
  9. use bizHd\saas\services\ApplyService;
  10. use bizHd\stat\classes\StatVisitClass;
  11. use bizGhs\shop\classes\ShopClass;
  12. use common\components\imgUtil;
  13. use common\components\stringUtil;
  14. use common\components\util;
  15. use Yii;
  16. class CustomController extends BaseController
  17. {
  18. //添加新花店 ssh 2021.1.8
  19. public function actionAdd()
  20. {
  21. $post = Yii::$app->request->post();
  22. $post['style'] = SjClass::STYLE_RETAIL;
  23. $post['introSjId'] = $this->sjId;
  24. $post['introStyle'] = SjClass::STYLE_SUPPLIER;
  25. $mobile = $post['mobile'] ?? '';
  26. $confirmMobile = $post['confirmMobile'];
  27. $address = $post['address'] ?? '';
  28. util::fail('功能已停用');
  29. if (stringUtil::isMobile($mobile) == false) {
  30. util::fail(' 请填写正确的手机号');
  31. }
  32. if ($mobile != $confirmMobile) {
  33. util::fail('二次号码填写不一致');
  34. }
  35. $name = $post['name'] ?? '';
  36. if (empty($name)) {
  37. util::fail('名称不能为空');
  38. }
  39. if (stringUtil::getWordNum($name) > 8) {
  40. util::fail('名称不能超过8个汉字');
  41. }
  42. $has = SjClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_RETAIL]);
  43. if (!empty($has)) {
  44. util::fail('名称已经存在');
  45. }
  46. $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_RETAIL]);
  47. if (!empty($has)) {
  48. util::fail('手机号已经添加过');
  49. }
  50. if (empty($address)) {
  51. util::fail('请填写地址');
  52. }
  53. $connection = Yii::$app->db;
  54. $transaction = $connection->beginTransaction();
  55. try {
  56. $shop = $this->shop;
  57. $sjId = $this->sjId;
  58. $shopId = $this->shopId;
  59. $sjName = $this->sj->name ?? '';
  60. $city = $shop->city ?? '';
  61. $dist = $shop->dist ?? '';
  62. $applyData = [
  63. 'name' => $name,
  64. 'licenseNo' => $mobile,
  65. 'certType' => ApplyClass::CERT_TYPE_MOBILE,
  66. 'mobile' => $mobile,
  67. 'introSjId' => $sjId,
  68. 'introSjName' => $sjName,
  69. 'introShopId' => $shopId,
  70. 'introStyle' => SjClass::STYLE_SUPPLIER,
  71. 'from' => ApplyClass::FROM_GHS,
  72. 'style' => SjClass::STYLE_RETAIL,
  73. 'province' => $shop->province ?? '',
  74. 'city' => $city,
  75. 'dist' => $dist,
  76. 'address' => $address,
  77. 'status' => ApplyClass::STATUS_PASS,
  78. 'replace' => 1,//1表示供应商添加的客户
  79. ];
  80. $apply = ApplyService::replaceRetail($applyData);
  81. $id = $apply['id'] ?? 0;
  82. $respond = ApplyService::pass($id);
  83. $shop = $respond['shop'] ?? [];
  84. $sj = $respond['sj'] ?? [];
  85. $hdSjId = $sj['id'] ?? 0;
  86. $hdShopId = $shop->id ?? 0;
  87. ApplyClass::updateById($id, ['sjId' => $hdSjId, 'shopId' => $hdShopId]);
  88. if (empty($hdShopId)) {
  89. util::fail('客户没有添加成功!');
  90. }
  91. $custom = CustomClass::build($this->shopId, $hdShopId);
  92. $custom['avatar'] = imgUtil::groupImg($custom['avatar']);
  93. //客户欠款额度设置
  94. $debt = isset($post['debt']) && $post['debt'] == CustomClass::IS_DEBT_YES ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
  95. $customId = $custom['id'] ?? 0;
  96. $debtLimit = isset($post['debtLimit']) && is_numeric($post['debtLimit']) ? $post['debtLimit'] : 5000;
  97. CustomClass::updateById($customId, ['debtLimit' => $debtLimit, 'debt' => $debt]);
  98. $transaction->commit();
  99. util::success($custom);
  100. } catch (\Exception $exception) {
  101. $transaction->rollBack();
  102. Yii::info("添加客户没有成功:" . $exception->getMessage());
  103. util::fail('添加客户没有成功');
  104. }
  105. }
  106. //向队列写入新花店数据 shizhongqi 2022.6.8
  107. public function actionCreateCustomers()
  108. {
  109. $post = Yii::$app->request->post();
  110. $post['style'] = SjClass::STYLE_RETAIL;
  111. $sjId = $this->sjId;
  112. $shopId = $this->shopId;
  113. for($i = 1; $i <= 5; $i++) {
  114. $name = $post['name' . $i];
  115. $mobile = $post['mobile' . $i];
  116. $confirmMobile = $post['confirmMobile' . $i];
  117. if (empty($name)) {
  118. Yii::info("花店名称不能为空");
  119. continue;
  120. }
  121. if (stringUtil::getWordNum($name) > 8) {
  122. Yii::info('名称不能超过8个汉字');
  123. continue;
  124. }
  125. if (empty($mobile)) {
  126. Yii::info("手机号不能为空");
  127. continue;
  128. }
  129. if (stringUtil::isMobile($mobile) == false) {
  130. Yii::info('手机号格式不正确');
  131. continue;
  132. }
  133. if ($mobile != $confirmMobile) {
  134. Yii::info("添加客户失败:mobile " . $mobile . ' != confirmMobile ' . $confirmMobile);
  135. continue;
  136. }
  137. //队列方式去处理
  138. $customerCachedKey = 'customers_list';
  139. $value = $name . '_' . $mobile . '_' . $this->sjId . '_' . $this->shop['id'] . '_' .$this->sj['name'] . '_' . $this->shopAdminId;
  140. Yii::$app->redis->executeCommand('LPUSH', [$customerCachedKey, $value]);
  141. Yii::info("添加客户:name - " . $name . ' == mobile - ' . $mobile);
  142. Yii::info("shopAdminId: " . $this->shopAdminId);
  143. }
  144. util::complete();
  145. }
  146. //向队列写入执行复制客户的任务 shizhongqi 2022.6.9
  147. public function actionCopyCustomers()
  148. {
  149. $shopId = $this->shopId;
  150. // 检测首店存在
  151. $defaultShopId = ShopClass::getDefaultShopId($shopId);
  152. if (empty($defaultShopId)) {
  153. Yii::info("通过分店 id = $shopId 取首店 id 失败");
  154. return;
  155. }
  156. $cachedKey = 'copy_firstShop_customers';
  157. Yii::$app->redis->executeCommand('LPUSH', [$cachedKey, $shopId . '_' . $defaultShopId]);
  158. Yii::info("复制首店的客户:分店id = " . $shopId . ' 首店id = ' . $defaultShopId);
  159. util::complete();
  160. }
  161. //客户列表 ssh 2021.7.8
  162. public function actionList()
  163. {
  164. $get = Yii::$app->request->get();
  165. $type = isset($get['type']) ? $get['type'] : 0;
  166. $name = isset($get['name']) ? $get['name'] : '';
  167. $where = ['ownMainId' => $this->mainId];
  168. $sort = 'visitTime DESC';
  169. if ($type == 1) {
  170. //消费排行
  171. $sort = 'buyAmount DESC';
  172. } else if ($type == 2) {
  173. //欠款客户
  174. $where['isDebt'] = 1;
  175. $sort = 'debtAmount DESC';
  176. }
  177. if (!empty($name)) {
  178. if (stringUtil::isLetter($name)) {
  179. $where['py'] = ['like', $name];
  180. } else {
  181. $where['name'] = ['like', $name];
  182. }
  183. }
  184. $list = CustomService::getCustomSortList($where, $sort);
  185. $main = $this->main;
  186. $list['totalUser'] = $main->totalUser ?? 0;
  187. $list['mayGatheringNum'] = $main->mayGatheringNum ?? 0;
  188. util::success($list);
  189. }
  190. //今日访客 ssh 20211022
  191. public function actionGetVisit()
  192. {
  193. $ids = StatVisitClass::getVisitCustomIds($this->shop);
  194. if (empty($ids)) {
  195. util::success(['list' => []]);
  196. }
  197. $sort = 'visitTime DESC';
  198. $where = ['id' => ['in', $ids]];
  199. $respond = CustomService::getCustomSortList($where, $sort);
  200. util::success($respond);
  201. }
  202. //客户详情 ssh 2021.1.8
  203. public function actionDetail()
  204. {
  205. $get = Yii::$app->request->get();
  206. $customId = $get['id'] ?? 0;
  207. $info = CustomService::getCustomInfo($customId);
  208. CustomClass::valid($info, $this->shopId);
  209. //手动添加的客户经纬度不完善不显示地址,引导完善客户地址
  210. if (isset($info['lat']) && empty($info['lat'])) {
  211. $info['address'] = '';
  212. }
  213. if (isset($info['long']) && empty($info['long'])) {
  214. $info['address'] = '';
  215. }
  216. $mainId = $info['mainId'] ?? 0;
  217. //超管的信息
  218. $superInfo = ShopAdminClass::getManager($mainId);
  219. $superName = $superInfo['name'] ?? '';
  220. $currentSuper = ['name' => $superName];
  221. $info['superInfo'] = $currentSuper;
  222. util::success($info);
  223. }
  224. //允许月结开关 ssh 2021.1.8
  225. public function actionDebt()
  226. {
  227. $get = Yii::$app->request->get();
  228. $debt = isset($get['debt']) ? $get['debt'] : 0;
  229. $customId = isset($get['customId']) ? $get['customId'] : 0;
  230. $custom = CustomClass::getById($customId);
  231. CustomClass::valid($custom, $this->shopId);
  232. CustomClass::debt($custom, $debt);
  233. util::complete();
  234. }
  235. //采购查看权限的黑白名单开关 shizhongqi 2021.08.08
  236. public function actionSetBlack()
  237. {
  238. $black = Yii::$app->request->get('black', 2);
  239. $customId = Yii::$app->request->get('customId', 0);
  240. $custom = CustomClass::getById($customId, true);
  241. CustomClass::valid($custom, $this->shopId);
  242. $custom->black = $black;
  243. $custom->save();
  244. $ghsId = $custom->ghsId ?? 0;
  245. $ghs = GhsClass::getById($ghsId, true);
  246. if (empty($ghs)) {
  247. util::fail('出错了');
  248. }
  249. $ghs->black = $black;
  250. $ghs->save();
  251. util::complete();
  252. }
  253. //欠款客户 ssh 2021.2.4
  254. public function actionDebtList()
  255. {
  256. $get = Yii::$app->request->get();
  257. $where = ['ownShopId' => $this->shopId, 'isDebt' => 1];
  258. if (isset($get['name']) && !empty($get['name'])) {
  259. $where['name'] = ['like', $get['name']];
  260. }
  261. $list = CustomService::getDebtList($where);
  262. //共X个客户,合计欠款XXX元
  263. $main = $this->main;
  264. $list['customNum'] = $main->mayGatheringNum ?? 0;
  265. $list['debtAmount'] = $main->mayGathering ?? 0.00;
  266. util::success($list);
  267. }
  268. //修改欠款额度 ssh 20210625
  269. public function actionUpdateDebtLimit()
  270. {
  271. $shopAdmin = $this->shopAdmin;
  272. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  273. util::fail('管理员才能操作');
  274. }
  275. $get = Yii::$app->request->get();
  276. $id = $get['id'] ?? 0;
  277. $debtLimit = $get['debtLimit'] ?? 0;
  278. if (is_numeric($debtLimit) == false || $debtLimit <= 0) {
  279. util::fail('请填写金额');
  280. }
  281. $custom = CustomClass::getById($id, true);
  282. CustomClass::valid($custom, $this->shopId);
  283. $custom->debtLimit = $debtLimit;
  284. $custom->save();
  285. util::complete();
  286. }
  287. //修改折扣 ssh 20210913
  288. public function actionChangeDiscount()
  289. {
  290. $shopAdmin = $this->shopAdmin;
  291. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  292. util::fail('管理员才能操作');
  293. }
  294. $get = Yii::$app->request->get();
  295. $id = $get['id'] ?? 0;
  296. $discount = $get['discount'] ?? 0;
  297. if (is_numeric($discount) == false || $discount > 1 || $discount == 0) {
  298. util::fail('请填写小数值');
  299. }
  300. $custom = CustomClass::getById($id, true);
  301. CustomClass::valid($custom, $this->shopId);
  302. $custom->discount = $discount;
  303. $custom->save();
  304. $ghsId = $custom->ghsId ?? 0;
  305. $ghs = GhsClass::getById($ghsId, true);
  306. $ghs->giveDiscount = $discount;
  307. $ghs->save();
  308. util::complete();
  309. }
  310. //修改客户等级 ssh 20211007
  311. public function actionChangeLevel()
  312. {
  313. $shopAdmin = $this->shopAdmin;
  314. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  315. util::fail('超管才能修改');
  316. }
  317. $get = Yii::$app->request->get();
  318. $customId = $get['customId'] ?? 0;
  319. $level = $get['level'] ?? 1;
  320. $custom = CustomClass::getById($customId, true);
  321. CustomClass::valid($custom, $this->shopId);
  322. $custom->level = $level;
  323. $custom->save();
  324. $ghsId = $custom->ghsId ?? 0;
  325. $ghs = GhsClass::getById($ghsId, true);
  326. if (empty($ghs)) {
  327. util::fail('出错了');
  328. }
  329. $ghs->giveLevel = $level;
  330. $ghs->save();
  331. $map = CustomClass::$levelMap;
  332. $name = $map[$level] ?? '';
  333. util::success(['levelName' => $name]);
  334. }
  335. }