GoodsController.java 16 KB

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