GoodsController.php 11 KB

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