ItemController.php 18 KB

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