package com.sqx.modules.printInfo.controller; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.sqx.common.utils.Result; import com.sqx.modules.address.entity.Address; import com.sqx.modules.goods.entity.GoodsShop; import com.sqx.modules.goods.service.GoodsShopService; import com.sqx.modules.order.entity.TbOrder; import com.sqx.modules.order.service.AppOrderService; import com.sqx.modules.order.service.impl.AppAppOrderServiceImpl; import com.sqx.modules.printInfo.entity.*; import com.sqx.modules.printInfo.service.PrintInfoService; import com.sqx.modules.printInfo.service.PrintInfoShopService; import com.sqx.modules.utils.fieYun.FeiYunUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** *

* 活动管理 *

* * @author 吴畅 * @since 2022-11-04 */ @RestController @Api(value = "管理端-打印模板信息", tags = {"管理端-打印模板信息"}) @RequestMapping("/admin/printInfo/") public class PrintInfoController { @Autowired private PrintInfoService printInfoService; @Autowired private PrintInfoShopService printInfoShopService; @Autowired private AppAppOrderServiceImpl appAppOrderService; @Autowired private GoodsShopService goodsShopService; @GetMapping("testPrintModel") @ApiOperation("获取打印信息列表") public Result testPrintModel(Long orderId) { // 查询订单信息 TbOrder tbOrder = appAppOrderService.selectOrderByIds(orderId); Long shopId = tbOrder.getShopId(); // 查询店铺信息 GoodsShop goodsShop = goodsShopService.selectGoodDatas(shopId); // 查询当天店铺所有已支付的订单号,按支付时间排序 tbOrder.setCountOrder(3); if (StringUtils.isNotEmpty(goodsShop.getSnCode())) { FeiYunUtils.print(goodsShop.getSnCode(), null, tbOrder); } else { return Result.error("当前商家未绑定打印机!"); } return Result.success(); } @GetMapping("getPrintInfoList") @ApiOperation("获取打印信息列表") public Result getPrintInfoList(Integer page, Integer limit) { return printInfoService.selectPrintInfoList(page, limit); } @GetMapping("getPrintInfo") @ApiOperation("获取打印信息") public Result getPrintInfoInfo(Integer id) { return printInfoService.selectPrintInfoById(id); } @PostMapping("insertPrintInfo") @ApiOperation("新增打印信息") public Result insertPrintInfo(@RequestBody PrintInfo printInfo) { return printInfoService.insertPrintInfo(printInfo); } @GetMapping("updatePrintInfo") @ApiOperation("更新打印信息") private Result updatePrintInfo(@RequestBody PrintInfo printInfo) { return printInfoService.updatePrintInfo(printInfo); } @GetMapping("getPrintDropList") @ApiOperation("获取打印信息类型下拉列表") public Result getPrintDropList() { int length = ePrintStatu.values().length; List result = new ArrayList<>(); for (int i = 1; i < length + 1; i++) { PrintDropVo data = new PrintDropVo(); data.setId(i); data.setName(ePrintStatu.stringOf(i)); result.add(data); } return Result.success().put("data", result); } /** * 根据店铺ID获取对应店铺的打印模板信息 * * @param shopId * @return */ @GetMapping("getPrintModelData") @ApiOperation("获取打印模板信息") public Result getPrintModelData(Integer shopId) throws JsonProcessingException { if (shopId == null) { return Result.error("商铺ID不能为空"); } PrintResultModelVo resultData = new PrintResultModelVo(); resultData.setShopId(shopId); List result = new ArrayList<>(); List infos = printInfoService.printInfos(); List typeIds = infos.stream().map(PrintInfo::getTypeId).distinct().collect(Collectors.toList()); PrintInfoShop pis = printInfoShopService.selectPrintInfoShopById(shopId); if (pis == null) { for (Integer typeId : typeIds) { List nowInfos = infos.stream().filter(e -> e.getTypeId().intValue() == typeId.intValue()).collect(Collectors.toList()); PrintModelResult data = new PrintModelResult(); List details = new ArrayList<>(); List types = new ArrayList<>(); for (PrintInfo nowInfo : nowInfos) { PrintModelDetailResult detailData = new PrintModelDetailResult(); detailData.setId(nowInfo.getId()); detailData.setPrintField(nowInfo.getPrintField()); detailData.setPrintName(nowInfo.getPrintName()); detailData.setIsCheck(0); details.add(detailData); PrintModelTypeResult typeData = new PrintModelTypeResult(); typeData.setId(nowInfo.getId()); typeData.setIsBlod(0); typeData.setWordSize(0); typeData.setPrintField(nowInfo.getPrintField()); typeData.setPrintName(nowInfo.getPrintName()); types.add(typeData); } if (nowInfos != null && nowInfos.size() > 0) { data.setId(typeId); data.setName(nowInfos.get(0).getTypeName()); } data.setDetails(details); data.setTypes(types); result.add(data); } } else { //要修改获取逻辑 resultData.setId(pis.getId()); if (pis.getBaseInfo().isEmpty()) { PrintModelResult data = new PrintModelResult(); List nowInfos = infos.stream().filter(e -> e.getTypeId().intValue() == ePrintStatu.Base.getValue()).collect(Collectors.toList()); List details = new ArrayList<>(); List types = new ArrayList<>(); for (PrintInfo nowInfo : nowInfos) { PrintModelDetailResult detailData = new PrintModelDetailResult(); detailData.setId(nowInfo.getId()); detailData.setPrintField(nowInfo.getPrintField()); detailData.setPrintName(nowInfo.getPrintName()); detailData.setIsCheck(0); details.add(detailData); PrintModelTypeResult typeData = new PrintModelTypeResult(); typeData.setId(nowInfo.getId()); typeData.setIsBlod(0); typeData.setWordSize(0); typeData.setPrintField(nowInfo.getPrintField()); typeData.setPrintName(nowInfo.getPrintName()); types.add(typeData); } data.setId(ePrintStatu.Base.getValue()); data.setName(nowInfos.get(0).getTypeName()); data.setDetails(details); data.setTypes(types); result.add(data); } else { ObjectMapper objectMapper = new ObjectMapper(); PrintModelResult data = objectMapper.readValue(pis.getBaseInfo(), PrintModelResult.class); result.add(data); } if (pis.getGoodInfo().isEmpty()) { PrintModelResult data = new PrintModelResult(); List nowInfos = infos.stream().filter(e -> e.getTypeId().intValue() == ePrintStatu.Good.getValue()).collect(Collectors.toList()); List details = new ArrayList<>(); List types = new ArrayList<>(); for (PrintInfo nowInfo : nowInfos) { PrintModelDetailResult detailData = new PrintModelDetailResult(); detailData.setId(nowInfo.getId()); detailData.setPrintField(nowInfo.getPrintField()); detailData.setPrintName(nowInfo.getPrintName()); detailData.setIsCheck(0); details.add(detailData); PrintModelTypeResult typeData = new PrintModelTypeResult(); typeData.setId(nowInfo.getId()); typeData.setIsBlod(0); typeData.setWordSize(0); typeData.setPrintField(nowInfo.getPrintField()); typeData.setPrintName(nowInfo.getPrintName()); types.add(typeData); } data.setId(ePrintStatu.Good.getValue()); data.setName(nowInfos.get(0).getTypeName()); data.setDetails(details); data.setTypes(types); result.add(data); } else { ObjectMapper objectMapper = new ObjectMapper(); PrintModelResult data = objectMapper.readValue(pis.getGoodInfo(), PrintModelResult.class); result.add(data); } if (pis.getPayInfo().isEmpty()) { PrintModelResult data = new PrintModelResult(); List nowInfos = infos.stream().filter(e -> e.getTypeId().intValue() == ePrintStatu.Pay.getValue()).collect(Collectors.toList()); List details = new ArrayList<>(); List types = new ArrayList<>(); for (PrintInfo nowInfo : nowInfos) { PrintModelDetailResult detailData = new PrintModelDetailResult(); detailData.setId(nowInfo.getId()); detailData.setPrintField(nowInfo.getPrintField()); detailData.setPrintName(nowInfo.getPrintName()); detailData.setIsCheck(0); details.add(detailData); PrintModelTypeResult typeData = new PrintModelTypeResult(); typeData.setId(nowInfo.getId()); typeData.setIsBlod(0); typeData.setWordSize(0); typeData.setPrintField(nowInfo.getPrintField()); typeData.setPrintName(nowInfo.getPrintName()); types.add(typeData); } data.setId(ePrintStatu.Pay.getValue()); data.setName(nowInfos.get(0).getTypeName()); data.setDetails(details); data.setTypes(types); result.add(data); } else { ObjectMapper objectMapper = new ObjectMapper(); PrintModelResult data = objectMapper.readValue(pis.getPayInfo(), PrintModelResult.class); result.add(data); } if (pis.getUserInfo().isEmpty()) { PrintModelResult data = new PrintModelResult(); List nowInfos = infos.stream().filter(e -> e.getTypeId().intValue() == ePrintStatu.User.getValue()).collect(Collectors.toList()); List details = new ArrayList<>(); List types = new ArrayList<>(); for (PrintInfo nowInfo : nowInfos) { PrintModelDetailResult detailData = new PrintModelDetailResult(); detailData.setId(nowInfo.getId()); detailData.setPrintField(nowInfo.getPrintField()); detailData.setPrintName(nowInfo.getPrintName()); detailData.setIsCheck(0); details.add(detailData); PrintModelTypeResult typeData = new PrintModelTypeResult(); typeData.setId(nowInfo.getId()); typeData.setIsBlod(0); typeData.setWordSize(0); typeData.setPrintField(nowInfo.getPrintField()); typeData.setPrintName(nowInfo.getPrintName()); types.add(typeData); } data.setId(ePrintStatu.User.getValue()); data.setName(nowInfos.get(0).getTypeName()); data.setDetails(details); data.setTypes(types); result.add(data); } else { ObjectMapper objectMapper = new ObjectMapper(); PrintModelResult data = objectMapper.readValue(pis.getUserInfo(), PrintModelResult.class); result.add(data); } } resultData.setModelDatas(result); return Result.success().put("data", resultData); } /** * 根据店铺ID获取对应店铺的打印模板信息 * * @param printResult 模板信息数据 * @return */ @PostMapping("insertPrintModelData") @ApiOperation("新增打印模板信息") public Result insertPrintModelData(@RequestBody PrintResultModelVo printResult) { PrintInfoShop pis = new PrintInfoShop(); pis.setShopId(printResult.getShopId()); if (printResult != null && printResult.getModelDatas() != null && printResult.getModelDatas().size() > 0) { for (PrintModelResult pmr : printResult.getModelDatas()) { switch (pmr.getId().intValue()) { case 1://基本信息:1 pis.setBaseInfo(JSON.toJSONString(pmr)); break; case 2:// 商品信息:2 pis.setGoodInfo(JSON.toJSONString(pmr)); break; case 3:// 支付信息:3 pis.setPayInfo(JSON.toJSONString(pmr)); break; case 4:// 用户信息:4 pis.setUserInfo(JSON.toJSONString(pmr)); break; default: break; } } } return printInfoShopService.insertPrintInfoShop(pis); } /** * 根据店铺ID获取对应店铺的打印模板信息 * * @param printResult 模板信息数据 * @return */ @PostMapping("updatePrintModelData") @ApiOperation("更新打印模板信息") public Result updatePrintModelData(@RequestBody PrintResultModelVo printResult) { PrintInfoShop pis = new PrintInfoShop(); pis.setId(printResult.getId()); pis.setShopId(printResult.getShopId()); if (printResult != null && printResult.getModelDatas() != null && printResult.getModelDatas().size() > 0) { for (PrintModelResult pmr : printResult.getModelDatas()) { switch (pmr.getId().intValue()) { case 1://基本信息:1 pis.setBaseInfo(JSON.toJSONString(pmr)); break; case 2:// 商品信息:2 pis.setGoodInfo(JSON.toJSONString(pmr)); break; case 3:// 支付信息:3 pis.setPayInfo(JSON.toJSONString(pmr)); break; case 4:// 用户信息:4 pis.setUserInfo(JSON.toJSONString(pmr)); break; default: break; } } } return printInfoShopService.updatePrintInfoShop(pis); } /** * 测试打印功能 */ @GetMapping("testPrintInfo") @ApiOperation("测试打印功能") public Result testPrintInfo(String sn) { FeiYunUtils.testPrint(sn); return Result.success(); } }