GoodsController.php 18 KB

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