GoodsController.php 11 KB

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