CustomController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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\shop\classes\ShopClass;
  11. use bizHd\stat\classes\StatVisitClass;
  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. $arr = [];
  111. for ($i = 1; $i <= 5; $i++) {
  112. $name = $post['name' . $i] ?? '';
  113. $mobile = $post['mobile' . $i] ?? '';
  114. $confirmMobile = $post['confirmMobile' . $i] ?? '';
  115. if (empty($name) && empty($mobile) && empty($confirmMobile)) {
  116. continue;
  117. }
  118. if (empty($name)) {
  119. util::fail('花店名称不能为空');
  120. }
  121. $has = SjClass::getByCondition(['name' => $name]);
  122. if (!empty($has)) {
  123. util::fail($name . ' 这个名称已经被别人使用了');
  124. }
  125. if (stringUtil::getWordNum($name) > 8) {
  126. util::fail('名称不能超过8个汉字');
  127. }
  128. if (empty($mobile)) {
  129. util::fail($name . " 手机号不能为空");
  130. }
  131. if (stringUtil::isMobile($mobile) == false) {
  132. util::fail($name . " 手机号格式不正确");
  133. }
  134. if ($mobile != $confirmMobile) {
  135. util::fail($name . " 二次手机号不一致");
  136. }
  137. $hasShop = ShopClass::getByCondition(['mobile' => $mobile], true);
  138. if (!empty($hasShop)) {
  139. util::fail($name . "这个客户已经添加过了");
  140. }
  141. $has = ApplyClass::getByCondition(['mobile' => $mobile]);
  142. if (!empty($has)) {
  143. util::fail($name . "这个客户已经添加过了哦");
  144. }
  145. $arr[] = ['name' => $name, 'mobile' => $mobile];
  146. }
  147. if (empty($arr)) {
  148. util::fail('请填写客户');
  149. }
  150. foreach ($arr as $item) {
  151. $name = $item['name'] ?? '';
  152. $mobile = $item['mobile'] ?? '';
  153. //队列方式去处理
  154. $customerCachedKey = 'batch_create_customer_list';
  155. $value = $name . '_' . $mobile . '_' . $this->sjId . '_' . $this->shop->id . '_' . $this->sj->name . '_' . $this->shopAdminId;
  156. Yii::$app->redis->executeCommand('LPUSH', [$customerCachedKey, $value]);
  157. Yii::info("供应商添加客户:name - " . $name . ' == mobile - ' . $mobile);
  158. Yii::info("shopAdminId: " . $this->shopAdminId);
  159. }
  160. util::complete();
  161. }
  162. //从首店同步客户 shizhongqi 2022.6.9
  163. public function actionCopyCustomers()
  164. {
  165. $shopId = $this->shopId;
  166. // 检测首店存在
  167. $defaultShopId = ShopClass::getDefaultShopId($this->shop);
  168. if (empty($defaultShopId)) {
  169. util::fail('没有找到首店');
  170. }
  171. $shop = ShopClass::getById($defaultShopId, true);
  172. if (empty($shop)) {
  173. util::fail('首店不存在');
  174. }
  175. $cachedKey = 'copy_firstShop_customers';
  176. Yii::$app->redis->executeCommand('LPUSH', [$cachedKey, $shopId . '_' . $defaultShopId]);
  177. Yii::info("从首店同步客户:分店id = " . $shopId . ' 首店id = ' . $defaultShopId);
  178. util::complete('已申请,1分钟后同步完成');
  179. }
  180. //客户列表 ssh 2021.7.8
  181. public function actionList()
  182. {
  183. $get = Yii::$app->request->get();
  184. $type = isset($get['type']) ? $get['type'] : 0;
  185. $name = isset($get['name']) ? $get['name'] : '';
  186. $where = ['ownMainId' => $this->mainId];
  187. $sort = 'visitTime DESC';
  188. if ($type == 1) {
  189. //消费排行
  190. $sort = 'buyAmount DESC';
  191. } else if ($type == 2) {
  192. //欠款客户
  193. $where['isDebt'] = 1;
  194. $sort = 'debtAmount DESC';
  195. }
  196. if (!empty($name)) {
  197. if (stringUtil::isLetter($name)) {
  198. $where['py'] = ['like', $name];
  199. } else {
  200. $where['name'] = ['like', $name];
  201. }
  202. }
  203. $list = CustomService::getCustomSortList($where, $sort);
  204. $main = $this->main;
  205. $list['totalUser'] = $main->totalUser ?? 0;
  206. $list['mayGatheringNum'] = $main->mayGatheringNum ?? 0;
  207. util::success($list);
  208. }
  209. //今日访客 ssh 20211022
  210. public function actionGetVisit()
  211. {
  212. $ids = StatVisitClass::getVisitCustomIds($this->shop);
  213. if (empty($ids)) {
  214. util::success(['list' => []]);
  215. }
  216. $sort = 'visitTime DESC';
  217. $where = ['id' => ['in', $ids]];
  218. $respond = CustomService::getCustomSortList($where, $sort);
  219. util::success($respond);
  220. }
  221. //客户详情 ssh 2021.1.8
  222. public function actionDetail()
  223. {
  224. $get = Yii::$app->request->get();
  225. $customId = $get['id'] ?? 0;
  226. $info = CustomService::getCustomInfo($customId);
  227. CustomClass::valid($info, $this->shopId);
  228. //手动添加的客户经纬度不完善不显示地址,引导完善客户地址
  229. if (isset($info['lat']) && empty($info['lat'])) {
  230. $info['address'] = '';
  231. }
  232. if (isset($info['long']) && empty($info['long'])) {
  233. $info['address'] = '';
  234. }
  235. $mainId = $info['mainId'] ?? 0;
  236. //超管的信息
  237. $superInfo = ShopAdminClass::getManager($mainId);
  238. $superName = $superInfo['name'] ?? '';
  239. $currentSuper = ['name' => $superName];
  240. $info['superInfo'] = $currentSuper;
  241. util::success($info);
  242. }
  243. //允许月结开关 ssh 2021.1.8
  244. public function actionDebt()
  245. {
  246. $get = Yii::$app->request->get();
  247. $debt = isset($get['debt']) ? $get['debt'] : 0;
  248. $customId = isset($get['customId']) ? $get['customId'] : 0;
  249. $custom = CustomClass::getById($customId);
  250. CustomClass::valid($custom, $this->shopId);
  251. CustomClass::debt($custom, $debt);
  252. util::complete();
  253. }
  254. //采购查看权限的黑白名单开关 shizhongqi 2021.08.08
  255. public function actionSetBlack()
  256. {
  257. $black = Yii::$app->request->get('black', 2);
  258. $customId = Yii::$app->request->get('customId', 0);
  259. $custom = CustomClass::getById($customId, true);
  260. CustomClass::valid($custom, $this->shopId);
  261. $custom->black = $black;
  262. $custom->save();
  263. $ghsId = $custom->ghsId ?? 0;
  264. $ghs = GhsClass::getById($ghsId, true);
  265. if (empty($ghs)) {
  266. util::fail('出错了');
  267. }
  268. $ghs->black = $black;
  269. $ghs->save();
  270. util::complete();
  271. }
  272. //欠款客户 ssh 2021.2.4
  273. public function actionDebtList()
  274. {
  275. $get = Yii::$app->request->get();
  276. $where = ['ownShopId' => $this->shopId, 'isDebt' => 1];
  277. if (isset($get['name']) && !empty($get['name'])) {
  278. $where['name'] = ['like', $get['name']];
  279. }
  280. $list = CustomService::getDebtList($where);
  281. //共X个客户,合计欠款XXX元
  282. $main = $this->main;
  283. $list['customNum'] = $main->mayGatheringNum ?? 0;
  284. $list['debtAmount'] = $main->mayGathering ?? 0.00;
  285. util::success($list);
  286. }
  287. //修改欠款额度 ssh 20210625
  288. public function actionUpdateDebtLimit()
  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. $debtLimit = $get['debtLimit'] ?? 0;
  297. if (is_numeric($debtLimit) == false || $debtLimit <= 0) {
  298. util::fail('请填写金额');
  299. }
  300. $custom = CustomClass::getById($id, true);
  301. CustomClass::valid($custom, $this->shopId);
  302. $custom->debtLimit = $debtLimit;
  303. $custom->save();
  304. util::complete();
  305. }
  306. //修改折扣 ssh 20210913
  307. public function actionChangeDiscount()
  308. {
  309. $shopAdmin = $this->shopAdmin;
  310. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  311. util::fail('管理员才能操作');
  312. }
  313. $get = Yii::$app->request->get();
  314. $id = $get['id'] ?? 0;
  315. $discount = $get['discount'] ?? 0;
  316. if (is_numeric($discount) == false || $discount > 1 || $discount == 0) {
  317. util::fail('请填写小数值');
  318. }
  319. $custom = CustomClass::getById($id, true);
  320. CustomClass::valid($custom, $this->shopId);
  321. $custom->discount = $discount;
  322. $custom->save();
  323. $ghsId = $custom->ghsId ?? 0;
  324. $ghs = GhsClass::getById($ghsId, true);
  325. $ghs->giveDiscount = $discount;
  326. $ghs->save();
  327. util::complete();
  328. }
  329. //修改客户等级 ssh 20211007
  330. public function actionChangeLevel()
  331. {
  332. $shopAdmin = $this->shopAdmin;
  333. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  334. util::fail('超管才能修改');
  335. }
  336. $get = Yii::$app->request->get();
  337. $customId = $get['customId'] ?? 0;
  338. $level = $get['level'] ?? 1;
  339. $custom = CustomClass::getById($customId, true);
  340. CustomClass::valid($custom, $this->shopId);
  341. $custom->level = $level;
  342. $custom->save();
  343. $ghsId = $custom->ghsId ?? 0;
  344. $ghs = GhsClass::getById($ghsId, true);
  345. if (empty($ghs)) {
  346. util::fail('出错了');
  347. }
  348. $ghs->giveLevel = $level;
  349. $ghs->save();
  350. $map = CustomClass::$levelMap;
  351. $name = $map[$level] ?? '';
  352. util::success(['levelName' => $name]);
  353. }
  354. }