GoodsController.php 10 KB

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