GoodsController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizGhs\order\classes\OrderItemClass;
  5. use bizHd\goods\classes\GoodsClass;
  6. use bizHd\goods\services\CategoryService;
  7. use bizHd\goods\services\GoodsCategoryService;
  8. use bizHd\goods\services\GoodsSettingService;
  9. use bizHd\saas\services\FestRiseService;
  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. public function actionSetSh()
  19. {
  20. $get = Yii::$app->request->get();
  21. $id = $get['id'] ?? 0;
  22. $info = GoodsClass::getById($id, true);
  23. if (empty($info)) {
  24. util::fail('没有找到商品');
  25. }
  26. GoodsClass::valid($info, $this->mainId);
  27. util::complete();
  28. }
  29. //商品打印 ssh 20220602
  30. public function actionPrint()
  31. {
  32. $get = Yii::$app->request->get();
  33. $id = $get['id'] ?? 0;
  34. $num = $get['num'] ?? 1;
  35. $info = GoodsClass::getById($id, true);
  36. if (empty($info)) {
  37. util::fail('没有找到商品');
  38. }
  39. $shopId = $this->shopId;
  40. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  41. $printLabelSn = $ext->printLabelSn ?? '';
  42. if (empty($printLabelSn)) {
  43. util::fail('请设置标签打印机');
  44. }
  45. $p = new printUtil($printLabelSn);
  46. $name = $info->name ?? '';
  47. $goodsSn = $info->sn ?? '';
  48. $flower = $info->flower ?? 0;
  49. $price = floatval($info->price) ?? 0;
  50. $content = '<TEXT x="360" y="30" font="12" w="2" h="2" r="90">' . $name . '</TEXT>';
  51. $content .= '<BC128 x="260" y="30" h="80" s="1" r="90" n="5" w="12">' . $goodsSn . '</BC128>';
  52. if ($flower == 0) {
  53. $content .= '<TEXT x="90" y="30" font="12" w="2" h="2" r="90">' . $price . '元</TEXT>';
  54. }
  55. $p->times = $num;
  56. $p->printLabelMsg($content);
  57. util::complete();
  58. }
  59. //添加商品 ssh 2019.12.7
  60. public function actionAdd()
  61. {
  62. $data = Yii::$app->request->post();
  63. $data['sjId'] = $this->sjId;
  64. $data['mainId'] = $this->mainId;
  65. $data['shopId'] = $this->shopId ?? 0;
  66. $data['property'] = 0;
  67. $data['flower'] = 1;
  68. unset($data['stock']);
  69. $return = GoodsClass::addGoods($data);
  70. util::success($return);
  71. }
  72. //根据sn获取商品详情 ssh 20220505
  73. public function actionGetBySn()
  74. {
  75. $sn = Yii::$app->request->get('sn');
  76. $goods = GoodsClass::getByCondition(['sn' => $sn], true);
  77. GoodsClass::valid($goods, $this->mainId);
  78. util::success($goods);
  79. }
  80. //绿植盆栽采购 ssh 20220405
  81. public function actionCgPlant()
  82. {
  83. $post = Yii::$app->request->post();
  84. $connection = Yii::$app->db;
  85. $transaction = $connection->beginTransaction();
  86. try {
  87. $post['mainId'] = $this->mainId ?? 0;
  88. $post['sjId'] = $this->sjId ?? 0;
  89. $post['shopId'] = $this->shopId ?? 0;
  90. $staff = $this->shopAdmin ?? [];
  91. $staffName = $staff->name ?? '';
  92. $post['staffName'] = $staffName;
  93. $post['staffId'] = $staff->id ?? 0;
  94. $respond = GoodsClass::cgPlant($post, $this->shop);
  95. $transaction->commit();
  96. util::success($respond);
  97. } catch (Exception $e) {
  98. $transaction->rollBack();
  99. util::fail();
  100. }
  101. }
  102. //商品列表 ssh 2019.12.7
  103. public function actionList()
  104. {
  105. $get = Yii::$app->request->get();
  106. $status = isset($get['status']) && is_numeric($get['status']) ? $get['status'] : -1;
  107. $cId = isset($get['cId']) && !empty($get['cId']) ? $get['cId'] : 0;
  108. $name = isset($get['name']) && !empty($get['name']) ? $get['name'] : '';
  109. $flowerNum = isset($get['flowerNum']) && $get['flowerNum'] > 0 ? $get['flowerNum'] : 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. $data = GoodsService::getGoodsList($where);
  126. util::success($data);
  127. }
  128. //获取商品详情 ssh 2019.12.7
  129. public function actionDetail()
  130. {
  131. $id = Yii::$app->request->get('id');
  132. $goods = GoodsClass::getGoodsInfo($id);
  133. GoodsClass::valid($goods, $this->mainId);
  134. util::success($goods);
  135. }
  136. //更新商品 ssh 2019.12.7
  137. public function actionUpdate()
  138. {
  139. $data = Yii::$app->request->post();
  140. $id = isset($data['id']) ? $data['id'] : 0;
  141. $info = GoodsService::getById($id);
  142. GoodsService::valid($info, $this->mainId);
  143. $data['sjId'] = $this->sjId;
  144. $data['mainId'] = $this->mainId;
  145. $data['shopId'] = $this->shopId ?? 0;
  146. $data['flower'] = $info['flower'] ?? 0;
  147. $connection = Yii::$app->db;
  148. $transaction = $connection->beginTransaction();
  149. try {
  150. GoodsClass::updateGoods($info, $data);
  151. $transaction->commit();
  152. util::complete();
  153. } catch (Exception $e) {
  154. $transaction->rollBack();
  155. util::fail();
  156. }
  157. }
  158. //商品排序 ssh 2020.3.11
  159. public function actionSort()
  160. {
  161. $id = Yii::$app->request->get('id', 0);
  162. $cId = Yii::$app->request->get('cId', 0);
  163. $inTurn = Yii::$app->request->get('inTurn', 0);
  164. //验证商品
  165. $info = GoodsService::getById($id);
  166. GoodsService::valid($info, $this->mainId);
  167. if ($cId != 0) {
  168. //验证分类
  169. $category = CategoryService::getById($cId);
  170. CategoryService::valid($category, $this->mainId);
  171. GoodsCategoryService::updateByCondition(['cId' => $cId, 'gId' => $id], ['inTurn' => $inTurn]);
  172. } else {
  173. GoodsService::updateById($id, ['inTurn' => $inTurn]);
  174. GoodsCategoryService::updateByCondition(['gId' => $id], ['inTurn' => $inTurn]);
  175. }
  176. util::complete('操作成功');
  177. }
  178. //删除商品 ssh 2019.12.7
  179. public function actionDelete()
  180. {
  181. $id = Yii::$app->request->get('id', 0);
  182. $goods = GoodsService::getById($id);
  183. GoodsService::valid($goods, $this->mainId);
  184. GoodsService::deleteGoods($goods);
  185. util::complete();
  186. }
  187. //上下架 ssh 2019.12.8
  188. public function actionChangeStatus()
  189. {
  190. $get = Yii::$app->request->get();
  191. $id = isset($get['id']) ? $get['id'] : 0;
  192. $status = isset($get['status']) ? $get['status'] : 1;
  193. $info = GoodsService::getById($id);
  194. GoodsService::valid($info, $this->mainId);
  195. GoodsService::changeStatus($id, $status);
  196. util::complete();
  197. }
  198. //商品说明 ssh 2019.12.8
  199. public function actionIntroduce()
  200. {
  201. $set = GoodsSettingService::getByCondition(['mainId' => $this->mainId]);
  202. $introduce = $set['goodsIntroduce'];
  203. util::success(['introduce' => $introduce]);
  204. }
  205. //修改商品说明 ssh 2019.12.8
  206. public function actionUpdateIntroduce()
  207. {
  208. $introduce = Yii::$app->request->post('introduce', '');
  209. GoodsSettingService::updateByCondition(['mainId' => $this->mainId], ['goodsIntroduce' => $introduce]);
  210. util::complete('提交成功');
  211. }
  212. //设置 ssh 2019.12.8
  213. public function actionSetting()
  214. {
  215. $return = GoodsSettingService::getSetting($this->mainId);
  216. util::success($return);
  217. }
  218. //更新涨价 ssh 2019.12.9
  219. public function actionUpdateRise()
  220. {
  221. $post = Yii::$app->request->post();
  222. $festIdList = isset($post['festIdList']) && !empty($post['festIdList']) ? $post['festIdList'] : [];
  223. unset($post['festIdList']);
  224. $riseSwitch = isset($post['riseSwitch']) && is_numeric($post['riseSwitch']) ? $post['riseSwitch'] : 1;
  225. if ($riseSwitch == 1) {
  226. if (empty($festIdList)) {
  227. util::fail('请选择节日');
  228. }
  229. FestRiseService::deleteByCondition(['mainId' => $this->mainId]);
  230. $add = [];
  231. foreach ($festIdList as $festId) {
  232. $add[] = ['mainId' => $this->mainId, 'festId' => $festId];
  233. }
  234. FestRiseService::batchAdd($add);
  235. }
  236. GoodsSettingService::updateByCondition(['mainId' => $this->mainId], $post);
  237. util::complete('提交成功');
  238. }
  239. //获取商品详情 ssh 2019.12.3
  240. public function actionMallDetail()
  241. {
  242. $id = Yii::$app->request->get('id', 0);
  243. $info = GoodsClass::getGoodsInfo($id);
  244. GoodsService::valid($info, $this->mainId);
  245. $setting = GoodsSettingService::getByCondition(['mainId' => $this->mainId]);
  246. $commonIntro = isset($setting['goodsIntroduce']) ? $setting['goodsIntroduce'] : '';
  247. $info['commonIntro'] = $commonIntro;
  248. util::success($info);
  249. }
  250. public function actionIndex()
  251. {
  252. $catWhere = ['mainId' => $this->mainId];
  253. $goodsWhere['mainId'] = $this->mainId;
  254. $goodsWhere['delStatus'] = 0;
  255. $classIds = CategoryService::getCategoryClassAll($catWhere);
  256. $goodsData = GoodsCategoryService::getGoodsAll($goodsWhere);
  257. $goodsCateData = GoodsCategoryService::getEnableGoodsCategory($this->mainId);
  258. //组装数据 [{classId:'','className:'',child:[{itemInfoData}]}]
  259. $data = GoodsCategoryService::assembleData($classIds, $goodsCateData, $goodsData);
  260. util::success($data);
  261. }
  262. //保存花材组合 ssh 2021.4.10
  263. public function actionSaveItemGroup()
  264. {
  265. $orderSn = Yii::$app->request->post('orderSn', '');
  266. $name = Yii::$app->request->post('name', '');
  267. $classId = Yii::$app->request->post('categoryId', 0);
  268. if (empty($name)) {
  269. util::fail("名称不能为空");
  270. }
  271. $cateExists = CategoryService::exists(['id' => $classId, 'mainId' => $this->mainId]);
  272. if (!$cateExists) {
  273. util::fail("分类不存在");
  274. }
  275. $itemInfo = OrderItemclass::getAllByCondition(['orderSn' => $orderSn], null, "*");
  276. if (empty($itemInfo)) {
  277. util::fail("订单不存在");
  278. }
  279. $order = OrderService::getOrderBySn($orderSn);
  280. OrderService::valid($order, $this->shopId);
  281. $save = GoodsService::saveItemGroup($order, $itemInfo, $classId, $name, $this->adminId);
  282. if ($save) {
  283. util::complete();
  284. }
  285. util::fail("保存失败");
  286. }
  287. }