CustomController.php 12 KB

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