ItemController.php 17 KB

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