GoodsController.php 15 KB

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