GoodsController.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. package com.kmall.admin.controller;
  2. import com.kmall.admin.dto.GoodsDetailsDto;
  3. import com.kmall.admin.dto.GoodsDto;
  4. import com.kmall.admin.dto.GoodsPanoramaDto;
  5. import com.kmall.admin.entity.GoodsEntity;
  6. import com.kmall.admin.entity.GoodsGalleryEntity;
  7. import com.kmall.admin.entity.mk.MkActivitiesEntity;
  8. import com.kmall.admin.entity.mk.MkActivityFormEntity;
  9. import com.kmall.admin.service.*;
  10. import com.kmall.admin.service.mk.MkActivitiesService;
  11. import com.kmall.admin.service.mk.MkActivityFormService;
  12. import com.kmall.admin.utils.ParamUtils;
  13. import com.kmall.admin.utils.ShiroUtils;
  14. import com.kmall.common.constant.Dict;
  15. import com.kmall.common.constant.JxlsXmlTemplateName;
  16. import com.kmall.admin.fromcomm.entity.SysUserEntity;
  17. import com.kmall.common.utils.*;
  18. import com.kmall.common.utils.excel.ExcelExport;
  19. import com.kmall.common.utils.excel.ExcelUtil;
  20. import com.kmall.common.utils.print.ticket.item.Goods;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.apache.shiro.authz.annotation.RequiresPermissions;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.web.bind.annotation.*;
  25. import org.springframework.web.multipart.MultipartFile;
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpServletResponse;
  28. import java.text.SimpleDateFormat;
  29. import java.util.*;
  30. /**
  31. * Controller
  32. *
  33. * @author Scott
  34. * @email
  35. * @date 2017-08-21 21:19:49
  36. */
  37. @RestController
  38. @RequestMapping("goods")
  39. public class GoodsController {
  40. @Autowired
  41. private GoodsService goodsService;
  42. @Autowired
  43. private GoodsGalleryService goodsGalleryService;
  44. @Autowired
  45. private OfflineCartService offlineCartService;
  46. @Autowired
  47. private ExcelUtil excelUtil;
  48. @Autowired
  49. private StoreService storeService;
  50. /**
  51. * 查看列表
  52. */
  53. @RequestMapping("/list")
  54. @RequiresPermissions("goods:list")
  55. public R list(@RequestParam Map<String, Object> params) {
  56. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  57. ParamUtils.setName(params, "name");
  58. String lastSaleTime = (String) params.get("lastSaleTime");
  59. if(org.apache.commons.lang.StringUtils.isNotEmpty(lastSaleTime)) {
  60. try {
  61. lastSaleTime = new String(lastSaleTime.getBytes("iso-8859-1"), "utf-8");
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65. lastSaleTime = DateUtils.getDate(lastSaleTime);
  66. params.put("lastSaleTime", lastSaleTime + " 00:00:00");
  67. }
  68. //查询列表数据
  69. Query query = new Query(params);
  70. query.put("isDelete", 0);
  71. List<GoodsEntity> goodsList = goodsService.queryList(query);
  72. int total = goodsService.queryTotal(query);
  73. PageUtils pageUtil = new PageUtils(goodsList, total, query.getLimit(), query.getPage());
  74. return R.ok().put("page", pageUtil);
  75. }
  76. /**
  77. * 查看信息
  78. */
  79. @RequestMapping("/info/{id}")
  80. @RequiresPermissions("goods:info")
  81. public R info(@PathVariable("id") Integer id) {
  82. GoodsEntity goods = goodsService.queryObject(id);
  83. if(goods != null) {
  84. GoodsGalleryEntity goodsGalleryEntity =goodsGalleryService.queryVideoObjectByGoodId(goods.getId());
  85. if(goodsGalleryEntity != null){
  86. goods.setVideoUrl(goodsGalleryEntity.getImgUrl());
  87. }
  88. }
  89. return R.ok().put("goods", goods);
  90. }
  91. /**
  92. * 查看信息
  93. */
  94. @RequestMapping("/infoByQuery")
  95. public R infoByQuery(@RequestParam Map<String, Object> params) {
  96. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  97. ParamUtils.setName(params, "name");
  98. //查询列表数据
  99. Query query = new Query(params);
  100. query.put("isDelete", 0);
  101. List<GoodsEntity> goodsList = goodsService.queryList(query);
  102. if(goodsList != null && goodsList.size() != 0) {
  103. return R.ok().put("goods", goodsList.get(0));
  104. }
  105. return R.ok().put("goods", new GoodsEntity());
  106. }
  107. /**
  108. * 保存
  109. */
  110. @RequestMapping("/save")
  111. @RequiresPermissions("goods:save")
  112. public R save(@RequestBody GoodsEntity goods) {
  113. goodsService.save(goods);
  114. return R.ok();
  115. }
  116. /**
  117. * 修改
  118. */
  119. @RequestMapping("/update")
  120. @RequiresPermissions("goods:update")
  121. public R update(@RequestBody GoodsEntity goods) {
  122. goodsService.update(goods);
  123. return R.ok();
  124. }
  125. /**
  126. * 删除
  127. */
  128. @RequestMapping("/delete")
  129. @RequiresPermissions("goods:delete")
  130. public R delete(@RequestBody Integer[] ids) {
  131. goodsService.deleteBatch(ids);
  132. return R.ok();
  133. }
  134. /**
  135. * 查看所有列表
  136. */
  137. @RequestMapping("/queryAll")
  138. public R queryAll(@RequestParam Map<String, Object> params) {
  139. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  140. params.put("isDelete", Integer.parseInt(Dict.isDelete.item_0.getItem()));
  141. params.put("isOnSale", Integer.parseInt(Dict.isOnSale.item_1.getItem()));
  142. List<GoodsEntity> list = goodsService.queryList(params);
  143. return R.ok().put("list", list);
  144. }
  145. /**
  146. * 商品回收站
  147. *
  148. * @param params
  149. * @return
  150. */
  151. @RequestMapping("/historyList")
  152. public R historyList(@RequestParam Map<String, Object> params) {
  153. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  154. //查询列表数据
  155. Query query = new Query(params);
  156. query.put("isDelete", 1);
  157. List<GoodsEntity> goodsList = goodsService.queryList(query);
  158. int total = goodsService.queryTotal(query);
  159. PageUtils pageUtil = new PageUtils(goodsList, total, query.getLimit(), query.getPage());
  160. return R.ok().put("page", pageUtil);
  161. }
  162. /**
  163. * 商品从回收站恢复
  164. */
  165. @RequestMapping("/back")
  166. @RequiresPermissions("goods:back")
  167. public R back(@RequestBody Integer[] ids) {
  168. goodsService.back(ids);
  169. return R.ok();
  170. }
  171. /**
  172. * 总计
  173. */
  174. @RequestMapping("/queryTotal")
  175. public R queryTotal(@RequestParam Map<String, Object> params) {
  176. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  177. params.put("isDelete", 0);
  178. int sum = goodsService.queryTotal(params);
  179. return R.ok().put("goodsSum", sum);
  180. }
  181. /**
  182. * 上架
  183. */
  184. @RequestMapping("/enSale")
  185. public R enSale(@RequestBody Integer id) {
  186. goodsService.enSale(id);
  187. return R.ok();
  188. }
  189. /**
  190. * 上架
  191. */
  192. @RequestMapping("/enSaleBatch")
  193. public R enSaleBatch(@RequestBody Integer[] ids) {
  194. goodsService.enSaleBatch(ids);
  195. return R.ok();
  196. }
  197. /**
  198. * 下架
  199. */
  200. @RequestMapping("/unSale")
  201. public R unSale(@RequestBody Integer id) {
  202. goodsService.unSale(id);
  203. return R.ok();
  204. }
  205. /**
  206. * 下架
  207. */
  208. @RequestMapping("/unSaleBatch")
  209. public R unSaleBatch(@RequestBody Integer[] ids) {
  210. goodsService.unSaleBatch(ids);
  211. return R.ok();
  212. }
  213. /**
  214. * 上传文件
  215. */
  216. @RequestMapping("/upload")
  217. public R upload(@RequestParam("file") MultipartFile file) {
  218. List<GoodsDto> goodsDtoList = new ArrayList<>();//商品信息
  219. try {
  220. Map<String, Object> beans = new HashMap<String, Object>();
  221. beans.put("GoodsDtoList", goodsDtoList);
  222. if (file.isEmpty()) {
  223. return R.error("文件不能为空!");
  224. }
  225. excelUtil.readExcel(JxlsXmlTemplateName.GOODS_DTO_LIST, beans, file.getInputStream());
  226. } catch (Exception e) {
  227. e.printStackTrace();
  228. return R.error("导入失败!");
  229. }
  230. int result = goodsService.uploadExcel(goodsDtoList,Integer.parseInt(Dict.exportDataType.item_1.getItem()));
  231. if(0 == result){
  232. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  233. }
  234. //上传文件
  235. return R.ok();
  236. }
  237. /**
  238. * 上传文件
  239. */
  240. @RequestMapping("/generalGoodsUpload")
  241. public R generalGoodsUpload(@RequestParam("file") MultipartFile file) {
  242. List<GoodsDto> generalGoodsDtoList = new ArrayList<>();//商品信息
  243. try {
  244. Map<String, Object> beans = new HashMap<String, Object>();
  245. beans.put("GeneralGoodsDtoList", generalGoodsDtoList);
  246. if (file.isEmpty()) {
  247. return R.error("文件不能为空!");
  248. }
  249. excelUtil.readExcel(JxlsXmlTemplateName.GENERAL_GOODS_DTO_LIST, beans, file.getInputStream());
  250. } catch (Exception e) {
  251. e.printStackTrace();
  252. return R.error("导入失败!");
  253. }
  254. int result = goodsService.uploadExcel(generalGoodsDtoList,Integer.parseInt(Dict.exportDataType.item_2.getItem()));
  255. if(0 == result){
  256. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  257. }
  258. //上传文件
  259. return R.ok();
  260. }
  261. /*@RequestMapping("/scannInfo")
  262. @RequiresPermissions("goods:scannInfo")
  263. public R scannInfo(@RequestParam Map<String, Object> params) {
  264. String goodsSn = (String)params.get("goodsSn");
  265. GoodsEntity goods = goodsService.queryObjectByGoodsSnAndBizType(goodsSn);
  266. if(goods == null) {
  267. return R.error("商品信息不存在");
  268. }
  269. List<OfflineCartEntity> cartEntityList = offlineCartService.offlineGoodsCart(goods);
  270. return R.ok().put("cartEntityList", cartEntityList);
  271. }*/
  272. @RequestMapping("/scannInfo/{prodBarcode}")
  273. @RequiresPermissions("goods:scannInfo")
  274. public R scannInfo(@PathVariable("prodBarcode")String prodBarcode) {
  275. SysUserEntity user = ShiroUtils.getUserEntity();
  276. if(user == null) {
  277. return R.error("用户登录超时,请重新登录");
  278. }
  279. if (!user.getRoleType().equalsIgnoreCase("2")) {
  280. return R.error("该操作只允许店员账户操作");
  281. }
  282. GoodsEntity goods = goodsService.queryObjectByProdBarcodeAndBizType(prodBarcode, user.getStoreId());
  283. if(goods == null) {
  284. return R.error("商品信息不存在");
  285. }
  286. return R.ok().put("goods", goods);
  287. }
  288. @RequestMapping("/details/{prodBarcode}/{storeId}")
  289. // @RequiresPermissions("goods:details") http://127.0.0.1:8080/goods/details/11111
  290. public R details(@PathVariable("prodBarcode")String prodBarcode,@PathVariable("storeId")String storeId) {
  291. SysUserEntity user = ShiroUtils.getUserEntity();
  292. if(user == null) {
  293. return R.error("用户登录超时,请重新登录");
  294. }
  295. if (!user.getRoleType().equalsIgnoreCase("2")) {
  296. return R.error("该操作只允许店员账户操作");
  297. }
  298. Map<String,Object> map = goodsService.calculateGoodsDetail(prodBarcode,storeId);
  299. if(map == null){
  300. return R.error("商品信息不存在");
  301. }
  302. return R.ok().put("goodsDetails", map.get("goods")).put("map",map);
  303. }
  304. /**
  305. * 根据商品编码或者条码查询商品信息(17.商品全景图)
  306. * @param keyword
  307. * @return
  308. */
  309. @GetMapping("/search/{keyword}")
  310. public R searchByKeyword(@PathVariable("keyword") String keyword){
  311. if (keyword == null || "".equals(keyword)){
  312. return R.error("请输入商品编码或者条码!");
  313. }
  314. GoodsPanoramaDto goodsPanoramaDto = goodsService.searchGoodsPanoramaDtoByKeyword(keyword);
  315. //GoodsEntity goods = goodsService.searchGoodsByKeyword(keyword);
  316. if (goodsPanoramaDto == null || "".equals(goodsPanoramaDto)) {
  317. return R.error("没有该商品!");
  318. }
  319. return R.ok().put("goodsPanoramaDto",goodsPanoramaDto);
  320. }
  321. /**
  322. * 所有商品模块导出
  323. * @param params 查询参数
  324. * @param response
  325. * @param request
  326. * @return
  327. */
  328. @RequiresPermissions("goods:export")
  329. @RequestMapping(value = "export")
  330. public R export(@RequestParam Map<String, Object> params, HttpServletResponse response, HttpServletRequest request) {
  331. ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
  332. params.put("isDelete", 0);
  333. String lastSaleTime = (String) params.get("lastSaleTime");
  334. if(org.apache.commons.lang.StringUtils.isNotEmpty(lastSaleTime)) {
  335. try {
  336. lastSaleTime = new String(lastSaleTime.getBytes("iso-8859-1"), "utf-8");
  337. } catch (Exception e) {
  338. e.printStackTrace();
  339. }
  340. lastSaleTime = DateUtils.getDate(lastSaleTime);
  341. params.put("lastSaleTime", lastSaleTime + " 00:00:00");
  342. }
  343. // 根据条件查询出列表
  344. List<GoodsEntity> goodsList = goodsService.queryExportList(params);
  345. ExcelExport ee = new ExcelExport("所有商品信息");
  346. String[] header = new String[]{"商户名称","第三方商户编号","商品编码","SKU","PLU","商品名称","商品英文名称","产品条码","货品业务类型","库存是否共享",
  347. "商品库存","日常价","成本价","是否上架","是否热销","录入日期","商品单位","商品税率","产品品牌","海关备案编号","计量单位","海关商品编码","国检规格型号",
  348. "原产国","海关申报要素","毛重(kg)","净重(kg)"};
  349. List<Map<String, Object>> list = new ArrayList<>();
  350. if (goodsList !=null && goodsList.size()>0){
  351. for (GoodsEntity goodsEntity : goodsList) {
  352. LinkedHashMap<String, Object> map = new LinkedHashMap<>();
  353. map.put("MerchName",goodsEntity.getMerchName());
  354. map.put("ThirdPartyMerchCode",goodsEntity.getThirdPartyMerchCode());
  355. map.put("GoodsSn",goodsEntity.getGoodsSn());
  356. map.put("Sku",goodsEntity.getSku());
  357. map.put("Plu",goodsEntity.getPlu());
  358. map.put("Name",goodsEntity.getName());
  359. map.put("EnglishName",goodsEntity.getEnglishName());
  360. String goodsBizType = goodsEntity.getGoodsBizType();
  361. Integer isStockShare = 0;
  362. if (goodsEntity.getIsStockShare()!=null){
  363. isStockShare = Integer.parseInt(goodsEntity.getIsStockShare());
  364. }
  365. map.put("ProdBarcode",goodsEntity.getProdBarcode());
  366. map.put("GoodsBizType",StringUtils.isEmpty(goodsBizType)?"":Dict.orderBizType.valueOf("item_"+goodsBizType).getItemName());
  367. map.put("IsStockShare",isStockShare==0?"否":"是");
  368. map.put("GoodsNumber",goodsEntity.getGoodsNumber());
  369. map.put("DailyPrice",goodsEntity.getDailyPrice());
  370. map.put("CostPrice",goodsEntity.getCostPrice());
  371. map.put("IsOnSale",goodsEntity.getIsOnSale()==0?"否":"是");
  372. map.put("IsHot",goodsEntity.getIsHot()==0?"否":"是");
  373. map.put("AddTime",goodsEntity.getAddTime());
  374. map.put("GoodsUnit",goodsEntity.getGoodsUnit());
  375. map.put("GoodsRate",goodsEntity.getGoodsRate());
  376. map.put("Brand",goodsEntity.getBrand());
  377. map.put("CusRecCode",goodsEntity.getCusRecCode());
  378. map.put("UnitCode",goodsEntity.getUnitCode());
  379. map.put("CusGoodsCode",goodsEntity.getCusGoodsCode());
  380. map.put("CiqProdModel",goodsEntity.getCiqProdModel());
  381. map.put("OriCntName",goodsEntity.getOriCntName());
  382. map.put("CusDeclEle",goodsEntity.getCusDeclEle());
  383. map.put("GrossWeight",goodsEntity.getGrossWeight());
  384. map.put("NetWeight",goodsEntity.getNetWeight());
  385. list.add(map);
  386. }
  387. }
  388. ee.addSheetByMap("所有商品信息", list, header);
  389. ee.export(response);
  390. return R.ok();
  391. }
  392. }