package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.google.gson.Gson;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.ActSuppliesCleaningRuleMapper;
import com.ruoyi.system.mapper.ActSuppliesDetailsMapper;
import com.ruoyi.system.mapper.ActSuppliesHistorydataMapper;
import com.ruoyi.system.service.IActSuppliesHistorydataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * 历史数据Service业务层处理
 *
 * @author dongjg
 * @date 2023-08-01
 */
@Service
@EnableTransactionManagement
public class ActSuppliesHistorydataServiceImpl implements IActSuppliesHistorydataService {
    @Autowired
    private ActSuppliesHistorydataMapper actSuppliesHistorydataMapper;

    @Autowired
    private ActSuppliesCleaningRuleMapper actSuppliesCleaningRuleMapper;

    @Autowired
    private ActSuppliesDetailsMapper actSuppliesDetailsMapper;


    /**
     * 查询 历史数据
     *
     * @param id 历史数据ID
     * @return 历史数据
     */
    @Override
    public ActSuppliesHistorydata selectActSuppliesHistorydataById(Long id) {
        //return actSuppliesHistorydataMapper.selectActSuppliesHistorydataById(id);
        return actSuppliesHistorydataMapper.selectActSuppliesHistorydataCopyById(id);

    }

    /**
     * 查询 历史数据列表
     *
     * @param actSuppliesHistorydata 历史数据
     * @return 历史数据
     */
    @Override
    public List<ActSuppliesHistorydata> selectActSuppliesHistorydataList(ActSuppliesHistorydata actSuppliesHistorydata) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        actSuppliesHistorydata.setCreateBy(user.getUserName());
        //return actSuppliesHistorydataMapper.selectActSuppliesHistorydataList(actSuppliesHistorydata);
        return actSuppliesHistorydataMapper.selectActSuppliesHistorydataCopyList(actSuppliesHistorydata);

    }

    /**
     * 新增 历史数据
     *
     * @param actSuppliesHistorydata 历史数据
     * @return 结果
     */
    @Override
    public int insertActSuppliesHistorydata(List<ActSuppliesHistorydata> actSuppliesHistorydata) {

        for (int i = 0; i < actSuppliesHistorydata.size(); i++) {

            Long status = actSuppliesHistorydata.get(i).getStatus();

        }
        return actSuppliesHistorydataMapper.insertActSuppliesHistorydata(actSuppliesHistorydata.get(0));
    }

    /**
     * 修改 历史数据
     *
     * @param actSuppliesHistorydata 历史数据
     * @return 结果
     */
    @Override
    public int updateActSuppliesHistorydata(ActSuppliesHistorydata actSuppliesHistorydata) {
        return actSuppliesHistorydataMapper.updateActSuppliesHistorydata(actSuppliesHistorydata);
    }

    /**
     * 批量删除 历史数据
     *
     * @param ids 需要删除的 历史数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteActSuppliesHistorydataByIds(Long[] ids) {

        int i = 0;
        for (Long id : ids) {
            ActSuppliesHistorydata actSuppliesHistorydata = actSuppliesHistorydataMapper.selectActSuppliesHistorydataCopyById(id);
            //删除历史表
            i = actSuppliesHistorydataMapper.deleteActSuppliesHistorydataById(id);
            //删除
            int i1 = actSuppliesHistorydataMapper.deleteActSuppliesHistorycontentById(actSuppliesHistorydata.getImportUuid());
            //删除清洗表
            int i2 = actSuppliesCleaningRuleMapper.deleteActSuppliesImportTableCopy(actSuppliesHistorydata.getImportUuid());
            i=i+i1;
            i=i+i2;
        }
        return i;
    }

    /**
     * 删除 历史数据信息
     *
     * @param id 历史数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteActSuppliesHistorydataById(Long id) {
        ActSuppliesHistorydata actSuppliesHistorydata = actSuppliesHistorydataMapper.selectActSuppliesHistorydataCopyById(id);
        //删除历史表
        int i = actSuppliesHistorydataMapper.deleteActSuppliesHistorydataById(id);
        //删除
        int i1 = actSuppliesHistorydataMapper.deleteActSuppliesHistorycontentById(actSuppliesHistorydata.getImportUuid());
        //删除清洗表
        int i2 = actSuppliesCleaningRuleMapper.deleteActSuppliesImportTableCopy(actSuppliesHistorydata.getImportUuid());
        i=i+i1;
        i=i+i2;
        return i;
    }

    /**
     * 新增 历史数据信息
     *
     * @param actSuppliesHistorydata 历史数据ID
     * @return 结果
     */
    @Override
    public int addInsertActSuppliesHistorydata(List<ActSuppliesHistorydata> actSuppliesHistorydata) {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        ActSuppliesHistorydata actSuppliesHistorydata1 = new ActSuppliesHistorydata();

        List<ActSuppliesHistorydata> s = actSuppliesHistorydataMapper.selectActSuppliesHistorydatauuid(actSuppliesHistorydata.get(0));

        if (s.size() > 0) {
            //如果存在相同时间和所属矿的表
            if (s.get(0).getCleanStatus() > 0) {

                for (int j = 0; j < s.size(); j++) {
                    if (s.get(j).getIdentifyingCode() == 0) {
                        //删除导入表数据
                        actSuppliesCleaningRuleMapper.deleteActSuppliesImportTable(s.get(j).getId());
                        actSuppliesCleaningRuleMapper.deleteActSuppliesImportCleaning(s.get(j).getId());
                    } else if (s.get(j).getIdentifyingCode() == 1) {
                        //删除生成表数据
                        actSuppliesCleaningRuleMapper.deleteActSuppliesSixMinesSummaryTable(s.get(j).getId());
                    }
                }

            }
            for (int i = 0; i < actSuppliesHistorydata.size(); i++) {
                if (actSuppliesHistorydata.get(i).getIdentifyingCode() == 0) {
                    for (int j = 0; j < s.size(); j++) {
                        if (s.get(j).getIdentifyingCode() == 0) {
                            actSuppliesHistorydata.get(i).setImportUuid(s.get(j).getImportUuid());
                        }
                    }
                }
                if (actSuppliesHistorydata.get(i).getIdentifyingCode() == 1) {
                    for (int j = 0; j < s.size(); j++) {
                        if (s.get(j).getIdentifyingCode() == 1) {
                            actSuppliesHistorydata.get(i).setImportUuid(s.get(j).getImportUuid());
                        }
                    }
                }

                actSuppliesHistorydata.get(i).setCreateBy(user.getUserName());
                actSuppliesHistorydata.get(i).setCreateTime(DateUtils.getNowDate());
                actSuppliesHistorydataMapper.updateActSuppliesHistorydatas(actSuppliesHistorydata.get(i));
                actSuppliesHistorydataMapper.updateActSUppliesHistoryContent(actSuppliesHistorydata.get(i));

            }
        } else {
            //如果没有重复数据直接新增导入表和生成表
            for (int i = 0; i < actSuppliesHistorydata.size(); i++) {

                UUID uuid = UUID.randomUUID();
                String uuids = StringUtils.remove(uuid.toString(), '-');
                if (actSuppliesHistorydata.get(i).getIdentifyingCode() == 0) {

                    actSuppliesHistorydata1 = actSuppliesHistorydata.get(i);
                    actSuppliesHistorydata1.setImportUuid(uuids);
                    actSuppliesHistorydata1.setStatus(0L);
                    actSuppliesHistorydata1.setIdentifyingCode(0L);
                    actSuppliesHistorydataMapper.insertActSuppliesHistorydata(actSuppliesHistorydata1);

                    actSuppliesHistorydataMapper.insertActSuppliesHistorycontent(actSuppliesHistorydata1);

                } else {
                    actSuppliesHistorydata1 = actSuppliesHistorydata.get(i);
                    actSuppliesHistorydata1.setImportUuid(uuids);
                    actSuppliesHistorydata1.setStatus(0L);
                    actSuppliesHistorydata1.setIdentifyingCode(1L);
                    actSuppliesHistorydataMapper.insertActSuppliesHistorydata(actSuppliesHistorydata1);

                    actSuppliesHistorydataMapper.insertActSuppliesHistorycontent(actSuppliesHistorydata1);

                }

            }

            for (int i = 0; i < actSuppliesHistorydata.size(); i++) {

                actSuppliesHistorydata.get(i).setCreateBy(user.getUserName());
                actSuppliesHistorydata.get(i).setCreateTime(DateUtils.getNowDate());
                actSuppliesHistorydataMapper.updateActSuppliesHistorydatas(actSuppliesHistorydata.get(i));

            }

        }

        return 0;
    }

    /**
     * 获取所有历史数据
     *
     * @return 结果
     */
    @Override
    public List<ActSuppliesHistorydata> selectActSuppliesHistorydata(Integer tylkStatus) {
        return actSuppliesHistorydataMapper.selectActSuppliesHistorydata(tylkStatus);
    }

    @Override
    public int updateActSuppliesHistorydataStatusById(Long gethId) {
        return actSuppliesHistorydataMapper.updateActSuppliesHistorydataStatusById(gethId);
    }

    /**
     * 生成表数据清洗
     *
     * @return 结果
     */
    @Override
    @Transactional
    public void addInsertImport(List<ActOperation1> actOperation2s) {
        //创建新线程
        Thread thread = new Thread() {

            public void run() {

                for (int i = 0; i < actOperation2s.size(); i++) {
                    //获取生成excel表数据存储到生成表实体类中
                    for (int x = 2; x < actOperation2s.get(i).getActOperation2().length; x++) {

                        ActSuppliesSixMinesSummaryTable actSuppliesSixMinesSummaryTable = new ActSuppliesSixMinesSummaryTable();

                        UUID uuid = UUID.randomUUID();
                        String s = StringUtils.remove(uuid.toString(), '-');
                        actSuppliesSixMinesSummaryTable.setId(s);

                        actSuppliesSixMinesSummaryTable.sethId(actOperation2s.get(i).gethId());
                        if (actOperation2s.get(i).getActOperation2()[x][0].getV() != null) {
                            actSuppliesSixMinesSummaryTable.setProjectId(actOperation2s.get(i).getActOperation2()[x][0].getV());
                        } else {
                            break;
                        }

                        actSuppliesSixMinesSummaryTable.setProjectName(actOperation2s.get(i).getActOperation2()[x][1].getV());
                        actSuppliesSixMinesSummaryTable.setDepartmentName(actOperation2s.get(i).getActOperation2()[x][3].getV());
                        actSuppliesSixMinesSummaryTable.setUnit(actOperation2s.get(i).getActOperation2()[x][4].getV());

                        if (actOperation2s.get(i).getActOperation2()[x][5].getV() != null) {
                            actSuppliesSixMinesSummaryTable.setNumber(Double.valueOf(actOperation2s.get(i).getActOperation2()[x][5].getV()));
                        }
                        if (actOperation2s.get(i).getActOperation2()[x][6].getV() != null) {
                            actSuppliesSixMinesSummaryTable.setNuitPrice(Double.valueOf(actOperation2s.get(i).getActOperation2()[x][6].getV()));
                        }
                        if (actOperation2s.get(i).getActOperation2()[x][7].getV() != null) {
                            actSuppliesSixMinesSummaryTable.setMoney(Double.valueOf(actOperation2s.get(i).getActOperation2()[x][7].getV()));
                        }
                        if (actOperation2s.get(i).getActOperation2()[x][8].getV() != null) {
                            actSuppliesSixMinesSummaryTable.setCostChannels(actOperation2s.get(i).getActOperation2()[x][8].getV());
                        }

                        actSuppliesSixMinesSummaryTable.setCreateBy(actOperation2s.get(i).getCreateBy());
                        actSuppliesSixMinesSummaryTable.sethDate(actOperation2s.get(i).gethDate());
                        actSuppliesSixMinesSummaryTable.setMining(actOperation2s.get(i).getMining());
                        //新增一条生成表数据
                        actSuppliesCleaningRuleMapper.addActSuppliesSixMinesSummaryTable(actSuppliesSixMinesSummaryTable);

                    }
                    //修改历史表中的数据状态（是否清洗）
                    actSuppliesHistorydataMapper.updateActSuppliesHistorydataStatusById(actOperation2s.get(i).gethId());

                }

            }

        };

        //结束线程
        thread.start();

    }

    /**
     * 导入表数据清洗
     *
     * @return 结果
     */
    @Override
    @Transactional
    public void addInsertImportss(List<ActOperation1> actOperation2s) {

        Thread thread = new Thread() {

            public void run() {

                //导入清洗表数据
                for (int i = 0; i < actOperation2s.size(); i++) {
                    //JSONArray parse = (JSONArray) JSONArray.parse(actOperation2s.get(i).getHistoryRole());
                    //int k=(int)parse.getJSONObject(0).get("HeaderRow");
                    //获取导入excel表数据存储到生成表实体类中
                    JSONArray parse1 = (JSONArray) JSONArray.parse(actOperation2s.get(i).getHistoryRole());
                    for (int x = (int) parse1.getJSONObject(0).get("HeaderRow"); x < actOperation2s.get(i).getActOperation2().length; x++) {
                        //判断数据是否为空，为空结束循环
                        if (actOperation2s.get(i).getActOperation2()[x] != null) {
                            ActSuppliesImportCleaning actSuppliesImportCleaning = new ActSuppliesImportCleaning();

                            UUID uuid = UUID.randomUUID();
                            String s = StringUtils.remove(uuid.toString(), '-');
                            actSuppliesImportCleaning.setId(s);
                            actSuppliesImportCleaning.setRoleId(actOperation2s.get(i).getRoleId());
                            actSuppliesImportCleaning.sethId(actOperation2s.get(i).gethId());
                            actSuppliesImportCleaning.setCreateBy(actOperation2s.get(i).getCreateBy());
                            actSuppliesImportCleaning.setCreateTime(DateUtils.getNowDate());
                            actSuppliesImportCleaning.setDate(actOperation2s.get(i).gethDate());
                            actSuppliesImportCleaning.setSskId(actOperation2s.get(i).getMining());
                            //获取数组x行的真实非空长度
                            //List<Object> actOperation2s1 = Arrays.asList(actOperation2s.get(i).getActOperation2()[x]);
                            //long count = Arrays.stream(actOperation2s.get(i).getActOperation2()[x]).filter(Objects::nonNull).count();
                            //循环保存到实体类字段中
                            JSONArray parse = (JSONArray) JSONArray.parse(actOperation2s.get(i).getHistoryRole());
                            if (actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("CodeColumn") - 1)] != null) {
                                if (actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("CodeColumn") - 1)].getV() != null) {
                                    //[{"HeaderRow": 1, "CodeColumn": 6, "DateColumn": 5, "NameColumn": 7, "MoneyColumn": 12, "NumberColumn": 11, "DepartmentColumn": 2}]
                                    actSuppliesImportCleaning.setNameColumn(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("NameColumn") - 1)].getV());
                                    actSuppliesImportCleaning.setCodeColumn(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("CodeColumn") - 1)].getV());
                                    actSuppliesImportCleaning.setDepartmentColumn(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("DepartmentColumn") - 1)].getV());
                                    actSuppliesImportCleaning.setNumberColumn(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("NumberColumn") - 1)].getV());
                                    actSuppliesImportCleaning.setMoneyColumn(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("MoneyColumn") - 1)].getV());
                                    Object dateColumn = parse.getJSONObject(0).get("DateColumn");
                                    if (parse.getJSONObject(0).get("DateColumn").hashCode() > 0) {
                                        actSuppliesImportCleaning.setDateColumn(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("DateColumn") - 1)].getV());
                                    }
                                    //actSuppliesImportCleaning.setDateColumn(actOperation2s.get(i).getActOperation2()[x][JSON.parseObject(actOperation2s.get(i).getHistoryRole()).getInteger("DateColumn")].getV());
                                    //新增一条生成表数据
                                    actSuppliesCleaningRuleMapper.addActSuppliesImportCleaning(actSuppliesImportCleaning);
                                }
                            }
                        } else {
                            break;
                        }

                    }
                }

//                for (int i = 0; i < actOperation2s.size(); i++) {
//
//                    //获取导入excel表数据存储到生成表实体类中
//                    for (int x = 0; x < actOperation2s.get(i).getActOperation2().length; x++) {
//                        //判断数据是否为空，为空结束循环
//                        if (actOperation2s.get(i).getActOperation2()[x] != null) {
//                            ActSuppliesImportCleaning actSuppliesImportCleaning = new ActSuppliesImportCleaning();
//
//                            UUID uuid = UUID.randomUUID();
//                            String s = StringUtils.remove(uuid.toString(), '-');
//                            actSuppliesImportCleaning.setId(s);
//                            actSuppliesImportCleaning.setRoleId(actOperation2s.get(i).gethId());
//                            actSuppliesImportCleaning.setDate(actOperation2s.get(i).gethDate());
//                            actSuppliesImportCleaning.setSskId(actOperation2s.get(i).getMining());
//                            //获取数组x行的真实非空长度
//                            List<Object> actOperation2s1 = Arrays.asList(actOperation2s.get(i).getActOperation2()[x]);
//                            long count = Arrays.stream(actOperation2s.get(i).getActOperation2()[x]).filter(Objects::nonNull).count();
//
//                            //循环保存到实体类字段中
//                            for (int y = 0; y < count; y++) {
//                                if (actOperation2s.get(i).getActOperation2()[x][y] != null) {
//                                    if (actOperation2s.get(i).getActOperation2()[x][y].getV() != null) {
//                                        //[{"HeaderRow": 1, "CodeColumn": 6, "DateColumn": 5, "NameColumn": 7, "MoneyColumn": 12, "NumberColumn": 11, "DepartmentColumn": 2}]
//                                        JSONArray parse = (JSONArray) JSONArray.parse(actOperation2s.get(i).getHistoryRole());
//                                        actSuppliesImportCleaning.setNameColumn(actOperation2s.get(i).getActOperation2()[x][(int) parse.getJSONObject(0).get("NameColumn")-1].getV());
//                                        actSuppliesImportCleaning.setCodeColumn(actOperation2s.get(i).getActOperation2()[x][(int) parse.getJSONObject(0).get("CodeColumn")-1].getV());
//                                        actSuppliesImportCleaning.setDepartmentColumn(actOperation2s.get(i).getActOperation2()[x][(int) parse.getJSONObject(0).get("DepartmentColumn")-1].getV());
//                                        actSuppliesImportCleaning.setNumberColumn(actOperation2s.get(i).getActOperation2()[x][(int) parse.getJSONObject(0).get("NumberColumn")-1].getV());
//                                        actSuppliesImportCleaning.setMoneyColumn(actOperation2s.get(i).getActOperation2()[x][(int) parse.getJSONObject(0).get("MoneyColumn")-1].getV());
//                                        actSuppliesImportCleaning.setDateColumn(actOperation2s.get(i).getActOperation2()[x][(int) parse.getJSONObject(0).get("DateColumn")-1].getV());
//                                    }
//                                }
//
//                            }
//                            //新增一条生成表数据
//                            actSuppliesCleaningRuleMapper.addActSuppliesImportCleaning(actSuppliesImportCleaning);
//                        } else {
//                            break;
//                        }
//
//                    }
//                }


                String[] attributes = new String[200];

                for (int i = 1; i <= 200; i++) {

                    attributes[i - 1] = 'A' + String.valueOf(i);

                }

                for (int i = 0; i < actOperation2s.size(); i++) {
                    //获取导入excel表数据存储到生成表实体类中
                    for (int x = 0; x < actOperation2s.get(i).getActOperation2().length; x++) {
                        //判断数据是否为空，为空结束循环
                        if (actOperation2s.get(i).getActOperation2()[x] != null) {
                            ActSuppliesImportTable actSuppliesImportTable = new ActSuppliesImportTable();

                            UUID uuid = UUID.randomUUID();
                            String s = StringUtils.remove(uuid.toString(), '-');
                            actSuppliesImportTable.setId(s);
                            actSuppliesImportTable.sethId(actOperation2s.get(i).gethId());
                            actSuppliesImportTable.sethDate(actOperation2s.get(i).gethDate());
                            actSuppliesImportTable.setMining(actOperation2s.get(i).getMining());
                            //获取数组x行的真实非空长度
                            List<Object> actOperation2s1 = Arrays.asList(actOperation2s.get(i).getActOperation2()[x]);
                            long count = Arrays.stream(actOperation2s.get(i).getActOperation2()[x]).filter(Objects::nonNull).count();

                            //循环保存到实体类字段中
                            for (int y = 0; y < count; y++) {
                                if (actOperation2s.get(i).getActOperation2()[x][y] != null) {
                                    if (actOperation2s.get(i).getActOperation2()[x][y].getV() != null) {
                                        try {
                                            actSuppliesImportTable.getClass().getMethod("set" + attributes[y], String.class).invoke(actSuppliesImportTable, actOperation2s.get(i).getActOperation2()[x][y].getV());

                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }

                                    }
                                }

                            }
                            //新增一条生成表数据
                            actSuppliesCleaningRuleMapper.addActSuppliesImportTable(actSuppliesImportTable);
                        } else {
                            break;
                        }

                    }
                    //修改历史表中的数据状态（是否清洗）
                    actSuppliesHistorydataMapper.updateActSuppliesHistorydataStatusById(actOperation2s.get(i).gethId());

                }

            }

        };

        thread.start();

    }


    /**
     * 通用生成表数据清洗
     *
     * @return 结果
     */
    @Override
    @Transactional
    public void addInsertConvert1(List<ActOperation1> actOperation2s) {
        Thread thread = new Thread() {

            public void run() {

                //String[] attributes = {"A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28", "A29", "A30", "A31", "A32", "A33", "A34", "A35", "A36", "A37", "A38", "A39", "A40", "A41", "A42", "A43", "A44", "A45", "A46", "A47", "A48", "A49", "A50", "A51", "A52", "A53", "A54", "A55", "A56", "A57", "A58", "A59", "A60", "A61", "A62", "A63"};

                String[] attributes = new String[200];

                for (int i = 1; i <= 200; i++) {

                    attributes[i - 1] = 'A' + String.valueOf(i);

                }

                for (int i = 0; i < actOperation2s.size(); i++) {
                    //获取导入excel表数据存储到生成表实体类中
                    for (int x = 0; x < actOperation2s.get(i).getActOperation2().length; x++) {
                        //判断数据是否为空，为空结束循环
                        if (actOperation2s.get(i).getActOperation2()[x] != null) {
                            ActSuppliesImportTable actSuppliesImportTable = new ActSuppliesImportTable();

                            UUID uuid = UUID.randomUUID();
                            String s = StringUtils.remove(uuid.toString(), '-');
                            actSuppliesImportTable.setId(s);
                            actSuppliesImportTable.sethId(actOperation2s.get(i).gethId());
                            actSuppliesImportTable.sethDate(actOperation2s.get(i).gethDate());
                            actSuppliesImportTable.setMining(actOperation2s.get(i).getMining());
                            //获取数组x行的真实非空长度
                            List<Object> actOperation2s1 = Arrays.asList(actOperation2s.get(i).getActOperation2()[x]);
                            long count = Arrays.stream(actOperation2s.get(i).getActOperation2()[x]).filter(Objects::nonNull).count();

                            //循环保存到实体类字段中
                            for (int y = 0; y < count; y++) {
                                if (actOperation2s.get(i).getActOperation2()[x][y] != null) {
                                    if (actOperation2s.get(i).getActOperation2()[x][y].getV() != null) {
                                        try {
                                            actSuppliesImportTable.getClass().getMethod("set" + attributes[y], String.class).invoke(actSuppliesImportTable, actOperation2s.get(i).getActOperation2()[x][y].getV());
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }

                                    }
                                }

                            }
                            //新增一条生成表数据
                            actSuppliesCleaningRuleMapper.addInsertConvert1(actSuppliesImportTable);
                        } else {
                            break;
                        }

                    }
                    //修改历史表中的数据状态（是否清洗）
                    actSuppliesHistorydataMapper.updateActSuppliesHistorydataStatusById(actOperation2s.get(i).gethId());

                }

            }

        };

        thread.start();
    }

    /**
     * 通用导入表数据清洗
     *
     * @return 结果
     */
    @Override
    @Transactional
    public void addInsertConvert2(List<ActOperation1> actOperation2s) {
        Thread thread = new Thread() {

            public void run() {


                String[] attributes = new String[200];

                for (int i = 1; i <= 200; i++) {

                    attributes[i - 1] = 'A' + String.valueOf(i);

                }

                for (int i = 0; i < actOperation2s.size(); i++) {
                    //获取导入excel表数据存储到生成表实体类中
                    for (int x = 0; x < actOperation2s.get(i).getActOperation2().length; x++) {
                        //判断数据是否为空，为空结束循环
                        if (actOperation2s.get(i).getActOperation2()[x] != null) {
                            ActSuppliesImportTable actSuppliesImportTable = new ActSuppliesImportTable();

                            UUID uuid = UUID.randomUUID();
                            String s = StringUtils.remove(uuid.toString(), '-');
                            actSuppliesImportTable.setId(s);
                            actSuppliesImportTable.sethId(actOperation2s.get(i).gethId());
                            actSuppliesImportTable.sethDate(actOperation2s.get(i).gethDate());
                            actSuppliesImportTable.setMining(actOperation2s.get(i).getMining());
                            //获取数组x行的真实非空长度
                            List<Object> actOperation2s1 = Arrays.asList(actOperation2s.get(i).getActOperation2()[x]);
                            long count = Arrays.stream(actOperation2s.get(i).getActOperation2()[x]).filter(Objects::nonNull).count();

                            //循环保存到实体类字段中
                            for (int y = 0; y < count; y++) {
                                if (actOperation2s.get(i).getActOperation2()[x][y] != null) {
                                    if (actOperation2s.get(i).getActOperation2()[x][y].getV() != null) {
                                        try {
                                            actSuppliesImportTable.getClass().getMethod("set" + attributes[y], String.class).invoke(actSuppliesImportTable, actOperation2s.get(i).getActOperation2()[x][y].getV());
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }

                                    }
                                }

                            }
                            //新增一条生成表数据
                            actSuppliesCleaningRuleMapper.addInsertConvert2(actSuppliesImportTable);
                        } else {
                            break;
                        }

                    }
                    //修改历史表中的数据状态（是否清洗）
                    actSuppliesHistorydataMapper.updateActSuppliesHistorydataStatusById(actOperation2s.get(i).gethId());

                }

            }

        };

        thread.start();
    }

    @Override
    public void addInsertImportAccpunt(List<ActOperation1> actOperation2s) {


    }


    /**
     * 凭证单导入表数据清洗
     *
     * @return 结果
     */
    @Override
    @Transactional
    public void addInsertImportssAccounts(List<ActOperation1> actOperation2s) {

//        Thread thread1 = new Thread(()->{
//            System.out.println("sssss");
//        });


//        Thread thread = new Thread() {
//
//            public void run() {

        //String[] attributes = {"A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28", "A29", "A30", "A31", "A32", "A33", "A34", "A35", "A36", "A37", "A38", "A39", "A40", "A41", "A42", "A43", "A44", "A45", "A46", "A47", "A48", "A49", "A50", "A51", "A52", "A53", "A54", "A55", "A56", "A57", "A58", "A59", "A60", "A61", "A62", "A63"};

        String[] attributes = new String[200];

        for (int i = 1; i <= 200; i++) {

            attributes[i - 1] = 'A' + String.valueOf(i);

        }

        //获取物料明细编码与32类编码对应关系
        ActSuppliesDetails actSuppliesDetails = new ActSuppliesDetails();
        List<ActSuppliesDetails> actSuppliesDetails1 = actSuppliesDetailsMapper.selectActSuppliesDetailsMaterialList(actSuppliesDetails);

        for (int i = 0; i < actOperation2s.size(); i++) {
            Gson gson = new Gson();
            JSONArray parse = (JSONArray) JSONArray.parse(actOperation2s.get(i).getConvenRole());
            Map<String, String>[] mapArray = gson.fromJson(actOperation2s.get(i).getHistoryRole(), Map[].class);
            Map<String, String[]> mapcode = new HashMap<>();
            for (Map<String, String> map : mapArray) {
                String key = map.get("key");
                String value = map.get("value");
                String[] values = value.split(",");
                mapcode.put(key, values);
            }
            Map<String, String> resultMap = new HashMap<>();
            for (ActSuppliesDetails entity : actSuppliesDetails1) {
                resultMap.put(entity.getCode(), entity.getCodes());
            }
            List<ActSuppliesImportCleaningAcc> actSuppliesImportCleaningAccs = new ArrayList<>();
            //获取导入excel表数据存储到生成表实体类中
            for (int x = ((int) parse.getJSONObject(0).get("HeaderRow") - 1); x < actOperation2s.get(i).getActOperation2().length; x++) {
                //判断数据是否为空，为空结束循环
                String[] s1 = mapcode.get(String.valueOf(x));
                if (actOperation2s.get(i).getActOperation2()[x] != null) {
                    ActSuppliesImportTable actSuppliesImportTable = new ActSuppliesImportTable();
                    ActSuppliesImportCleaningAcc actSuppliesImportCleaningAcc = new ActSuppliesImportCleaningAcc();
                    UUID uuid = UUID.randomUUID();
                    String s = StringUtils.remove(uuid.toString(), '-');
                    if (s1 != null) {
                        actSuppliesImportTable.setKmCode(s1[0]);
                    }
                    actSuppliesImportTable.setId(s);
                    actSuppliesImportTable.sethId(actOperation2s.get(i).gethId());
                    actSuppliesImportTable.sethDate(actOperation2s.get(i).gethDate());
                    actSuppliesImportTable.setMining(actOperation2s.get(i).getMining());
                    actSuppliesImportTable.setExportUuid(actOperation2s.get(i).getExportUuid());

                    if (s1 != null) {
                        actSuppliesImportCleaningAcc.setKmCode(s1[0]);
                        actSuppliesImportCleaningAcc.setsOfExpenses(s1[1]);
                    }
                    actSuppliesImportCleaningAcc.setId(s);
                    actSuppliesImportCleaningAcc.sethId(actOperation2s.get(i).gethId());
                    actSuppliesImportCleaningAcc.setHdate(actOperation2s.get(i).gethDate());
                    actSuppliesImportCleaningAcc.setSskId(actOperation2s.get(i).getMining());
                    actSuppliesImportCleaningAcc.setYyUuid(actOperation2s.get(i).getExportUuid());

                    //获取数组x行的真实非空长度
                    List<Object> actOperation2s1 = Arrays.asList(actOperation2s.get(i).getActOperation2()[x]);
                    long count = Arrays.stream(actOperation2s.get(i).getActOperation2()[x]).filter(Objects::nonNull).count();
                    //循环保存到实体类字段中
                    for (int y = 0; y < count; y++) {
                        if (actOperation2s.get(i).getActOperation2()[x][y] != null) {
                            if (actOperation2s.get(i).getActOperation2()[x][y].getV() != null) {
                                try {
                                    actSuppliesImportTable.getClass().getMethod("set" + attributes[y], String.class).invoke(actSuppliesImportTable, actOperation2s.get(i).getActOperation2()[x][y].getV());
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                    }

                    //新增一条生成表数据(全部)
                    //actSuppliesCleaningRuleMapper.addInsertConvertAcc(actSuppliesImportTable);

                    if (actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("CodeColumn") - 1)] != null) {
                        if (actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("CodeColumn") - 1)].getV() != null) {
                            //[{"HeaderRow": 1, "CodeColumn": 6, "DateColumn": 5, "NameColumn": 7, "MoneyColumn": 12, "NumberColumn": 11, "DepartmentColumn": 2}]
                            actSuppliesImportCleaningAcc.setNameColumn(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("NameColumn") - 1)].getV());
                            actSuppliesImportCleaningAcc.setCodeColumn(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("CodeColumn") - 1)].getV());
                            actSuppliesImportCleaningAcc.setDepartmentColumn(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("DepartmentColumn") - 1)].getV());

                            if (actOperation2s.get(i).getMining().equals("011702")) {

                                int moneycolumn = (int) parse.getJSONObject(0).get("MoneyColumn") - 1;
                                int numbercolumn = (int) parse.getJSONObject(0).get("NumberColumn") - 1;

                                while (true) {

                                    if (actOperation2s.get(i).getActOperation2()[x][moneycolumn] != null && actOperation2s.get(i).getActOperation2()[x][moneycolumn].getV() != null) {

                                        actSuppliesImportCleaningAcc.setNumberColumn(actOperation2s.get(i).getActOperation2()[x][numbercolumn].getV());
                                        actSuppliesImportCleaningAcc.setMoneyColumn(actOperation2s.get(i).getActOperation2()[x][moneycolumn].getV());
                                        DecimalFormat df = new DecimalFormat("#.00");
                                        actSuppliesImportCleaningAcc.setMoneyColumns(df.format(Double.parseDouble(actOperation2s.get(i).getActOperation2()[x][moneycolumn].getV()) * Double.parseDouble(actOperation2s.get(i).getActOperation2()[x][numbercolumn].getV())));
                                        break;
                                    } else {
                                        moneycolumn = moneycolumn + 5;
                                        numbercolumn = numbercolumn + 5;
                                        if (moneycolumn > count) {
                                            break;
                                        }
                                    }

                                }

                            } else {

                                actSuppliesImportCleaningAcc.setNumberColumn(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("NumberColumn") - 1)].getV());
                                actSuppliesImportCleaningAcc.setMoneyColumn(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("MoneyColumn") - 1)].getV());
                                BigDecimal moneyColumn1 = new BigDecimal(Double.parseDouble(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("MoneyColumn") - 1)].getV()));
                                BigDecimal moneyColumn2 = new BigDecimal(Double.parseDouble(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("NumberColumn") - 1)].getV()));
                                DecimalFormat df = new DecimalFormat("#.00");
                                actSuppliesImportCleaningAcc.setMoneyColumns(df.format(Double.parseDouble(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("MoneyColumn") - 1)].getV()) * Double.parseDouble(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("NumberColumn") - 1)].getV())));

                            }

                            Object dateColumn = parse.getJSONObject(0).get("DateColumn");
                            if (parse.getJSONObject(0).get("DateColumn").hashCode() > 0) {
                                actSuppliesImportCleaningAcc.setDateColumn(actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("DateColumn") - 1)].getV());
                            }

                            String codeColumn = actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("CodeColumn") - 1)].getV();
                            if (resultMap.containsKey(codeColumn.substring(0, 6))) {
                                actSuppliesImportCleaningAcc.setCategories(resultMap.get(codeColumn.substring(0, 6)));
                            } else if (resultMap.containsKey(codeColumn.substring(0, 4))) {
                                actSuppliesImportCleaningAcc.setCategories(resultMap.get(codeColumn.substring(0, 4)));
                            } else if (resultMap.containsKey(codeColumn.substring(0, 2))) {
                                actSuppliesImportCleaningAcc.setCategories(resultMap.get(codeColumn.substring(0, 2)));
                            } else {
                                actSuppliesImportCleaningAcc.setCategories("10");
                            }
                            //actSuppliesImportCleaning.setDateColumn(actOperation2s.get(i).getActOperation2()[x][JSON.parseObject(actOperation2s.get(i).getHistoryRole()).getInteger("DateColumn")].getV());
                            //新增一条生成表数据（筛选）
//                                    actSuppliesCleaningRuleMapper.addActSuppliesImportCleaningAcc(actSuppliesImportCleaningAcc);
                            actSuppliesImportCleaningAccs.add(actSuppliesImportCleaningAcc);
                        }
                    }

                } else {
                    break;
                }
                if (actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("CodeColumn") - 1)] == null) {
                    actSuppliesCleaningRuleMapper.addActSuppliesImportCleaningAcc(actSuppliesImportCleaningAccs);
                    actSuppliesImportCleaningAccs.clear();
                    break;
                } else {
                    String codeColumn = actOperation2s.get(i).getActOperation2()[x][((int) parse.getJSONObject(0).get("CodeColumn") - 1)].getV();
                }

                if (x % 500 == 0) {
                    actSuppliesCleaningRuleMapper.addActSuppliesImportCleaningAcc(actSuppliesImportCleaningAccs);
                    actSuppliesImportCleaningAccs.clear();
                } else if (x == actOperation2s.get(i).getActOperation2().length - 1) {
                    actSuppliesCleaningRuleMapper.addActSuppliesImportCleaningAcc(actSuppliesImportCleaningAccs);
                    actSuppliesImportCleaningAccs.clear();
                }

            }

            //修改历史表中的数据状态（是否清洗）
            actSuppliesHistorydataMapper.updateActSuppliesHistorydataStatusByIdAcc(actOperation2s.get(i).gethId());

        }

//            }
//
//        };
//
//        thread.start();


    }

    @Override
    @Transactional
    public void addInsertActSuppliesHistorydataAcc(List<ActSuppliesHistorydata> actSuppliesHistorydata) {
        //        //最大值
//        int maxnum = 0;
//        //判断所属矿是否为唐山矿
//        if ("010101".equalsIgnoreCase(mining)) {
//            maxnum = 1;
//        }
//        //判断所属矿是否为林西矿
//        if ("011704".equalsIgnoreCase(mining)) {
//            maxnum = 1;
//        }
//        //判断所属矿是否为东欢坨矿
//        if ("010102".equalsIgnoreCase(mining)) {
//            maxnum = 1;
//        }
//        //判断所属矿是否为单矿
//        if ("011204".equalsIgnoreCase(mining)) {
//            maxnum = 1;
//        }
//        //判断所属矿是否为吕矿
//        if ("011702".equalsIgnoreCase(mining)) {
//            maxnum = 1;
//        }
//        //判断所属矿是否为范各庄矿
//        if ("011701".equalsIgnoreCase(mining)) {
//            maxnum = 1;
//        }
//        //判断所属矿是否为红矿
//        if ("011502".equalsIgnoreCase(mining)) {
//            maxnum = 1;
//        }
//        //判断所属矿是否为钱家营矿
//        if ("010105".equalsIgnoreCase(mining)) {
//            maxnum = 1;
//        }
        //新增前删除关联三个表
//        ActSuppliesHistorydata ashd = actSuppliesHistorydata.get(0);
//        //获取所属矿ID
//        String mining = ashd.getMining();
//        ActSuppliesHistorydata actSuppliesHistorydata2 = new ActSuppliesHistorydata();
//        actSuppliesHistorydata2.setMining(mining);
//        actSuppliesHistorydata2.setDate(ashd.getDate());
//        List<ActSuppliesHistorydata> list = actSuppliesHistorydataMapper.selectActSuppliesHistorydataCopyList(actSuppliesHistorydata2);
//        for (ActSuppliesHistorydata suppliesHistorydata : list) {
//            //删除历史表
//            actSuppliesHistorydataMapper.deleteActSuppliesHistorydataById(suppliesHistorydata.getId());
//            //删除
//            actSuppliesHistorydataMapper.deleteActSuppliesHistorycontentById(suppliesHistorydata.getImportUuid());
//            //删除清洗表
//            actSuppliesCleaningRuleMapper.deleteActSuppliesImportTableCopy(suppliesHistorydata.getImportUuid());
//        }

        //新增
        SysUser user = SecurityUtils.getLoginUser().getUser();
        ActSuppliesHistorydata actSuppliesHistorydata1 = new ActSuppliesHistorydata();
        for (int i = 0; i < actSuppliesHistorydata.size(); i++) {
            UUID uuid = UUID.randomUUID();
            String uuids = StringUtils.remove(uuid.toString(), '-');
            if (actSuppliesHistorydata.get(i).getIdentifyingCode() == 0) {
                actSuppliesHistorydata1 = actSuppliesHistorydata.get(i);
                actSuppliesHistorydata1.setImportUuid(uuids);
                actSuppliesHistorydata1.setStatus(0L);
                actSuppliesHistorydata1.setIdentifyingCode(0L);
                actSuppliesHistorydata1.setCreateBy(user.getUserName());
                actSuppliesHistorydata1.setCreateTime(DateUtils.getNowDate());
                actSuppliesHistorydataMapper.insertActSuppliesHistorydataAcc(actSuppliesHistorydata1);
                actSuppliesHistorydataMapper.insertActSuppliesHistorycontentAcc(actSuppliesHistorydata1);
            } else {
                actSuppliesHistorydata1 = actSuppliesHistorydata.get(i);
                actSuppliesHistorydata1.setImportUuid(uuids);
                actSuppliesHistorydata1.setStatus(0L);
                actSuppliesHistorydata1.setIdentifyingCode(1L);
                actSuppliesHistorydata1.setCreateBy(user.getUserName());
                actSuppliesHistorydata1.setCreateTime(DateUtils.getNowDate());
                actSuppliesHistorydataMapper.insertActSuppliesHistorydataAcc(actSuppliesHistorydata1);
                actSuppliesHistorydataMapper.insertActSuppliesHistorycontentAcc(actSuppliesHistorydata1);
            }

        }

    }

    @Override
    public List<ActSuppliesHistorydata> selectActSuppliesHistorydataAcc(ActSuppliesHistorydata actSuppliesHistorydata) {

        actSuppliesHistorydata.setIdentifyingCode(0L);
        return actSuppliesHistorydataMapper.selectActSuppliesHistorydataAcc(actSuppliesHistorydata);

    }

    @Override
    public List<ActSuppliesImportCleaningAcc> selectActSuppliesCleaningCopyAcc(Long id) {

        return actSuppliesCleaningRuleMapper.selectActSuppliesCleaningCopy(id);

    }


}
