GoodsController.php 14 KB

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