package com.pqs9900.serviceImpl.HisData;

import com.pqs9900.mapper.HisData.DataHarmrateVMapper;
import com.pqs9900.pojo.HisData.DataHarmpowerS;
import com.pqs9900.pojo.HisData.DataHarmrateV;
import com.pqs9900.service.HisData.DataHarmrateVService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/** @Author denghuajun
 * @Description //TODO 谐波电压含有率
 * @Date  2018/12/17 10:54
 **/
@Service
public class DataHarmrateVServiceImpl implements DataHarmrateVService {

    private static final Logger logger= LoggerFactory.getLogger(DataHarmrateVServiceImpl.class);
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    private DataHarmrateVMapper dataHarmrateVMapper;
    @Override
    public int saveDataHarmrateV(DataHarmrateV dataHarmrateV) {
        return dataHarmrateVMapper.insert(dataHarmrateV);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDataHarmrateV(List<DataHarmrateV> list) {
        return dataHarmrateVMapper.insertDataHarmrateV(list);
    }


    /**
     * 采用线程池进行数据入库
     * @param listDataHarmrateV 谐波电压含有率数据
     */
    @Override
    public Future insertDataHarmrateVExecutor(List<DataHarmrateV> listDataHarmrateV) {
        int inum = 100 ;//每次解析入库条目数
        Callable callable = new Callable() {
            @Override
            public Object call() throws Exception {
                int nCyclicTimes;//循环次数（总长度/批量入库条目数）
                int fromIndex,toIndex;//获取数据的起始和结束下标
                try {
                    nCyclicTimes = (int) Math.ceil(listDataHarmrateV.size() / (double) inum);
                    List<DataHarmrateV> tmpListDataHarmrateV = new ArrayList<>();
                    for (int i = 0; i < nCyclicTimes; i++) {
                        tmpListDataHarmrateV.clear();
                        //假如是最后一次循环则获取循环到结束
                        fromIndex = i * inum;
                        if (i == nCyclicTimes - 1) {
                            toIndex = listDataHarmrateV.size();
                        } else {
                            toIndex = (i + 1) * inum;
                        }
                        //可以先按下标截取，再入库
                        tmpListDataHarmrateV.addAll(listDataHarmrateV.subList(fromIndex, toIndex));
                        // TODO 做批量入库的操作
                        dataHarmrateVMapper.insertDataHarmrateV(tmpListDataHarmrateV);
                    }
                    return "SUCCESS";
                } catch (Exception e) {
                    logger.error("谐波电压含有率导入异常，异常是" + e.toString());
                    return "FAIL";
                }
            }
        };
        return taskExecutor.submit(callable);
    }
}
