CustomController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. }
  142. util::complete();
  143. }
  144. //客户列表 ssh 2021.7.8
  145. public function actionList()
  146. {
  147. $get = Yii::$app->request->get();
  148. $type = isset($get['type']) ? $get['type'] : 0;
  149. $name = isset($get['name']) ? $get['name'] : '';
  150. $where = ['ownMainId' => $this->mainId];
  151. $sort = 'visitTime DESC';
  152. if ($type == 1) {
  153. //消费排行
  154. $sort = 'buyAmount DESC';
  155. } else if ($type == 2) {
  156. //欠款客户
  157. $where['isDebt'] = 1;
  158. $sort = 'debtAmount DESC';
  159. }
  160. if (!empty($name)) {
  161. if (stringUtil::isLetter($name)) {
  162. $where['py'] = ['like', $name];
  163. } else {
  164. $where['name'] = ['like', $name];
  165. }
  166. }
  167. $list = CustomService::getCustomSortList($where, $sort);
  168. $main = $this->main;
  169. $list['totalUser'] = $main->totalUser ?? 0;
  170. $list['mayGatheringNum'] = $main->mayGatheringNum ?? 0;
  171. util::success($list);
  172. }
  173. //今日访客 ssh 20211022
  174. public function actionGetVisit()
  175. {
  176. $ids = StatVisitClass::getVisitCustomIds($this->shop);
  177. if (empty($ids)) {
  178. util::success(['list' => []]);
  179. }
  180. $sort = 'visitTime DESC';
  181. $where = ['id' => ['in', $ids]];
  182. $respond = CustomService::getCustomSortList($where, $sort);
  183. util::success($respond);
  184. }
  185. //客户详情 ssh 2021.1.8
  186. public function actionDetail()
  187. {
  188. $get = Yii::$app->request->get();
  189. $customId = $get['id'] ?? 0;
  190. $info = CustomService::getCustomInfo($customId);
  191. CustomClass::valid($info, $this->shopId);
  192. //手动添加的客户经纬度不完善不显示地址,引导完善客户地址
  193. if (isset($info['lat']) && empty($info['lat'])) {
  194. $info['address'] = '';
  195. }
  196. if (isset($info['long']) && empty($info['long'])) {
  197. $info['address'] = '';
  198. }
  199. $mainId = $info['mainId'] ?? 0;
  200. //超管的信息
  201. $superInfo = ShopAdminClass::getManager($mainId);
  202. $superName = $superInfo['name'] ?? '';
  203. $currentSuper = ['name' => $superName];
  204. $info['superInfo'] = $currentSuper;
  205. util::success($info);
  206. }
  207. //允许月结开关 ssh 2021.1.8
  208. public function actionDebt()
  209. {
  210. $get = Yii::$app->request->get();
  211. $debt = isset($get['debt']) ? $get['debt'] : 0;
  212. $customId = isset($get['customId']) ? $get['customId'] : 0;
  213. $custom = CustomClass::getById($customId);
  214. CustomClass::valid($custom, $this->shopId);
  215. CustomClass::debt($custom, $debt);
  216. util::complete();
  217. }
  218. //采购查看权限的黑白名单开关 shizhongqi 2021.08.08
  219. public function actionSetBlack()
  220. {
  221. $black = Yii::$app->request->get('black', 2);
  222. $customId = Yii::$app->request->get('customId', 0);
  223. $custom = CustomClass::getById($customId, true);
  224. CustomClass::valid($custom, $this->shopId);
  225. $custom->black = $black;
  226. $custom->save();
  227. $ghsId = $custom->ghsId ?? 0;
  228. $ghs = GhsClass::getById($ghsId, true);
  229. if (empty($ghs)) {
  230. util::fail('出错了');
  231. }
  232. $ghs->black = $black;
  233. $ghs->save();
  234. util::complete();
  235. }
  236. //欠款客户 ssh 2021.2.4
  237. public function actionDebtList()
  238. {
  239. $get = Yii::$app->request->get();
  240. $where = ['ownShopId' => $this->shopId, 'isDebt' => 1];
  241. if (isset($get['name']) && !empty($get['name'])) {
  242. $where['name'] = ['like', $get['name']];
  243. }
  244. $list = CustomService::getDebtList($where);
  245. //共X个客户,合计欠款XXX元
  246. $main = $this->main;
  247. $list['customNum'] = $main->mayGatheringNum ?? 0;
  248. $list['debtAmount'] = $main->mayGathering ?? 0.00;
  249. util::success($list);
  250. }
  251. //修改欠款额度 ssh 20210625
  252. public function actionUpdateDebtLimit()
  253. {
  254. $shopAdmin = $this->shopAdmin;
  255. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  256. util::fail('管理员才能操作');
  257. }
  258. $get = Yii::$app->request->get();
  259. $id = $get['id'] ?? 0;
  260. $debtLimit = $get['debtLimit'] ?? 0;
  261. if (is_numeric($debtLimit) == false || $debtLimit <= 0) {
  262. util::fail('请填写金额');
  263. }
  264. $custom = CustomClass::getById($id, true);
  265. CustomClass::valid($custom, $this->shopId);
  266. $custom->debtLimit = $debtLimit;
  267. $custom->save();
  268. util::complete();
  269. }
  270. //修改折扣 ssh 20210913
  271. public function actionChangeDiscount()
  272. {
  273. $shopAdmin = $this->shopAdmin;
  274. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  275. util::fail('管理员才能操作');
  276. }
  277. $get = Yii::$app->request->get();
  278. $id = $get['id'] ?? 0;
  279. $discount = $get['discount'] ?? 0;
  280. if (is_numeric($discount) == false || $discount > 1 || $discount == 0) {
  281. util::fail('请填写小数值');
  282. }
  283. $custom = CustomClass::getById($id, true);
  284. CustomClass::valid($custom, $this->shopId);
  285. $custom->discount = $discount;
  286. $custom->save();
  287. $ghsId = $custom->ghsId ?? 0;
  288. $ghs = GhsClass::getById($ghsId, true);
  289. $ghs->giveDiscount = $discount;
  290. $ghs->save();
  291. util::complete();
  292. }
  293. //修改客户等级 ssh 20211007
  294. public function actionChangeLevel()
  295. {
  296. $shopAdmin = $this->shopAdmin;
  297. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  298. util::fail('超管才能修改');
  299. }
  300. $get = Yii::$app->request->get();
  301. $customId = $get['customId'] ?? 0;
  302. $level = $get['level'] ?? 1;
  303. $custom = CustomClass::getById($customId, true);
  304. CustomClass::valid($custom, $this->shopId);
  305. $custom->level = $level;
  306. $custom->save();
  307. $ghsId = $custom->ghsId ?? 0;
  308. $ghs = GhsClass::getById($ghsId, true);
  309. if (empty($ghs)) {
  310. util::fail('出错了');
  311. }
  312. $ghs->giveLevel = $level;
  313. $ghs->save();
  314. $map = CustomClass::$levelMap;
  315. $name = $map[$level] ?? '';
  316. util::success(['levelName' => $name]);
  317. }
  318. }