package com.njcn.harmonic.service.upload.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.harmonic.mapper.upload.RUploadCommPointBusMapper;
import com.njcn.harmonic.pojo.param.UploadDataParam;
import com.njcn.harmonic.pojo.po.upload.RUploadCommPointBus;
import com.njcn.harmonic.service.upload.RUploadCommPointBusService;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.web.enums.GWSendEnum;
import com.njcn.web.factory.PageFactory;
import com.njcn.web.pojo.param.SendParam;
import com.njcn.web.utils.GwSendUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 公共连接点母线电能质量统计 服务实现类
 * </p>
 *
 * @author cdf
 * @since 2024-08-15
 */
@Service
public class RUploadCommPointBusServiceImpl extends ServiceImpl<RUploadCommPointBusMapper, RUploadCommPointBus> implements RUploadCommPointBusService {

    @Override
    public Page<RUploadCommPointBus> getCommPointBusPage(UploadDataParam param) {
        DateTime dateTime = DateUtil.parse(param.getSearchBeginTime(), DatePattern.NORM_DATE_PATTERN);
        if(param.getDataType().equals("02")){
            param.setSearchBeginTime(DateUtil.format(dateTime,DatePattern.NORM_MONTH_PATTERN));
        }else if(param.getDataType().equals("01")){
            param.setSearchBeginTime(DateUtil.format(dateTime,DatePattern.NORM_YEAR_PATTERN));
        }
        LambdaQueryWrapper<RUploadCommPointBus> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RUploadCommPointBus::getStatisticalType,param.getDataType())
                .eq(RUploadCommPointBus::getStatisticalDate,param.getSearchBeginTime());
        return this.page(new Page<>(PageFactory.getPageNum(param),PageFactory.getPageSize(param)),lambdaQueryWrapper);
    }

    @Override
    public Boolean upGw(UploadDataParam param) {
        if(StrUtil.isBlank(param.getSearchBeginTime())){
            throw new BusinessException("日期不可为空");
        }
        LambdaQueryWrapper<RUploadCommPointBus> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        String temDate = param.getSearchBeginTime();
        if(StrUtil.isBlank(param.getDataType())){
            //类型为空则上送日月年数据
            DateTime dateTime = DateUtil.parse(param.getSearchBeginTime(),DatePattern.NORM_DATE_PATTERN);
            String month = DateUtil.format(dateTime,DatePattern.NORM_MONTH_PATTERN);
            String year = DateUtil.format(dateTime,DatePattern.NORM_YEAR_PATTERN);
            lambdaQueryWrapper.in(RUploadCommPointBus::getStatisticalDate, Stream.of(temDate,month,year).collect(Collectors.toList()));
        }else {
            if(param.getDataType().equals(DicDataEnum.STATISTICAL_TYPE_M.getCode())){
                temDate = DateUtil.format(DateUtil.parse(param.getSearchBeginTime(),DatePattern.NORM_DATE_PATTERN),DatePattern.NORM_MONTH_PATTERN);
            }else if(param.getDataType().equals(DicDataEnum.STATISTICAL_TYPE_Y.getCode())){
                temDate = DateUtil.format(DateUtil.parse(param.getSearchBeginTime(),DatePattern.NORM_DATE_PATTERN),DatePattern.NORM_YEAR_PATTERN);
            }
            lambdaQueryWrapper.eq(RUploadCommPointBus::getStatisticalType,param.getDataType())
                    .eq(RUploadCommPointBus::getStatisticalDate,temDate);
        }
        List<RUploadCommPointBus> rUploadCommPointBusList = this.list(lambdaQueryWrapper);
        if(CollUtil.isEmpty(rUploadCommPointBusList)){
            throw new BusinessException("查询数据为空");
        }
        List<List<RUploadCommPointBus>> list = CollUtil.split(rUploadCommPointBusList,100);

        for(int i=0;i<list.size();i++){
            SendParam sendParam = new SendParam();

            if(i==0){
                sendParam.setIsAppend("0");
            }else if(i==list.size()-1){
                sendParam.setIsAppend("2");
            }else {
                sendParam.setIsAppend("1");
            }

            if(StrUtil.isNotBlank(param.getDataType())){
                sendParam.setStatisticalType(param.getDataType());

            }
            sendParam.setStatisticalDate(temDate);
            sendParam.setStats(list.get(i));
            Map<String, String> sendRes = GwSendUtil.newSend(sendParam, GWSendEnum.COMM_POINT);
            List<String> ids = list.get(i).stream().map(RUploadCommPointBus::getObjId).collect(Collectors.toList());
            int count = GwSendUtil.returnInfoMsg(ids,sendRes);
            System.out.println("上送成功,上送成功返回"+count+"条,上传数据"+list.get(i).size()+"条");

                LambdaUpdateWrapper<RUploadCommPointBus> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lambdaUpdateWrapper.set(RUploadCommPointBus::getUploadStatus,1).in(RUploadCommPointBus::getObjId,ids);
                this.update(lambdaUpdateWrapper);

        }
        return true;
    }




}
