PhaseUtil.java 6.72 KB
package com.phase;

import com.alibaba.fastjson.JSON;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class PhaseUtil {
    public static void main(String[] args) {
        String speechName = args[0];
        String currentDir = System.getProperty("user.dir");
        String filePath = currentDir + File.separator + speechName + ".xlsx";
        //获取excel文件
        List<Map<String, String>> orgPhaseList = ExcelUtil.readExcel(filePath,
                0);
        //将excel文件转换为格式化的json
        ArrayList<SpeechBigPhaseEditDTO> bigPhaseList = getBigPhaseListByMapList(orgPhaseList);
        //将json输出到系统文件
        SpeechPhaseDTO speechPhaseDTO = SpeechPhaseDTO.builder().phaseList(bigPhaseList).speechName(speechName).stlId(0L).build();
        String phaseJson = JSON.toJSONString(speechPhaseDTO);
        System.out.println(phaseJson);
        putOutFile(phaseJson, currentDir, speechName);
    }

    private static void putOutFile(String phaseJson, String currentDir, String speechName) {
        String filePath = currentDir + File.separator + speechName + ".txt";
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(new File(filePath));
            out.write(phaseJson.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static ArrayList<SpeechBigPhaseEditDTO> getBigPhaseListByMapList(List<Map<String, String>> orgPhaseList) {
        ArrayList<SpeechBigPhaseEditDTO> bigPhaseList = new ArrayList<>();
        HashMap<Integer, SpeechBigPhaseEditDTO> bigPhaseNumMap = new HashMap<>();
        //循环处理每一条话术配置
        int rowNum = 2;
        for (Map<String, String> orgPhase : orgPhaseList) {
            if (!StringUtils.isEmpty(orgPhase.get(ColDesc.COL_HINT_NUM.getCol()))) {
                System.out.println("当前处理excel行号:" + rowNum);
                //获取大环节对象
                SpeechBigPhaseEditDTO bigPhaseEditDTO = fetchBigPhase(bigPhaseNumMap, bigPhaseList, orgPhase);
                //生成小环节对象
                SpeechLittlePhaseAddDTO littlePhaseAddDTO;
                try {
                    littlePhaseAddDTO = fetchLittlePhase(orgPhase);
                } catch (Exception e) {
                    throw new RuntimeException("请检查第" + rowNum + "行的数据是否存在参数缺失,或者数据格式问题!");
                }
                //将小环节封装入大环节对象的hintList属性中
                bigPhaseEditDTO.getHintsList().add(littlePhaseAddDTO.transToVO());
            }
            rowNum++;
        }
        sortHintList(bigPhaseList);
        return bigPhaseList;
    }

    private static void sortHintList(ArrayList<SpeechBigPhaseEditDTO> bigPhaseList) {
        //将大环节列表按小环节序号排序
        bigPhaseList.sort(Comparator.comparingInt(SpeechBigPhaseEditDTO::getPhaseNum));
        for (SpeechBigPhaseEditDTO speechBigPhaseEditDTO : bigPhaseList) {
            List<SpeechLittlePhaseEditDTO> hintsList = speechBigPhaseEditDTO.getHintsList();
            //将小环节列表按小环节序号排序
            hintsList.sort(Comparator.comparingInt(SpeechLittlePhaseEditDTO::getHintNum));
        }
    }

    private static SpeechLittlePhaseAddDTO fetchLittlePhase(Map<String, String> orgPhase) {
        int littlePhaseSerialNum = Integer.parseInt(orgPhase.get(ColDesc.COL_HINT_NUM.getCol()));
        String littlePhaseName = orgPhase.get(ColDesc.COL_HINT_NAME.getCol());
        int triggerConditionSwitch = Integer.parseInt(orgPhase.get(ColDesc.COL_CONDITION_TYPE.getCol()));
        String triggerConditionContent = orgPhase.get(ColDesc.COL_CONDITION_CONTENT.getCol());
        int phaseType = Integer.parseInt(orgPhase.get(ColDesc.COL_HINT_TYPE.getCol()));
        int fileType = 0;
        int identityType = 0;
        int idcardOcrSwitch = 0;
        int popup = 2;
        try {
            fileType = Integer.parseInt(orgPhase.get(ColDesc.COL_DOCTYPE.getCol()));
        } catch (NumberFormatException e) {
        }
        try {
            identityType = Integer.parseInt(orgPhase.get(ColDesc.COL_CERTIFICATE_TYPE.getCol()));
        } catch (NumberFormatException e) {
        }
        try {
            idcardOcrSwitch = Integer.parseInt(orgPhase.get(ColDesc.COL_OCR.getCol()));
        } catch (NumberFormatException e) {
        }
        try {
            popup = Integer.parseInt(orgPhase.get(ColDesc.COL_POPUP.getCol()));
        } catch (NumberFormatException e) {
        }
        String prdCode = orgPhase.get(ColDesc.COL_PRD_CODE.getCol());
        String ttsContent = orgPhase.get(ColDesc.COL_TTS.getCol());
        String littlePhaseTitle = orgPhase.get(ColDesc.COL_HINT_TITLE.getCol());
        String sure = orgPhase.get(ColDesc.COL_SURE.getCol());
        String no = orgPhase.get(ColDesc.COL_NO.getCol());
        return SpeechLittlePhaseAddDTO.builder()
                .littlePhaseSerialNum(littlePhaseSerialNum)
                .littlePhaseName(littlePhaseName)
                .triggerConditionSwitch(triggerConditionSwitch)
                .triggerConditionContent(triggerConditionContent)
                .phaseType(phaseType)
                .sure(sure)
                .no(no)
                .fileType(fileType)
                .identityType(identityType)
                .idcardOcrSwitch(idcardOcrSwitch)
                .ttsContent(ttsContent)
                .littlePhaseTitle(littlePhaseTitle)
                .bindUnique("")
                .popup(popup)
                .docPrdCode(prdCode)
                .build();

    }

    private static SpeechBigPhaseEditDTO fetchBigPhase(HashMap<Integer, SpeechBigPhaseEditDTO> bigPhaseNumMap, ArrayList<SpeechBigPhaseEditDTO> bigPhaseList, Map<String, String> orgPhase) {
        Integer bigPhaseNum = Integer.parseInt(orgPhase.get(ColDesc.COL_PHASE_NUM.getCol()));
        SpeechBigPhaseEditDTO bigPhaseEditDTO = bigPhaseNumMap.get(bigPhaseNum);
        if (bigPhaseEditDTO == null) {
            bigPhaseEditDTO = new SpeechBigPhaseEditDTO();
            bigPhaseEditDTO.setPhaseNum(bigPhaseNum);
            bigPhaseEditDTO.setPhaseTitle(orgPhase.get(ColDesc.COL_PHASE_TITLE.getCol()));
            bigPhaseEditDTO.setHintsList(new ArrayList<>());
            bigPhaseNumMap.put(bigPhaseNum, bigPhaseEditDTO);
            bigPhaseList.add(bigPhaseEditDTO);
        }
        return bigPhaseEditDTO;
    }
}