ItemController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. /**
  3. * 零售端花材管理控制器
  4. * 主要用途:供花店后台管理花材,例如:获取花材列表、隐藏或显示花材、修改或清除花材展示备注说明、限购设置等。
  5. * 使用角色:花店管理员/商户管理员。
  6. * 解决问题:在总店修改或清除花材备注说明时,能跨越直营和加盟店的限制,一键同步更新到所有分店,保证全店备注信息统一。
  7. */
  8. namespace hd\controllers;
  9. use bizHd\shop\classes\ShopClass;
  10. use biz\item\classes\PtItemClass;
  11. use bizHd\item\classes\ItemClass;
  12. use bizGhs\pictext\classes\PicTextGhsItemClass;
  13. use bizHd\custom\classes\CustomClass;
  14. use bizHd\product\classes\ProductClass;
  15. use bizHd\wx\classes\WxOpenClass;
  16. use common\components\dict;
  17. use common\components\imgUtil;
  18. use common\components\miniUtil;
  19. use common\components\PosterUtil;
  20. use common\components\stringUtil;
  21. use common\components\util;
  22. use Yii;
  23. class ItemController extends BaseController
  24. {
  25. //列出所有花材 ssh 20240222
  26. public function actionGetItemList()
  27. {
  28. $get = Yii::$app->request->get();
  29. $search = $get['search'] ?? '';
  30. $requestType = $get['requestType'] ?? 'kd';
  31. $classId = $get['classId'] ?? 0;
  32. $lookStock = $get['lookStock'] ?? 0;
  33. $where['mainId'] = $this->mainId;
  34. if ($requestType == 'kd') {
  35. $where['delStatus'] = 0;
  36. $where['status'] = 1;
  37. } elseif ($requestType == 'itemList') {
  38. $where['delStatus'] = 0;
  39. if ($lookStock == 1) {
  40. $where['stock>'] = 0;
  41. }
  42. if ($lookStock == 2) {
  43. $where['stock'] = 0;
  44. }
  45. unset($where['status']);
  46. } elseif ($requestType == 'changePrice') {
  47. $where['delStatus'] = 0;
  48. $where['stock>'] = 0;
  49. } elseif ($requestType == 'hasStockList') {
  50. $where['delStatus'] = 0;
  51. $where['stock>'] = 0;
  52. } elseif ($requestType == 'book') {
  53. $where['delStatus'] = 0;
  54. unset($where['status']);
  55. } elseif ($requestType == 'outList') {
  56. $where['delStatus'] = 0;
  57. $where['status'] = 2;
  58. $where['removeStatus'] = 0;
  59. } elseif ($requestType == 'stopList') {
  60. $where['delStatus'] = 1;
  61. unset($where['status']);
  62. $where['removeStatus'] = 0;
  63. } elseif ($requestType == 'removeList') {
  64. $where['delStatus'] = 1;
  65. unset($where['status']);
  66. $where['removeStatus'] = 1;
  67. } elseif ($requestType == 'cg') {
  68. $where['delStatus'] = 0;
  69. unset($where['status']);
  70. } elseif ($requestType == 'changeStock') {
  71. $where['delStatus'] = 0;
  72. unset($where['status']);
  73. } elseif ($requestType == 'check') {
  74. $where['delStatus'] = 0;
  75. unset($where['status']);
  76. } elseif ($requestType == 'stockOut') {
  77. $where['delStatus'] = 0;
  78. $where['status'] = 1;
  79. } elseif ($requestType == 'break') {
  80. $where['delStatus'] = 0;
  81. $where['status'] = 1;
  82. } elseif ($requestType == 'part') {
  83. $where['delStatus'] = 0;
  84. $where['status'] = 1;
  85. } else {
  86. util::fail('没有定义的请求方式');
  87. }
  88. if (!empty($search)) {
  89. if (stringUtil::isLetter($search)) {
  90. $search = strtolower($search);
  91. $where['py'] = ['like', $search];
  92. } else {
  93. $where['name'] = ['like', $search];
  94. }
  95. } else {
  96. if (!empty($classId)) {
  97. if ($classId == -1) {
  98. $where['discountPrice>'] = 0;
  99. } elseif ($classId == -2) {
  100. $where['reachNum>'] = 0;
  101. } elseif ($classId == -3) {
  102. $where['presell'] = 1;
  103. } else {
  104. $where['classId'] = $classId;
  105. }
  106. }
  107. }
  108. $customId = $get['customId'] ?? 0;
  109. $custom = CustomClass::getById($customId, true);
  110. if (!empty($custom)) {
  111. if ($custom->shopId != $this->shopId) {
  112. util::fail('不是你的客户哦,编号55236');
  113. }
  114. }
  115. $level = 0;
  116. if($requestType == 'itemList'){
  117. //如果是花材列表页,则显示三个价格,其他地方,比如拆散和报损那些,只显示零售价
  118. $level = 1;
  119. }
  120. $field = '*';
  121. $result = ProductClass::getList($field, $where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC]);
  122. $list = $result['list'] ?? [];
  123. if ($requestType == 'kd') {
  124. $list = ProductClass::kdItemGroup($list, $level);
  125. } elseif ($requestType == 'itemList') {
  126. $list = ProductClass::itemListGroup($list, $level);
  127. } elseif ($requestType == 'changePrice') {
  128. $list = ProductClass::changePriceGroup($list, $level);
  129. } elseif ($requestType == 'break') {
  130. $list = ProductClass::breakItemGroup($list, $level);
  131. } elseif ($requestType == 'part') {
  132. $list = ProductClass::partItemGroup($list, $level);
  133. } elseif ($requestType == 'check') {
  134. $list = ProductClass::checkItemGroup($list, $level);
  135. } elseif ($requestType == 'cg') {
  136. $list = ProductClass::cgItemGroup($list, $level);
  137. } elseif ($requestType == 'changeStock') {
  138. $list = ProductClass::changeStockGroup($list, $level);
  139. } elseif ($requestType == 'stockOut') {
  140. $list = ProductClass::stockOutGroup($list, $level);
  141. } else {
  142. util::fail('没有数据');
  143. }
  144. $result['list'] = $list;
  145. util::success($result);
  146. }
  147. //增加和减少半扎 ssh 20250417
  148. public function actionChangeHalf()
  149. {
  150. util::fail('此功能已停用');
  151. $post = Yii::$app->request->post();
  152. $productId = $post['productId'] ?? '';
  153. $add = $post['add'] ?? 0;
  154. $remark = $post['remark'] ?? '';
  155. $product = ProductClass::getLockById($productId);
  156. if (empty($product)) {
  157. util::fail('没有找到花材');
  158. }
  159. if ($product->mainId != $this->mainId) {
  160. util::fail('不是你的花材');
  161. }
  162. if ($product->ratioType == 1) {
  163. util::fail('若干扎的花材不能减半份');
  164. }
  165. //避免重复提交
  166. util::checkRepeatCommit($this->adminId, 4);
  167. $connection = Yii::$app->db;
  168. $transaction = $connection->beginTransaction();
  169. try {
  170. $shop = $this->shop;
  171. $staff = $this->shopAdmin;
  172. $staffName = $staff->name ?? '';
  173. $staffId = $staff->id;
  174. $adminId = $this->adminId;
  175. $params = [
  176. 'staffId' => $staffId,
  177. 'staffName' => $staffName,
  178. 'adminId' => $adminId,
  179. 'remark' => $remark,
  180. ];
  181. ItemClass::modifyHalf($shop, $product, $params, $add);
  182. $product = ProductClass::getLockById($productId);
  183. $stock = $product->stock;
  184. $stock = floatval($stock);
  185. $transaction->commit();
  186. util::success(['stock' => $stock]);
  187. } catch (\Exception $e) {
  188. $transaction->rollBack();
  189. Yii::info("加减半份失败原因:" . $e->getMessage());
  190. util::fail('加减半份失败');
  191. }
  192. }
  193. //检测是否要刷新
  194. public function actionCheckRefresh()
  195. {
  196. $mainId = $this->mainId;
  197. $staffId = $this->shopAdminId;
  198. $respond = Yii::$app->redis->executeCommand('GET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
  199. if ($respond == 'yes') {
  200. util::success(['remind' => 1]);
  201. }
  202. util::complete();
  203. }
  204. //列出所有花材,包括特价花材列表 ssh 20220315
  205. public function actionShowList()
  206. {
  207. $headers = Yii::$app->getRequest()->getHeaders();
  208. $requestVersion = $headers->get('appVersion');
  209. $currentVersion = dict::getDict('appVersion');
  210. if ($requestVersion < $currentVersion) {
  211. util::fail('请先升级系统');
  212. }
  213. $get = Yii::$app->request->get();
  214. $isCashier = $get['isCashier'] ?? 0;
  215. $mainId = $this->mainId;
  216. if ($isCashier == 1) {
  217. //去掉收银台提示价格更新
  218. $staffId = $this->shopAdminId;
  219. Yii::$app->redis->executeCommand('DEL', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
  220. }
  221. $where['mainId'] = $mainId;
  222. $where['delStatus'] = 0;
  223. $where['status'] = 1;
  224. $list = \bizHd\item\classes\ItemClass::getShowList($where);
  225. $productList = [];
  226. if (!empty($list)) {
  227. foreach ($list as $key => $item) {
  228. if (!empty($item)) {
  229. $productList = array_merge($productList, $item);
  230. }
  231. }
  232. }
  233. util::success(['list' => $list, 'productList' => $productList]);
  234. }
  235. public function actionAdd()
  236. {
  237. util::fail('开发中');
  238. }
  239. public function actionBatchAdd()
  240. {
  241. util::fail('开发中');
  242. }
  243. public function actionDelete()
  244. {
  245. util::fail('不能删除哦!');
  246. }
  247. //获取平台里的花材信息
  248. public function actionPlatformItem()
  249. {
  250. $py = Yii::$app->request->get('py', '');
  251. $where = [];
  252. if ($py) {
  253. $where = ['py' => $py];
  254. }
  255. $list = PtItemClass::getItemList($where);
  256. util::success($list);
  257. }
  258. //获取给散客下单的花材海报 ssh 20220111
  259. public function actionGetPoster()
  260. {
  261. $get = Yii::$app->request->get();
  262. $id = $get['id'] ?? 0;
  263. $item = ItemClass::getById($id, true);
  264. if (empty($item) || $item->mainId != $this->mainId) {
  265. util::fail('获取失败');
  266. }
  267. $cover = $item->cover ?? '';
  268. if (empty($cover)) {
  269. util::fail('获取失败');
  270. }
  271. $cover = imgUtil::normalizeOssObjectPath($cover);
  272. $merchant = WxOpenClass::getMallWxInfo();
  273. $page = 'pages/item/detail';
  274. $ptStyle = dict::getDict('ptStyle', 'mall');
  275. $shop = $this->shop;
  276. $shopId = $shop->id ?? 0;
  277. //scene不能超过32个字条
  278. $scene = "id=" . $id . '&a=' . $shopId;
  279. $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
  280. $miniCode = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle, $envVersion);
  281. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  282. $prefix = imgUtil::getPrefix();
  283. $level = 0;
  284. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  285. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  286. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($item, $level, $priceMap, $addPriceMap);
  287. $staff = $this->shopAdmin;
  288. $staffName = $staff->name ?? '';
  289. $url = PosterUtil::buildMallItemPosterUrl($prefix, PosterUtil::withShopAndRemark([
  290. 'cover' => $cover,
  291. 'miniCode' => $miniCode,
  292. 'priceText' => '¥' . floatval($price),
  293. 'name' => $item->name ?? '',
  294. 'staffText' => $staffName . ' 为您推荐',
  295. 'mainId' => $this->mainId,
  296. ], $this->shop, $this->sj ?? null, $item));
  297. util::success(['imgUrl' => $url]);
  298. }
  299. //获取花材描述
  300. public function actionDesc()
  301. {
  302. $itemId = Yii::$app->request->get('itemId', 0);
  303. $picText = PicTextGhsItemClass::getByCondition(['mainId'=> $this->mainId, 'ghsItemId' => $itemId, 'delStatus' => 0], true);
  304. if (empty($picText)) {
  305. // 适配前端的返回处理 -- 本来要使用 util::fail()
  306. util::success('not_exist');
  307. }
  308. if ($picText->mainId != $this->mainId) {
  309. util::fail('没有找到描述');
  310. }
  311. util::success($picText);
  312. }
  313. //创建花材描述(花材介绍)
  314. public function actionCreateDesc()
  315. {
  316. $form = new \ghs\models\item\CreateDescForm(); // 暂时使用同一份 ghs,项目独立时,再复制一份
  317. $form->load(Yii::$app->request->post(), '');
  318. if (!$form->validateForm()) {
  319. return;
  320. }
  321. $data = $form->attributes;
  322. $exists = PicTextGhsItemClass::exists(['mainId'=>$this->mainId, 'ghsItemId' => $data['itemId'], 'delStatus' => 0]);
  323. if ($exists) {
  324. util::fail('该商品已存在图文内容,请勿重复添加');
  325. }
  326. $data['mainId'] = $this->mainId;
  327. $data['ghsItemId'] = $data['itemId'];
  328. $result = PicTextGhsItemClass::add($data);
  329. if ($result) {
  330. util::success($result);
  331. }
  332. util::fail('创建失败');
  333. }
  334. //更新花材描述(花材介绍)
  335. public function actionUpdateDesc()
  336. {
  337. $form = new \ghs\models\item\CreateDescForm();
  338. $form->load(Yii::$app->request->post(), '');
  339. if (!$form->validateForm()) {
  340. return;
  341. }
  342. $data = $form->attributes;
  343. $id = $data['id'];
  344. $info = PicTextGhsItemClass::getById($id, false, 'id');
  345. if (empty($info)) {
  346. util::fail('描述不存在');
  347. }
  348. $data['mainId'] = $this->mainId;
  349. $result = PicTextGhsItemClass::updateById($id, $data);
  350. if ($result) {
  351. util::success($result);
  352. }
  353. util::fail('更新失败');
  354. }
  355. public function actionChangeFrontHide()
  356. {
  357. $get = Yii::$app->request->get();
  358. $id = $get['id'] ?? 0;
  359. $status = $get['status'] ?? '';
  360. if (is_numeric($status) == false) {
  361. util::fail('请选择方式');
  362. }
  363. $info = ItemClass::getById($id, true);
  364. if (empty($info)) {
  365. util::fail('没有找到花材');
  366. }
  367. if ($info->mainId != $this->mainId) {
  368. util::fail('不是你的花材呢');
  369. }
  370. $info->frontHide = $status;
  371. $info->save();
  372. util::complete();
  373. }
  374. public function actionChangeVirtualStock()
  375. {
  376. $get = Yii::$app->request->get();
  377. $id = $get['id'] ?? 0;
  378. $virtualStock = $get['virtualStock'] ?? 0;
  379. $item = ItemClass::getById($id, true);
  380. if (empty($item)) {
  381. util::fail('没有找到花材');
  382. }
  383. if ($item->mainId != $this->mainId) {
  384. util::fail('不是你的花材');
  385. }
  386. $item->virtualStock = $virtualStock;
  387. $item->save();
  388. util::complete('修改成功');
  389. }
  390. //取消花材全部客户的已限数,重新开始计算
  391. public function actionClearLimitBuy()
  392. {
  393. $get = Yii::$app->request->get();
  394. $id = $get['id'] ?? '';
  395. $item = ItemClass::getById($id, true);
  396. if (empty($item)) {
  397. util::fail('没有找到花材');
  398. }
  399. if ($item->mainId != $this->mainId) {
  400. util::fail('不是你的花材');
  401. }
  402. ProductClass::clearLimitBuyCache($id);
  403. util::complete('清除成功');
  404. }
  405. //清空单个客户的已购
  406. public function actionClearOneLimitBuy()
  407. {
  408. $get = Yii::$app->request->get();
  409. $productId = $get['productId'] ?? '';
  410. $customId = $get['customId'] ?? 0;
  411. $item = ItemClass::getById($productId, true);
  412. if (empty($item)) {
  413. util::fail('没有找到花材');
  414. }
  415. if ($item->mainId != $this->mainId) {
  416. util::fail('不是你的花材');
  417. }
  418. $custom = CustomClass::getById($customId, true);
  419. if (empty($custom)) {
  420. util::fail('没有客户');
  421. }
  422. if ($custom->shopId != $this->shopId) {
  423. util::fail('不是你的客户');
  424. }
  425. ProductClass::baseClearLimitBuy($productId, $customId);
  426. util::complete('清除成功');
  427. }
  428. //零售端--花材已购客户列表
  429. public function actionHasLimitBuyCustomList()
  430. {
  431. $get = Yii::$app->request->get();
  432. $id = $get['id'] ?? '';
  433. $item = ItemClass::getById($id, true);
  434. if (empty($item)) {
  435. util::fail('没有找到花材');
  436. }
  437. if ($item->mainId != $this->mainId) {
  438. util::fail('不是你的花材');
  439. }
  440. $customList = ProductClass::getHasLimitBuyList($item);
  441. util::success(['customList' => $customList]);
  442. }
  443. /**
  444. * 修改花材展示备注说明
  445. * 职责:更新指定花材的备注内容。如果当前店铺是总店,则需将修改同步到商户下的所有分店(不论直营还是加盟店)。
  446. * 入参:id (花材ID,来自 GET 传参), itemRemark (备注内容,来自 GET 传参)
  447. * 返回:保存成功提示(JSON 格式)
  448. * 副作用:会更新该商家下所有分店对应花材的备注说明。
  449. */
  450. public function actionUpdateItemRemark()
  451. {
  452. $get = Yii::$app->request->get();
  453. $id = $get['id'] ?? 0;
  454. $itemRemark = trim($get['itemRemark'] ?? '');
  455. if (empty($itemRemark)) {
  456. util::fail('请填写备注');
  457. }
  458. if (mb_strlen($itemRemark) > 200) {
  459. util::fail('备注不能超过200字');
  460. }
  461. $info = ItemClass::getById($id, true);
  462. if (empty($info)) {
  463. util::fail('没有找到花材');
  464. }
  465. if ($info->mainId != $this->mainId) {
  466. util::fail('无法操作');
  467. }
  468. $info->itemRemark = $itemRemark;
  469. $info->save();
  470. // 复杂分支:若当前店铺是总店(default = 1),不管分店是直营还是加盟,都需要将修改后的花材备注同步至旗下所有分店,保证全渠道花材备注的一致性
  471. $shop = $this->shop;
  472. if (isset($shop->default) && $shop->default == 1) {
  473. $sjId = $shop->sjId ?? 0;
  474. $ptItemId = $info->itemId ?? 0;
  475. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  476. foreach ($chainShopList as $chainShop) {
  477. $chainShopId = $chainShop->id ?? 0;
  478. if ($chainShopId == $shop->id) {
  479. continue;
  480. }
  481. $chainMainId = $chainShop->mainId ?? 0;
  482. $chainProduct = ItemClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  483. if (empty($chainProduct)) {
  484. continue;
  485. }
  486. $chainProduct->itemRemark = $itemRemark;
  487. $chainProduct->save();
  488. }
  489. }
  490. util::complete('保存成功');
  491. }
  492. /**
  493. * 清除花材展示备注说明
  494. * 职责:清空指定花材的备注内容。如果当前店铺是总店,则需同步清除商户下的所有分店(不论直营还是加盟店)对应花材的备注内容。
  495. * 入参:id (花材ID,来自 GET 传参)
  496. * 返回:清除成功提示(JSON 格式)
  497. * 副作用:会清空该商家下所有分店对应花材的备注说明。
  498. */
  499. public function actionClearItemRemark()
  500. {
  501. $get = Yii::$app->request->get();
  502. $id = $get['id'] ?? 0;
  503. $info = ItemClass::getById($id, true);
  504. if (empty($info)) {
  505. util::fail('没有找到花材');
  506. }
  507. if ($info->mainId != $this->mainId) {
  508. util::fail('无法操作');
  509. }
  510. $info->itemRemark = '';
  511. $info->save();
  512. // 复杂分支:若当前店铺是总店(default = 1),不管分店是直营还是加盟,都需要同步清空其花材备注说明,确保全渠道数据同步与统一
  513. $shop = $this->shop;
  514. if (isset($shop->default) && $shop->default == 1) {
  515. $sjId = $shop->sjId ?? 0;
  516. $ptItemId = $info->itemId ?? 0;
  517. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  518. foreach ($chainShopList as $chainShop) {
  519. $chainShopId = $chainShop->id ?? 0;
  520. if ($chainShopId == $shop->id) {
  521. continue;
  522. }
  523. $chainMainId = $chainShop->mainId ?? 0;
  524. $chainProduct = ItemClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  525. if (empty($chainProduct)) {
  526. continue;
  527. }
  528. $chainProduct->itemRemark = '';
  529. $chainProduct->save();
  530. }
  531. }
  532. util::complete('清除成功');
  533. }
  534. }