GoodsController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizGhs\order\classes\OrderItemClass;
  5. use bizHd\goods\classes\GoodsCategoryClass;
  6. use bizHd\goods\classes\GoodsClass;
  7. use bizHd\goods\classes\GoodsSettingClass;
  8. use bizHd\goods\services\CategoryService;
  9. use bizHd\goods\services\GoodsCategoryService;
  10. use bizHd\goods\services\GoodsSettingService;
  11. use bizHd\pictext\classes\PicTextGoodsClass;
  12. use bizHd\wx\classes\WxOpenClass;
  13. use common\components\dict;
  14. use common\components\imgUtil;
  15. use common\components\miniUtil;
  16. use common\components\PosterUtil;
  17. use common\components\printUtil;
  18. use common\components\stringUtil;
  19. use Yii;
  20. use common\components\util;
  21. use bizHd\goods\services\GoodsService;
  22. use bizHd\order\services\OrderService;
  23. class GoodsController extends BaseController
  24. {
  25. public $guestAccess = ['detail'];
  26. //商品打印 ssh 20220602
  27. public function actionPrint()
  28. {
  29. $get = Yii::$app->request->get();
  30. $id = $get['id'] ?? 0;
  31. $num = $get['num'] ?? 1;
  32. $info = GoodsClass::getById($id, true);
  33. if (empty($info)) {
  34. util::fail('没有找到商品');
  35. }
  36. $shop = $this->shop;
  37. if (empty($info->sn)) {
  38. $info = GoodsClass::generateSn($info, $shop);
  39. }
  40. $shopId = $this->shopId;
  41. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  42. $printLabelSn = $ext->printLabelSn ?? '';
  43. if (empty($printLabelSn)) {
  44. util::fail('请设置标签打印机');
  45. }
  46. $p = new printUtil($printLabelSn);
  47. $name = $info->name ?? '';
  48. $goodsSn = $info->sn ?? '';
  49. $price = floatval($info->price) ?? 0;
  50. if (stringUtil::getWordNum($name) > 7) {
  51. $content = '<TEXT x="30" y="45" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
  52. } else {
  53. $content = '<TEXT x="30" y="45" font="12" w="2" h="2" r="0">' . $name . '</TEXT>';
  54. }
  55. $content .= '<BC128 x="30" y="115" h="75" s="1" r="0" n="3" w="10">#' . $goodsSn . '</BC128>';
  56. if ($price > 0) {
  57. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $price . '元</TEXT>';
  58. }
  59. $p->times = $num;
  60. $p->printLabelMsg($content);
  61. util::complete();
  62. }
  63. //添加商品 ssh 2019.12.7
  64. public function actionAdd()
  65. {
  66. $form = new \hd\models\goods\AddForm();
  67. $form->load(Yii::$app->request->post(), '');
  68. if (!$form->validateForm()) {
  69. return;
  70. }
  71. //$data = Yii::$app->request->post();
  72. $data = $form->attributes;
  73. $data['sjId'] = $this->sjId;
  74. $data['mainId'] = $this->mainId;
  75. $data['shopId'] = $this->shopId ?? 0;
  76. $data['property'] = 0;
  77. $data['flower'] = 1;
  78. $staff = $this->shopAdmin;
  79. $data['staffName'] = $staff->name ?? '';
  80. $connection = Yii::$app->db;
  81. $transaction = $connection->beginTransaction();
  82. try {
  83. $return = GoodsClass::addGoods($data);
  84. GoodsClass::syncUseCases($return->id, $this->mainId, $data['useCaseIdList'] ?? []);
  85. GoodsClass::syncSpecs($return, $data);
  86. $transaction->commit();
  87. util::success($return);
  88. } catch (\Exception $e) {
  89. Yii::error('商品添加失败:' . $e->getMessage(), 'goods.add');
  90. $transaction->rollBack();
  91. util::fail();
  92. }
  93. }
  94. //根据sn获取商品详情 ssh 20220505
  95. public function actionGetBySn()
  96. {
  97. $sn = Yii::$app->request->get('sn');
  98. $mainId = $this->mainId;
  99. $goods = GoodsClass::getByCondition(['mainId' => $mainId, 'sn' => $sn], true);
  100. GoodsClass::valid($goods, $this->mainId);
  101. util::success($goods);
  102. }
  103. //绿植盆栽采购 ssh 20220405
  104. public function actionCgPlant()
  105. {
  106. $post = Yii::$app->request->post();
  107. $connection = Yii::$app->db;
  108. $transaction = $connection->beginTransaction();
  109. try {
  110. $post['mainId'] = $this->mainId ?? 0;
  111. $post['sjId'] = $this->sjId ?? 0;
  112. $post['shopId'] = $this->shopId ?? 0;
  113. $staff = $this->shopAdmin ?? [];
  114. $staffName = $staff->name ?? '';
  115. $post['staffName'] = $staffName;
  116. $post['staffId'] = $staff->id ?? 0;
  117. $respond = GoodsClass::cgPlant($post, $this->shop);
  118. $transaction->commit();
  119. util::success($respond);
  120. } catch (\Exception $e) {
  121. $transaction->rollBack();
  122. util::fail();
  123. }
  124. }
  125. //商品列表 ssh 2019.12.7
  126. public function actionList()
  127. {
  128. $get = Yii::$app->request->get();
  129. $cId = isset($get['cId']) ? intval($get['cId']) : 0;
  130. $priceType = isset($get['priceType']) ? $get['priceType'] : null;
  131. $shop = $this->shop;
  132. $custom = $this->custom;
  133. $argument = [];
  134. if (!empty($cId)) {
  135. $argument['catId'] = $cId;
  136. }
  137. if ($priceType != null && in_array($priceType, [0, 1])) {
  138. $argument['priceType'] = $priceType;
  139. }
  140. $argument['pageSize'] = 20;
  141. $argument['requestType'] = 'goodsList';
  142. $data = GoodsCategoryClass::getGoodsList($argument, $shop, $custom);
  143. util::success($data);
  144. }
  145. public function actionGetGoodsInfoList()
  146. {
  147. $get = Yii::$app->request->get();
  148. $cId = $get['cId'] ?? 0;
  149. $flowerNum = $get['flowerNum'] ?? 0;
  150. if (empty($cId)) {
  151. util::fail('分类不能为空');
  152. }
  153. $where = ['cId' => $cId];
  154. $params = [];
  155. if (!empty($flowerNum)) {
  156. $params['flowerNum'] = $flowerNum;
  157. }
  158. $data = GoodsClass::getGoodsData($where, $params);
  159. util::success($data);
  160. }
  161. //获取商品详情 ssh 2019.12.7
  162. public function actionDetail()
  163. {
  164. $id = intval(Yii::$app->request->get('id'));
  165. $shop = $this->shop;
  166. $custom = $this->custom;
  167. //只取原价
  168. $params = ['getOriginalPrice' => 1];
  169. $goods = GoodsClass::getGoodsInfo($id, $shop, $custom, $params);
  170. GoodsClass::valid($goods, $this->mainId);
  171. $goods = GoodsClass::appendUseCasesAndSpecs($goods, $this->mainId);
  172. util::success($goods);
  173. }
  174. // 花束海报(散客扫码进 mall 小程序下单)ssh 20260602
  175. public function actionGetPoster()
  176. {
  177. $get = Yii::$app->request->get();
  178. $id = $get['id'] ?? 0;
  179. $goods = GoodsClass::getById($id, true);
  180. if (empty($goods)) {
  181. util::fail('获取失败');
  182. }
  183. GoodsClass::valid($goods, $this->mainId);
  184. $rawCover = $goods->cover ?? '';
  185. $shopImg = $goods->shopImg ?? '';
  186. if ($rawCover === '' && ($shopImg === '' || $shopImg === '[]')) {
  187. util::fail('请先上传花束封面');
  188. }
  189. $cover = GoodsClass::getPosterCoverPath($goods);
  190. $merchant = WxOpenClass::getMallWxInfo();
  191. $page = 'pages/goods/detail';
  192. $ptStyle = dict::getDict('ptStyle', 'mall');
  193. $shop = $this->shop;
  194. $shopId = $shop->id ?? 0;
  195. $scene = 'id=' . $id . '&a=' . $shopId;
  196. $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
  197. $miniCode = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle, $envVersion);
  198. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  199. $prefix = imgUtil::getPrefix();
  200. $goodsInfo = GoodsClass::getGoodsInfo($id, $shop, $this->custom, []);
  201. $priceType = $goodsInfo['priceType'] ?? 0;
  202. if ($priceType == 1) {
  203. $priceText = '¥' . floatval($goodsInfo['price'] ?? 0);
  204. } else {
  205. $priceText = '请咨询';
  206. }
  207. $staff = $this->shopAdmin;
  208. $staffName = $staff->name ?? '';
  209. $staffText = $staffName . ' 为您推荐';
  210. $url = PosterUtil::buildMallGoodsPosterUrl($prefix, PosterUtil::withShopAndRemark([
  211. 'cover' => $cover,
  212. 'miniCode' => $miniCode,
  213. 'priceText' => $priceText,
  214. 'name' => $goods->name ?? '',
  215. 'staffText' => $staffText,
  216. 'mainId' => $this->mainId,
  217. ], $this->shop, $this->sj ?? null, ''));
  218. util::success(['imgUrl' => $url]);
  219. }
  220. //更新商品 ssh 2019.12.7
  221. public function actionUpdate()
  222. {
  223. $form = new \hd\models\goods\UpdateForm();
  224. $form->load(Yii::$app->request->post(), '');
  225. if (!$form->validateForm()) {
  226. return;
  227. }
  228. $data = $form->attributes;
  229. $id = $data['id'];
  230. $info = GoodsClass::getById($id);
  231. if (empty($info)) {
  232. util::fail('没有找到商品');
  233. }
  234. if ($info['mainId'] != $this->mainId) {
  235. util::fail('不是你的商品');
  236. }
  237. $data['sjId'] = $this->sjId;
  238. $data['mainId'] = $this->mainId;
  239. $data['shopId'] = $this->shopId ?? 0;
  240. $data['flower'] = $info['flower'] ?? 0;
  241. $connection = Yii::$app->db;
  242. $transaction = $connection->beginTransaction();
  243. try {
  244. $staff = $this->shopAdmin;
  245. GoodsClass::updateGoods($info, $data, $staff);
  246. GoodsClass::syncUseCases($id, $this->mainId, $data['useCaseIdList'] ?? []);
  247. GoodsClass::syncSpecs($info, $data);
  248. $transaction->commit();
  249. util::complete();
  250. } catch (\Exception $e) {
  251. // 记录错误日志
  252. Yii::error('商品更新失败:' . $e->getMessage(), 'goods.update'); //. "\n" . $e->getTraceAsString()
  253. $transaction->rollBack();
  254. util::fail();
  255. }
  256. }
  257. //商品排序
  258. public function actionSort()
  259. {
  260. $post = Yii::$app->request->post();
  261. $items = $post['items'];
  262. $cId = intval($post['cId']);
  263. //验证商品
  264. // 提取所有商品ID
  265. $categoryIds = array_map(function ($item) {
  266. return intval($item['id']);
  267. }, $items);
  268. // 批量获取商品信息
  269. $goodsInfos = GoodsClass::getByIds($categoryIds);
  270. // 批量验证商品
  271. foreach ($goodsInfos as $info) {
  272. GoodsService::valid($info, $this->mainId);
  273. }
  274. $category = CategoryService::getById($cId);
  275. CategoryService::valid($category, $this->mainId);
  276. // 更新商品排序
  277. foreach ($items as $item) {
  278. $id = intval($item['id']);
  279. $inTurn = intval($item['inTurn']);
  280. GoodsCategoryClass::updateByCondition(['cId' => $cId, 'gId' => $id], ['inTurn' => $inTurn]);
  281. }
  282. util::complete('排序完成');
  283. }
  284. //删除商品 ssh 2019.12.7
  285. public function actionDelete()
  286. {
  287. $id = Yii::$app->request->get('id', 0);
  288. $goods = GoodsService::getById($id);
  289. GoodsService::valid($goods, $this->mainId);
  290. GoodsService::deleteGoods($goods);
  291. util::complete();
  292. }
  293. //上下架 ssh 2019.12.8
  294. public function actionChangeStatus()
  295. {
  296. $get = Yii::$app->request->get();
  297. $id = isset($get['id']) ? $get['id'] : 0;
  298. $status = isset($get['status']) ? $get['status'] : 1;
  299. $info = GoodsService::getById($id);
  300. GoodsService::valid($info, $this->mainId);
  301. GoodsService::changeStatus($id, $status);
  302. util::complete();
  303. }
  304. //商品说明 ssh 2019.12.8
  305. public function actionIntroduce()
  306. {
  307. $set = GoodsSettingService::getByCondition(['mainId' => $this->mainId]);
  308. $introduce = $set['goodsIntroduce'];
  309. util::success(['introduce' => $introduce]);
  310. }
  311. //修改商品说明 ssh 2019.12.8
  312. public function actionUpdateIntroduce()
  313. {
  314. $introduce = Yii::$app->request->post('introduce', '');
  315. GoodsSettingService::updateByCondition(['mainId' => $this->mainId], ['goodsIntroduce' => $introduce]);
  316. util::complete('提交成功');
  317. }
  318. //获取商品描述
  319. public function actionDesc()
  320. {
  321. $goodsId = Yii::$app->request->get('id', 0);
  322. $picText = PicTextGoodsClass::getByCondition(['mainId' => $this->mainId, 'goodsId' => $goodsId, 'delStatus' => 0], true);
  323. if (empty($picText)) {
  324. // 适配前端的返回处理 -- 本来要使用 util::fail()
  325. util::success('not_exist');
  326. }
  327. if ($picText->mainId != $this->mainId) {
  328. util::fail('没有找到描述');
  329. }
  330. util::success($picText);
  331. }
  332. //创建商品描述(商品介绍)
  333. public function actionCreateDesc()
  334. {
  335. $form = new \hd\models\goods\CreateDescForm();
  336. $form->load(Yii::$app->request->post(), '');
  337. if (!$form->validateForm()) {
  338. return;
  339. }
  340. $data = $form->attributes;
  341. $exists = PicTextGoodsClass::exists(['goodsId' => $data['goodsId'], 'delStatus' => 0]);
  342. if ($exists) {
  343. util::fail('该商品已存在图文内容,请勿重复添加');
  344. }
  345. $data['mainId'] = $this->mainId;
  346. $result = PicTextGoodsClass::add($data);
  347. if ($result) {
  348. util::success($result);
  349. }
  350. util::fail('创建失败');
  351. }
  352. //更新商品描述(商品介绍)
  353. public function actionUpdateDesc()
  354. {
  355. $form = new \hd\models\goods\CreateDescForm();
  356. $form->load(Yii::$app->request->post(), '');
  357. if (!$form->validateForm()) {
  358. return;
  359. }
  360. $data = $form->attributes;
  361. $id = $data['id'];
  362. $info = PicTextGoodsClass::getById($id, false, 'id');
  363. if (empty($info)) {
  364. util::fail('描述不存在');
  365. }
  366. $data['mainId'] = $this->mainId;
  367. $result = PicTextGoodsClass::updateById($id, $data);
  368. if ($result) {
  369. util::success($result);
  370. }
  371. util::fail('更新失败');
  372. }
  373. //设置 ssh 2019.12.8
  374. public function actionSetting()
  375. {
  376. $return = GoodsSettingService::getSetting($this->mainId);
  377. util::success($return);
  378. }
  379. //更新涨价 ssh 2019.12.9
  380. public function actionUpdateRise()
  381. {
  382. $form = new \hd\models\goods\UpdateRiseForm();
  383. $form->load(Yii::$app->request->post(), '');
  384. if (!$form->validateForm()) {
  385. return;
  386. }
  387. $post = $form->attributes;
  388. $festIdList = isset($post['festIdList']) && !empty($post['festIdList']) ? $post['festIdList'] : [];
  389. unset($post['festIdList']);
  390. $post['riseSwitch'] = (int)$post['riseSwitch'];
  391. $post['riseType'] = (int)$post['riseType'];
  392. $post['riseAmount'] = $post['riseSwitch'] == 1 ? (float)$post['riseAmount'] : 0;
  393. // $riseSwitch = isset($post['riseSwitch']) && is_numeric($post['riseSwitch']) ? $post['riseSwitch'] : 1;
  394. // if ($riseSwitch == 1) {
  395. // if (empty($festIdList)) {
  396. // util::fail('请选择节日');
  397. // }
  398. // FestRiseService::deleteByCondition(['mainId' => $this->mainId]);
  399. // $add = [];
  400. // foreach ($festIdList as $festId) {
  401. // $add[] = ['mainId' => $this->mainId, 'festId' => $festId];
  402. // }
  403. // FestRiseService::batchAdd($add);
  404. // }
  405. GoodsSettingClass::updateByCondition(['mainId' => $this->mainId], $post);
  406. util::complete('提交成功');
  407. }
  408. //获取商品详情 ssh 2019.12.3
  409. public function actionMallDetail()
  410. {
  411. $id = Yii::$app->request->get('id', 0);
  412. $goods = GoodsClass::getGoodsInfo($id);
  413. GoodsService::valid($goods, $this->mainId);
  414. // 取商家商品统一配置表中的商品说明
  415. $setting = GoodsSettingService::getByCondition(['mainId' => $this->mainId]);
  416. $commonIntro = isset($setting['goodsIntroduce']) ? $setting['goodsIntroduce'] : '';
  417. $goods['commonIntro'] = $commonIntro;
  418. util::success($goods);
  419. }
  420. public function actionIndex()
  421. {
  422. $catWhere = ['mainId' => $this->mainId];
  423. $goodsWhere['mainId'] = $this->mainId;
  424. $goodsWhere['delStatus'] = 0;
  425. $classIds = CategoryService::getCategoryClassAll($catWhere);
  426. $goodsData = GoodsCategoryService::getGoodsAll($goodsWhere);
  427. $goodsCateData = GoodsCategoryService::getEnableGoodsCategory($this->mainId);
  428. //组装数据 [{classId:'','className:'',child:[{itemInfoData}]}]
  429. $data = GoodsCategoryService::assembleData($classIds, $goodsCateData, $goodsData);
  430. util::success($data);
  431. }
  432. //保存花材组合 ssh 2021.4.10
  433. public function actionSaveItemGroup()
  434. {
  435. $orderSn = Yii::$app->request->post('orderSn', '');
  436. $name = Yii::$app->request->post('name', '');
  437. $classId = Yii::$app->request->post('categoryId', 0);
  438. if (empty($name)) {
  439. util::fail("名称不能为空");
  440. }
  441. $cateExists = CategoryService::exists(['id' => $classId, 'mainId' => $this->mainId]);
  442. if (!$cateExists) {
  443. util::fail("分类不存在");
  444. }
  445. $itemInfo = OrderItemclass::getAllByCondition(['orderSn' => $orderSn], null, "*");
  446. if (empty($itemInfo)) {
  447. util::fail("订单不存在");
  448. }
  449. $order = OrderService::getOrderBySn($orderSn);
  450. OrderService::valid($order, $this->shopId);
  451. $save = GoodsService::saveItemGroup($order, $itemInfo, $classId, $name, $this->adminId);
  452. if ($save) {
  453. util::complete();
  454. }
  455. util::fail("保存失败");
  456. }
  457. //创建编号 ssh 20251110
  458. public function actionCreateSn()
  459. {
  460. $get = Yii::$app->request->get();
  461. $id = $get['id'] ?? 0;
  462. $info = GoodsClass::getById($id, true);
  463. if (empty($info)) {
  464. util::fail('没有找到商品');
  465. }
  466. if ($info->mainId != $this->mainId) {
  467. util::fail('不是你的商品');
  468. }
  469. $shop = $this->shop;
  470. GoodsClass::generateSn($info, $shop);
  471. util::complete('创建成功');
  472. }
  473. }