ItemController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\item\classes\PtItemClass;
  4. use bizGhs\custom\classes\CustomClass;
  5. use bizGhs\item\classes\ItemClass;
  6. use common\components\dict;
  7. use common\components\imgUtil;
  8. use common\components\miniUtil;
  9. use common\components\noticeUtil;
  10. use common\components\stringUtil;
  11. use common\components\util;
  12. use bizGhs\product\classes\ProductClass;
  13. use bizHd\wx\classes\WxOpenClass;
  14. use Yii;
  15. class ItemController extends BaseController
  16. {
  17. //花材申请平台认证 ssh 20231125
  18. public function actionApplyAuth()
  19. {
  20. $get = Yii::$app->request->get();
  21. $id = $get['id'] ?? 0;
  22. $info = ItemClass::getById($id, true);
  23. if (empty($info)) {
  24. util::fail('没有找到花材');
  25. }
  26. if ($info->mainId != $this->mainId) {
  27. util::fail('不是你的花材哦');
  28. }
  29. $shop = $this->shop;
  30. $sjName = $shop->merchantName ?? '';
  31. $shopName = $shop->shopName ?? '';
  32. $name = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  33. $ptItemId = $info->itemId ?? 0;
  34. $ptItem = PtItemClass::getById($ptItemId, true);
  35. if (empty($ptItem)) {
  36. util::fail('没有找到平台花材');
  37. }
  38. $ptItemName = $ptItem->name ?? '';
  39. noticeUtil::push($name . " 申请将【{$ptItemName}/{$ptItemId}】进行认证", '15280215347');
  40. util::complete();
  41. }
  42. //简单花材的列表
  43. public function actionSimpleList()
  44. {
  45. $get = Yii::$app->request->get();
  46. $name = $get['name'] ?? '';
  47. $where = ['mainId' => $this->mainId, 'delStatus' => 0];
  48. if (!empty($name)) {
  49. if (stringUtil::isLetter($name)) {
  50. $where['py'] = ['like', $name];
  51. } else {
  52. $where['name'] = ['like', $name];
  53. }
  54. }
  55. $respond = ItemClass::getSimpleList($where);
  56. util::success($respond);
  57. }
  58. public function actionChangeFrontHide()
  59. {
  60. $get = Yii::$app->request->get();
  61. $id = $get['id'] ?? 0;
  62. $status = $get['status'] ?? '';
  63. if (is_numeric($status) == false) {
  64. util::fail('请选择方式');
  65. }
  66. $info = ItemClass::getById($id, true);
  67. if (empty($info)) {
  68. util::fail('没有找到花材');
  69. }
  70. if ($info->mainId != $this->mainId) {
  71. util::fail('无法操作');
  72. }
  73. $info->frontHide = $status;
  74. $info->save();
  75. util::complete();
  76. }
  77. //检测是否要刷新
  78. public function actionCheckRefresh()
  79. {
  80. $mainId = $this->mainId;
  81. $staffId = $this->shopAdminId;
  82. $respond = Yii::$app->redis->executeCommand('GET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
  83. if ($respond == 'yes') {
  84. util::success(['remind' => 1]);
  85. }
  86. util::complete();
  87. }
  88. //批量恢复花材 ssh 20230206
  89. public function actionBatchRecover()
  90. {
  91. $post = Yii::$app->request->post();
  92. $string = $post['ids'] ?? '';
  93. $ids = json_decode($string, true);
  94. $ids = array_unique(array_filter($ids));
  95. if (empty($ids)) {
  96. util::fail('请选择花材');
  97. }
  98. $infoList = ItemClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,mainId,status,delStatus,removeStatus', null, true);
  99. if (empty($infoList)) {
  100. util::fail('请选择花材哦');
  101. }
  102. foreach ($infoList as $info) {
  103. if ($info->mainId != $this->mainId) {
  104. util::fail('请选择自己的花材');
  105. }
  106. $info->removeStatus = 0;
  107. $info->save();
  108. }
  109. util::complete('操作成功');
  110. }
  111. //批量删除花材 ssh 202302026
  112. public function actionBatchRemove()
  113. {
  114. $post = Yii::$app->request->post();
  115. $string = $post['ids'] ?? '';
  116. $ids = json_decode($string, true);
  117. $ids = array_unique(array_filter($ids));
  118. if (empty($ids)) {
  119. util::fail('请选择花材');
  120. }
  121. $infoList = ItemClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,mainId,status,delStatus,removeStatus', null, true);
  122. if (empty($infoList)) {
  123. util::fail('请选择花材哦');
  124. }
  125. foreach ($infoList as $info) {
  126. if ($info->mainId != $this->mainId) {
  127. util::fail('请选择自己的花材');
  128. }
  129. $name = $info->name ?? '';
  130. if ($info->delStatus != 1) {
  131. util::fail($name . ' 停用了才能删除');
  132. }
  133. $info->removeStatus = 1;
  134. $info->save();
  135. }
  136. util::complete('操作成功');
  137. }
  138. //批量启用花材 ssh 20230206
  139. public function actionBatchEnable()
  140. {
  141. $post = Yii::$app->request->post();
  142. $string = $post['ids'] ?? '';
  143. $ids = json_decode($string, true);
  144. $ids = array_unique(array_filter($ids));
  145. if (empty($ids)) {
  146. util::fail('请选择花材');
  147. }
  148. $infoList = ItemClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,mainId,status,delStatus,removeStatus', null, true);
  149. if (empty($infoList)) {
  150. util::fail('请选择花材哦');
  151. }
  152. foreach ($infoList as $info) {
  153. if ($info->mainId != $this->mainId) {
  154. util::fail('请选择自己的花材');
  155. }
  156. $info->delStatus = 0;
  157. $info->save();
  158. }
  159. util::complete('操作成功');
  160. }
  161. //批量停用花材 20230206
  162. public function actionBatchStop()
  163. {
  164. $post = Yii::$app->request->post();
  165. $string = $post['ids'] ?? '';
  166. $ids = json_decode($string, true);
  167. $ids = array_unique(array_filter($ids));
  168. if (empty($ids)) {
  169. util::fail('请选择花材');
  170. }
  171. $infoList = ItemClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,mainId,status,delStatus,removeStatus', null, true);
  172. if (empty($infoList)) {
  173. util::fail('请选择花材哦');
  174. }
  175. foreach ($infoList as $info) {
  176. if ($info->mainId != $this->mainId) {
  177. util::fail('请选择自己的花材');
  178. }
  179. $name = $info->name ?? '';
  180. if ($info->status != 2) {
  181. util::fail($name . ' 下载了才能停用');
  182. }
  183. $info->delStatus = 1;
  184. $info->save();
  185. }
  186. util::complete('操作成功');
  187. }
  188. //获取给散客下单的花材海报 ssh 20220111
  189. public function actionGetSkPoster()
  190. {
  191. $get = Yii::$app->request->get();
  192. $id = $get['id'] ?? 0;
  193. $item = ItemClass::getById($id, true);
  194. if (empty($item) || $item->mainId != $this->mainId) {
  195. util::fail('获取失败');
  196. }
  197. $cover = $item->cover ?? '';
  198. if (empty($cover)) {
  199. util::fail('获取失败');
  200. }
  201. $merchant = WxOpenClass::getMallWxInfo();
  202. $page = 'pages/item/detail';
  203. $ptStyle = dict::getDict('ptStyle', 'mall');
  204. $shop = $this->shop;
  205. $lsShopId = $shop->lsShopId ?? 0;
  206. //scene不能超过32个字条
  207. $scene = "id=" . $id . '&a=' . $lsShopId;
  208. $miniCode = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle);
  209. $miniCode = $miniCode . '?x-oss-process=image/resize,m_fill,h_200,w_200';
  210. $miniCodeBase64 = stringUtil::ossBase64($miniCode);
  211. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  212. $prefix = imgUtil::getPrefix();
  213. $price = $item->skPrice ?? 0;
  214. if ($item->discountPrice > 0) {
  215. $skMore = $item->skMore ?? 0;
  216. $price = bcadd($item->discountPrice, $skMore, 2);
  217. }
  218. $newPrice = '¥' . floatval($price);
  219. $priceBase64 = stringUtil::ossBase64($newPrice);
  220. $name = $item->name ?? '';
  221. $nameBase64 = stringUtil::ossBase64($name);
  222. $staff = $this->shopAdmin;
  223. $staffName = $staff->name ?? '';
  224. $newStaffName = $staffName . ' 为您推荐';
  225. $staffNameBase64 = stringUtil::ossBase64($newStaffName);
  226. //花材主图
  227. $itemCover = $cover . '?x-oss-process=image/resize,m_fill,h_750,w_750';
  228. $itemCoverBase64 = stringUtil::ossBase64($itemCover);
  229. $logoBase64 = '';
  230. if (in_array($this->mainId, [52, 1459, 13495])) {
  231. $logo = 'poster/hyxh_ncd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  232. if ($this->mainId == 1459) {
  233. $logo = 'poster/hyxh_gcd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  234. }
  235. if ($this->mainId == 13495) {
  236. $logo = 'poster/hyxh_hzd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  237. }
  238. $logoBase64 = stringUtil::ossBase64($logo);
  239. }
  240. $url = $prefix . 'poster/1234.jpg?x-oss-process=image';
  241. //花材主图
  242. $url .= '/watermark,image_' . $itemCoverBase64 . ',g_nw,x_0,y_0';
  243. //小程序码
  244. $url .= '/watermark,image_' . $miniCodeBase64 . ',g_se';
  245. if (!empty($logoBase64)) {
  246. //logo
  247. $url .= '/watermark,image_' . $logoBase64 . ',g_se,x_65,y_65';
  248. }
  249. //为你推荐
  250. $url .= '/watermark,text_' . $staffNameBase64 . ',g_sw,x_18,y_235,color_8B8989,type_d3F5LW1pY3JvaGVp,size_30';
  251. //标题
  252. $url .= '/watermark,text_' . $nameBase64 . ',g_sw,x_18,y_145';
  253. //价格
  254. $url .= '/watermark,text_' . $priceBase64 . ',g_sw,x_18,y_50,color_EE2C2C,type_d3F5LW1pY3JvaGVp,size_50';
  255. $respond = ['imgUrl' => $url];
  256. util::success($respond);
  257. }
  258. //获取给花店下单的花材海报 ssh 20220111
  259. public function actionGetHdPoster()
  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. $merchant = WxOpenClass::getWxInfo();
  272. $page = 'admin/ghsProduct/detail';
  273. $ptStyle = dict::getDict('ptStyle', 'hd');
  274. //小程序码,scene不能超过32个字条
  275. $scene = "id=" . $id . '&sId=' . $this->shopId;
  276. $miniCode = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle);
  277. $miniCode = $miniCode . '?x-oss-process=image/resize,m_fill,h_200,w_200';
  278. $miniCodeBase64 = stringUtil::ossBase64($miniCode);
  279. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  280. $prefix = imgUtil::getPrefix();
  281. $price = $item->price ?? 0;
  282. if ($item->discountPrice > 0) {
  283. $price = $item->discountPrice;
  284. }
  285. $newPrice = '¥' . floatval($price);
  286. $priceBase64 = stringUtil::ossBase64($newPrice);
  287. $name = $item->name ?? '';
  288. $nameBase64 = stringUtil::ossBase64($name);
  289. $staff = $this->shopAdmin;
  290. $staffName = $staff->name ?? '';
  291. $newStaffName = $staffName . ' 为您推荐';
  292. $staffNameBase64 = stringUtil::ossBase64($newStaffName);
  293. //花材主图
  294. $itemCover = $cover . '?x-oss-process=image/resize,m_fill,h_750,w_750';
  295. $itemCoverBase64 = stringUtil::ossBase64($itemCover);
  296. $logoBase64 = '';
  297. if (in_array($this->mainId, [52, 1459, 13495])) {
  298. $logo = 'poster/hyxh_ncd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  299. if ($this->mainId == 1459) {
  300. $logo = 'poster/hyxh_gcd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  301. }
  302. if ($this->mainId == 13495) {
  303. $logo = 'poster/hyxh_hzd_logo.png?x-oss-process=image/resize,m_fill,h_90,w_90';
  304. }
  305. $logoBase64 = stringUtil::ossBase64($logo);
  306. }
  307. $url = $prefix . 'poster/1234.jpg?x-oss-process=image';
  308. //花材主图
  309. $url .= '/watermark,image_' . $itemCoverBase64 . ',g_nw,x_0,y_0';
  310. //小程序码
  311. $url .= '/watermark,image_' . $miniCodeBase64 . ',g_se';
  312. if (!empty($logoBase64)) {
  313. //logo
  314. $url .= '/watermark,image_' . $logoBase64 . ',g_se,x_65,y_65';
  315. }
  316. //为你推荐
  317. $url .= '/watermark,text_' . $staffNameBase64 . ',g_sw,x_18,y_235,color_8B8989,type_d3F5LW1pY3JvaGVp,size_30';
  318. //标题
  319. $url .= '/watermark,text_' . $nameBase64 . ',g_sw,x_18,y_145';
  320. //价格
  321. $url .= '/watermark,text_' . $priceBase64 . ',g_sw,x_18,y_50,color_EE2C2C,type_d3F5LW1pY3JvaGVp,size_50';
  322. $respond = ['imgUrl' => $url];
  323. util::success($respond);
  324. }
  325. //是否有C级价格大于B级的 ssh 20221204
  326. public function actionGetErrorPrice()
  327. {
  328. $shop = $this->shop;
  329. $list = ItemClass::errorPrice($shop);
  330. util::success(['list' => $list]);
  331. }
  332. //列出所有花材,包括特价花材列表 ssh 20220315
  333. public function actionShowList()
  334. {
  335. $get = Yii::$app->request->get();
  336. $customId = $get['customId'] ?? 0;
  337. $custom = CustomClass::getById($customId, true);
  338. $where['mainId'] = $this->mainId;
  339. $where['delStatus'] = 0;
  340. $where['status'] = 1;
  341. $list = ItemClass::getShowList($where, $custom);
  342. $mainId = $this->mainId;
  343. $get = Yii::$app->request->get();
  344. $isCashier = $get['isCashier'] ?? 0;
  345. if ($isCashier == 1) {
  346. //去掉收银台提示价格更新
  347. $staffId = $this->shopAdminId;
  348. Yii::$app->redis->executeCommand('DEL', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
  349. }
  350. util::success(['list' => $list]);
  351. }
  352. public function actionAdd()
  353. {
  354. util::fail('开发中');
  355. }
  356. //批量添加花材
  357. public function actionBatchAdd()
  358. {
  359. $shopAdmin = $this->shopAdmin;
  360. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  361. util::fail('超管才能操作');
  362. }
  363. $post = Yii::$app->request->post();
  364. //[{"itemId":"1175","classId":"3548","price":"5","skPrice":"8","cost":"1","name":"卡娜百合"}]
  365. $data = $post['data'];
  366. if (empty($data)) {
  367. util::fail("请选花材");
  368. }
  369. $data = json_decode($data, true);
  370. if (is_array($data) == false || empty($data)) {
  371. util::fail("请选花材");
  372. }
  373. $shop = $this->shop;
  374. $shopId = $this->shopId;
  375. $mainId = $this->mainId;
  376. $sjId = $this->sjId;
  377. $adminId = $this->adminId;
  378. $ids = array_column($data, 'itemId');
  379. $ids = array_unique(array_filter($ids));
  380. $has = ProductClass::getByCondition(['mainId' => $mainId, 'itemId' => ['in', $ids]]);
  381. if (!empty($has)) {
  382. $hasName = $has->name ?? '';
  383. util::fail($hasName . " 已经添加过了");
  384. }
  385. $ptItemInfo = PtItemClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id');
  386. $connection = Yii::$app->db;
  387. $transaction = $connection->beginTransaction();
  388. try {
  389. foreach ($data as $v) {
  390. $ptItemId = $v['itemId'] ?? 0;
  391. $ratio = $ptItemInfo[$ptItemId]['ratio'] ?? 20;
  392. $shelfLife = $ptItemInfo[$ptItemId]['shelfLife'] ?? 7;
  393. $ratioType = $ptItemInfo[$ptItemId]['ratioType'] ?? 0;
  394. $variety = $ptItemInfo[$ptItemId]['variety'] ?? 0;
  395. $stockWarning = $ptItemInfo[$ptItemId]['stockWarning'] ?? 5;
  396. $weight = $ptItemInfo[$ptItemId]['weight'] ?? 1;
  397. $bigUnit = $ptItemInfo[$ptItemId]['bigUnit'] ?? '扎';
  398. $smallUnit = $ptItemInfo[$ptItemId]['smallUnit'] ?? '支';
  399. $bigUnitId = $ptItemInfo[$ptItemId]['bigUnitId'] ?? 1;
  400. $smallUnitId = $ptItemInfo[$ptItemId]['smallUnitId'] ?? 2;
  401. $cover = $ptItemInfo[$ptItemId]['cover'] ?? 'default-img.png';
  402. $v['ratio'] = $ratio;
  403. $v['cover'] = $cover;
  404. $v['shelfLife'] = $shelfLife;
  405. $v['ratioType'] = $ratioType;
  406. $v['variety'] = $variety;
  407. $v['stockWarning'] = $stockWarning;
  408. $v['weight'] = $weight;
  409. $v['bigUnit'] = $bigUnit;
  410. $v['smallUnit'] = $smallUnit;
  411. $v['bigUnitId'] = $bigUnitId;
  412. $v['smallUnitId'] = $smallUnitId;
  413. $v['adminId'] = $adminId;
  414. $v['sjId'] = $sjId;
  415. $v['shopId'] = $shopId;
  416. $v['mainId'] = $mainId;
  417. $v['staffName'] = $shopAdmin->name ?? '';
  418. $name = $v['name'] ?? '';
  419. $price = is_numeric($v['price']) ? $v['price'] : 0;
  420. $skPrice = is_numeric($v['skPrice']) ? $v['skPrice'] : 0;
  421. $cost = is_numeric($v['cost']) ? $v['cost'] : 0;
  422. if ($price <= 0) {
  423. util::fail("请填写{$name}的批发价");
  424. }
  425. if ($skPrice <= 0) {
  426. util::fail("请填写{$name}的零售价");
  427. }
  428. if ($cost <= 0) {
  429. util::fail("请填写{$name}的成本价");
  430. }
  431. if ($price > $skPrice) {
  432. util::fail("{$name}的批发价比零售价还高");
  433. }
  434. if ($cost > $price) {
  435. util::fail("{$name}的成本价比批发价还高");
  436. }
  437. $skMore = bcsub($skPrice, $price, 2);
  438. $addPrice = bcsub($price, $cost, 2);
  439. $v['skMore'] = $skMore;
  440. $v['addPrice'] = $addPrice;
  441. $v['hjAddPrice'] = $addPrice;
  442. $v['hjPrice'] = $price;
  443. ProductClass::addProduct($v, $shop);
  444. }
  445. $transaction->commit();
  446. util::complete('添加成功');
  447. } catch (Exception $e) {
  448. $transaction->rollBack();
  449. util::fail();
  450. }
  451. }
  452. public function actionDelete()
  453. {
  454. util::fail('不能删除哦!');
  455. }
  456. //获取平台里的花材信息
  457. public function actionPlatformItem()
  458. {
  459. $py = Yii::$app->request->get('py', '');
  460. $where = [];
  461. if ($py) {
  462. $where = ['py' => $py];
  463. }
  464. $list = PtItemClass::getItemList($where);
  465. util::success($list);
  466. }
  467. //列出需要进行颜色管理的花材 ssh 20220820
  468. public function actionColorList()
  469. {
  470. $mainId = $this->mainId ?? 0;
  471. $list = ItemClass::getAllByCondition(['mainId' => $mainId, 'variety' => 1], null, '*');
  472. util::success(['list' => $list]);
  473. }
  474. //多颜色管理启用与停用 ssh 20221229
  475. public function actionChangeVariety()
  476. {
  477. $get = Yii::$app->request->get();
  478. $id = $get['id'] ?? 0;
  479. $variety = $get['variety'] ?? 0;
  480. $info = ItemClass::getById($id, true);
  481. if (empty($info) || $info->mainId != $this->mainId) {
  482. util::fail('操作失败');
  483. }
  484. $info->variety = $variety;
  485. $info->save();
  486. util::complete('操作成功');
  487. }
  488. //拆散花材 ssh 20221229
  489. public function actionBreakItem()
  490. {
  491. util::fail('开发中');
  492. util::complete();
  493. }
  494. }