GoodsController.php 11 KB

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