package com.kmall.admin.controller; import com.kmall.admin.dto.GoodsDetailsDto; import com.kmall.admin.dto.GoodsDto; import com.kmall.admin.dto.GoodsPanoramaDto; import com.kmall.admin.entity.GoodsEntity; import com.kmall.admin.entity.GoodsGalleryEntity; import com.kmall.admin.entity.mk.MkActivitiesEntity; import com.kmall.admin.entity.mk.MkActivityFormEntity; import com.kmall.admin.service.*; import com.kmall.admin.service.mk.MkActivitiesService; import com.kmall.admin.service.mk.MkActivityFormService; import com.kmall.admin.utils.ParamUtils; import com.kmall.admin.utils.ShiroUtils; import com.kmall.common.constant.Dict; import com.kmall.common.constant.JxlsXmlTemplateName; import com.kmall.admin.fromcomm.entity.SysUserEntity; import com.kmall.common.utils.*; import com.kmall.common.utils.excel.ExcelExport; import com.kmall.common.utils.excel.ExcelUtil; import com.kmall.common.utils.print.ticket.item.Goods; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.text.SimpleDateFormat; import java.util.*; /** * Controller * * @author Scott * @email * @date 2017-08-21 21:19:49 */ @RestController @RequestMapping("goods") public class GoodsController { @Autowired private GoodsService goodsService; @Autowired private GoodsGalleryService goodsGalleryService; @Autowired private OfflineCartService offlineCartService; @Autowired private ExcelUtil excelUtil; @Autowired private StoreService storeService; /** * 查看列表 */ @RequestMapping("/list") @RequiresPermissions("goods:list") public R list(@RequestParam Map params) { ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode"); ParamUtils.setName(params, "name"); String lastSaleTime = (String) params.get("lastSaleTime"); if(org.apache.commons.lang.StringUtils.isNotEmpty(lastSaleTime)) { try { lastSaleTime = new String(lastSaleTime.getBytes("iso-8859-1"), "utf-8"); } catch (Exception e) { e.printStackTrace(); } lastSaleTime = DateUtils.getDate(lastSaleTime); params.put("lastSaleTime", lastSaleTime + " 00:00:00"); } //查询列表数据 Query query = new Query(params); query.put("isDelete", 0); List goodsList = goodsService.queryList(query); int total = goodsService.queryTotal(query); PageUtils pageUtil = new PageUtils(goodsList, total, query.getLimit(), query.getPage()); return R.ok().put("page", pageUtil); } /** * 查看信息 */ @RequestMapping("/info/{id}") @RequiresPermissions("goods:info") public R info(@PathVariable("id") Integer id) { GoodsEntity goods = goodsService.queryObject(id); if(goods != null) { GoodsGalleryEntity goodsGalleryEntity =goodsGalleryService.queryVideoObjectByGoodId(goods.getId()); if(goodsGalleryEntity != null){ goods.setVideoUrl(goodsGalleryEntity.getImgUrl()); } } return R.ok().put("goods", goods); } /** * 查看信息 */ @RequestMapping("/infoByQuery") public R infoByQuery(@RequestParam Map params) { ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode"); ParamUtils.setName(params, "name"); //查询列表数据 Query query = new Query(params); query.put("isDelete", 0); List goodsList = goodsService.queryList(query); if(goodsList != null && goodsList.size() != 0) { return R.ok().put("goods", goodsList.get(0)); } return R.ok().put("goods", new GoodsEntity()); } /** * 保存 */ @RequestMapping("/save") @RequiresPermissions("goods:save") public R save(@RequestBody GoodsEntity goods) { goodsService.save(goods); return R.ok(); } /** * 修改 */ @RequestMapping("/update") @RequiresPermissions("goods:update") public R update(@RequestBody GoodsEntity goods) { goodsService.update(goods); return R.ok(); } /** * 删除 */ @RequestMapping("/delete") @RequiresPermissions("goods:delete") public R delete(@RequestBody Integer[] ids) { goodsService.deleteBatch(ids); return R.ok(); } /** * 查看所有列表 */ @RequestMapping("/queryAll") public R queryAll(@RequestParam Map params) { ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode"); params.put("isDelete", Integer.parseInt(Dict.isDelete.item_0.getItem())); params.put("isOnSale", Integer.parseInt(Dict.isOnSale.item_1.getItem())); List list = goodsService.queryList(params); return R.ok().put("list", list); } /** * 商品回收站 * * @param params * @return */ @RequestMapping("/historyList") public R historyList(@RequestParam Map params) { ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode"); //查询列表数据 Query query = new Query(params); query.put("isDelete", 1); List goodsList = goodsService.queryList(query); int total = goodsService.queryTotal(query); PageUtils pageUtil = new PageUtils(goodsList, total, query.getLimit(), query.getPage()); return R.ok().put("page", pageUtil); } /** * 商品从回收站恢复 */ @RequestMapping("/back") @RequiresPermissions("goods:back") public R back(@RequestBody Integer[] ids) { goodsService.back(ids); return R.ok(); } /** * 总计 */ @RequestMapping("/queryTotal") public R queryTotal(@RequestParam Map params) { ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode"); params.put("isDelete", 0); int sum = goodsService.queryTotal(params); return R.ok().put("goodsSum", sum); } /** * 上架 */ @RequestMapping("/enSale") public R enSale(@RequestBody Integer id) { goodsService.enSale(id); return R.ok(); } /** * 上架 */ @RequestMapping("/enSaleBatch") public R enSaleBatch(@RequestBody Integer[] ids) { goodsService.enSaleBatch(ids); return R.ok(); } /** * 下架 */ @RequestMapping("/unSale") public R unSale(@RequestBody Integer id) { goodsService.unSale(id); return R.ok(); } /** * 下架 */ @RequestMapping("/unSaleBatch") public R unSaleBatch(@RequestBody Integer[] ids) { goodsService.unSaleBatch(ids); return R.ok(); } /** * 上传文件 */ @RequestMapping("/upload") public R upload(@RequestParam("file") MultipartFile file) { List goodsDtoList = new ArrayList<>();//商品信息 try { Map beans = new HashMap(); beans.put("GoodsDtoList", goodsDtoList); if (file.isEmpty()) { return R.error("文件不能为空!"); } excelUtil.readExcel(JxlsXmlTemplateName.GOODS_DTO_LIST, beans, file.getInputStream()); } catch (Exception e) { e.printStackTrace(); return R.error("导入失败!"); } int result = goodsService.uploadExcel(goodsDtoList,Integer.parseInt(Dict.exportDataType.item_1.getItem())); if(0 == result){ throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查"); } //上传文件 return R.ok(); } /** * 上传文件 */ @RequestMapping("/generalGoodsUpload") public R generalGoodsUpload(@RequestParam("file") MultipartFile file) { List generalGoodsDtoList = new ArrayList<>();//商品信息 try { Map beans = new HashMap(); beans.put("GeneralGoodsDtoList", generalGoodsDtoList); if (file.isEmpty()) { return R.error("文件不能为空!"); } excelUtil.readExcel(JxlsXmlTemplateName.GENERAL_GOODS_DTO_LIST, beans, file.getInputStream()); } catch (Exception e) { e.printStackTrace(); return R.error("导入失败!"); } int result = goodsService.uploadExcel(generalGoodsDtoList,Integer.parseInt(Dict.exportDataType.item_2.getItem())); if(0 == result){ throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查"); } //上传文件 return R.ok(); } /*@RequestMapping("/scannInfo") @RequiresPermissions("goods:scannInfo") public R scannInfo(@RequestParam Map params) { String goodsSn = (String)params.get("goodsSn"); GoodsEntity goods = goodsService.queryObjectByGoodsSnAndBizType(goodsSn); if(goods == null) { return R.error("商品信息不存在"); } List cartEntityList = offlineCartService.offlineGoodsCart(goods); return R.ok().put("cartEntityList", cartEntityList); }*/ @RequestMapping("/scannInfo/{prodBarcode}") @RequiresPermissions("goods:scannInfo") public R scannInfo(@PathVariable("prodBarcode")String prodBarcode) { SysUserEntity user = ShiroUtils.getUserEntity(); if(user == null) { return R.error("用户登录超时,请重新登录"); } if (!user.getRoleType().equalsIgnoreCase("2")) { return R.error("该操作只允许店员账户操作"); } GoodsEntity goods = goodsService.queryObjectByProdBarcodeAndBizType(prodBarcode, user.getStoreId()); if(goods == null) { return R.error("商品信息不存在"); } return R.ok().put("goods", goods); } @RequestMapping("/details/{prodBarcode}/{storeId}") // @RequiresPermissions("goods:details") http://127.0.0.1:8080/goods/details/11111 public R details(@PathVariable("prodBarcode")String prodBarcode,@PathVariable("storeId")String storeId) { SysUserEntity user = ShiroUtils.getUserEntity(); if(user == null) { return R.error("用户登录超时,请重新登录"); } if (!user.getRoleType().equalsIgnoreCase("2")) { return R.error("该操作只允许店员账户操作"); } Map map = goodsService.calculateGoodsDetail(prodBarcode,storeId); if(map == null){ return R.error("商品信息不存在"); } return R.ok().put("goodsDetails", map.get("goods")).put("map",map); } /** * 根据商品编码或者条码查询商品信息(17.商品全景图) * @param keyword * @return */ @GetMapping("/search/{keyword}") public R searchByKeyword(@PathVariable("keyword") String keyword){ if (keyword == null || "".equals(keyword)){ return R.error("请输入商品编码或者条码!"); } GoodsPanoramaDto goodsPanoramaDto = goodsService.searchGoodsPanoramaDtoByKeyword(keyword); //GoodsEntity goods = goodsService.searchGoodsByKeyword(keyword); if (goodsPanoramaDto == null || "".equals(goodsPanoramaDto)) { return R.error("没有该商品!"); } return R.ok().put("goodsPanoramaDto",goodsPanoramaDto); } /** * 所有商品模块导出 * @param params 查询参数 * @param response * @param request * @return */ @RequiresPermissions("goods:export") @RequestMapping(value = "export") public R export(@RequestParam Map params, HttpServletResponse response, HttpServletRequest request) { ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode"); params.put("isDelete", 0); String lastSaleTime = (String) params.get("lastSaleTime"); if(org.apache.commons.lang.StringUtils.isNotEmpty(lastSaleTime)) { try { lastSaleTime = new String(lastSaleTime.getBytes("iso-8859-1"), "utf-8"); } catch (Exception e) { e.printStackTrace(); } lastSaleTime = DateUtils.getDate(lastSaleTime); params.put("lastSaleTime", lastSaleTime + " 00:00:00"); } // 根据条件查询出列表 List goodsList = goodsService.queryExportList(params); ExcelExport ee = new ExcelExport("所有商品信息"); String[] header = new String[]{"商户名称","第三方商户编号","商品编码","SKU","PLU","商品名称","商品英文名称","产品条码","货品业务类型","库存是否共享", "商品库存","日常价","成本价","是否上架","是否热销","录入日期","商品单位","商品税率","产品品牌","海关备案编号","计量单位","海关商品编码","国检规格型号", "原产国","海关申报要素","毛重(kg)","净重(kg)"}; List> list = new ArrayList<>(); if (goodsList !=null && goodsList.size()>0){ for (GoodsEntity goodsEntity : goodsList) { LinkedHashMap map = new LinkedHashMap<>(); map.put("MerchName",goodsEntity.getMerchName()); map.put("ThirdPartyMerchCode",goodsEntity.getThirdPartyMerchCode()); map.put("GoodsSn",goodsEntity.getGoodsSn()); map.put("Sku",goodsEntity.getSku()); map.put("Plu",goodsEntity.getPlu()); map.put("Name",goodsEntity.getName()); map.put("EnglishName",goodsEntity.getEnglishName()); String goodsBizType = goodsEntity.getGoodsBizType(); Integer isStockShare = 0; if (goodsEntity.getIsStockShare()!=null){ isStockShare = Integer.parseInt(goodsEntity.getIsStockShare()); } map.put("ProdBarcode",goodsEntity.getProdBarcode()); map.put("GoodsBizType",StringUtils.isEmpty(goodsBizType)?"":Dict.orderBizType.valueOf("item_"+goodsBizType).getItemName()); map.put("IsStockShare",isStockShare==0?"否":"是"); map.put("GoodsNumber",goodsEntity.getGoodsNumber()); map.put("DailyPrice",goodsEntity.getDailyPrice()); map.put("CostPrice",goodsEntity.getCostPrice()); map.put("IsOnSale",goodsEntity.getIsOnSale()==0?"否":"是"); map.put("IsHot",goodsEntity.getIsHot()==0?"否":"是"); map.put("AddTime",goodsEntity.getAddTime()); map.put("GoodsUnit",goodsEntity.getGoodsUnit()); map.put("GoodsRate",goodsEntity.getGoodsRate()); map.put("Brand",goodsEntity.getBrand()); map.put("CusRecCode",goodsEntity.getCusRecCode()); map.put("UnitCode",goodsEntity.getUnitCode()); map.put("CusGoodsCode",goodsEntity.getCusGoodsCode()); map.put("CiqProdModel",goodsEntity.getCiqProdModel()); map.put("OriCntName",goodsEntity.getOriCntName()); map.put("CusDeclEle",goodsEntity.getCusDeclEle()); map.put("GrossWeight",goodsEntity.getGrossWeight()); map.put("NetWeight",goodsEntity.getNetWeight()); list.add(map); } } ee.addSheetByMap("所有商品信息", list, header); ee.export(response); return R.ok(); } }