GoodsController.php 10 KB

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