package cn.com.victorysoft.business.calc.service;

import cn.com.victorysoft.business.calc.dao.WellTubeDataSyncDao;
import cn.com.victorysoft.business.calc.dao.WellTubeDataSyncDao;
import cn.com.victorysoft.business.calc.entity.Fla55;
import cn.com.victorysoft.business.sys.param.UnitWellMonthPageParam;
import cn.com.victorysoft.business.technology.dao.WellTubeDao;
import cn.com.victorysoft.business.technology.dao.WellTubeDao;
import cn.com.victorysoft.business.technology.entity.WellTool;
import cn.com.victorysoft.business.technology.entity.WellTube;
import cn.com.victorysoft.business.util.BeanUtils;
import cn.com.victorysoft.business.util.DateUtils;
import cn.com.victorysoft.business.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 井下工具数据同步服务
 */
@Slf4j
@Service
public class WellTubeDataSyncService {

    @Resource
    private WellTubeDataSyncDao wellTubeDataSyncDao;
    @Resource
    private WellTubeDao wellTubeDao;

    /**
     * 同步数据
     */
    @Transactional
    public void sync(Date startDate, Date endDate) {
        String month = DateUtils.format(endDate, "yyyyMM");
        String prevMonth = DateUtils.format(DateUtils.addMonths(endDate, -1), "yyyyMM");
        // 查询源头
        List<WellTube> list = wellTubeDataSyncDao.selectList(startDate, endDate);
        // 查询工具附录
        List<Fla55> fla55List = wellTubeDataSyncDao.selectFla55();
        Map<String, Fla55> fla55Map = fla55List.stream().collect(Collectors.toMap(Fla55::getDm, v -> v));
        // 查询上月
        UnitWellMonthPageParam param = new UnitWellMonthPageParam();
        param.setMonth(prevMonth);
        List<WellTube> prevMonthList = wellTubeDao.selectList(param);
        Map<String, WellTube> prevMonthMap = prevMonthList.stream().collect(Collectors.toMap(WellTube::getJh, v -> v));
        // 合并源头数据和上月数据
        for (WellTube wellTube : list) {
            // 工具计算
            String fgqlx = wellTube.getFgqlx();
            if (StringUtils.isNotEmpty(fgqlx)) {
                String[] fgqlxArray = fgqlx.split(",");
                List<String> fgqlxList = Arrays.asList(fgqlxArray);
                Map<String, List<Fla55>> fgqlxMap = fgqlxList.stream()
                        .map(fla55Map::get)
                        .collect(Collectors.groupingBy(Fla55::getDmmc));
                List<String> fgqlxmcList = new ArrayList<>();
                List<String> fgqlxslList = new ArrayList<>();
                fgqlxMap.forEach((key, value) -> {
                    fgqlxmcList.add(key);
                    fgqlxslList.add(String.valueOf(value.size()));
                });
                wellTube.setFgqlx(String.join(",", fgqlxmcList));
                wellTube.setFgqjs(String.join(",", fgqlxslList));
            }
            WellTube prevWellTube = prevMonthMap.get(wellTube.getJh());
            if (prevWellTube != null) {
                BeanUtils.mergeBean(wellTube, prevWellTube);
            }
        }
        if (list.size() > 0) {
            // 先删除，后插入
            wellTubeDao.deleteByMonth(month);
            // 每批commit的个数
            int batchCount = 500;
            int index = 0;
            do {
                int batchLastIndex = Math.min(index + batchCount, list.size());
                List<WellTube> tmp = list.subList(index, batchLastIndex);
                wellTubeDao.insertBatch(tmp);
                index = index + batchCount;
            } while (index < list.size());
        }
    }

}
