GoodsController.php 15 KB

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