package com.ydtech.modules.order.controller; import cn.dev33.satoken.annotation.SaIgnore; import cn.hutool.core.collection.CollUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.ydtech.exception.SystemException; import com.ydtech.modules.admin.constants.UserConstants; import com.ydtech.modules.admin.model.po.SysBarcodeConfig; import com.ydtech.modules.admin.service.SysBarcodeConfigService; import com.ydtech.modules.admin.service.SysQRCodeRelationService; import com.ydtech.modules.esm.model.EsmInsCompany; import com.ydtech.modules.esm.service.EsmInsCompanyService; import com.ydtech.modules.order.entity.InsAreaCompany; import com.ydtech.modules.order.entity.InsOrderQrCode; import com.ydtech.modules.order.entity.InsOrders; import com.ydtech.modules.order.entity.vo.QrCodeVo; import com.ydtech.modules.order.service.InsAreaCompanyService; import com.ydtech.modules.order.service.InsOrderQrCodeService; import com.ydtech.modules.order.service.InsOrdersService; import com.ydtech.modules.protocol.constants.ValidStatus; import com.ydtech.modules.protocol.entity.po.PtlAgreement; import com.ydtech.modules.protocol.service.PtlAgreementService; import com.ydtech.utils.StringUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.json.MappingJackson2JsonView; import javax.annotation.Resource; import java.io.IOException; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Objects; @Api(tags = "订单二维码管理") @Controller @RequestMapping(value = "/order/qrCode") public class QrCodeController { @Resource private InsOrderQrCodeService insOrderQrCodeService; @Resource private InsAreaCompanyService insAreaCompanyService; @Resource SysQRCodeRelationService sysQRCodeRelationService; @Resource SysBarcodeConfigService sysBarcodeConfigService; @Resource InsOrdersService insOrdersService; @Resource private PtlAgreementService ptlAgreementService; //二维码生成地址 @Value("${qrcode.genAddress}") private String qrCodeGenAddress; //二维码生成地址 @Value("${qrcode.imageSrc}") private String imageSrc; @Autowired private EsmInsCompanyService esmInsCompanyService; @GetMapping(value = "/getQrCode") @ApiOperation(value = "获取二维码") public ModelAndView getQrCode(String areaCompanyId) { InsOrderQrCode insOrderQrCode = insOrderQrCodeService.getOneOrSave(areaCompanyId); InsAreaCompany insAreaCompany = insAreaCompanyService.getById(areaCompanyId); Integer qrCodeType = sysQRCodeRelationService.getQRCodeType(insAreaCompany.getApiType(), insAreaCompany.getCompanyId(), insAreaCompany.getInscompany()); ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView()); if (!Objects.isNull(insOrderQrCode)) { InsOrders insOrders = insOrdersService.getById(insOrderQrCode.getOrderno()); QrCodeVo qrCodeVo = new QrCodeVo(); String qrCodeUrl = qrCodeGenAddress + "?areaCompanyId=" + areaCompanyId; qrCodeVo.setQrCodeUrl(qrCodeUrl); qrCodeVo.setInscompany(insAreaCompany.getInscompany());//保险公司 qrCodeVo.setSumpremium(insAreaCompany.getSumpremium());//总保费 qrCodeVo.setSypremium(insAreaCompany.getSypremium());//商业险 qrCodeVo.setJqpremium(insAreaCompany.getJqpremium());//交强险 qrCodeVo.setTaxamount(insAreaCompany.getTaxamount());//车船税 qrCodeVo.setJypremium(insAreaCompany.getJypremium());//驾驭险 qrCodeVo.setCApplyNme(insOrders.getApplyinfo().getString("name"));//投保人 qrCodeVo.setCInsuredNme(insOrders.getInsureinfo().getString("name"));//被保人 qrCodeVo.setCOwnerNme(insOrders.getOwnerinfo().getString("name"));//车主 qrCodeVo.setCPlateNo(insOrders.getLicenseno());//车牌号 qrCodeVo.setJqStartDate(insAreaCompany.getJqStartDate());//交强开始时间 qrCodeVo.setJqEndDate(insAreaCompany.getJqEndDate());//交强结束时间 qrCodeVo.setSyStartDate(insAreaCompany.getSyStartDate()); qrCodeVo.setSysEndDate(insAreaCompany.getSyEndDate()); qrCodeVo.setQrCodeType(qrCodeType); modelAndView.addObject("code", 200); modelAndView.addObject("data", qrCodeVo); modelAndView.addObject("msg", ""); } else { modelAndView.addObject("code", 500); modelAndView.addObject("data", ""); modelAndView.addObject("msg", "二维码不存在"); } return modelAndView; } @PostMapping(value = "/destructionQrCode") @ApiOperation(value = "销毁二维码") public ModelAndView destructionQrCode(String areaCompanyId) { ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView()); boolean b = insOrderQrCodeService.updateQrCode(areaCompanyId, 0); if (b) { modelAndView.addObject("code", 200); modelAndView.addObject("data", "二维码销毁成功"); modelAndView.addObject("msg", ""); return modelAndView; } throw new SystemException("二维码销毁失败"); } @GetMapping(value = "/index") @ApiOperation(value = "扫描二维码地址") @SaIgnore public Object index(String areaCompanyId) throws IOException { InsOrderQrCode insOrderQrCode = insOrderQrCodeService.getOne(areaCompanyId); InsAreaCompany insAreaCompany = insAreaCompanyService.getById(areaCompanyId); if (ValidStatus.AVS_0.getCode().equals(Integer.parseInt(insAreaCompany.getCodeValidStatus()))) { //重定向到错误页面中 return getModelAndView("费用失效,请重新报价。"); } // 根据保险公司获取二维码配置方式 LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(SysBarcodeConfig::getCompanyId, insAreaCompany.getCompanyId()) .eq(SysBarcodeConfig::getApiType, insAreaCompany.getApiType()); List configs = sysBarcodeConfigService.list(wrapper); if (CollUtil.isEmpty(getConfigs(configs)) || configs.size() > 1) { //重定向到错误页面中 return getModelAndView("配置已失效,请联系客服处理"); } Integer configType = configs.get(0).getType(); if (insOrderQrCode == null) { //重定向到错误页面中 return getModelAndView(""); } //更新二维码扫描时间 insOrderQrCode.setScanCodeTime(new Date()); insOrderQrCodeService.updateById(insOrderQrCode); LocalDateTime createtime = insAreaCompany.getCreatetime(); LocalDate qrcodeCreateTime = createtime.toLocalDate(); LocalDate currentDate = LocalDate.now(); if (Math.abs(currentDate.toEpochDay() - qrcodeCreateTime.toEpochDay()) >= 1) { //重定向到错误页面中 return getModelAndView("二维码仅当天支付有效"); } PtlAgreement ptlAgreement = ptlAgreementService.getById(insAreaCompany.getAgreementId()); // 禁用状态 if (ValidStatus.AVS_0.getCode().equals(Integer.parseInt(ptlAgreement.getValidStatus()))) { //重定向到错误页面中 return getModelAndView(""); } LocalDateTime endDate = ptlAgreement.getEndDate(); // 订单创建时间在协议失效时间之后提示错误 if (insAreaCompany.getCreatetime().isAfter(endDate)) { return getModelAndView(""); } if (UserConstants.CONFIG_TYPE_2.equals(configType) || UserConstants.CONFIG_TYPE_1.equals(configType)) { InsOrders insOrders = insOrdersService.getById(insOrderQrCode.getOrderno()); // 获取图标 EsmInsCompany company = esmInsCompanyService.getById(insAreaCompany.getCompanyId()); ModelAndView modelAndView = new ModelAndView(); QrCodeVo qrCodeVo = new QrCodeVo(); qrCodeVo.setLogo(company.getLogo()); if (UserConstants.CONFIG_TYPE_1.equals(configType)) { modelAndView.setViewName("qrcode1"); qrCodeVo.setQrCodeUrl("data:image/png;base64," + insAreaCompany.getPaymentLink()); } else { modelAndView.setViewName("qrcode"); qrCodeVo.setQrCodeUrl(insAreaCompany.getPaymentLink()); } qrCodeVo.setImageSrc(imageSrc); qrCodeVo.setInscompany(insAreaCompany.getInscompany());//保险公司 qrCodeVo.setInscompanyCode(insAreaCompany.getCompanyId());//保险公司编号 qrCodeVo.setSumpremium(insAreaCompany.getSumpremium());//总保费 qrCodeVo.setSypremium(insAreaCompany.getSypremium());//商业险 qrCodeVo.setJqpremium(insAreaCompany.getJqpremium());//交强险 qrCodeVo.setTaxamount(insAreaCompany.getTaxamount());//车船税 qrCodeVo.setJypremium(insAreaCompany.getJypremium());//驾驭险 qrCodeVo.setCApplyNme(insOrders.getApplyinfo().getString("name"));//投保人 qrCodeVo.setCInsuredNme(insOrders.getInsureinfo().getString("name"));//被保人 qrCodeVo.setCOwnerNme(insOrders.getOwnerinfo().getString("name"));//车主 qrCodeVo.setCPlateNo(insOrders.getLicenseno());//车牌号 qrCodeVo.setJqStartDate(insAreaCompany.getJqStartDate());//交强开始时间 qrCodeVo.setJqEndDate(insAreaCompany.getJqEndDate());//交强结束时间 qrCodeVo.setSyStartDate(insAreaCompany.getSyStartDate()); qrCodeVo.setSysEndDate(insAreaCompany.getSyEndDate()); qrCodeVo.setSyStartDate(insAreaCompany.getSyStartDate()); qrCodeVo.setSysEndDate(insAreaCompany.getSyEndDate()); modelAndView.addObject("data", qrCodeVo); return modelAndView; } else if (UserConstants.CONFIG_TYPE_0.equals(configType)) { return "redirect:" + insOrderQrCode.getJumpUrl(); } else { return getModelAndView("配置已失效,请联系客服处理"); } } private static List getConfigs(List configs) { return configs; } private static ModelAndView getModelAndView(String message) { //重定向到错误页面中 ModelAndView modelAndView = new ModelAndView("qrcode_error"); HashMap map = new HashMap<>(); if (StringUtils.isNotEmpty(message)) { map.put("message", message); } else { map.put("message", "协议已失效,请联系客服处理"); } modelAndView.addAllObjects(map); return modelAndView; } @GetMapping(value = "/error") @ApiOperation(value = "错误页面显示") public String error() { return "qrcode_error"; } }