|
@@ -0,0 +1,200 @@
|
|
|
+package com.qunzhixinxi.hnqz.admin.listener;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DatePattern;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.SysEntContractInfo;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.WmDaDrugEntDrugtable;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.WmPackageContract;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.WmPackageTaskTypeQty;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.WmScorePackage;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.WmTaskType;
|
|
|
+import com.qunzhixinxi.hnqz.admin.event.PackageContractEvent;
|
|
|
+import com.qunzhixinxi.hnqz.admin.mapper.SysEntContractInfoMapper;
|
|
|
+import com.qunzhixinxi.hnqz.admin.mapper.WmPackageTaskTypeQtyMapper;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.SysFileService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.WmDaDrugEntDrugtableService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.WmPackageContractService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.WmScorePackageService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.WmTaskTypeService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.util.OsEnvUtils;
|
|
|
+import com.qunzhixinxi.hnqz.common.core.util.PdfUtils;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.Getter;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.event.EventListener;
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
+import org.springframework.mock.web.MockMultipartFile;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 积分包合同生成
|
|
|
+ *
|
|
|
+ * @author jimmy
|
|
|
+ * @date 2024-08-17 21:33
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@AllArgsConstructor
|
|
|
+@Configuration
|
|
|
+public class PackageContractEventListener {
|
|
|
+
|
|
|
+ @Getter
|
|
|
+ @AllArgsConstructor
|
|
|
+ private enum MAH {
|
|
|
+
|
|
|
+ ENT_1("中国生物水痘生产企业", "北京市朝阳区双桥路123号中国生物基地10-12号", "张三", "买还买提", "13388889999"),
|
|
|
+ ENT_2("中国生物水痘生产企业", "北京市朝阳区双桥路123号中国生物基地10-12号", "张三", "买还买提", "13388889999"),
|
|
|
+ ENT_3("中国生物水痘生产企业", "北京市朝阳区双桥路123号中国生物基地10-12号", "张三", "买还买提", "13388889999"),
|
|
|
+ ENT_4("中国生物水痘生产企业", "北京市朝阳区双桥路123号中国生物基地10-12号", "张三", "买还买提", "13388889999"),
|
|
|
+ ENT_5("中国生物水痘生产企业", "北京市朝阳区双桥路123号中国生物基地10-12号", "张三", "买还买提", "13388889999"),
|
|
|
+ ENT_6("中国生物水痘生产企业", "北京市朝阳区双桥路123号中国生物基地10-12号", "张三", "买还买提", "13388889999"),
|
|
|
+ ENT_7("中国生物水痘生产企业", "北京市朝阳区双桥路123号中国生物基地10-12号", "张三", "买还买提", "13388889999");
|
|
|
+
|
|
|
+
|
|
|
+ public static MAH get(String entName) {
|
|
|
+ return Arrays.stream(MAH.values()).filter(ent -> ent.name.equals(entName)).findFirst().orElse(null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private final String name;
|
|
|
+ private final String address;
|
|
|
+ private final String respName;
|
|
|
+ private final String contractName;
|
|
|
+ private final String contractPhone;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private final WmPackageContractService packageContractService;
|
|
|
+ private final SysFileService fileService;
|
|
|
+ private final WmScorePackageService packageService;
|
|
|
+ private final WmDaDrugEntDrugtableService drugEntDrugtableService;
|
|
|
+ private final SysEntContractInfoMapper entContractInfoMapper;
|
|
|
+ private final WmPackageTaskTypeQtyMapper packageTaskTypeQtyMapper;
|
|
|
+ private final WmTaskTypeService taskTypeService;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 推送事件
|
|
|
+ *
|
|
|
+ * @param event 事件
|
|
|
+ */
|
|
|
+ @Async
|
|
|
+ @Order
|
|
|
+ @EventListener(PackageContractEvent.class)
|
|
|
+ public void pushEvent(PackageContractEvent event) throws IOException {
|
|
|
+
|
|
|
+ Integer pkgId = event.getPackageId();
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+
|
|
|
+ log.info("生成市场服务包(id:{})合同", pkgId);
|
|
|
+
|
|
|
+ // 获取环境变量
|
|
|
+ Map<String, String> envPaths = OsEnvUtils.getEachEnvPaths();
|
|
|
+
|
|
|
+ // 生成合同
|
|
|
+ String templatePath = envPaths.get(OsEnvUtils.TargetFile.TEMPLATE.getName()) + "pkg_tmpl_v19.pdf";
|
|
|
+ final String outFilePath =
|
|
|
+ // 根据环境获取临时目录
|
|
|
+ envPaths.get(OsEnvUtils.TargetFile.CONTACT.getName())
|
|
|
+ // 拼接包ID
|
|
|
+ + "PID" + pkgId
|
|
|
+ // 拼接时间
|
|
|
+ + "_" + now.format(DateTimeFormatter.ofPattern(DatePattern.PURE_DATETIME_PATTERN))
|
|
|
+ + ".pdf";
|
|
|
+
|
|
|
+ Map<String, String> params = buildContractParams(pkgId, now);
|
|
|
+
|
|
|
+ PdfUtils.resumeGenerate(templatePath, outFilePath, envPaths.get(OsEnvUtils.TargetFile.FONT.getName()) + "SimSun.ttc", params);
|
|
|
+
|
|
|
+
|
|
|
+ // 上传到oss
|
|
|
+ File contractPdf = new File(outFilePath);
|
|
|
+ MockMultipartFile multipartFile = new MockMultipartFile("file", contractPdf.getName(), null, Files.newInputStream(contractPdf.toPath()));
|
|
|
+ Map<String, String> uploaded = fileService.upload(multipartFile);
|
|
|
+ String fileId = uploaded.get("fileId");
|
|
|
+
|
|
|
+ // 保存合同对象
|
|
|
+ WmPackageContract pc = new WmPackageContract();
|
|
|
+ pc.setPackageId(pkgId);
|
|
|
+ pc.setFileId(Integer.valueOf(fileId));
|
|
|
+ pc.setCreateTime(now);
|
|
|
+
|
|
|
+ packageContractService.save(pc);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, String> buildContractParams(Integer pkgId, LocalDateTime now) {
|
|
|
+
|
|
|
+ Map<String, String> params = new HashMap<>(29);
|
|
|
+
|
|
|
+ // 获取积分包
|
|
|
+ WmScorePackage pkg = packageService.getById(pkgId.toString());
|
|
|
+
|
|
|
+ // 通过药品获取甲方信息
|
|
|
+ String[] drugtable = pkg.getDrugtable();
|
|
|
+ List<WmDaDrugEntDrugtable> drugtables = drugEntDrugtableService.listByIds(Arrays.stream(drugtable).collect(Collectors.toSet()));
|
|
|
+ String drugEntName = drugtables.get(0).getDrugEntName();
|
|
|
+ MAH mah = MAH.get(drugEntName);
|
|
|
+
|
|
|
+ // 甲方信息
|
|
|
+ params.put("party_a_name", mah.getName());
|
|
|
+ params.put("party_a_address", mah.getAddress());
|
|
|
+ params.put("party_a_resp_name", mah.getRespName());
|
|
|
+ params.put("party_a_contact_name", mah.getContractName());
|
|
|
+ params.put("party_a_contact_phone", mah.getContractPhone());
|
|
|
+
|
|
|
+
|
|
|
+ // 乙方信息
|
|
|
+ SysEntContractInfo contractInfo = entContractInfoMapper.selectById(pkg.getSendPackageDeptId());
|
|
|
+ params.put("party_b_name", contractInfo.getEntName());
|
|
|
+ params.put("party_b_address", contractInfo.getAddress());
|
|
|
+ params.put("party_b_resp_name", contractInfo.getRespName());
|
|
|
+ params.put("party_b_contact_name", contractInfo.getContactName());
|
|
|
+ params.put("party_b_contact_phone", contractInfo.getContactPhone());
|
|
|
+
|
|
|
+ // 合同岂止时间
|
|
|
+ params.put("issue_date", DateTimeFormatter.ofPattern(DatePattern.CHINESE_DATE_PATTERN).format(pkg.getCreateTime()));
|
|
|
+ params.put("expiry_date", DateTimeFormatter.ofPattern(DatePattern.CHINESE_DATE_PATTERN).format(pkg.getEndTime()));
|
|
|
+
|
|
|
+
|
|
|
+ // 任务数量统计
|
|
|
+
|
|
|
+ List<WmPackageTaskTypeQty> qtyList = packageTaskTypeQtyMapper.listByPackageId(pkgId);
|
|
|
+ int total = qtyList.stream().mapToInt(WmPackageTaskTypeQty::getQty).sum();
|
|
|
+ Set<Integer> ttIds = qtyList.stream().map(WmPackageTaskTypeQty::getTaskTypeId).collect(Collectors.toSet());
|
|
|
+
|
|
|
+ List<WmTaskType> list = taskTypeService.list(Wrappers.<WmTaskType>lambdaQuery().in(WmTaskType::getId, ttIds));
|
|
|
+ Map<String, WmTaskType> collect = list.stream().collect(Collectors.toMap(WmTaskType::getId, Function.identity()));
|
|
|
+
|
|
|
+
|
|
|
+ for (int i = 1, size = qtyList.size() + 1; i <= size; i++) {
|
|
|
+ String prefix = "s" + i;
|
|
|
+ WmPackageTaskTypeQty qty = qtyList.get(i);
|
|
|
+ WmTaskType taskType = collect.get(qty.getTaskTypeId().toString());
|
|
|
+
|
|
|
+ Integer count = qty.getQty();
|
|
|
+ params.put(prefix + "_name", taskType.getTaskTypeName());
|
|
|
+ params.put(prefix + "_qty", count.toString());
|
|
|
+ params.put(prefix + "_price", taskType.getScore().toString());
|
|
|
+
|
|
|
+
|
|
|
+ params.put(prefix + "_rate", String.format(".2%f%%", count.doubleValue() / total));
|
|
|
+ }
|
|
|
+
|
|
|
+ params.put("sign_date", DateTimeFormatter.ofPattern(DatePattern.CHINESE_DATE_PATTERN).format(now.toLocalDate()));
|
|
|
+ return params;
|
|
|
+ }
|
|
|
+}
|