package com.huatek.busi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.google.common.collect.Lists;
import com.huatek.busi.dto.CalculateBasicDataDto;
import com.huatek.busi.entity.CalculateBasicData;
import com.huatek.busi.mapper.CalculateBasicDataMapper;
import com.huatek.busi.service.ICalculateBasicDataService;
import com.huatek.busi.util.DynamicCreateTable;
import com.huatek.busi.vo.CalculateBasicDataVo;
import com.huatek.frame.base.util.Constants;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.sql.dao.impl.BaseDao;
import com.huatek.torch.util.uuid.Uuid;
import com.opencsv.CSVParser;
import com.opencsv.CSVReader;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Component
@Service(interfaceClass = ICalculateBasicDataService.class)
@Transactional(rollbackFor = Exception.class)
public class CalculateBasicDataServiceImpl extends BaseDao<CalculateBasicData, String> implements ICalculateBasicDataService {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private CalculateBasicDataMapper calculateBasicDataMapper;

    @Override
    public Map<String, Object> delete(){
        String msg;
        String tableName = Constants.TABLE_NAME;
        String dropSqlSplice = dropSqlSplice(tableName);
        String sqlSplice = sqlSplice(tableName);
        int re = DynamicCreateTable.operateObject(jdbcTemplate, tableName, sqlSplice, dropSqlSplice);
        if(re == 1){
            msg = "BUSINESS.DELETE.SUCCESS";
        }else {
            msg = "BUSINESS.DELETE.FAIL";
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, msg, re);
    }

    @Override
    public Map<String, Object> init() {

        Runnable r1 = () -> gISmec();
        new Thread(r1).start();

        Runnable r2 = () -> gISPD();
        new Thread(r2).start();

        Runnable r3 = () -> switch1();
        new Thread(r3).start();
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.INIT.DATA.SUCCESS", "");
    }

    @Override
    public Map<String, Object> findList(CalculateBasicDataVo calculateBasicDataVo){
        List<CalculateBasicDataDto> list = calculateBasicDataMapper.findList(calculateBasicDataVo);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.QUERY.SUCCESS", list);
    }

    private void gISmec(){
        // 主文件名称
        String mainFileName = Constants.GISMEC;
        // 全路径
        String pathName = "/gis/GISmec/0/9/dy_open09.csv";
        // 分闸合闸状态
        Integer status = 0;
        // 状态
        Integer flag = 9;
        // 子文件名
        String childFileName = "dy_open09";
        Integer line = 26;
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/0/9/dy_open10.csv";
        childFileName = "dy_open10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 10;
        pathName = "/gis/GISmec/0/10/fj_gy_open09.csv";
        childFileName = "fj_gy_open09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/0/10/fj_gy_open10.csv";
        childFileName = "fj_gy_open10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 11;
        pathName = "/gis/GISmec/0/11/fj_open09.csv";
        childFileName = "fj_open09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/0/11/fj_open10.csv";
        childFileName = "fj_open10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 12;
        pathName = "/gis/GISmec/0/12/fs_dy_open09.csv";
        childFileName = "fs_dy_open09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/0/12/fs_dy_open10.csv";
        childFileName = "fs_dy_open10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 13;
        pathName = "/gis/GISmec/0/13/fs_open09.csv";
        childFileName = "fs_open09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/0/13/fs_open10.csv";
        childFileName = "fs_open10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 14;
        pathName = "/gis/GISmec/0/14/fzdctks09.csv";
        childFileName = "fzdctks09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/0/14/fzdctks10.csv";
        childFileName = "fzdctks10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 15;
        pathName = "/gis/GISmec/0/15/gy_open09.csv";
        childFileName = "gy_open09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/0/15/gy_open10.csv";
        childFileName = "gy_open10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 16;
        pathName = "/gis/GISmec/0/16/zc_open09.csv";
        childFileName = "zc_open09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/0/16/zc_open10.csv";
        childFileName = "zc_open10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 0;
        status = 1;
        pathName = "/gis/GISmec/1/0/dy_close09.csv";
        childFileName = "dy_close09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/1/0/dy_close10.csv";
        childFileName = "dy_close10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 1;
        pathName = "/gis/GISmec/1/1/fj_close09.csv";
        childFileName = "fj_close09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/1/1/fj_close10.csv";
        childFileName = "fj_close10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 2;
        pathName = "/gis/GISmec/1/2/fj_gy_close09.csv";
        childFileName = "fj_gy_close09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/1/2/fj_gy_close10.csv";
        childFileName = "fj_gy_close10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 3;
        pathName = "/gis/GISmec/1/3/fs_close09.csv";
        childFileName = "fs_close09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/1/3/fs_close10.csv";
        childFileName = "fs_close10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 4;
        pathName = "/gis/GISmec/1/4/fs_dy_close09.csv";
        childFileName = "fs_dy_close09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/1/4/fs_dy_close10.csv";
        childFileName = "fs_dy_close10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 5;
        pathName = "/gis/GISmec/1/5/gy_close09.csv";
        childFileName = "gy_close09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/1/5/gy_close10.csv";
        childFileName = "gy_close10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 6;
        pathName = "/gis/GISmec/1/6/hj_09.csv";
        childFileName = "hj_09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/1/6/hj_10.csv";
        childFileName = "hj_10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 7;
        pathName = "/gis/GISmec/1/7/hs_09.csv";
        childFileName = "hs_09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/1/7/hs_10.csv";
        childFileName = "hs_10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 8;
        pathName = "/gis/GISmec/1/8/zc_close09.csv";
        childFileName = "zc_close09";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        pathName = "/gis/GISmec/1/8/zc_close10.csv";
        childFileName = "zc_close10";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();
    }

    private void gISPD(){
        Integer line = 0;
        Integer status = -1;
        String mainFileName = Constants.GISPD;
        for(int i = 1; i < 91; i++){
            Integer flag = 0;
            String pathName = "/gis/"+ mainFileName +"/"+ flag +"/"+ i +".csv";
            new Thread(new JobThread(mainFileName, pathName, status, flag, String.valueOf(i), line, "")).start();

            flag = 1;
            pathName = "/gis/"+ mainFileName +"/"+ flag +"/"+ i +".csv";
            new Thread(new JobThread(mainFileName, pathName, status, flag, String.valueOf(i), line, "")).start();

            flag = 2;
            pathName = "/gis/"+ mainFileName +"/"+ flag +"/"+ i +".csv";
            new Thread(new JobThread(mainFileName, pathName, status, flag, String.valueOf(i), line, "")).start();
        }
    }

    private void switch1(){
        // 主文件名称
        String mainFileName = Constants.SWITCH;
        // 全路径
        String pathName = "/gis/switch/0/0/43.csv";
        // 分闸合闸状态
        Integer status = 0;
        Integer line = 0;
        // 状态
        Integer flag = 0;
        // 子文件名
        String childFileName = "43";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "45";
        pathName = "/gis/switch/0/0/45.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "47";
        pathName = "/gis/switch/0/0/47.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "49";
        pathName = "/gis/switch/0/0/49.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "51";
        pathName = "/gis/switch/0/0/51.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 1;
        childFileName = "43";
        pathName = "/gis/switch/0/1/43.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "45";
        pathName = "/gis/switch/0/1/45.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "47";
        pathName = "/gis/switch/0/1/47.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "49";
        pathName = "/gis/switch/0/1/49.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "51";
        pathName = "/gis/switch/0/1/51.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 2;
        childFileName = "49";
        pathName = "/gis/switch/0/2/49.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "51";
        pathName = "/gis/switch/0/2/51.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "53";
        pathName = "/gis/switch/0/2/53.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "55";
        pathName = "/gis/switch/0/2/55.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "57";
        pathName = "/gis/switch/0/2/57.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 3;
        childFileName = "47";
        pathName = "/gis/switch/0/3/47.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "49";
        pathName = "/gis/switch/0/3/49.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "51";
        pathName = "/gis/switch/0/3/51.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "53";
        pathName = "/gis/switch/0/3/53.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "55";
        pathName = "/gis/switch/0/3/55.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 4;
        childFileName = "15";
        pathName = Constants.PATH_NAME_1;
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "five")).start();

        childFileName = "17";
        pathName = Constants.PATH_NAME_2;
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "five")).start();

        flag = 5;
        childFileName = "17";
        pathName = Constants.PATH_NAME_3;
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "five")).start();

        childFileName = "19";
        pathName = Constants.PATH_NAME_4;
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "five")).start();

        flag = 6;
        childFileName = "21";
        pathName = Constants.PATH_NAME_5;
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "five")).start();

        childFileName = "23";
        pathName = Constants.PATH_NAME_6;
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "five")).start();

        status = 1;
        flag = 7;
        childFileName = "50";
        pathName = "/gis/switch/1/7/50.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "52";
        pathName = "/gis/switch/1/7/52.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "54";
        pathName = "/gis/switch/1/7/54.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "56";
        pathName = "/gis/switch/1/7/56.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "58";
        pathName = "/gis/switch/1/7/58.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "60";
        pathName = "/gis/switch/1/7/60.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 8;
        childFileName = "52";
        pathName = "/gis/switch/1/8/52.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "54";
        pathName = "/gis/switch/1/8/54.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "56";
        pathName = "/gis/switch/1/8/56.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "58";
        pathName = "/gis/switch/1/8/58.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "60";
        pathName = "/gis/switch/1/8/60.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "62";
        pathName = "/gis/switch/1/8/62.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 9;
        childFileName = "50";
        pathName = "/gis/switch/1/9/50.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "52";
        pathName = "/gis/switch/1/9/52.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "54";
        pathName = "/gis/switch/1/9/54.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "56";
        pathName = "/gis/switch/1/9/56.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "58";
        pathName = "/gis/switch/1/9/58.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "60";
        pathName = "/gis/switch/1/9/60.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 10;
        childFileName = "48";
        pathName = "/gis/switch/1/10/48.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "50";
        pathName = "/gis/switch/1/10/50.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "52";
        pathName = "/gis/switch/1/10/52.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "54";
        pathName = "/gis/switch/1/10/54.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        childFileName = "56";
        pathName = "/gis/switch/1/10/56.csv";
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "")).start();

        flag = 11;
        childFileName = "20";
        pathName = Constants.PATH_NAME_7;
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "five")).start();

        childFileName = "22";
        pathName = Constants.PATH_NAME_8;
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "five")).start();

        flag = 12;
        childFileName = "18";
        pathName = Constants.PATH_NAME_9;
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "five")).start();

        childFileName = "20";
        pathName = Constants.PATH_NAME_10;
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "five")).start();

        flag = 13;
        childFileName = "20";
        pathName = Constants.PATH_NAME_11;
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "five")).start();

        childFileName = "22";
        pathName = Constants.PATH_NAME_12;
        new Thread(new JobThread(mainFileName, pathName, status, flag, childFileName, line, "five")).start();
    }

    /**
     * 多线程读取文件数据写入数据库中
     */
    public class JobThread implements Runnable {
        private String mainFileName, pathName,childFileName, five;
        private Integer status, flag, line;
        public JobThread(String mainFileName, String pathName, Integer status, Integer flag, String childFileName, Integer line, String five){
            this.mainFileName = mainFileName;
            this.pathName = pathName;
            this.status = status;
            this.flag = flag;
            this.childFileName = childFileName;
            this.line = line;
            this.five = five;
        }
        @Override
        public void run() {
            readFile(mainFileName, pathName, status, flag, childFileName, line, five);
        }
    }

    /**
     * 读取csv文件
     * @param mainFileName 主文件名称
     * @param pathName 全路径
     * @param status 分闸合闸状态
     * @param flag 状态
     * @param childFileName 子文件名
     * @param line 行号索引
     * @throws IOException
     */
    @SneakyThrows
    private void readFile(String mainFileName, String pathName, Integer status, Integer flag, String childFileName, Integer line, String five) {
        try {
            String basePath = this.getClass().getResource(pathName).getPath();
            InputStream inputStream = new FileInputStream(new File(basePath));
            DataInputStream in = new DataInputStream(inputStream);
            CSVReader csvReader = new CSVReader(new InputStreamReader(in, "GBK"), CSVParser.DEFAULT_SEPARATOR,
                    CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER, line);
            int index = 1;
            String[] strs;
            List<CalculateBasicData> list = Lists.newArrayList();
            while ((strs = csvReader.readNext()) != null) {
                CalculateBasicData calculateBasicData = new CalculateBasicData();
                calculateBasicData.setId(Uuid.getUUID());
                calculateBasicData.setMainFileName(mainFileName);
                if(StrUtil.isEmpty(five)){
                    if(Objects.equals(mainFileName, Constants.GISPD)){
                        if(ArrayUtil.isEmpty(strs[3])){
                            calculateBasicData.setValueOne(Constants.NO_DATA);
                        }else {
                            calculateBasicData.setValueOne(strs[3].trim());
                        }

                        if(ArrayUtil.isEmpty(strs[4])){
                            calculateBasicData.setValueTwo(Constants.NO_DATA);
                        }else {
                            calculateBasicData.setValueTwo(strs[4].trim());
                        }
                        calculateBasicData.setValueThree(Constants.NO_DATA);
                        calculateBasicData.setValueFour(Constants.NO_DATA);
                    }else {
                        if(ArrayUtil.isEmpty(strs[0])){
                            calculateBasicData.setValueOne(Constants.NO_DATA);
                        }else {
                            calculateBasicData.setValueOne(strs[0].trim());
                        }

                        if(ArrayUtil.isEmpty(strs[1])){
                            calculateBasicData.setValueTwo(Constants.NO_DATA);
                        }else {
                            calculateBasicData.setValueTwo(strs[1].trim());
                        }

                        if(ArrayUtil.isEmpty(strs[2])){
                            calculateBasicData.setValueThree(Constants.NO_DATA);
                        }else {
                            calculateBasicData.setValueThree(strs[2].trim());
                        }

                        if(ArrayUtil.isEmpty(strs[3])){
                            calculateBasicData.setValueFour(Constants.NO_DATA);
                        }else {
                            calculateBasicData.setValueFour(strs[3].trim());
                        }
                    }
                    calculateBasicData.setValueFive(Constants.NO_DATA);
                }else {
                    if(ArrayUtil.isEmpty(strs[0])){
                        calculateBasicData.setValueOne(Constants.NO_DATA);
                    }else {
                        calculateBasicData.setValueOne(strs[0].trim());
                    }

                    if(ArrayUtil.isEmpty(strs[1])){
                        calculateBasicData.setValueTwo(Constants.NO_DATA);
                    }else {
                        calculateBasicData.setValueTwo(strs[1].trim());
                    }

                    if(ArrayUtil.isEmpty(strs[2])){
                        calculateBasicData.setValueThree(Constants.NO_DATA);
                    }else {
                        calculateBasicData.setValueThree(strs[2].trim());
                    }

                    if(ArrayUtil.isEmpty(strs[3])){
                        calculateBasicData.setValueFour(Constants.NO_DATA);
                    }else {
                        calculateBasicData.setValueFour(strs[3].trim());
                    }

                    if(ArrayUtil.isEmpty(strs[4])){
                        calculateBasicData.setValueFive(Constants.NO_DATA);
                    }else {
                        calculateBasicData.setValueFive(strs[4].trim());
                    }
                }
                calculateBasicData.setStatus(status);
                calculateBasicData.setFlag(flag);
                calculateBasicData.setChildFileName(childFileName);
                calculateBasicData.setSort(index++);
                calculateBasicData.setCreateTime(LocalDateTime.now());
                list.add(calculateBasicData);
            }

            if(CollectionUtil.isNotEmpty(list)){
                Runnable runnable = () -> this.addLocal(list);
                new Thread(runnable).start();
            }
            csvReader.close();
            in.close();
        } catch (FileNotFoundException | UnsupportedEncodingException ex) {
            log.error("读取CSV文件异常:{}", ex.getMessage());
        }
    }

    /**
     * 删除表结构
     * @param tableName
     * @return
     */
    private String dropSqlSplice(String tableName){
        StringBuffer sb = new StringBuffer();
        sb.append("DROP TABLE "+ tableName +"");
        return sb.toString();
    }

    /**
     * 创建表结构
     * @param tableName
     * @return
     */
    private String sqlSplice(String tableName){
        StringBuffer sb = new StringBuffer();
        sb.append("CREATE TABLE `"+ tableName +"` (");
        sb.append("  `id` varchar(32) NOT NULL COMMENT '主键唯一标识',");
        sb.append("  `main_file_name` varchar(32) DEFAULT NULL COMMENT '主文件名',");
        sb.append("  `value_one` varchar(32) DEFAULT NULL COMMENT '数值1',");
        sb.append("  `value_two` varchar(32) DEFAULT NULL COMMENT '数值2',");
        sb.append("  `value_three` varchar(32) DEFAULT NULL COMMENT '数值3',");
        sb.append("  `value_four` varchar(32) DEFAULT NULL COMMENT '数值4',");
        sb.append("  `value_five` varchar(32) DEFAULT NULL COMMENT '数值5',");
        sb.append("  `status` int(11) DEFAULT NULL COMMENT '分闸合闸状态',");
        sb.append("  `flag` int(11) DEFAULT NULL COMMENT '状态',");
        sb.append("  `child_file_name` varchar(32) DEFAULT NULL COMMENT '子文件名',");
        sb.append("  `sort` bigint(20) DEFAULT NULL COMMENT '排序字段',");
        sb.append("  `create_time` datetime DEFAULT NULL COMMENT '创建时间',");
        sb.append("  PRIMARY KEY (`id`),");
        sb.append("  KEY `main_file_name` (`main_file_name`) USING BTREE,");
        sb.append("  KEY `child_file_name` (`child_file_name`) USING BTREE");
        sb.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;");
        return sb.toString();
    }
}
