package com.gyee.runeconomy.service.auto.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.common.model.StringUtils;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.mapper.auto.ProEconSystemEfficiencyEquipmentMapper;
import com.gyee.runeconomy.model.auto.ProBasicEquipment;
import com.gyee.runeconomy.model.auto.ProEconSystemEfficiencyEquipment;
import com.gyee.runeconomy.service.auto.IProEconSystemEfficiencyEquipmentService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 光伏系统效率 服务实现类
 * </p>
 *
 * @author wang
 * @since 2023-10-24
 */
@Service
public class ProEconSystemEfficiencyEquipmentServiceImpl extends ServiceImpl<ProEconSystemEfficiencyEquipmentMapper, ProEconSystemEfficiencyEquipment> implements IProEconSystemEfficiencyEquipmentService {

    @Override
    public boolean batchAdd(List<ProEconSystemEfficiencyEquipment> proEconStationPowers) {
        try {
            proEconStationPowers.stream().forEach(i -> {
                QueryWrapper<ProEconSystemEfficiencyEquipment> qw = new QueryWrapper<>();
                qw.lambda().eq(ProEconSystemEfficiencyEquipment::getDataDate, i.getDataDate());
                qw.lambda().eq(ProEconSystemEfficiencyEquipment::getForeignKeyId, i.getForeignKeyId());
                Long count = baseMapper.selectCount(qw);
                if (count > 0) {
                    baseMapper.update(i, qw);
                } else if (count <= 0) {
                    baseMapper.insert(i);
                }

            });
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    @Override
    public List<ProEconSystemEfficiencyEquipment> getbq(String foreignKeyId, Date date) {
        List<ProEconSystemEfficiencyEquipment> currlist = new ArrayList<>();
        if (date != null) {
            List<ProBasicEquipment> collect = CacheContext.wtls.stream()
                    .filter(wt -> foreignKeyId.equals(wt.getWindpowerstationId()))
                    .collect(Collectors.toList());

            collect.stream().forEach(i -> {
                QueryWrapper<ProEconSystemEfficiencyEquipment> qw = new QueryWrapper<>();
                if (StringUtils.isNotEmpty(i.getId())) {
                    qw.lambda().eq(ProEconSystemEfficiencyEquipment::getEquipmentId, i.getId());
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                String dateString = sdf.format(date);
                qw.lambda().apply("to_char(data_date, 'YYYY-MM') = '" + dateString + "'");
                qw.lambda().orderByDesc(ProEconSystemEfficiencyEquipment::getDataDate).last("LIMIT 1");
                List<ProEconSystemEfficiencyEquipment> selectedList = baseMapper.selectList(qw);
                currlist.addAll(selectedList);
            });
        }
        return currlist;
    }

    @Override
    public List<ProEconSystemEfficiencyEquipment> gethb(String foreignKeyId, Date date) {

        List<ProEconSystemEfficiencyEquipment> currlist = new ArrayList<>();
        if (date != null) {
            List<ProBasicEquipment> collect = CacheContext.wtls.stream()
                    .filter(wt -> foreignKeyId.equals(wt.getWindpowerstationId()))
                    .collect(Collectors.toList());

            // 将日期减去一个月
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MONTH, -1);
            Date previousMonth = cal.getTime();

            collect.stream().forEach(i -> {
                QueryWrapper<ProEconSystemEfficiencyEquipment> qw = new QueryWrapper<>();
                if (StringUtils.isNotEmpty(i.getId())) {
                    qw.lambda().eq(ProEconSystemEfficiencyEquipment::getEquipmentId, i.getId());
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                String dateString = sdf.format(previousMonth);
                qw.lambda().apply("to_char(data_date, 'YYYY-MM') = '" + dateString + "'");
                qw.lambda().orderByDesc(ProEconSystemEfficiencyEquipment::getDataDate).last("LIMIT 1");
                List<ProEconSystemEfficiencyEquipment> selectedList = baseMapper.selectList(qw);
                currlist.addAll(selectedList);
            });
        }
        return currlist;
    }


    @Override
    public List<ProEconSystemEfficiencyEquipment> gettb(String foreignKeyId, Date date) {

        List<ProEconSystemEfficiencyEquipment> currlist = new ArrayList<>();
        if (date != null) {
            List<ProBasicEquipment> collect = CacheContext.wtls.stream()
                    .filter(wt -> foreignKeyId.equals(wt.getWindpowerstationId()))
                    .collect(Collectors.toList());

            // 将日期减去一个月
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.YEAR, -1);
            Date oneYearAgo = cal.getTime();

            collect.stream().forEach(i -> {
                QueryWrapper<ProEconSystemEfficiencyEquipment> qw = new QueryWrapper<>();
                if (StringUtils.isNotEmpty(i.getId())) {
                    qw.lambda().eq(ProEconSystemEfficiencyEquipment::getEquipmentId, i.getId());
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                String dateString = sdf.format(oneYearAgo);
                qw.lambda().apply("to_char(data_date, 'YYYY-MM') = '" + dateString + "'");
                qw.lambda().orderByDesc(ProEconSystemEfficiencyEquipment::getDataDate).last("LIMIT 1");
                List<ProEconSystemEfficiencyEquipment> selectedList = baseMapper.selectList(qw);
                currlist.addAll(selectedList);
            });
        }
        return currlist;
    }

    //    @Override
    //    public List<ProEconSystemEfficiencyEquipment> getbq(String foreignKeyId, Date date) {
    //        List<ProEconSystemEfficiencyEquipment> currlist = new ArrayList<>();
    //        if (date != null) {
    //            List<ProBasicEquipment> collect = CacheContext.wtls.stream()
    //                    .filter(wt -> foreignKeyId.equals(wt.getWindpowerstationId()))
    //                    .collect(Collectors.toList());
    //            for (ProBasicEquipment wt : collect) {
    //                QueryWrapper<ProEconSystemEfficiencyEquipment> qw = new QueryWrapper<>();
    //                if (StringUtils.isNotEmpty(wt.getId())) {
    //                    qw.lambda().eq(ProEconSystemEfficiencyEquipment::getEquipmentId, wt.getId());
    //                }
    //                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    //                String dateString = sdf.format(date);
    //                qw.lambda().apply("to_char(data_date, 'YYYY-MM') = '" + dateString + "'");
    //                qw.lambda().orderByDesc(ProEconSystemEfficiencyEquipment::getDataDate).last("LIMIT 1");
    //                List<ProEconSystemEfficiencyEquipment> selectedList = baseMapper.selectList(qw);
    //                currlist.addAll(selectedList);
    //            }
    //        }
    //        return currlist;
    //    }

    //    @Override
    //    public List<ProEconSystemEfficiencyEquipment> gethb(String foreignKeyId, Date date) {
    //
    //        List<ProEconSystemEfficiencyEquipment> currlist = new ArrayList<>();
    //        if (date != null) {
    //            List<ProBasicEquipment> collect = CacheContext.wtls.stream()
    //                    .filter(wt -> foreignKeyId.equals(wt.getWindpowerstationId()))
    //                    .collect(Collectors.toList());
    //
    //            // 将日期减去一个月
    //            Calendar cal = Calendar.getInstance();
    //            cal.setTime(date);
    //            cal.add(Calendar.MONTH, -1);
    //            Date previousMonth = cal.getTime();
    //
    //            for (ProBasicEquipment wt : collect) {
    //                QueryWrapper<ProEconSystemEfficiencyEquipment> qw = new QueryWrapper<>();
    //                if (StringUtils.isNotEmpty(wt.getId())) {
    //                    qw.lambda().eq(ProEconSystemEfficiencyEquipment::getEquipmentId, wt.getId());
    //                }
    //                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    //                String dateString = sdf.format(previousMonth);
    //                qw.lambda().apply("to_char(data_date, 'YYYY-MM') = '" + dateString + "'");
    //                qw.lambda().orderByDesc(ProEconSystemEfficiencyEquipment::getDataDate).last("LIMIT 1");
    //                List<ProEconSystemEfficiencyEquipment> selectedList = baseMapper.selectList(qw);
    //                currlist.addAll(selectedList);
    //            }
    //        }
    //        return currlist;
    //    }


    //    @Override
    //    public List<ProEconSystemEfficiencyEquipment> gettb(String foreignKeyId, Date date) {
    ////        QueryWrapper<ProEconSystemEfficiencyEquipment> qw = null;
    ////        // 当前数据
    ////        List<ProEconSystemEfficiencyEquipment> currlist = new ArrayList<>();
    ////        if (null != date) {
    ////            Calendar cal = Calendar.getInstance();
    ////            cal.setTime(date);
    ////            cal.add(Calendar.YEAR, -1);
    ////            Date oneYearAgo = cal.getTime();
    ////
    ////            qw = new QueryWrapper<>();
    ////            if (StringUtils.isNotEmpty(foreignKeyId)) {
    ////                qw.lambda().eq(ProEconSystemEfficiencyEquipment::getStationId, foreignKeyId);
    ////            }
    ////            // 获取传入日期的年月
    ////            String yearMonth = new SimpleDateFormat("yyyy-MM").format(oneYearAgo);
    ////            // 设置日期为固定的一天（如1日）
    ////            String fixedDate = yearMonth + "-01";
    ////            qw.lambda().apply("to_char(data_date, 'YYYY-MM-DD')::timestamp without time zone >= to_timestamp('" + fixedDate + "', 'YYYY-MM-DD')");
    ////            qw.lambda().apply("to_char(data_date, 'YYYY-MM-DD')::timestamp without time zone < to_timestamp('" + fixedDate + "', 'YYYY-MM-DD') + interval '1 month'");
    ////            qw.lambda().orderByAsc(ProEconSystemEfficiencyEquipment::getOrderNum);
    ////            currlist = baseMapper.selectList(qw);
    ////        }
    ////        return currlist;
    //
    //        List<ProEconSystemEfficiencyEquipment> currlist = new ArrayList<>();
    //        if (date != null) {
    //            List<ProBasicEquipment> collect = CacheContext.wtls.stream()
    //                    .filter(wt -> foreignKeyId.equals(wt.getWindpowerstationId()))
    //                    .collect(Collectors.toList());
    //
    //            // 将日期减去一个月
    //            Calendar cal = Calendar.getInstance();
    //            cal.setTime(date);
    //            cal.add(Calendar.YEAR, -1);
    //            Date oneYearAgo = cal.getTime();
    //
    //            for (ProBasicEquipment wt : collect) {
    //                QueryWrapper<ProEconSystemEfficiencyEquipment> qw = new QueryWrapper<>();
    //                if (StringUtils.isNotEmpty(wt.getId())) {
    //                    qw.lambda().eq(ProEconSystemEfficiencyEquipment::getEquipmentId, wt.getId());
    //                }
    //                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    //                String dateString = sdf.format(oneYearAgo);
    //                qw.lambda().apply("to_char(data_date, 'YYYY-MM') = '" + dateString + "'");
    //                qw.lambda().orderByDesc(ProEconSystemEfficiencyEquipment::getDataDate).last("LIMIT 1");
    //                List<ProEconSystemEfficiencyEquipment> selectedList = baseMapper.selectList(qw);
    //                currlist.addAll(selectedList);
    //            }
    //        }
    //        return currlist;
    //    }

}
