package com.zcxk.xxljob.jobs;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zcxk.core.common.util.BeanCopyUtils;
import com.zcxk.core.utils.DateUtil;
import com.zcxk.rmcp.api.vo.MeterReadRecordAggregationVo;
import com.zcxk.rmcp.core.dao.MeterReadWaterRecordMapper;
import com.zcxk.rmcp.core.dao.mongo.MeterReadRecordDao;
import com.zcxk.rmcp.core.entity.MeterReadWaterRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Andy
 * @version V1.0
 * @description: 汇总小区每天相同类型表的用水量
 * @date 2021/7/28
 **/
@Component
@Slf4j
public class MeterReadWaterRecordHandler {

    /**
     * 最多一次插入条数
     */
    private static final int BATCH_SIZE = 1000;

    /**
     * 查询条数
     */
    private static final int LIMIT = 10000;

    @Resource
    private MeterReadRecordDao meterReadRecordDao;

    @Resource
    private MeterReadWaterRecordMapper meterReadWaterRecordMapper;

    @XxlJob("meterReadWaterRecordHandler")
    public ReturnT<String> execute(String string) {
        int page = 1;
        int total = 0;
        LocalDate date = DateUtil.parseLocalDate(new Date());
        date = date.plusDays(-1);
        int statDay = Integer.parseInt(date.toString().replaceAll("-",""));
        log.info("==================【汇总小区每天相同类型表的用水量】begin==================日期：{}", statDay);
        while (true) {
            List<MeterReadRecordAggregationVo> list =  meterReadRecordDao.aggregationCommunityWaterConsumption(date.getYear(), statDay, page, LIMIT);
            if (CollectionUtils.isEmpty(list)){
                break;
            }
            List<MeterReadWaterRecord> waterRecord = new ArrayList<>();
            for (MeterReadRecordAggregationVo meterReadRecordVo : list) {
                MeterReadWaterRecord meterReadWaterRecord = new MeterReadWaterRecord();
                BeanCopyUtils.copyProperties(meterReadRecordVo, meterReadWaterRecord, MeterReadWaterRecord.class);
                meterReadWaterRecord.setCreateDate(LocalDateTime.now());
                meterReadWaterRecord.setStatDay(meterReadRecordVo.getReadDate());
                meterReadWaterRecord.setUpdateDate(LocalDateTime.now());
                waterRecord.add(meterReadWaterRecord);
                total++;
                if (waterRecord.size() >=  BATCH_SIZE) {
                    batchInsert(waterRecord);
                    waterRecord.clear();
                }
            }
            if (waterRecord.size() > 0 ){
                batchInsert(waterRecord);
            }
            page++;
        }
        log.info("==================【汇总小区每天相同类型表的用水量】end==================日期：{},总数量：{}", statDay, total);
        return ReturnT.SUCCESS;
    }

    /**
    * @author Andy
    * @description 批量插入
    * @date 15:17 2021/7/28
    * @param list
    * @return void
    **/
    private void batchInsert(List<MeterReadWaterRecord> list){
        try {
            meterReadWaterRecordMapper.batchInsert(list);
        } catch (Exception e) {
            log.error("【汇总小区每天相同类型表的用水量】报错error", e);
        }
    }



}
