GoodsController.php 12 KB

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