GoodsController.php 14 KB

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