ItemController.php 17 KB

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