package com.yeejoin.amos.boot.module.hygf.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.boot.module.hygf.api.dto.MonthPowerDto;
import com.yeejoin.amos.boot.module.hygf.api.entity.AllPower;
import com.yeejoin.amos.boot.module.hygf.api.entity.MonthPower;
import com.yeejoin.amos.boot.module.hygf.api.entity.YearPower;
import com.yeejoin.amos.boot.module.hygf.api.service.IMonthPowerService;
import com.yeejoin.amos.boot.module.hygf.api.tdenginemapper.MonthPowerMapper;
import com.yeejoin.amos.boot.module.hygf.api.tdenginemapper.YearPowerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.typroject.tyboot.core.rdbms.service.BaseService;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 按月统计服务实现类
 *
 * @author system_generator
 * @date 2023-09-20
 */
@Service
public class MonthPowerServiceImpl extends BaseService<MonthPowerDto,MonthPower,MonthPowerMapper> implements IMonthPowerService {

    @Autowired
    MonthPowerMapper monthPowerMapper;
    @Autowired
    YearPowerServiceImpl yearPowerServiceImpl;
    @Autowired
    AllPowerServiceImpl allPowerServiceImpl;
    @Autowired
    YearPowerMapper YearPowerMapper;

    /**
     * 分页查询
     */
    public Page<MonthPowerDto> queryForMonthPowerPage(Page<MonthPowerDto> page)   {
        return this.queryForPage(page, null, false);
    }

    /**
     * 列表查询 示例
     */
    public List<MonthPowerDto> queryForMonthPowerList()   {
        return this.queryForList("" , false);
    }


    @Override
    @Transactional
    public void getMonthPower() {
        List<MonthPower> list= monthPowerMapper.getMonthPowerint();
//        LocalDateTime now = LocalDateTime.now();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date dayTime = cal.getTime();

//        DateTimeFormatter formatterday = DateTimeFormatter.ofPattern("YYYY-MM-dd");
//        String formattedDateday = now.format(formatterday);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
        String formattedDateday =simpleDateFormat.format(dayTime);

        LambdaQueryWrapper<MonthPower> wrapperday = new LambdaQueryWrapper<>();
        wrapperday.eq(MonthPower::getDay, formattedDateday);
        List<MonthPower> listdday=  this.list(wrapperday);
        if(listdday!=null&&!listdday.isEmpty()){
            for (MonthPower monthPower : list) {
                for (MonthPower power : listdday) {
                    if(monthPower.getThirdStationId().equals(power.getThirdStationId())
                            &&monthPower.getYearMonth().equals(power.getYearMonth())
                            &&monthPower.getDay().equals(power.getDay())
                    ){
                        monthPower.setCreatedTime(power.getCreatedTime());
                    }else{

                    }
                }
            }
            this.saveBatch(list);
        }else{
            this.saveBatch(list);
        }

//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM");
//        String formattedDate = now.format(formatter);

        SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM");
        String formattedDate =formatter.format(dayTime);

        List<YearPower> listd=   monthPowerMapper.getSumMonthPower(formattedDate);


        LambdaQueryWrapper<YearPower> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YearPower::getMonth, formattedDate);
        List<YearPower> listdY=  yearPowerServiceImpl.list(wrapper);
         if(listdY!=null&&!listdY.isEmpty()){
             for (YearPower yearPower : listd) {
                 for (YearPower power : listdY) {
                     if(yearPower.getThirdStationId()!=null&&yearPower.getThirdStationId().equals(power.getThirdStationId())
                             &&yearPower.getYear().equals(power.getYear())
                             &&yearPower.getMonth().equals(power.getMonth())
                     ){
                         //yearPower.setPower(power.getPower());
                         yearPower.setCreatedTime(power.getCreatedTime());
                     }
                 }
             }
             yearPowerServiceImpl.saveBatch(listd);
         }else{
             yearPowerServiceImpl.saveBatch(listd);
         }

//        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("YYYY");
//        String formattedDate1 = now.format(formatter1);

        SimpleDateFormat formatter1 = new SimpleDateFormat("YYYY");
        String formattedDate1 =formatter1.format(dayTime);

        List<AllPower> listdy=   monthPowerMapper.getSumYearPower(formattedDate1);

        LambdaQueryWrapper<AllPower> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(AllPower::getYear, formattedDate1);
        List<AllPower> listdall=  allPowerServiceImpl.list(wrapper1);
        if(listdall!=null&&!listdall.isEmpty()){
            for (AllPower yearPower : listdy) {
                for (AllPower power : listdall) {
                    if(yearPower.getThirdStationId()!=null&&yearPower.getThirdStationId().equals(power.getThirdStationId())
                            &&yearPower.getYear().equals(power.getYear())
                    ){
                       // yearPower.setPower(power.getPower());
                        yearPower.setCreatedTime(power.getCreatedTime());
                    }
                }
            }
            allPowerServiceImpl.saveBatch(listdy);
        }else{
            allPowerServiceImpl.saveBatch(listdy);
        }

    }




}