|  | @@ -1,6 +1,5 @@
 | 
	
		
			
				|  |  |  package com.ym.mec.biz.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
	
	
		
			
				|  | @@ -18,7 +17,7 @@ import com.ym.mec.biz.dal.entity.*;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.entity.TenantContractRecord.TenantContractRecordEnum;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.enums.*;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.vo.PlatformServePageVo;
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.dal.vo.TenantInfoInfoPageVo;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.vo.TenantInfoPageVo;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.service.*;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.exception.BizException;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.page.PageInfo;
 | 
	
	
		
			
				|  | @@ -55,6 +54,7 @@ import javax.servlet.http.HttpServletRequest;
 | 
	
		
			
				|  |  |  import java.io.File;
 | 
	
		
			
				|  |  |  import java.io.IOException;
 | 
	
		
			
				|  |  |  import java.math.BigDecimal;
 | 
	
		
			
				|  |  | +import java.math.MathContext;
 | 
	
		
			
				|  |  |  import java.math.RoundingMode;
 | 
	
		
			
				|  |  |  import java.nio.charset.Charset;
 | 
	
		
			
				|  |  |  import java.util.*;
 | 
	
	
		
			
				|  | @@ -488,8 +488,8 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |       * @return
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public PageInfo<TenantInfoInfoPageVo> queryPage(Map<String, Object> param) {
 | 
	
		
			
				|  |  | -        Page<TenantInfoInfoPageVo> pageInfo = PageUtil.getPageInfo(param);
 | 
	
		
			
				|  |  | +    public PageInfo<TenantInfoPageVo> queryPage(Map<String, Object> param) {
 | 
	
		
			
				|  |  | +        Page<TenantInfoPageVo> pageInfo = PageUtil.getPageInfo(param);
 | 
	
		
			
				|  |  |          pageInfo.setAsc("a.id_");
 | 
	
		
			
				|  |  |          return PageUtil.pageInfo(baseMapper.queryPage(pageInfo, param));
 | 
	
		
			
				|  |  |      }
 | 
	
	
		
			
				|  | @@ -503,25 +503,35 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |       * 预览协议
 | 
	
		
			
				|  |  |       *
 | 
	
		
			
				|  |  |       * @param tenantId 机构id
 | 
	
		
			
				|  |  | +     * @param en       协议类型 开通还是续费
 | 
	
		
			
				|  |  | +     * @param val      续费的时间长度
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public String getContract(Integer tenantId) {
 | 
	
		
			
				|  |  | -        List<TenantInfoInfoPageVo> tenantInfoList = baseMapper.queryPage(new HashMap<String, Object>() {{
 | 
	
		
			
				|  |  | +    public String getContract(Integer tenantId, TenantContractRecordEnum en, Integer val) {
 | 
	
		
			
				|  |  | +        List<TenantInfoPageVo> tenantInfoList = baseMapper.queryPage(new HashMap<String, Object>() {{
 | 
	
		
			
				|  |  |              put("tenantId", tenantId);
 | 
	
		
			
				|  |  |          }});
 | 
	
		
			
				|  |  |          //查询协议中的信息
 | 
	
		
			
				|  |  | -        TenantInfoInfoPageVo tenantInfo = Optional.ofNullable(tenantInfoList)
 | 
	
		
			
				|  |  | +        TenantInfoPageVo tenantInfo = Optional.ofNullable(tenantInfoList)
 | 
	
		
			
				|  |  |                  .filter(CollectionUtils::isNotEmpty)
 | 
	
		
			
				|  |  |                  .map(a -> a.get(0))
 | 
	
		
			
				|  |  |                  .orElseThrow(() -> new BizException("未查询到机构信息"));
 | 
	
		
			
				|  |  | -        Date now = new Date();
 | 
	
		
			
				|  |  | +        //如果本次协议是开通服务则直接取开通时的价格
 | 
	
		
			
				|  |  | +        if (TenantContractRecordEnum.OPEN.equals(en)) {
 | 
	
		
			
				|  |  | +            tenantInfo.setContractPrice(tenantInfo.getOpenPrice());
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            //续费时 取合同价
 | 
	
		
			
				|  |  | +            BigDecimal contractAmount = tenantInfo.getContractPrice().multiply(new BigDecimal(val), new MathContext(1, RoundingMode.HALF_UP));
 | 
	
		
			
				|  |  | +            tenantInfo.setContractPrice(contractAmount);
 | 
	
		
			
				|  |  | +            tenantInfo.setExpiryCount(val);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          //获取协议变量参数
 | 
	
		
			
				|  |  | -        Map<String, Object> param = getContractParam(tenantInfo, now);
 | 
	
		
			
				|  |  | +        Map<String, Object> param = getContractParam(tenantInfo);
 | 
	
		
			
				|  |  |          //生成模版
 | 
	
		
			
				|  |  |          FreemarkerTemplateEngine templateEngine = FreemarkerTemplateEngine.getInstance();
 | 
	
		
			
				|  |  |          templateEngine.setClassForTemplateLoading(TenantInfoServiceImpl.class, "/config/contracts/");
 | 
	
		
			
				|  |  |          //文件地址验证
 | 
	
		
			
				|  |  | -        String srcPath = contractBaseDir + "/" + lexiaoyaContract + "/" + tenantId + "-" + tenantInfo.getServeName() + DateUtils.formatDate(now, "yyyyMMddHHmmss") + ".html";
 | 
	
		
			
				|  |  | +        String srcPath = contractBaseDir + "/" + lexiaoyaContract + "/" + tenantId + "-" + tenantInfo.getServeName() + DateUtils.formatDate(new Date(), "yyyyMMddHHmmss") + ".html";
 | 
	
		
			
				|  |  |          File srcFile = new File(srcPath);
 | 
	
		
			
				|  |  |          if (!srcFile.getParentFile().exists()) {
 | 
	
		
			
				|  |  |              srcFile.getParentFile().mkdirs();
 | 
	
	
		
			
				|  | @@ -535,14 +545,15 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |              throw new BizException("读取产品协议出错");
 | 
	
		
			
				|  |  |          } finally {
 | 
	
		
			
				|  |  |              //删除文件
 | 
	
		
			
				|  |  | -//            FileUtils.deleteQuietly(srcFile);
 | 
	
		
			
				|  |  | +            FileUtils.deleteQuietly(srcFile);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * 获取协议变量参数
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    private Map<String, Object> getContractParam(TenantInfoInfoPageVo tenantInfo, Date now) {
 | 
	
		
			
				|  |  | +    private Map<String, Object> getContractParam(TenantInfoPageVo tenantInfo) {
 | 
	
		
			
				|  |  | +        Date now = new Date();
 | 
	
		
			
				|  |  |          //将数据转换为Map
 | 
	
		
			
				|  |  |          Map<String, Object> param = WrapperUtil.toMap(tenantInfo);
 | 
	
		
			
				|  |  |          //当前时间
 | 
	
	
		
			
				|  | @@ -550,7 +561,7 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |          //服务过期时间
 | 
	
		
			
				|  |  |          param.put("expireDate", DateUtils.formatDate(getExpiryDate(tenantInfo.getExpiryCount(), tenantInfo.getExpiryUnit(), now), "yyyy年MM月dd日"));
 | 
	
		
			
				|  |  |          //购买服务的时长单位
 | 
	
		
			
				|  |  | -        param.put("expiryUnit", TenantProductInfo.MONTH.equals(tenantInfo.getExpiryUnit()) ? "月" : "年");
 | 
	
		
			
				|  |  | +        param.put("expiryUnit", TenantProductInfo.MONTH.equals(tenantInfo.getExpiryUnit()) ? "个月" : "年");
 | 
	
		
			
				|  |  |          //乙方公章
 | 
	
		
			
				|  |  |          param.put("officialSealB", "https://daya.ks3-cn-beijing.ksyun.com/202202/Sx6rzWm.png");
 | 
	
		
			
				|  |  |          //获取产品名称
 | 
	
	
		
			
				|  | @@ -615,9 +626,11 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |       * 生成PDF协议上传到服务器并返回协议地址
 | 
	
		
			
				|  |  |       *
 | 
	
		
			
				|  |  |       * @param tenantId 机构id
 | 
	
		
			
				|  |  | +     * @param en       协议类型 开通还是续费
 | 
	
		
			
				|  |  | +     * @param val      续费的时间长度-续费才有值
 | 
	
		
			
				|  |  |       * @return 协议地址
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    public String getContractAndUpLoad(Integer tenantId) {
 | 
	
		
			
				|  |  | +    private String getContractAndUpLoad(Integer tenantId, TenantContractRecordEnum en, Integer val) {
 | 
	
		
			
				|  |  |          Date now = new Date();
 | 
	
		
			
				|  |  |          //创建PDF本次缓存地址
 | 
	
		
			
				|  |  |          String srcPdfPath = contractBaseDir + lexiaoyaContract + DateUtils.formatDate(now, "yyyyMMddHHmmss") + "/" + tenantId + "_" + java.time.LocalDateTime.now().getSecond() + ".pdf";
 | 
	
	
		
			
				|  | @@ -627,7 +640,7 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |              srcFile.getParentFile().mkdirs();
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          //获取协议内容
 | 
	
		
			
				|  |  | -        String contractHtml = getContract(tenantId);
 | 
	
		
			
				|  |  | +        String contractHtml = getContract(tenantId, en, val);
 | 
	
		
			
				|  |  |          // 生成PDF格式协议
 | 
	
		
			
				|  |  |          try {
 | 
	
		
			
				|  |  |              PDFUtil.renderToPDFByData(TenantInfoServiceImpl.class.getResource("/").getFile(), contractHtml, srcPdfPath, "simsun.ttc");
 | 
	
	
		
			
				|  | @@ -700,7 +713,7 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |          Map<String, Object> result = new HashMap<>();
 | 
	
		
			
				|  |  |          TenantProductInfo productInfo = getProductInfo(tenantId);
 | 
	
		
			
				|  |  |          //续费时 取合同价
 | 
	
		
			
				|  |  | -        BigDecimal amount = productInfo.getContractPrice().multiply(new BigDecimal(val));
 | 
	
		
			
				|  |  | +        BigDecimal amount = productInfo.getContractPrice().multiply(new BigDecimal(val), new MathContext(1, RoundingMode.HALF_UP));
 | 
	
		
			
				|  |  |          //生成订单编号
 | 
	
		
			
				|  |  |          String orderNo = idGenerator.generatorId("payment") + "";
 | 
	
		
			
				|  |  |          int orderState = 0;
 | 
	
	
		
			
				|  | @@ -765,16 +778,17 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |          tenantProductInfoService.updateById(productInfo);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          //生成协议并上传得到地址
 | 
	
		
			
				|  |  | -        String contractPath = getContractAndUpLoad(tenantId);
 | 
	
		
			
				|  |  | +        TenantContractRecordEnum renew = TenantContractRecordEnum.RENEW;
 | 
	
		
			
				|  |  | +        String contractPath = getContractAndUpLoad(tenantId, renew, val);
 | 
	
		
			
				|  |  |          //写入协议记录
 | 
	
		
			
				|  |  | -        tenantContractRecordService.insertContractRecord(tenantId, contractPath, TenantContractRecordEnum.RENEW);
 | 
	
		
			
				|  |  | +        tenantContractRecordService.insertContractRecord(tenantId, contractPath, renew);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          //发送邮件短信
 | 
	
		
			
				|  |  |          Map<String, Object> par = new HashMap<>();
 | 
	
		
			
				|  |  |          par.put("tenantId", tenantId);
 | 
	
		
			
				|  |  | -        List<TenantInfoInfoPageVo> tenantInfos = baseMapper.queryPage(par);
 | 
	
		
			
				|  |  | +        List<TenantInfoPageVo> tenantInfos = baseMapper.queryPage(par);
 | 
	
		
			
				|  |  |          if (CollectionUtils.isNotEmpty(tenantInfos)) {
 | 
	
		
			
				|  |  | -            TenantInfoInfoPageVo tenantInfo = tenantInfos.get(0);
 | 
	
		
			
				|  |  | +            TenantInfoPageVo tenantInfo = tenantInfos.get(0);
 | 
	
		
			
				|  |  |              //邮件
 | 
	
		
			
				|  |  |              if (StringUtils.isNotBlank(tenantInfo.getEmail())) {
 | 
	
		
			
				|  |  |                  //机构名称 服务名称 学员上限 有效期
 | 
	
	
		
			
				|  | @@ -941,9 +955,10 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |          productInfo.setTenantId(tenantId);
 | 
	
		
			
				|  |  |          tenantProductInfoService.updateByTenantId(productInfo);
 | 
	
		
			
				|  |  |          //生成协议并上传得到地址
 | 
	
		
			
				|  |  | -        String contractPath = getContractAndUpLoad(tenantId);
 | 
	
		
			
				|  |  | +        TenantContractRecordEnum open = TenantContractRecordEnum.OPEN;
 | 
	
		
			
				|  |  | +        String contractPath = getContractAndUpLoad(tenantId, open, null);
 | 
	
		
			
				|  |  |          //写入协议记录
 | 
	
		
			
				|  |  | -        tenantContractRecordService.insertContractRecord(tenantId, contractPath, TenantContractRecordEnum.OPEN);
 | 
	
		
			
				|  |  | +        tenantContractRecordService.insertContractRecord(tenantId, contractPath, open);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
	
		
			
				|  | @@ -1072,7 +1087,7 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |          Map<String, Object> par = new HashMap<>();
 | 
	
		
			
				|  |  |          par.put("expiryDate", maturity);
 | 
	
		
			
				|  |  |          par.put("state", 1);
 | 
	
		
			
				|  |  | -        List<TenantInfoInfoPageVo> maturityTenant = baseMapper.queryPage(par);
 | 
	
		
			
				|  |  | +        List<TenantInfoPageVo> maturityTenant = baseMapper.queryPage(par);
 | 
	
		
			
				|  |  |          maturityTenant.forEach(t -> {
 | 
	
		
			
				|  |  |              TenantInfo tenantInfo = baseMapper.selectById(t.getId());
 | 
	
		
			
				|  |  |              tenantInfo.setState(2);
 | 
	
	
		
			
				|  | @@ -1089,12 +1104,12 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |          Map<String, Object> par = new HashMap<>();
 | 
	
		
			
				|  |  |          par.put("state", 1);
 | 
	
		
			
				|  |  |          par.put("expiryDate", expiryDate);
 | 
	
		
			
				|  |  | -        List<TenantInfoInfoPageVo> tenantList = baseMapper.queryPage(par);
 | 
	
		
			
				|  |  | +        List<TenantInfoPageVo> tenantList = baseMapper.queryPage(par);
 | 
	
		
			
				|  |  |          expiringSend(tenantList, DateUtils.formatDate(expiryDate, "yyyy年MM月dd日"), i);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      //向即将到期的机构发送信息
 | 
	
		
			
				|  |  | -    private void expiringSend(List<TenantInfoInfoPageVo> infoList, String dateStr, int i) {
 | 
	
		
			
				|  |  | +    private void expiringSend(List<TenantInfoPageVo> infoList, String dateStr, int i) {
 | 
	
		
			
				|  |  |          infoList.forEach(t -> {
 | 
	
		
			
				|  |  |              //邮件
 | 
	
		
			
				|  |  |              if (StringUtils.isNotBlank(t.getEmail())) {
 |