GoodsController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. $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 ($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. $cId = isset($get['cId']) ? intval($get['cId']) : 0;
  104. $priceType = isset($get['priceType']) ? $get['priceType'] : null;
  105. $shop = $this->shop;
  106. $custom = $this->custom;
  107. $argument = [];
  108. if (!empty($cId)) {
  109. $argument['catId'] = $cId;
  110. }
  111. if ($priceType != null && in_array($priceType, [0, 1])) {
  112. $argument['priceType'] = $priceType;
  113. }
  114. $argument['pageSize'] = 20;
  115. $argument['requestType'] = 'goodsList';
  116. $data = GoodsCategoryClass::getGoodsList($argument, $shop, $custom);
  117. util::success($data);
  118. }
  119. public function actionGetGoodsInfoList()
  120. {
  121. $get = Yii::$app->request->get();
  122. $cId = $get['cId'] ?? 0;
  123. $flowerNum = $get['flowerNum'] ?? 0;
  124. if (empty($cId)) {
  125. util::fail('分类不能为空');
  126. }
  127. $where = ['cId' => $cId];
  128. $params = [];
  129. if (!empty($flowerNum)) {
  130. $params['flowerNum'] = $flowerNum;
  131. }
  132. $data = GoodsClass::getGoodsData($where, $params);
  133. util::success($data);
  134. }
  135. //获取商品详情 ssh 2019.12.7
  136. public function actionDetail()
  137. {
  138. $id = intval(Yii::$app->request->get('id'));
  139. $shop = $this->shop;
  140. $custom = $this->custom;
  141. //只取原价
  142. $params = ['getOriginalPrice' => 1];
  143. $goods = GoodsClass::getGoodsInfo($id, $shop, $custom, $params);
  144. GoodsClass::valid($goods, $this->mainId);
  145. util::success($goods);
  146. }
  147. //更新商品 ssh 2019.12.7
  148. public function actionUpdate()
  149. {
  150. $form = new \hd\models\goods\UpdateForm();
  151. $form->load(Yii::$app->request->post(), '');
  152. if (!$form->validateForm()) {
  153. return;
  154. }
  155. $data = $form->attributes;
  156. $id = $data['id'];
  157. $info = GoodsClass::getById($id);
  158. if (empty($info)) {
  159. util::fail('没有找到商品');
  160. }
  161. if ($info['mainId'] != $this->mainId) {
  162. util::fail('不是你的商品');
  163. }
  164. $data['sjId'] = $this->sjId;
  165. $data['mainId'] = $this->mainId;
  166. $data['shopId'] = $this->shopId ?? 0;
  167. $data['flower'] = $info['flower'] ?? 0;
  168. if (!empty($info['spu'])) {
  169. //util::fail('美团商品暂时不能修改');
  170. }
  171. $connection = Yii::$app->db;
  172. $transaction = $connection->beginTransaction();
  173. try {
  174. $staff = $this->shopAdmin;
  175. GoodsClass::updateGoods($info, $data, $staff);
  176. $transaction->commit();
  177. util::complete();
  178. } catch (\Exception $e) {
  179. // 记录错误日志
  180. Yii::error('商品更新失败:' . $e->getMessage(), 'goods.update'); //. "\n" . $e->getTraceAsString()
  181. $transaction->rollBack();
  182. util::fail();
  183. }
  184. }
  185. //商品排序
  186. public function actionSort()
  187. {
  188. $post = Yii::$app->request->post();
  189. $items = $post['items'];
  190. $cId = intval($post['cId']);
  191. //验证商品
  192. // 提取所有商品ID
  193. $categoryIds = array_map(function ($item) {
  194. return intval($item['id']);
  195. }, $items);
  196. // 批量获取商品信息
  197. $goodsInfos = GoodsClass::getByIds($categoryIds);
  198. // 批量验证商品
  199. foreach ($goodsInfos as $info) {
  200. GoodsService::valid($info, $this->mainId);
  201. }
  202. $category = CategoryService::getById($cId);
  203. CategoryService::valid($category, $this->mainId);
  204. // 更新商品排序
  205. foreach ($items as $item) {
  206. $id = intval($item['id']);
  207. $inTurn = intval($item['inTurn']);
  208. GoodsCategoryClass::updateByCondition(['cId' => $cId, 'gId' => $id], ['inTurn' => $inTurn]);
  209. }
  210. util::complete('排序完成');
  211. }
  212. //删除商品 ssh 2019.12.7
  213. public function actionDelete()
  214. {
  215. $id = Yii::$app->request->get('id', 0);
  216. $goods = GoodsService::getById($id);
  217. GoodsService::valid($goods, $this->mainId);
  218. GoodsService::deleteGoods($goods);
  219. util::complete();
  220. }
  221. //上下架 ssh 2019.12.8
  222. public function actionChangeStatus()
  223. {
  224. $get = Yii::$app->request->get();
  225. $id = isset($get['id']) ? $get['id'] : 0;
  226. $status = isset($get['status']) ? $get['status'] : 1;
  227. $info = GoodsService::getById($id);
  228. GoodsService::valid($info, $this->mainId);
  229. GoodsService::changeStatus($id, $status);
  230. util::complete();
  231. }
  232. //商品说明 ssh 2019.12.8
  233. public function actionIntroduce()
  234. {
  235. $set = GoodsSettingService::getByCondition(['mainId' => $this->mainId]);
  236. $introduce = $set['goodsIntroduce'];
  237. util::success(['introduce' => $introduce]);
  238. }
  239. //修改商品说明 ssh 2019.12.8
  240. public function actionUpdateIntroduce()
  241. {
  242. $introduce = Yii::$app->request->post('introduce', '');
  243. GoodsSettingService::updateByCondition(['mainId' => $this->mainId], ['goodsIntroduce' => $introduce]);
  244. util::complete('提交成功');
  245. }
  246. //获取商品描述
  247. public function actionDesc()
  248. {
  249. $goodsId = Yii::$app->request->get('id', 0);
  250. $picText = PicTextGoodsClass::getByCondition(['mainId' => $this->mainId, 'goodsId' => $goodsId, 'delStatus' => 0], true);
  251. if (empty($picText)) {
  252. // 适配前端的返回处理 -- 本来要使用 util::fail()
  253. util::success('not_exist');
  254. }
  255. if ($picText->mainId != $this->mainId) {
  256. util::fail('没有找到描述');
  257. }
  258. util::success($picText);
  259. }
  260. //创建商品描述(商品介绍)
  261. public function actionCreateDesc()
  262. {
  263. $form = new \hd\models\goods\CreateDescForm();
  264. $form->load(Yii::$app->request->post(), '');
  265. if (!$form->validateForm()) {
  266. return;
  267. }
  268. $data = $form->attributes;
  269. $exists = PicTextGoodsClass::exists(['goodsId' => $data['goodsId'], 'delStatus' => 0]);
  270. if ($exists) {
  271. util::fail('该商品已存在图文内容,请勿重复添加');
  272. }
  273. $data['mainId'] = $this->mainId;
  274. $result = PicTextGoodsClass::add($data);
  275. if ($result) {
  276. util::success($result);
  277. }
  278. util::fail('创建失败');
  279. }
  280. //更新商品描述(商品介绍)
  281. public function actionUpdateDesc()
  282. {
  283. $form = new \hd\models\goods\CreateDescForm();
  284. $form->load(Yii::$app->request->post(), '');
  285. if (!$form->validateForm()) {
  286. return;
  287. }
  288. $data = $form->attributes;
  289. $id = $data['id'];
  290. $info = PicTextGoodsClass::getById($id, false, 'id');
  291. if (empty($info)) {
  292. util::fail('描述不存在');
  293. }
  294. $data['mainId'] = $this->mainId;
  295. $result = PicTextGoodsClass::updateById($id, $data);
  296. if ($result) {
  297. util::success($result);
  298. }
  299. util::fail('更新失败');
  300. }
  301. //设置 ssh 2019.12.8
  302. public function actionSetting()
  303. {
  304. $return = GoodsSettingService::getSetting($this->mainId);
  305. util::success($return);
  306. }
  307. //更新涨价 ssh 2019.12.9
  308. public function actionUpdateRise()
  309. {
  310. $form = new \hd\models\goods\UpdateRiseForm();
  311. $form->load(Yii::$app->request->post(), '');
  312. if (!$form->validateForm()) {
  313. return;
  314. }
  315. $post = $form->attributes;
  316. $festIdList = isset($post['festIdList']) && !empty($post['festIdList']) ? $post['festIdList'] : [];
  317. unset($post['festIdList']);
  318. // $riseSwitch = isset($post['riseSwitch']) && is_numeric($post['riseSwitch']) ? $post['riseSwitch'] : 1;
  319. // if ($riseSwitch == 1) {
  320. // if (empty($festIdList)) {
  321. // util::fail('请选择节日');
  322. // }
  323. // FestRiseService::deleteByCondition(['mainId' => $this->mainId]);
  324. // $add = [];
  325. // foreach ($festIdList as $festId) {
  326. // $add[] = ['mainId' => $this->mainId, 'festId' => $festId];
  327. // }
  328. // FestRiseService::batchAdd($add);
  329. // }
  330. GoodsSettingClass::updateByCondition(['mainId' => $this->mainId], $post);
  331. util::complete('提交成功');
  332. }
  333. //获取商品详情 ssh 2019.12.3
  334. public function actionMallDetail()
  335. {
  336. $id = Yii::$app->request->get('id', 0);
  337. $info = GoodsClass::getGoodsInfo($id);
  338. GoodsService::valid($info, $this->mainId);
  339. $setting = GoodsSettingService::getByCondition(['mainId' => $this->mainId]);
  340. $commonIntro = isset($setting['goodsIntroduce']) ? $setting['goodsIntroduce'] : '';
  341. $info['commonIntro'] = $commonIntro;
  342. util::success($info);
  343. }
  344. public function actionIndex()
  345. {
  346. $catWhere = ['mainId' => $this->mainId];
  347. $goodsWhere['mainId'] = $this->mainId;
  348. $goodsWhere['delStatus'] = 0;
  349. $classIds = CategoryService::getCategoryClassAll($catWhere);
  350. $goodsData = GoodsCategoryService::getGoodsAll($goodsWhere);
  351. $goodsCateData = GoodsCategoryService::getEnableGoodsCategory($this->mainId);
  352. //组装数据 [{classId:'','className:'',child:[{itemInfoData}]}]
  353. $data = GoodsCategoryService::assembleData($classIds, $goodsCateData, $goodsData);
  354. util::success($data);
  355. }
  356. //保存花材组合 ssh 2021.4.10
  357. public function actionSaveItemGroup()
  358. {
  359. $orderSn = Yii::$app->request->post('orderSn', '');
  360. $name = Yii::$app->request->post('name', '');
  361. $classId = Yii::$app->request->post('categoryId', 0);
  362. if (empty($name)) {
  363. util::fail("名称不能为空");
  364. }
  365. $cateExists = CategoryService::exists(['id' => $classId, 'mainId' => $this->mainId]);
  366. if (!$cateExists) {
  367. util::fail("分类不存在");
  368. }
  369. $itemInfo = OrderItemclass::getAllByCondition(['orderSn' => $orderSn], null, "*");
  370. if (empty($itemInfo)) {
  371. util::fail("订单不存在");
  372. }
  373. $order = OrderService::getOrderBySn($orderSn);
  374. OrderService::valid($order, $this->shopId);
  375. $save = GoodsService::saveItemGroup($order, $itemInfo, $classId, $name, $this->adminId);
  376. if ($save) {
  377. util::complete();
  378. }
  379. util::fail("保存失败");
  380. }
  381. }