package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.mapper.ActReadDataMapper;
import com.ruoyi.system.service.ActReadDataService;
import com.ruoyi.system.service.impl.Conversion.MineConver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;
import java.util.regex.Pattern;

/**
 * 2024/3/21
 */
@Service
public class ActReadDataServiceImpl implements ActReadDataService {
    @Autowired
    private  MineConver mineConver;

    private static ActReadDataMapper actReadDataMapper;

    public ActReadDataServiceImpl(ActReadDataMapper actReadDataMapper) {
        this.actReadDataMapper = actReadDataMapper;
    }

    private static final String LK = "011702";

    public static final Pattern PATTERN = Pattern.compile("\\d+");

    @Override
    public AjaxResult read(String mineId) {
//        mineId = mineId.substring(1, mineId.length() - 1);
        //获取导入表未清洗数据条数
        int count = actReadDataMapper.selectCleanToOne(mineId);
        if (count <= 0) {
            return new AjaxResult(500, "数据库没有：" + mineId + "数据，无法清洗");
        } else {
            //开启多线程，每次开启5个线程，执行完后

            //lambda表达式，开启线程
            for (int i = 0; i <count; i++) {

                Future<String> future = mineConver.readToId(mineId ,i);
                try {
                    // 尝试获取结果，如果异步方法未完成，这里会抛出异常
                    future.get();
                    System.out.println("异步方法开启线程成功！" + i);
                } catch (Exception e) {
                    System.out.println("异步方法开启线程失败！！！！！！！" + i);
                }

            }
        }
        return new AjaxResult(200, "清洗成功");
    }

    @Override
    public AjaxResult readOfMId(String mineId, String format) {
        int count = actReadDataMapper.readOfMId(mineId, format);
        return new AjaxResult(200, count + "");
    }

    @Override
    public AjaxResult delectOfMId(String mineId, String dateNY) {
        String s = actReadDataMapper.selectOfMIdToHis(mineId, dateNY);
        actReadDataMapper.deleteOfMIdToHis(mineId, dateNY);
        actReadDataMapper.deleteOfMIdToClear(mineId, dateNY);
        actReadDataMapper.deleteToContent(s);

        return new AjaxResult(200, "删除成功");
    }
/*
    @Override
    public AjaxResult read(String mineId) {

        int i = 0;
        while (true) {
            int count = actReadDataMapper.selectCleanToOne(mineId);
            if (count != 0) {
                readToId(mineId, count);
                i++;
            } else if (i != 0) {
                break;
            } else {
                return new AjaxResult(500, "数据库没有：" + mineId + "数据，无法清洗");
            }
        }
        return new AjaxResult(200, "清洗成功");
    }
*/

    /*@Async("defaultThreadPoolExecutor")
    void readToId(String mineId, int c) {
        synchronized ("0") {
            long start = System.currentTimeMillis();
            List<ActReadHistoryModel> list;
            if (c >= 5) {
                list = actReadDataMapper.readDateToHistory(mineId, 5);
            } else {
                list = actReadDataMapper.readDateToHistory(mineId, c);
            }
            System.err.println("查询" + c + "条时间为：" + (System.currentTimeMillis() - start));
            Map<String, String> materialMap = new HashMap<>();
            List<ActHistoryRoleTypeCodeModel> listMaterial = actReadDataMapper.selectMaterialCode();
            for (ActHistoryRoleTypeCodeModel model : listMaterial) {
                materialMap.putIfAbsent(model.getKey(), model.getValue());
            }
            MineConver mineConver;
            if (mineId.equals(LK)) {
                mineConver = new MineConver(new LkSpecialMethod(actReadDataMapper));
            } else {
                mineConver = new MineConver(new OtherMethod(actReadDataMapper));
            }
            for (ActReadHistoryModel actReadHistoryModel : list) {
                mineConver.clean(actReadHistoryModel, materialMap);
            }
            long end = System.currentTimeMillis();
            System.err.println(end - start);
            System.err.println("本次清洗了：" + list.size() + "条数据");
        }
    }*/
}
