package com.njcn.prepare.harmonic.service.mysql.Impl.line;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.event.pojo.po.RMpEventDetailD;
import com.njcn.event.pojo.po.RMpEventDetailM;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpEventDetailDMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.line.RMpEventDetailDService;
import com.njcn.prepare.harmonic.service.mysql.line.RMpEventDetailMService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/12/28  14:58【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@AllArgsConstructor
public class RMpEventDetailDServiceImpl extends MppServiceImpl<RMpEventDetailDMapper, RMpEventDetailD> implements RMpEventDetailDService {


    private final RMpEventDetailDMapper rMpEventDetailDMapper;

    private final RMpEventDetailMService rMpEventDetailMService;

    /**
     * @param lineId
     * @param dataDate
     * @Description: queryOneMothData
     * @Param: [lineId, dataDate]
     * @return: java.util.List<com.njcn.prepare.harmonic.pojo.mysql.po.line.RMpEventDetailDPO>
     * @Author: clam
     * @Date: 2022/12/28
     */
    @Override
    public List<RMpEventDetailD> queryOneMothData(String lineId, String dataDate) {

        QueryWrapper<RMpEventDetailD> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("measurement_point_id", lineId).
                eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7));
        List<RMpEventDetailD> list = rMpEventDetailDMapper.selectList(queryWrapper);
        return list;
    }

    /***
     *监测点暂态指标明细--月表算法
     */
    @Override
    public void handleMonth(CalculatedParam calculatedParam) {
        List<String> lineIds = calculatedParam.getIdList();
        //以尺寸1000分片
        List<List<String>> pendingIds = ListUtils.partition(lineIds, 1000);
        DateTime beginMonth = DateUtil.beginOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        DateTime endMonth = DateUtil.endOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        for (List<String> pendingId : pendingIds) {
            List<RMpEventDetailM> rMpEventDetailMList = this.baseMapper.handleMonth(pendingId, DateUtil.format(beginMonth, DatePattern.NORM_DATE_PATTERN), beginMonth, endMonth);
            rMpEventDetailMService.saveOrUpdateBatchByMultiId(rMpEventDetailMList);
        }
    }

}
