ItemController.php 17 KB

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