GoodsController.php 14 KB

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