ItemController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 == 'outList') {
  128. // 已下架列表:字段加工与花材列表一致(原先落入 else 导致接口失败)
  129. $list = ProductClass::itemListGroup($list, 1);
  130. } elseif ($requestType == 'changePrice') {
  131. $list = ProductClass::changePriceGroup($list, $level);
  132. } elseif ($requestType == 'break') {
  133. $list = ProductClass::breakItemGroup($list, $level);
  134. } elseif ($requestType == 'part') {
  135. $list = ProductClass::partItemGroup($list, $level);
  136. } elseif ($requestType == 'check') {
  137. $list = ProductClass::checkItemGroup($list, $level);
  138. } elseif ($requestType == 'cg') {
  139. $list = ProductClass::cgItemGroup($list, $level);
  140. } elseif ($requestType == 'changeStock') {
  141. $list = ProductClass::changeStockGroup($list, $level);
  142. } elseif ($requestType == 'stockOut') {
  143. $list = ProductClass::stockOutGroup($list, $level);
  144. } else {
  145. util::fail('没有数据');
  146. }
  147. $result['list'] = $list;
  148. util::success($result);
  149. }
  150. //增加和减少半扎 ssh 20250417
  151. public function actionChangeHalf()
  152. {
  153. util::fail('此功能已停用');
  154. $post = Yii::$app->request->post();
  155. $productId = $post['productId'] ?? '';
  156. $add = $post['add'] ?? 0;
  157. $remark = $post['remark'] ?? '';
  158. $product = ProductClass::getLockById($productId);
  159. if (empty($product)) {
  160. util::fail('没有找到花材');
  161. }
  162. if ($product->mainId != $this->mainId) {
  163. util::fail('不是你的花材');
  164. }
  165. if ($product->ratioType == 1) {
  166. util::fail('若干扎的花材不能减半份');
  167. }
  168. //避免重复提交
  169. util::checkRepeatCommit($this->adminId, 4);
  170. $connection = Yii::$app->db;
  171. $transaction = $connection->beginTransaction();
  172. try {
  173. $shop = $this->shop;
  174. $staff = $this->shopAdmin;
  175. $staffName = $staff->name ?? '';
  176. $staffId = $staff->id;
  177. $adminId = $this->adminId;
  178. $params = [
  179. 'staffId' => $staffId,
  180. 'staffName' => $staffName,
  181. 'adminId' => $adminId,
  182. 'remark' => $remark,
  183. ];
  184. ItemClass::modifyHalf($shop, $product, $params, $add);
  185. $product = ProductClass::getLockById($productId);
  186. $stock = $product->stock;
  187. $stock = floatval($stock);
  188. $transaction->commit();
  189. util::success(['stock' => $stock]);
  190. } catch (\Exception $e) {
  191. $transaction->rollBack();
  192. Yii::info("加减半份失败原因:" . $e->getMessage());
  193. util::fail('加减半份失败');
  194. }
  195. }
  196. //检测是否要刷新
  197. public function actionCheckRefresh()
  198. {
  199. $mainId = $this->mainId;
  200. $staffId = $this->shopAdminId;
  201. $respond = Yii::$app->redis->executeCommand('GET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
  202. if ($respond == 'yes') {
  203. util::success(['remind' => 1]);
  204. }
  205. util::complete();
  206. }
  207. //列出所有花材,包括特价花材列表 ssh 20220315
  208. public function actionShowList()
  209. {
  210. $headers = Yii::$app->getRequest()->getHeaders();
  211. $requestVersion = $headers->get('appVersion');
  212. $currentVersion = dict::getDict('appVersion');
  213. if ($requestVersion < $currentVersion) {
  214. util::fail('请先升级系统');
  215. }
  216. $get = Yii::$app->request->get();
  217. $isCashier = $get['isCashier'] ?? 0;
  218. $mainId = $this->mainId;
  219. if ($isCashier == 1) {
  220. //去掉收银台提示价格更新
  221. $staffId = $this->shopAdminId;
  222. Yii::$app->redis->executeCommand('DEL', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
  223. }
  224. $where['mainId'] = $mainId;
  225. $where['delStatus'] = 0;
  226. $where['status'] = 1;
  227. $list = \bizHd\item\classes\ItemClass::getShowList($where);
  228. $productList = [];
  229. if (!empty($list)) {
  230. foreach ($list as $key => $item) {
  231. if (!empty($item)) {
  232. $productList = array_merge($productList, $item);
  233. }
  234. }
  235. }
  236. util::success(['list' => $list, 'productList' => $productList]);
  237. }
  238. public function actionAdd()
  239. {
  240. util::fail('开发中');
  241. }
  242. public function actionBatchAdd()
  243. {
  244. util::fail('开发中');
  245. }
  246. public function actionDelete()
  247. {
  248. util::fail('不能删除哦!');
  249. }
  250. //获取平台里的花材信息
  251. public function actionPlatformItem()
  252. {
  253. $py = Yii::$app->request->get('py', '');
  254. $where = [];
  255. if ($py) {
  256. $where = ['py' => $py];
  257. }
  258. $list = PtItemClass::getItemList($where);
  259. util::success($list);
  260. }
  261. //获取给散客下单的花材海报 ssh 20220111
  262. public function actionGetPoster()
  263. {
  264. $get = Yii::$app->request->get();
  265. $id = $get['id'] ?? 0;
  266. $item = ItemClass::getById($id, true);
  267. if (empty($item) || $item->mainId != $this->mainId) {
  268. util::fail('获取失败');
  269. }
  270. $cover = $item->cover ?? '';
  271. if (empty($cover)) {
  272. util::fail('获取失败');
  273. }
  274. $cover = imgUtil::normalizeOssObjectPath($cover);
  275. $merchant = WxOpenClass::getMallWxInfo();
  276. $page = 'pages/item/detail';
  277. $ptStyle = dict::getDict('ptStyle', 'mall');
  278. $shop = $this->shop;
  279. $shopId = $shop->id ?? 0;
  280. //scene不能超过32个字条
  281. $scene = "id=" . $id . '&a=' . $shopId;
  282. $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
  283. $miniCode = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle, $envVersion);
  284. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  285. $prefix = imgUtil::getPrefix();
  286. $level = 0;
  287. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  288. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  289. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($item, $level, $priceMap, $addPriceMap);
  290. $staff = $this->shopAdmin;
  291. $staffName = $staff->name ?? '';
  292. $url = PosterUtil::buildMallItemPosterUrl($prefix, PosterUtil::withShopAndRemark([
  293. 'cover' => $cover,
  294. 'miniCode' => $miniCode,
  295. 'priceText' => '¥' . floatval($price),
  296. 'name' => $item->name ?? '',
  297. 'staffText' => $staffName . ' 为您推荐',
  298. 'mainId' => $this->mainId,
  299. ], $this->shop, $this->sj ?? null, $item));
  300. util::success(['imgUrl' => $url]);
  301. }
  302. //获取花材描述
  303. public function actionDesc()
  304. {
  305. $itemId = Yii::$app->request->get('itemId', 0);
  306. $picText = PicTextGhsItemClass::getByCondition(['mainId'=> $this->mainId, 'ghsItemId' => $itemId, 'delStatus' => 0], true);
  307. if (empty($picText)) {
  308. // 适配前端的返回处理 -- 本来要使用 util::fail()
  309. util::success('not_exist');
  310. }
  311. if ($picText->mainId != $this->mainId) {
  312. util::fail('没有找到描述');
  313. }
  314. util::success($picText);
  315. }
  316. //创建花材描述(花材介绍)
  317. public function actionCreateDesc()
  318. {
  319. $form = new \ghs\models\item\CreateDescForm(); // 暂时使用同一份 ghs,项目独立时,再复制一份
  320. $form->load(Yii::$app->request->post(), '');
  321. if (!$form->validateForm()) {
  322. return;
  323. }
  324. $data = $form->attributes;
  325. $exists = PicTextGhsItemClass::exists(['mainId'=>$this->mainId, 'ghsItemId' => $data['itemId'], 'delStatus' => 0]);
  326. if ($exists) {
  327. util::fail('该商品已存在图文内容,请勿重复添加');
  328. }
  329. $data['mainId'] = $this->mainId;
  330. $data['ghsItemId'] = $data['itemId'];
  331. $result = PicTextGhsItemClass::add($data);
  332. if ($result) {
  333. util::success($result);
  334. }
  335. util::fail('创建失败');
  336. }
  337. //更新花材描述(花材介绍)
  338. public function actionUpdateDesc()
  339. {
  340. $form = new \ghs\models\item\CreateDescForm();
  341. $form->load(Yii::$app->request->post(), '');
  342. if (!$form->validateForm()) {
  343. return;
  344. }
  345. $data = $form->attributes;
  346. $id = $data['id'];
  347. $info = PicTextGhsItemClass::getById($id, false, 'id');
  348. if (empty($info)) {
  349. util::fail('描述不存在');
  350. }
  351. $data['mainId'] = $this->mainId;
  352. $result = PicTextGhsItemClass::updateById($id, $data);
  353. if ($result) {
  354. util::success($result);
  355. }
  356. util::fail('更新失败');
  357. }
  358. public function actionChangeFrontHide()
  359. {
  360. $get = Yii::$app->request->get();
  361. $id = $get['id'] ?? 0;
  362. $status = $get['status'] ?? '';
  363. if (is_numeric($status) == false) {
  364. util::fail('请选择方式');
  365. }
  366. $info = ItemClass::getById($id, true);
  367. if (empty($info)) {
  368. util::fail('没有找到花材');
  369. }
  370. if ($info->mainId != $this->mainId) {
  371. util::fail('不是你的花材呢');
  372. }
  373. $info->frontHide = $status;
  374. $info->save();
  375. util::complete();
  376. }
  377. public function actionChangeVirtualStock()
  378. {
  379. $get = Yii::$app->request->get();
  380. $id = $get['id'] ?? 0;
  381. $virtualStock = $get['virtualStock'] ?? 0;
  382. $item = ItemClass::getById($id, true);
  383. if (empty($item)) {
  384. util::fail('没有找到花材');
  385. }
  386. if ($item->mainId != $this->mainId) {
  387. util::fail('不是你的花材');
  388. }
  389. $item->virtualStock = $virtualStock;
  390. $item->save();
  391. util::complete('修改成功');
  392. }
  393. //取消花材全部客户的已限数,重新开始计算
  394. public function actionClearLimitBuy()
  395. {
  396. $get = Yii::$app->request->get();
  397. $id = $get['id'] ?? '';
  398. $item = ItemClass::getById($id, true);
  399. if (empty($item)) {
  400. util::fail('没有找到花材');
  401. }
  402. if ($item->mainId != $this->mainId) {
  403. util::fail('不是你的花材');
  404. }
  405. ProductClass::clearLimitBuyCache($id);
  406. util::complete('清除成功');
  407. }
  408. //清空单个客户的已购
  409. public function actionClearOneLimitBuy()
  410. {
  411. $get = Yii::$app->request->get();
  412. $productId = $get['productId'] ?? '';
  413. $customId = $get['customId'] ?? 0;
  414. $item = ItemClass::getById($productId, true);
  415. if (empty($item)) {
  416. util::fail('没有找到花材');
  417. }
  418. if ($item->mainId != $this->mainId) {
  419. util::fail('不是你的花材');
  420. }
  421. $custom = CustomClass::getById($customId, true);
  422. if (empty($custom)) {
  423. util::fail('没有客户');
  424. }
  425. if ($custom->shopId != $this->shopId) {
  426. util::fail('不是你的客户');
  427. }
  428. ProductClass::baseClearLimitBuy($productId, $customId);
  429. util::complete('清除成功');
  430. }
  431. //零售端--花材已购客户列表
  432. public function actionHasLimitBuyCustomList()
  433. {
  434. $get = Yii::$app->request->get();
  435. $id = $get['id'] ?? '';
  436. $item = ItemClass::getById($id, true);
  437. if (empty($item)) {
  438. util::fail('没有找到花材');
  439. }
  440. if ($item->mainId != $this->mainId) {
  441. util::fail('不是你的花材');
  442. }
  443. $customList = ProductClass::getHasLimitBuyList($item);
  444. util::success(['customList' => $customList]);
  445. }
  446. /**
  447. * 修改花材展示备注说明
  448. * 职责:更新指定花材的备注内容。如果当前店铺是总店,则需将修改同步到商户下的所有分店(不论直营还是加盟店)。
  449. * 入参:id (花材ID,来自 GET 传参), itemRemark (备注内容,来自 GET 传参)
  450. * 返回:保存成功提示(JSON 格式)
  451. * 副作用:会更新该商家下所有分店对应花材的备注说明。
  452. */
  453. public function actionUpdateItemRemark()
  454. {
  455. $get = Yii::$app->request->get();
  456. $id = $get['id'] ?? 0;
  457. $itemRemark = trim($get['itemRemark'] ?? '');
  458. if (empty($itemRemark)) {
  459. util::fail('请填写备注');
  460. }
  461. if (mb_strlen($itemRemark) > 200) {
  462. util::fail('备注不能超过200字');
  463. }
  464. $info = ItemClass::getById($id, true);
  465. if (empty($info)) {
  466. util::fail('没有找到花材');
  467. }
  468. if ($info->mainId != $this->mainId) {
  469. util::fail('无法操作');
  470. }
  471. $info->itemRemark = $itemRemark;
  472. $info->save();
  473. // 复杂分支:若当前店铺是总店(default = 1),不管分店是直营还是加盟,都需要将修改后的花材备注同步至旗下所有分店,保证全渠道花材备注的一致性
  474. $shop = $this->shop;
  475. if (isset($shop->default) && $shop->default == 1) {
  476. $sjId = $shop->sjId ?? 0;
  477. $ptItemId = $info->itemId ?? 0;
  478. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  479. foreach ($chainShopList as $chainShop) {
  480. $chainShopId = $chainShop->id ?? 0;
  481. if ($chainShopId == $shop->id) {
  482. continue;
  483. }
  484. $chainMainId = $chainShop->mainId ?? 0;
  485. $chainProduct = ItemClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  486. if (empty($chainProduct)) {
  487. continue;
  488. }
  489. $chainProduct->itemRemark = $itemRemark;
  490. $chainProduct->save();
  491. }
  492. }
  493. util::complete('保存成功');
  494. }
  495. /**
  496. * 清除花材展示备注说明
  497. * 职责:清空指定花材的备注内容。如果当前店铺是总店,则需同步清除商户下的所有分店(不论直营还是加盟店)对应花材的备注内容。
  498. * 入参:id (花材ID,来自 GET 传参)
  499. * 返回:清除成功提示(JSON 格式)
  500. * 副作用:会清空该商家下所有分店对应花材的备注说明。
  501. */
  502. public function actionClearItemRemark()
  503. {
  504. $get = Yii::$app->request->get();
  505. $id = $get['id'] ?? 0;
  506. $info = ItemClass::getById($id, true);
  507. if (empty($info)) {
  508. util::fail('没有找到花材');
  509. }
  510. if ($info->mainId != $this->mainId) {
  511. util::fail('无法操作');
  512. }
  513. $info->itemRemark = '';
  514. $info->save();
  515. // 复杂分支:若当前店铺是总店(default = 1),不管分店是直营还是加盟,都需要同步清空其花材备注说明,确保全渠道数据同步与统一
  516. $shop = $this->shop;
  517. if (isset($shop->default) && $shop->default == 1) {
  518. $sjId = $shop->sjId ?? 0;
  519. $ptItemId = $info->itemId ?? 0;
  520. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  521. foreach ($chainShopList as $chainShop) {
  522. $chainShopId = $chainShop->id ?? 0;
  523. if ($chainShopId == $shop->id) {
  524. continue;
  525. }
  526. $chainMainId = $chainShop->mainId ?? 0;
  527. $chainProduct = ItemClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  528. if (empty($chainProduct)) {
  529. continue;
  530. }
  531. $chainProduct->itemRemark = '';
  532. $chainProduct->save();
  533. }
  534. }
  535. util::complete('清除成功');
  536. }
  537. }