GoodsController.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. @ResponseBody
  218. public R upload(@RequestParam("file") MultipartFile file) {
  219. List<GoodsDto> goodsDtoList = new ArrayList<>();//商品信息
  220. try {
  221. Map<String, Object> beans = new HashMap<String, Object>();
  222. beans.put("GoodsDtoList", goodsDtoList);
  223. if (file.isEmpty()) {
  224. return R.error("文件不能为空!");
  225. }
  226. excelUtil.readExcel(JxlsXmlTemplateName.GOODS_DTO_LIST, beans, file.getInputStream());
  227. } catch (Exception e) {
  228. e.printStackTrace();
  229. return R.error("导入失败!");
  230. }
  231. goodsService.uploadExcel(goodsDtoList,Integer.parseInt(Dict.exportDataType.item_1.getItem()));
  232. //上传文件
  233. return R.ok();
  234. }
  235. /**
  236. * 上传文件
  237. */
  238. @RequestMapping("/generalGoodsUpload")
  239. @ResponseBody
  240. public R generalGoodsUpload(@RequestParam("file") MultipartFile file) {
  241. List<GoodsDto> generalGoodsDtoList = new ArrayList<>();//商品信息
  242. try {
  243. Map<String, Object> beans = new HashMap<String, Object>();
  244. beans.put("GeneralGoodsDtoList", generalGoodsDtoList);
  245. if (file.isEmpty()) {
  246. return R.error("文件不能为空!");
  247. }
  248. excelUtil.readExcel(JxlsXmlTemplateName.GENERAL_GOODS_DTO_LIST, beans, file.getInputStream());
  249. } catch (Exception e) {
  250. e.printStackTrace();
  251. return R.error("导入失败!");
  252. }
  253. goodsService.uploadExcel(generalGoodsDtoList,Integer.parseInt(Dict.exportDataType.item_2.getItem()));
  254. //上传文件
  255. return R.ok();
  256. }
  257. /*@RequestMapping("/scannInfo")
  258. @RequiresPermissions("goods:scannInfo")
  259. public R scannInfo(@RequestParam Map<String, Object> params) {
  260. String goodsSn = (String)params.get("goodsSn");
  261. GoodsEntity goods = goodsService.queryObjectByGoodsSnAndBizType(goodsSn);
  262. if(goods == null) {
  263. return R.error("商品信息不存在");
  264. }
  265. List<OfflineCartEntity> cartEntityList = offlineCartService.offlineGoodsCart(goods);
  266. return R.ok().put("cartEntityList", cartEntityList);
  267. }*/
  268. @RequestMapping("/scannInfo/{prodBarcode}")
  269. @RequiresPermissions("goods:scannInfo")
  270. public R scannInfo(@PathVariable("prodBarcode")String prodBarcode) {
  271. SysUserEntity user = ShiroUtils.getUserEntity();
  272. if(user == null) {
  273. return R.error("用户登录超时,请重新登录");
  274. }
  275. if (!user.getRoleType().equalsIgnoreCase("2")) {
  276. return R.error("该操作只允许店员账户操作");
  277. }
  278. GoodsEntity goods = goodsService.queryObjectByProdBarcodeAndBizType(prodBarcode, user.getStoreId());
  279. if(goods == null) {
  280. return R.error("商品信息不存在");
  281. }
  282. return R.ok().put("goods", goods);
  283. }
  284. @RequestMapping("/details/{prodBarcode}/{storeId}")
  285. // @RequiresPermissions("goods:details") http://127.0.0.1:8080/goods/details/11111
  286. public R details(@PathVariable("prodBarcode")String prodBarcode,@PathVariable("storeId")String storeId) {
  287. SysUserEntity user = ShiroUtils.getUserEntity();
  288. if(user == null) {
  289. return R.error("用户登录超时,请重新登录");
  290. }
  291. if (!user.getRoleType().equalsIgnoreCase("2")) {
  292. return R.error("该操作只允许店员账户操作");
  293. }
  294. Map<String,Object> map = goodsService.calculateGoodsDetail(prodBarcode,storeId);
  295. if(map == null){
  296. return R.error("商品信息不存在");
  297. }
  298. return R.ok().put("goodsDetails", map.get("goods")).put("map",map);
  299. }
  300. /**
  301. * 根据商品编码或者条码查询商品信息(17.商品全景图)
  302. * @param keyword
  303. * @return
  304. */
  305. @GetMapping("/search/{keyword}")
  306. public R searchByKeyword(@PathVariable("keyword") String keyword){
  307. if (keyword == null || "".equals(keyword)){
  308. return R.error("请输入商品编码或者条码!");
  309. }
  310. GoodsPanoramaDto goodsPanoramaDto = goodsService.searchGoodsPanoramaDtoByKeyword(keyword);
  311. //GoodsEntity goods = goodsService.searchGoodsByKeyword(keyword);
  312. if (goodsPanoramaDto == null || "".equals(goodsPanoramaDto)) {
  313. return R.error("没有该商品!");
  314. }
  315. return R.ok().put("goodsPanoramaDto",goodsPanoramaDto);
  316. }
  317. /**
  318. * 所有商品模块导出
  319. * @param params 查询参数
  320. * @param response
  321. * @param request
  322. * @return
  323. */
  324. @RequiresPermissions("goods:export")
  325. @RequestMapping(value = "export")
  326. public R export(@RequestParam Map<String, Object> params, HttpServletResponse response, HttpServletRequest request) {
  327. ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
  328. params.put("isDelete", 0);
  329. String lastSaleTime = (String) params.get("lastSaleTime");
  330. if(org.apache.commons.lang.StringUtils.isNotEmpty(lastSaleTime)) {
  331. try {
  332. lastSaleTime = new String(lastSaleTime.getBytes("iso-8859-1"), "utf-8");
  333. } catch (Exception e) {
  334. e.printStackTrace();
  335. }
  336. lastSaleTime = DateUtils.getDate(lastSaleTime);
  337. params.put("lastSaleTime", lastSaleTime + " 00:00:00");
  338. }
  339. // 根据条件查询出列表
  340. List<GoodsEntity> goodsList = goodsService.queryExportList(params);
  341. ExcelExport ee = new ExcelExport("所有商品信息");
  342. String[] header = new String[]{"商户名称","第三方商户编号","商品编码","SKU","PLU","商品名称","商品英文名称","产品条码","货品业务类型","库存是否共享",
  343. "商品库存","日常价","成本价","是否上架","是否热销","录入日期","商品单位","商品税率","产品品牌","海关备案编号","计量单位","海关商品编码","国检规格型号",
  344. "原产国","海关申报要素","毛重(kg)","净重(kg)"};
  345. List<Map<String, Object>> list = new ArrayList<>();
  346. if (goodsList !=null && goodsList.size()>0){
  347. for (GoodsEntity goodsEntity : goodsList) {
  348. LinkedHashMap<String, Object> map = new LinkedHashMap<>();
  349. map.put("MerchName",goodsEntity.getMerchName());
  350. map.put("ThirdPartyMerchCode",goodsEntity.getThirdPartyMerchCode());
  351. map.put("GoodsSn",goodsEntity.getGoodsSn());
  352. map.put("Sku",goodsEntity.getSku());
  353. map.put("Plu",goodsEntity.getPlu());
  354. map.put("Name",goodsEntity.getName());
  355. map.put("EnglishName",goodsEntity.getEnglishName());
  356. String goodsBizType = goodsEntity.getGoodsBizType();
  357. Integer isStockShare = 0;
  358. if (goodsEntity.getIsStockShare()!=null){
  359. isStockShare = Integer.parseInt(goodsEntity.getIsStockShare());
  360. }
  361. map.put("ProdBarcode",goodsEntity.getProdBarcode());
  362. map.put("GoodsBizType",StringUtils.isEmpty(goodsBizType)?"":Dict.orderBizType.valueOf("item_"+goodsBizType).getItemName());
  363. map.put("IsStockShare",isStockShare==0?"否":"是");
  364. map.put("GoodsNumber",goodsEntity.getGoodsNumber());
  365. map.put("DailyPrice",goodsEntity.getDailyPrice());
  366. map.put("CostPrice",goodsEntity.getCostPrice());
  367. map.put("IsOnSale",goodsEntity.getIsOnSale()==0?"否":"是");
  368. map.put("IsHot",goodsEntity.getIsHot()==0?"否":"是");
  369. map.put("AddTime",goodsEntity.getAddTime());
  370. map.put("GoodsUnit",goodsEntity.getGoodsUnit());
  371. map.put("GoodsRate",goodsEntity.getGoodsRate());
  372. map.put("Brand",goodsEntity.getBrand());
  373. map.put("CusRecCode",goodsEntity.getCusRecCode());
  374. map.put("UnitCode",goodsEntity.getUnitCode());
  375. map.put("CusGoodsCode",goodsEntity.getCusGoodsCode());
  376. map.put("CiqProdModel",goodsEntity.getCiqProdModel());
  377. map.put("OriCntName",goodsEntity.getOriCntName());
  378. map.put("CusDeclEle",goodsEntity.getCusDeclEle());
  379. map.put("GrossWeight",goodsEntity.getGrossWeight());
  380. map.put("NetWeight",goodsEntity.getNetWeight());
  381. list.add(map);
  382. }
  383. }
  384. ee.addSheetByMap("所有商品信息", list, header);
  385. ee.export(response);
  386. return R.ok();
  387. }
  388. }