ItemController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. $field = '*';
  109. $result = ProductClass::getList($field, $where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC]);
  110. $list = $result['list'] ?? [];
  111. if ($requestType == 'kd') {
  112. $list = ProductClass::kdItemGroup($list, $level);
  113. } elseif ($requestType == 'itemList') {
  114. $list = ProductClass::itemListGroup($list, $level);
  115. } elseif ($requestType == 'changePrice') {
  116. $list = ProductClass::changePriceGroup($list, $level);
  117. } elseif ($requestType == 'break') {
  118. $list = ProductClass::breakItemGroup($list, $level);
  119. } elseif ($requestType == 'part') {
  120. $list = ProductClass::partItemGroup($list, $level);
  121. } elseif ($requestType == 'check') {
  122. $list = ProductClass::checkItemGroup($list, $level);
  123. } elseif ($requestType == 'cg') {
  124. $list = ProductClass::cgItemGroup($list, $level);
  125. } elseif ($requestType == 'changeStock') {
  126. $list = ProductClass::changeStockGroup($list, $level);
  127. } elseif ($requestType == 'stockOut') {
  128. $list = ProductClass::stockOutGroup($list, $level);
  129. } else {
  130. util::fail('没有数据');
  131. }
  132. $result['list'] = $list;
  133. util::success($result);
  134. }
  135. //增加和减少半扎 ssh 20250417
  136. public function actionChangeHalf()
  137. {
  138. util::fail('此功能已停用');
  139. $post = Yii::$app->request->post();
  140. $productId = $post['productId'] ?? '';
  141. $add = $post['add'] ?? 0;
  142. $remark = $post['remark'] ?? '';
  143. $product = ProductClass::getLockById($productId);
  144. if (empty($product)) {
  145. util::fail('没有找到花材');
  146. }
  147. if ($product->mainId != $this->mainId) {
  148. util::fail('不是你的花材');
  149. }
  150. if ($product->ratioType == 1) {
  151. util::fail('若干扎的花材不能减半份');
  152. }
  153. //避免重复提交
  154. util::checkRepeatCommit($this->adminId, 4);
  155. $connection = Yii::$app->db;
  156. $transaction = $connection->beginTransaction();
  157. try {
  158. $shop = $this->shop;
  159. $staff = $this->shopAdmin;
  160. $staffName = $staff->name ?? '';
  161. $staffId = $staff->id;
  162. $adminId = $this->adminId;
  163. $params = [
  164. 'staffId' => $staffId,
  165. 'staffName' => $staffName,
  166. 'adminId' => $adminId,
  167. 'remark' => $remark,
  168. ];
  169. ItemClass::modifyHalf($shop, $product, $params, $add);
  170. $product = ProductClass::getLockById($productId);
  171. $stock = $product->stock;
  172. $stock = floatval($stock);
  173. $transaction->commit();
  174. util::success(['stock' => $stock]);
  175. } catch (\Exception $e) {
  176. $transaction->rollBack();
  177. Yii::info("加减半份失败原因:" . $e->getMessage());
  178. util::fail('加减半份失败');
  179. }
  180. }
  181. //检测是否要刷新
  182. public function actionCheckRefresh()
  183. {
  184. $mainId = $this->mainId;
  185. $staffId = $this->shopAdminId;
  186. $respond = Yii::$app->redis->executeCommand('GET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
  187. if ($respond == 'yes') {
  188. util::success(['remind' => 1]);
  189. }
  190. util::complete();
  191. }
  192. //列出所有花材,包括特价花材列表 ssh 20220315
  193. public function actionShowList()
  194. {
  195. $headers = Yii::$app->getRequest()->getHeaders();
  196. $requestVersion = $headers->get('appVersion');
  197. $currentVersion = dict::getDict('appVersion');
  198. if ($requestVersion < $currentVersion) {
  199. util::fail('请先升级系统');
  200. }
  201. $get = Yii::$app->request->get();
  202. $isCashier = $get['isCashier'] ?? 0;
  203. $mainId = $this->mainId;
  204. if ($isCashier == 1) {
  205. //去掉收银台提示价格更新
  206. $staffId = $this->shopAdminId;
  207. Yii::$app->redis->executeCommand('DEL', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
  208. }
  209. $where['mainId'] = $mainId;
  210. $where['delStatus'] = 0;
  211. $where['status'] = 1;
  212. $list = \bizHd\item\classes\ItemClass::getShowList($where);
  213. $productList = [];
  214. if (!empty($list)) {
  215. foreach ($list as $key => $item) {
  216. if (!empty($item)) {
  217. $productList = array_merge($productList, $item);
  218. }
  219. }
  220. }
  221. util::success(['list' => $list, 'productList' => $productList]);
  222. }
  223. public function actionAdd()
  224. {
  225. util::fail('开发中');
  226. }
  227. public function actionBatchAdd()
  228. {
  229. util::fail('开发中');
  230. }
  231. public function actionDelete()
  232. {
  233. util::fail('不能删除哦!');
  234. }
  235. //获取平台里的花材信息
  236. public function actionPlatformItem()
  237. {
  238. $py = Yii::$app->request->get('py', '');
  239. $where = [];
  240. if ($py) {
  241. $where = ['py' => $py];
  242. }
  243. $list = PtItemClass::getItemList($where);
  244. util::success($list);
  245. }
  246. //获取给散客下单的花材海报 ssh 20220111
  247. public function actionGetPoster()
  248. {
  249. $get = Yii::$app->request->get();
  250. $id = $get['id'] ?? 0;
  251. $item = ItemClass::getById($id, true);
  252. if (empty($item) || $item->mainId != $this->mainId) {
  253. util::fail('获取失败');
  254. }
  255. $cover = $item->cover ?? '';
  256. if (empty($cover)) {
  257. util::fail('获取失败');
  258. }
  259. $merchant = WxOpenClass::getMallWxInfo();
  260. $page = 'pages/item/detail';
  261. $ptStyle = dict::getDict('ptStyle', 'mall');
  262. $shop = $this->shop;
  263. $shopId = $shop->id ?? 0;
  264. //scene不能超过32个字条
  265. $scene = "id=" . $id . '&a=' . $shopId;
  266. $miniCode = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle);
  267. $miniCode = $miniCode . '?x-oss-process=image/resize,m_fill,h_200,w_200';
  268. $miniCodeBase64 = stringUtil::ossBase64($miniCode);
  269. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  270. $prefix = imgUtil::getPrefix();
  271. $price = $item->skPrice ?? 0;
  272. if ($item->discountPrice > 0) {
  273. $skMore = $item->skMore ?? 0;
  274. $price = bcadd($item->discountPrice, $skMore, 2);
  275. }
  276. $newPrice = '¥' . floatval($price);
  277. $priceBase64 = stringUtil::ossBase64($newPrice);
  278. $name = $item->name ?? '';
  279. $nameBase64 = stringUtil::ossBase64($name);
  280. $staff = $this->shopAdmin;
  281. $staffName = $staff->name ?? '';
  282. $newStaffName = $staffName . ' 为您推荐';
  283. $staffNameBase64 = stringUtil::ossBase64($newStaffName);
  284. //花材主图
  285. $itemCover = $cover . '?x-oss-process=image/resize,m_fill,h_750,w_750';
  286. $itemCoverBase64 = stringUtil::ossBase64($itemCover);
  287. $logoBase64 = '';
  288. if (in_array($this->mainId, [52, 1459, 13495])) {
  289. $logo = 'poster/hyxh_ncd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  290. if ($this->mainId == 1459) {
  291. $logo = 'poster/hyxh_gcd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  292. }
  293. if ($this->mainId == 13495) {
  294. $logo = 'poster/hyxh_hzd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  295. }
  296. $logoBase64 = stringUtil::ossBase64($logo);
  297. }
  298. $url = $prefix . 'poster/1234.jpg?x-oss-process=image';
  299. //花材主图
  300. $url .= '/watermark,image_' . $itemCoverBase64 . ',g_nw,x_0,y_0';
  301. //小程序码
  302. $url .= '/watermark,image_' . $miniCodeBase64 . ',g_se';
  303. if (!empty($logoBase64)) {
  304. //logo
  305. $url .= '/watermark,image_' . $logoBase64 . ',g_se,x_65,y_65';
  306. }
  307. //为你推荐
  308. $url .= '/watermark,text_' . $staffNameBase64 . ',g_sw,x_18,y_235,color_8B8989,type_d3F5LW1pY3JvaGVp,size_30';
  309. //标题
  310. $url .= '/watermark,text_' . $nameBase64 . ',g_sw,x_18,y_145';
  311. //价格
  312. $url .= '/watermark,text_' . $priceBase64 . ',g_sw,x_18,y_50,color_EE2C2C,type_d3F5LW1pY3JvaGVp,size_50';
  313. $respond = ['imgUrl' => $url];
  314. util::success($respond);
  315. }
  316. //获取花材描述
  317. public function actionDesc()
  318. {
  319. $itemId = Yii::$app->request->get('itemId', 0);
  320. $picText = PicTextGhsItemClass::getByCondition(['mainId'=> $this->mainId, 'ghsItemId' => $itemId, 'delStatus' => 0], true);
  321. if (empty($picText)) {
  322. // 适配前端的返回处理 -- 本来要使用 util::fail()
  323. util::success('not_exist');
  324. }
  325. if ($picText->mainId != $this->mainId) {
  326. util::fail('没有找到描述');
  327. }
  328. util::success($picText);
  329. }
  330. //创建花材描述(花材介绍)
  331. public function actionCreateDesc()
  332. {
  333. $form = new \ghs\models\item\CreateDescForm(); // 暂时使用同一份 ghs,项目独立时,再复制一份
  334. $form->load(Yii::$app->request->post(), '');
  335. if (!$form->validateForm()) {
  336. return;
  337. }
  338. $data = $form->attributes;
  339. $exists = PicTextGhsItemClass::exists(['mainId'=>$this->mainId, 'ghsItemId' => $data['itemId'], 'delStatus' => 0]);
  340. if ($exists) {
  341. util::fail('该商品已存在图文内容,请勿重复添加');
  342. }
  343. $data['mainId'] = $this->mainId;
  344. $data['ghsItemId'] = $data['itemId'];
  345. $result = PicTextGhsItemClass::add($data);
  346. if ($result) {
  347. util::success($result);
  348. }
  349. util::fail('创建失败');
  350. }
  351. //更新花材描述(花材介绍)
  352. public function actionUpdateDesc()
  353. {
  354. $form = new \ghs\models\item\CreateDescForm();
  355. $form->load(Yii::$app->request->post(), '');
  356. if (!$form->validateForm()) {
  357. return;
  358. }
  359. $data = $form->attributes;
  360. $id = $data['id'];
  361. $info = PicTextGhsItemClass::getById($id, false, 'id');
  362. if (empty($info)) {
  363. util::fail('描述不存在');
  364. }
  365. $data['mainId'] = $this->mainId;
  366. $result = PicTextGhsItemClass::updateById($id, $data);
  367. if ($result) {
  368. util::success($result);
  369. }
  370. util::fail('更新失败');
  371. }
  372. }