package com.zfire.jiasm.syncdata.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zfire.jiasm.syncdata.plus.entity.ItfGreeToFaInstallSynRule;
import com.zfire.jiasm.syncdata.plus.entity.ItfGreeToFaRepairSynRule;
import com.zfire.jiasm.syncdata.plus.service.ItfGreeToFaInstallSynRuleService;
import com.zfire.jiasm.syncdata.plus.service.ItfGreeToFaRepairSynRuleService;
import com.zfire.jiasm.syncdata.pojo.bo.DownRepairOrderGD;
import com.zfire.jiasm.syncdata.service.OrderDownloadGDService;
import com.zfire.jiasm.syncdata.service.PublicService;
import com.zfire.jiasm.syncdata.utils.DataUtil;
import com.zfire.jiasm.syncdata.utils.HttpUtil;
import com.zfire.jiasm.syncdata.utils.TokenCenter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
@Service
public class OrderDownloadGDServiceImpl implements OrderDownloadGDService {
    @Value("${gdbaseurl}")
    private String baseurl = "";
    @Value("${nullCols}")
    private String nullCols;

    @Autowired
    TokenCenter tokenCenter;
    @Resource
    ItfGreeToFaInstallSynRuleService itfGreeToFaInstallSynRuleService;
    @Resource
    ItfGreeToFaRepairSynRuleService itfGreeToFaRepairSynRuleService;
    @Resource
    private PublicService publicService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    private Set<String> nullDateCols;


    @Override
    public void processRepairDownload() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DownRepairOrderGD data = new DownRepairOrderGD();
        JSONArray datas = new JSONArray();
        try {
            ItfGreeToFaRepairSynRule synRule = itfGreeToFaRepairSynRuleService.lambdaQuery()
                    .orderByAsc(ItfGreeToFaRepairSynRule::getId)
                    .last("limit 1")
                    .one();
            if (synRule==null){
                return;
            }

            log.info("开始从工单系统获取维修工单: \uF0B7{}",sdf.format(new Date()));

            // 数据源表名
            String srcTabNames = synRule.getSrcTabName();
            String[] srcTabnameArray = srcTabNames.trim().split("\\Q|\\E");

            // 数据源表列
            String srcTabColLists = synRule.getSrcTabColList();
            String[] srcTabColListArray = srcTabColLists.trim().split("\\Q|\\E");

            // 数据目的表名
            String destTabNames = synRule.getDestTabName();
            String[] destTabNameArray = destTabNames.trim().split("\\Q|\\E");

            // 数据目的表列
            String destTabColLists = synRule.getDestTabColList();
            String[] destTabColListArray = destTabColLists.trim().split("\\Q|\\E");

            // 销售公司编码
            String salesWebsitNumber = synRule.getSalesWebsitNumber();
            String downloadBatchNo = publicService.getUUID();
            String id = "";
            // 获取数据 这里的数据为数组，会有多个工单的情况
            data = downloadOrderFromGD("sync/repairorder");
            id = data.getId();
            datas = data.getData();

            if (datas ==null){
                return;
            }

            log.info("从工单系统获取维修工单下载了{}条数据",datas.size());

            for (int i = 0; i < datas.size(); i++) {
                JSONObject sheet = datas.getJSONObject(i);
                try{
                    // 维修工单主表必须存在
                    JSONObject mainTableData = sheet.getJSONObject("tblAssign");
                    if (mainTableData != null) {
                        String downloadTaskNo = publicService.getUUID();
                        DataUtil.initLastModifiedDate(mainTableData,"last_modified_date");
                        addRepairData(mainTableData, downloadBatchNo, downloadTaskNo, sheet,
                                srcTabnameArray, srcTabColListArray, destTabNameArray, destTabColListArray,
                                salesWebsitNumber,id);
                    } else {
                        log.error("维修工单找不到主表数据,downloadBatchNo[" + downloadBatchNo + "]");
                    }
                } catch (Exception ex) {
                    log.info("维修工单下载落download表失败：{}" ,JSONObject.toJSONString(sheet));
                    ex.printStackTrace();
                }
            }

            log.info("维修工单同步下载状态");
            // 同步下载状态，修改本地下载记录表状态 gree_stat_syn_status =1
            updateRepairStatus();

            log.info("维修工单下载完成");
        } catch (Exception ex) {
            log.error("从工单系统获取维修工单失败", ex);
        }

    }

    @Override
    public void processInstallDownload() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DownRepairOrderGD data = new DownRepairOrderGD();
        JSONArray datas = new JSONArray();
        try {
            ItfGreeToFaInstallSynRule synRule = itfGreeToFaInstallSynRuleService.lambdaQuery()
                    .orderByAsc(ItfGreeToFaInstallSynRule::getId)
                    .last("limit 1")
                    .one();
            if (synRule==null){
                return;
            }

            log.info("开始从工单系统获取安装工单: \uF0B7{}",sdf.format(new Date()));

            // 数据源表名
            String srcTabNames = synRule.getSrcTabName();
            String[] srcTabnameArray = srcTabNames.trim().split("\\Q|\\E");

            // 数据源表列
            String srcTabColLists = synRule.getSrcTabColList();
            String[] srcTabColListArray = srcTabColLists.trim().split("\\Q|\\E");

            // 数据目的表名
            String destTabNames = synRule.getDestTabName();
            String[] destTabNameArray = destTabNames.trim().split("\\Q|\\E");

            // 数据目的表列
            String destTabColLists = synRule.getDestTabColList();
            String[] destTabColListArray = destTabColLists.trim().split("\\Q|\\E");

            // 销售公司编码
            String salesWebsitNumber = synRule.getSalesWebsitNumber();
            String downloadBatchNo = publicService.getUUID();
            String id = "";
            // 获取数据 这里的数据为数组，会有多个工单的情况
            data = downloadOrderFromGD("sync/installorder");
            id = data.getId();
            datas = data.getData();

            if (datas ==null){
                return;
            }

            log.info("从工单系统获取安装工单下载了{}条数据",datas.size());

            try{
                for (int i = 0; i < datas.size(); i++) {
                    JSONObject sheet = datas.getJSONObject(i);
                    JSONArray mainTableData = sheet.getJSONArray("TBL_AZ_ASSIGN_LC_LS");
                    if (mainTableData != null && mainTableData.size() > 0) {
                        String downloadTaskNo = publicService.getUUID();
                        DataUtil.initLastModifiedDate(mainTableData.getJSONObject(0),"lastModifiedDate");
                        addInstallData(mainTableData, downloadBatchNo, downloadTaskNo, sheet,
                                srcTabnameArray, srcTabColListArray, destTabNameArray, destTabColListArray,
                                salesWebsitNumber,id);
                    } else {
                        log.error("安装工单找不到主表数据,downloadBatchNo[" + downloadBatchNo + "]");
                    }
                }
            } catch (Exception ex) {
                log.info("安装工单下载失败：{}" ,ex);
                ex.printStackTrace();
            }


            log.info("安装工单同步下载状态");
            // 同步下载状态，修改本地下载记录表状态 gree_stat_syn_status =1
            updateInstallStatus();

            log.info("安装工单下载完成");
        } catch (Exception ex) {
            log.error("从工单系统获取安装工单失败", ex);
        }

    }

    private void updateInstallStatus() {
        try {
            List<Map<String, Object>> result = getNeedUpdateTaskData("itf_install_batch_download_gd_rec");
            String url = baseurl + "sync/installorderup";
            for (Map<String, Object> row : result) {
                Long synReqId = (Long) row.get("syn_req_id");
                String gdid = (String) row.get("gdid");
                try {
                    Map<String, Object> paramMap = new HashMap<String, Object>();
                    paramMap.put("id", gdid);
                    JSONObject jsonResult = HttpUtil.httpRequestWithGD(url, "POST",JSONObject.toJSONString(paramMap));

                    String code = jsonResult.getString("code");
                    if ("200".equals(code)) {
                        updateGDSuccess(gdid, "itf_install_batch_download_gd_rec");
                    } else {
                        updateGDError(gdid, "与工单系统同步失败", "itf_install_batch_download_gd_rec");
                    }
                } catch (Exception ex) {
                    log.error("工单系统更新安装主表下载状态失败syn_req_id=" + synReqId, ex);
                    updateGDError(gdid, ex.getMessage(), "itf_install_batch_download_gd_rec");
                }
            }
        } catch (Exception ex) {
            log.error("工单系统更新安装主表下载状态失败", ex);
        }
    }

    private void updateRepairStatus() {
        try {
            List<Map<String, Object>> result = getNeedUpdateTaskData("itf_repair_batch_download_gd_rec");
            String url = baseurl + "sync/repairorderup";
            for (Map<String, Object> row : result) {
                Long synReqId = (Long) row.get("syn_req_id");
                String gdid = (String) row.get("gdid");
                try {
                    Map<String, Object> paramMap = new HashMap<String, Object>();
                    paramMap.put("id", gdid);
                    JSONObject jsonResult = HttpUtil.httpRequestWithGD(url, "POST",JSONObject.toJSONString(paramMap));

                    String code = jsonResult.getString("code");
                    if ("200".equals(code)) {
                        updateGDSuccess(gdid, "itf_repair_batch_download_gd_rec");
                    } else {
                        updateError(synReqId, "与工单系统同步失败", "itf_repair_batch_download_gd_rec");
                    }
                } catch (Exception ex) {
                    log.error("工单系统更新维修主表下载状态失败syn_req_id=" + synReqId, ex);
                    updateGDError(gdid, ex.getMessage(), "itf_repair_batch_download_gd_rec");
                }
            }
        } catch (Exception ex) {
            log.error("工单系统更新维修主表下载状态失败", ex);
        }
    }

    public void updateError(Long synReqId, String errorMessage, String tabName) {
        jdbcTemplate.update(
                "update " + tabName + " set gree_stat_syn_status=99,gree_stat_syn_err_msg=? where syn_req_id=?",
                new Object[] { errorMessage, synReqId });
    }

    public void updateGDError(String gdid, String errorMessage, String tabName) {
        jdbcTemplate.update(
                "update " + tabName + " set gree_stat_syn_status=99,gree_stat_syn_err_msg=? where gdid=?",
                new Object[] { errorMessage, gdid });
    }

    public void updateSuccess(Long synReqId, String tabName) {
        jdbcTemplate.update("update " + tabName + " set gree_stat_syn_status=1,gree_stat_syn_time=? where syn_req_id=?",
                new Object[] { new Date(System.currentTimeMillis()), synReqId });
    }

    public void updateGDSuccess(String gdid, String tabName) {
        jdbcTemplate.update("update " + tabName + " set gree_stat_syn_status=1,gree_stat_syn_time=? where gdid=?",
                new Object[] { new Date(System.currentTimeMillis()), gdid });
    }

    public List<Map<String, Object>> getNeedUpdateTaskData(String tabName) {
        List<Map<String, Object>> result = jdbcTemplate
                .queryForList("SELECT * from " + tabName
                        + " where gree_stat_syn_status=0 order by syn_req_id ");
        return result;
    }


    private DownRepairOrderGD downloadOrderFromGD(String api) throws Exception {
        DownRepairOrderGD datas = new DownRepairOrderGD();
        String url = baseurl + api;
        JSONObject jsonResult = HttpUtil.httpRequestGetPgidGD(url, "GET", null);
        String code = jsonResult.getString("code");
        if ("200".equals(code)) {
            String json = jsonResult.getString("data");
            JSONObject jsona = JSONObject.parseObject(json);
            String id = jsona.getString("id");
            datas.setId(id);
            datas.setData(jsona.getJSONArray("data"));
            return datas;
        } else {
            String errorMessage = jsonResult.getString("message");
            if (errorMessage == null || errorMessage.trim().length() == 0) {
                errorMessage = "失败";
            }
            throw new Exception(errorMessage);
        }

    }


    @Transactional(rollbackFor = Exception.class)
    public void addInstallData(JSONArray mainTableData, String downloadBatchNo, String downloadTaskNo, JSONObject sheet,
                              String[] srcTabnameArray, String[] srcTabColListArray, String[] destTabNameArray,
                              String[] destTabColListArray, String salesWebsitNumber,String id) throws Exception {
        // 查看同一时间同一工单是否存在下载记录表 存在不再保存本次的同步数据
        if (insertInstallRecData(mainTableData.getJSONObject(0), downloadBatchNo, downloadTaskNo, salesWebsitNumber,id)) {
            parseInstallData(sheet, srcTabnameArray, srcTabColListArray, destTabNameArray, destTabColListArray, downloadTaskNo,
                    true, downloadBatchNo);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void addRepairData(JSONObject mainTableData, String downloadBatchNo, String downloadTaskNo, JSONObject sheet,
                              String[] srcTabnameArray, String[] srcTabColListArray, String[] destTabNameArray,
                              String[] destTabColListArray, String salesWebsitNumber, String id) throws Exception {
        // 查看同一时间同一工单是否存在下载记录表 存在不再保存本次的同步数据
        if (insertRepairRecData(mainTableData, downloadBatchNo, downloadTaskNo, salesWebsitNumber, id)) {
            parseRepairData(sheet, srcTabnameArray, srcTabColListArray, destTabNameArray, destTabColListArray, downloadTaskNo,
                    false, downloadBatchNo);
        }
    }

    private boolean insertInstallRecData(JSONObject aData, String downloadBatchNo, String downloadTaskNo,
                                        String salesWebsitNumber, String id) throws Exception {

        // 查看 相同时间同一单数据
        List<Map<String, Object>> rec = isInstallDuplicate(aData);


        boolean duplicate = (rec.size() > 0);

        String sql = "insert into itf_install_batch_download_gd_rec (gdid,sales_websit_number,download_batch_no,download_task_no,"
                + "pgguid,created_by,created_date,last_modified_by,last_modified_date,pgid,"
                + "extendfiled1,extendfiled2,extendfiled3,extendfiled4,extendfiled5"
                + (duplicate ? ",gree_stat_syn_err_msg" : "") + ") values (" + "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?"
                + (duplicate ? ",?" : "") + ") ";

        // 存在 说明重复获取，不再保存数据
        if (duplicate) {
            if (!recHasNoUploadStatus(rec)) {
                jdbcTemplate.update(sql,
                        new Object[] { id,salesWebsitNumber, downloadBatchNo, downloadTaskNo, aData.getString("pgguid"),
                                aData.getString("createdBy"), aData.getString("createdDate"),
                                aData.getString("lastModifiedBy"), aData.getString("lastModifiedDate"),
                                aData.getString("pgid"), aData.getString("extendfiled1"),
                                aData.getString("extendfiled2"), aData.getString("extendfiled3"),
                                aData.getString("extendfiled4"), aData.getString("extendfiled5"), "任务已经存在，不再产生下载数据" });
            }
            return false;
        } else {
            jdbcTemplate.update(sql, new Object[] { id,salesWebsitNumber, downloadBatchNo, downloadTaskNo,
                    aData.getString("pgguid"), aData.getString("createdBy"), aData.getString("createdDate"),
                    aData.getString("lastModifiedBy"), aData.getString("lastModifiedDate"), aData.getString("pgid"),
                    aData.getString("extendfiled1"), aData.getString("extendfiled2"), aData.getString("extendfiled3"),
                    aData.getString("extendfiled4"), aData.getString("extendfiled5") });
            return true;
        }

    }

    private boolean insertRepairRecData(JSONObject aData, String downloadBatchNo, String downloadTaskNo,
                                        String salesWebsitNumber, String id) throws Exception {

        // 查看 相同时间同一单数据
        List<Map<String, Object>> rec = isRepairDuplicate(aData);


        boolean duplicate = (rec.size() > 0);

        String sql = "insert into itf_repair_batch_download_gd_rec (sales_websit_number,download_batch_no,download_task_no,"
                + "created_by,created_date,last_modified_by,last_modified_date,pgid,"
                + "extjson1,extjson2,extjson3,extjson4,extjson5,gdid" + (duplicate ? ",gree_stat_syn_err_msg" : "")
                + ") values (" + "?,?,?,?,?,?,?,?,?,?,?,?,?,?" + (duplicate ? ",?" : "") + ") ";

        // 存在 说明重复获取，不再保存数据
        if (duplicate) {
            if (!recHasNoUploadStatus(rec)) {
                jdbcTemplate.update(sql,
                        new Object[] { salesWebsitNumber, downloadBatchNo, downloadTaskNo,
                                aData.getString("created_by"), aData.getString("created_date"),
                                aData.getString("last_modified_by"), aData.getString("last_modified_date"),
                                aData.getString("pgid"), aData.getString("extjson1"), aData.getString("extjson2"),
                                aData.getString("extjson3"), aData.getString("extjson4"),
                                aData.getString("extjson5"), id,
                                "任务已经存在，不再产生下载数据" });
            }
            return false;
        } else {
            jdbcTemplate.update(sql,
                    new Object[] { salesWebsitNumber, downloadBatchNo, downloadTaskNo, aData.getString("created_by"),
                            aData.getString("created_date"), aData.getString("last_modified_by"),
                            aData.getString("last_modified_date"), aData.getString("pgid"), aData.getString("extjson1"),
                            aData.getString("extjson2"), aData.getString("extjson3"), aData.getString("extjson4"),
                            aData.getString("extjson5"),id });
            return true;
        }

    }

    private List<Map<String, Object>> isInstallDuplicate(JSONObject aData) throws DataAccessException, ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<Map<String, Object>> result = jdbcTemplate.queryForList(
                "select * from itf_install_batch_download_gd_rec where pgguid=? and last_modified_date=?",
                new Object[] { aData.getString("pgguid"), sdf.parse(aData.getString("lastModifiedDate")) });

        return result;
    }

    private List<Map<String, Object>> isRepairDuplicate(JSONObject aData) throws DataAccessException, ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<Map<String, Object>> result = jdbcTemplate.queryForList(
                "select * from itf_repair_batch_download_gd_rec where pgid=? and last_modified_date=?",
                new Object[] { aData.getString("pgid"), sdf.parse(aData.getString("last_modified_date")) });

        return result;
    }

    private boolean recHasNoUploadStatus(List<Map<String, Object>> rec) {
        for (Map<String, Object> data : rec) {
            int greeStatSynStatus = (Integer) data.get("gree_stat_syn_status");
            if (greeStatSynStatus == 0) {
                return true;
            }
        }
        return false;
    }

    private void parseInstallData(JSONObject sheet, String[] srcTabnameArray, String[] srcTabColListArray,
                                 String[] destTabNameArray, String[] destTabColListArray, String downloadTaskNo, boolean isInstall,
                                 String downloadBatchNo) throws Exception {

        for (int i = 0; i < srcTabnameArray.length; i++) {

            String srcTabname = srcTabnameArray[i];
            String srcTabColList = srcTabColListArray[i];
            String destTabName = destTabNameArray[i];
            String destTabColList = destTabColListArray[i];

            String[] arySrcTabCol = srcTabColList.split("\\Q,\\E");

            JSONArray tableData = new JSONArray();

            if (isInstall) {
                tableData = sheet.getJSONArray(srcTabname.toUpperCase());
            } else {
                if ("tblAssign".equalsIgnoreCase(DataUtil.fromDbName2ServiceName(srcTabname))) {
                    tableData = new JSONArray();
                    tableData.add(sheet.getJSONObject("tblAssign"));
                } else {
                    tableData = sheet.getJSONArray(DataUtil.fromDbName2ServiceName(srcTabname));
                }

            }

            if (tableData == null){
                continue;
            }

            for (int j = 0; j < tableData.size(); j++) {
                parseInstallData(isInstall, downloadBatchNo, downloadTaskNo, destTabName, destTabColList, arySrcTabCol,
                        tableData.getJSONObject(j));
            }
        }
    }

    private void parseRepairData(JSONObject sheet, String[] srcTabnameArray, String[] srcTabColListArray,
                           String[] destTabNameArray, String[] destTabColListArray, String downloadTaskNo, boolean isInstall,
                           String downloadBatchNo) throws Exception {

        for (int i = 0; i < srcTabnameArray.length; i++) {

            String srcTabname = srcTabnameArray[i];
            String srcTabColList = srcTabColListArray[i];
            String destTabName = destTabNameArray[i];
            String destTabColList = destTabColListArray[i];

            String[] arySrcTabCol = srcTabColList.split("\\Q,\\E");

            JSONArray tableData = new JSONArray();

            if (isInstall) {
                tableData = sheet.getJSONArray(srcTabname.toUpperCase());
            } else {
                if ("tblAssign".equalsIgnoreCase(DataUtil.fromDbName2ServiceName(srcTabname))) {
                    tableData = new JSONArray();
                    tableData.add(sheet.getJSONObject("tblAssign"));
                } else {
                    tableData = sheet.getJSONArray(DataUtil.fromDbName2ServiceName(srcTabname));
                }

            }

            if (tableData == null){
                continue;
            }

            for (int j = 0; j < tableData.size(); j++) {
                parseRepairAData(isInstall, downloadBatchNo, downloadTaskNo, destTabName, destTabColList, arySrcTabCol,
                        tableData.getJSONObject(j));
            }
        }
    }

    public void parseInstallData(boolean isInstall, String downloadBatchNo, String downloadTaskNo, String destTabName,
                                 String destTabColList, String[] arySrcTabCol, JSONObject aData) throws Exception {

        String table = "itf_tbl_az_assign_platform_download";

        String[] arySrcTabColValue = new String[arySrcTabCol.length];

        for (int j = 0; j < arySrcTabCol.length; j++) {
            if (isInstall) {
                if (destTabName.equals(table) && (j==2 || j== 4 || j==9)){
                    if (StringUtils.isEmpty(DataUtil.findValue(DataUtil.fromDbName2ServiceName(arySrcTabCol[j].trim()),aData))){
                        arySrcTabColValue[j] = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                        continue;
                    }
                }

                if (destTabName.equals(table) && (j==5 || j== 6)){
                    if (StringUtils.isEmpty(DataUtil.findValue(DataUtil.fromDbName2ServiceName(arySrcTabCol[j].trim()),aData))){
                        arySrcTabColValue[j] = "0";
                        continue;
                    }
                }

                arySrcTabColValue[j] = DataUtil.findValue(DataUtil.fromDbName2ServiceName(arySrcTabCol[j].trim()), aData);
            } else {
                String colName = arySrcTabCol[j].trim();
                arySrcTabColValue[j] = DataUtil.findValue(colName, aData);
                if (getNullColSet().contains(colName)) {
                    if ("".equals(arySrcTabColValue[j])) {
                        arySrcTabColValue[j] = null;
                    }
                }
            }

        }

        insertAData(downloadBatchNo, downloadTaskNo, destTabName, destTabColList, arySrcTabColValue);

    }

    public void parseRepairAData(boolean isInstall, String downloadBatchNo, String downloadTaskNo, String destTabName,
                           String destTabColList, String[] arySrcTabCol, JSONObject aData) throws Exception {
        String table = "itf_tbl_az_assign_platform_download";
        String daijian = "itf_tbl_assign_daijian_download";
        String yd = "itf_tbl_assign_xzyd_download";

        String[] arySrcTabColValue = new String[arySrcTabCol.length];

        for (int j = 0; j < arySrcTabCol.length; j++) {
            if (isInstall) {
                if (destTabName.equals(table) && (j==2 || j== 4 || j==9)){
                    if (StringUtils.isEmpty(DataUtil.findValue(DataUtil.fromDbName2ServiceName(arySrcTabCol[j].trim()),aData))){
                        arySrcTabColValue[j] = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                        continue;
                    }
                }
                arySrcTabColValue[j] = DataUtil.findValue(DataUtil.fromDbName2ServiceName(arySrcTabCol[j].trim()),
                        aData);
            } else {
                if (destTabName.equals(daijian)){
                    //创建时间 cjdt
                    DataUtil.initLastModifiedDate(aData,"cjdt");
                    // 商品大类 splb
                    DataUtil.initOther(aData,"splb");
                    // 替换编码销售公司库存 thbmxsgsnum
                    DataUtil.initOther(aData,"thbmxsgsnum");
                    // 替换编码区域库存 thbmqynum
                    DataUtil.initOther(aData,"thbmqynum");
                    // 替换编码网点库存 thbmwdnum
                    DataUtil.initOther(aData,"thbmwdnum");
                    // 配件系统处理标识 pjxtflag
                    DataUtil.initOther(aData,"pjxtflag");
                    // 区域配件库存 djquyunum
                    DataUtil.initOther(aData,"djquyunum");
                    // 销售公司配件库存 djxsgsnum
                    DataUtil.initOther(aData,"djxsgsnum");
                    // 网点配件库存 djwdnum
                    DataUtil.initOther(aData,"djwdnum");
                } else if (destTabName.equals(yd)){
                    DataUtil.initOther(aData,"cshu");
                    DataUtil.initOther(aData,"ydbz");
                }

                String colName = arySrcTabCol[j].trim();
                arySrcTabColValue[j] = DataUtil.findValue(colName, aData);
                if (getNullColSet().contains(colName)) {
                    if ("".equals(arySrcTabColValue[j])) {
                        arySrcTabColValue[j] = null;
                    }
                }
            }
        }

        insertAData(downloadBatchNo, downloadTaskNo, destTabName, destTabColList, arySrcTabColValue);
    }

    public void insertAData(String downloadBatchNo, String downloadTaskNo, String destTabName, String destTabColList,
                            Object[] arySrcTabColValue) throws Exception {

        StringBuffer mark = new StringBuffer();
        for (int i = 0; i < arySrcTabColValue.length; i++) {
            if (mark.length() > 0) {
                mark.append(",");
            }
            mark.append("?");
        }

        String sql = "insert into " + destTabName + " (download_batch_no,download_task_no," + destTabColList
                + ") values (?,?," + mark + ") ";

        Object[] insertValue = new Object[arySrcTabColValue.length + 2];
        insertValue[0] = downloadBatchNo;
        insertValue[1] = downloadTaskNo;
        System.arraycopy(arySrcTabColValue, 0, insertValue, 2, arySrcTabColValue.length);
        jdbcTemplate.update(sql, insertValue);

    }

    private synchronized Set<String> getNullColSet(){
        if(nullDateCols==null) {
            nullDateCols=new HashSet<String>();
            String[] nullColArray=nullCols.split("\\Q,\\E");
            for(String value:nullColArray) {
                nullDateCols.add(value);
            }
        }
        return nullDateCols;
    }

}
