/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.feesapi.bmgl.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.bmgl.entity.BmYmjzConfig;
import com.tofly.feesapi.bmgl.entity.BmYmjzConfigLs;
import com.tofly.feesapi.bmgl.mapper.BmYmjzConfigMapper;
import com.tofly.feesapi.bmgl.service.BmYmjzConfigLsService;
import com.tofly.feesapi.bmgl.service.BmYmjzConfigService;
import com.tofly.feesapi.common.enums.CwJzTimeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 月末结转CONFIG
 *
 * @author x
 * @date Wed Mar 18 00:00:00 CST 2026
 */
@Slf4j
@Service("bmYmjzConfigService")
public class BmYmjzConfigServiceImpl extends ServiceImpl<BmYmjzConfigMapper, BmYmjzConfig> implements BmYmjzConfigService {
    @Autowired
    private BmYmjzConfigLsService bmYmjzConfigLsService;


    public boolean IsExist(String month, String day, String time) {

        log.info("查询是否存在:{}{}{}", month, day, time);
        QueryWrapper<BmYmjzConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("MONTH", month)
                .eq("DAY", day)
                .eq("TIME", time);

        return !this.list(queryWrapper).isEmpty();
    }

    @Override
    public Map getSeachTime(String strdate, String tp) {
        Map map = new HashMap();
        String upltime = "";
        String nowtime = "";
        BmYmjzConfig upConfig;
        BmYmjzConfig nowConfig;
        if ("1".equals(tp)) {
            int year = Integer.parseInt(strdate.substring(0, 4));
            int month = Integer.parseInt(strdate.substring(4, 6));
            List<BmYmjzConfig> nowlist = this.list();
            if (year != LocalDate.now().getYear()) {
                nowlist = bmYmjzConfigLsService.lambdaQuery()
                        .eq(BmYmjzConfigLs::getJzyear, year)
                        .list().stream().map(item -> {
                            BmYmjzConfig bmYmjzConfig = new BmYmjzConfig();
                            bmYmjzConfig.setCode(item.getCode());
                            bmYmjzConfig.setName(item.getName());
                            bmYmjzConfig.setMonth(item.getMonth());
                            bmYmjzConfig.setDay(item.getDay());
                            bmYmjzConfig.setTime(item.getTime());
                            return bmYmjzConfig;
                        }).collect(Collectors.toList());
            }
            nowConfig = nowlist.stream().filter(bmYmjzConfig -> bmYmjzConfig.getMonth().intValue() == month)
                    .findFirst().orElse(null);
            if (strdate.endsWith("01")) {
                List<BmYmjzConfigLs> uplist = bmYmjzConfigLsService.lambdaQuery()
                        .eq(BmYmjzConfigLs::getJzyear, year - 1)
                        .list();
                upConfig = uplist.stream().filter(item -> item.getMonth().intValue() == 12)
                        .map(item -> {
                            BmYmjzConfig bmYmjzConfig = new BmYmjzConfig();
                            bmYmjzConfig.setCode(item.getCode());
                            bmYmjzConfig.setName(item.getName());
                            bmYmjzConfig.setMonth(item.getMonth());
                            bmYmjzConfig.setDay(item.getDay());
                            bmYmjzConfig.setTime(item.getTime());
                            return bmYmjzConfig;
                        })
                        .findFirst().orElse(null);
                if (StringUtil.isNull(nowConfig)) {
                    upltime = (year - 1) + "-12-27 " + CwJzTimeEnum.Default.getName() + ":00";
                } else {
                    upltime = (year - 1) + "-12-" + nowConfig.getDay() + " " + upConfig.getTime() + ":00";
                }
            } else {
                upConfig = nowlist.stream().filter(item -> item.getMonth().intValue() == month - 1)
                        .findFirst().orElse(null);
                if (StringUtil.isNull(upConfig)) {
                    CwJzTimeEnum cwJzTimeEnum = Arrays.stream(CwJzTimeEnum.values())
                            .filter(item -> item.getValue().equals(String.valueOf(month - 1))).findFirst().get();
                    upltime = year + "-" + (month - 1) + "-27 " + CwJzTimeEnum.Default.getName() + ":00";
                } else {
                    upltime = year + "-" + (month - 1) + "-" + upConfig.getDay() + " " + upConfig.getTime() + ":00";
                }
            }
            if (StringUtil.isNull(nowConfig)) {
                CwJzTimeEnum cwJzTimeEnum = Arrays.stream(CwJzTimeEnum.values())
                        .filter(item -> item.getValue().equals(String.valueOf(month))).findFirst().get();
                nowtime = year + "-" + month + "-27 " + CwJzTimeEnum.Default.getName() + ":00";
            } else {
                nowtime = year + "-" + month + "-" + nowConfig.getDay() + " " + nowConfig.getTime() + ":00";
            }
        } else {
            int year = Integer.parseInt(strdate);
            List<BmYmjzConfigLs> uplist = bmYmjzConfigLsService.lambdaQuery()
                    .eq(BmYmjzConfigLs::getJzyear, year - 1)
                    .list();
            upConfig = uplist.stream().filter(item -> item.getMonth().intValue() == 12)
                    .map(item -> {
                        BmYmjzConfig bmYmjzConfig = new BmYmjzConfig();
                        bmYmjzConfig.setCode(item.getCode());
                        bmYmjzConfig.setName(item.getName());
                        bmYmjzConfig.setMonth(item.getMonth());
                        bmYmjzConfig.setDay(item.getDay());
                        bmYmjzConfig.setTime(item.getTime());
                        return bmYmjzConfig;
                    })
                    .findFirst().orElse(null);
            if (LocalDate.now().getYear() == year) {
                List<BmYmjzConfig> nowlist = this.list();
                nowConfig = nowlist.stream().filter(item -> item.getMonth().intValue() == 12)
                        .findFirst().orElse(null);
            } else {
                List<BmYmjzConfigLs> nowlist = bmYmjzConfigLsService.lambdaQuery()
                        .eq(BmYmjzConfigLs::getJzyear, year)
                        .list();
                nowConfig = nowlist.stream().filter(item -> item.getMonth().intValue() == 12)
                        .map(item -> {
                            BmYmjzConfig bmYmjzConfig = new BmYmjzConfig();
                            bmYmjzConfig.setCode(item.getCode());
                            bmYmjzConfig.setName(item.getName());
                            bmYmjzConfig.setMonth(item.getMonth());
                            bmYmjzConfig.setDay(item.getDay());
                            bmYmjzConfig.setTime(item.getTime());
                            return bmYmjzConfig;
                        }).findFirst().orElse(null);
            }
            if (StringUtil.isNull(upConfig)) {
                upltime = year - 1 + "-12-27 " + CwJzTimeEnum.Default.getName() + ":00";
            } else {
                upltime = year - 1 + "-12-" + upConfig.getDay() + " " + upConfig.getTime() + ":00";
            }
            if (StringUtil.isNull(nowConfig)) {
                nowtime = year + "-12-27 " + CwJzTimeEnum.Default.getName() + ":00";
            } else {
                nowtime = year + "-12-" + nowConfig.getDay() + " " + nowConfig.getTime() + ":00";
            }
        }
        map.put("upltime", upltime);
        map.put("nowtime", nowtime);
        return map;
    }

    @Override
    public String getCwrq(Date startDate) {
        String year = DateUtil.format(startDate, "yyyy");
        String month = DateUtil.format(startDate, "MM");
        String day = DateUtil.format(startDate, "dd");
        String time = DateUtil.format(startDate, "HH:mm");
        // 判断是否是当年
        if (year.equals(String.valueOf(LocalDate.now().getYear()))) {
            List<BmYmjzConfig> list = this.list();
            BmYmjzConfig nowConfig = list.stream().filter(item -> item.getMonth().intValue() == Integer.parseInt(month))
                    .findFirst().orElse(null);
            if (StringUtil.isNull(nowConfig)) {
                if (Long.parseLong(day) < 27) {
                    return year + month;
                } else {
                    if (month.equals("12")) {
                        return (Long.parseLong(year) + 1) + "01";
                    }
                    return year + String.format("%02d", Integer.parseInt(month) + 1);
                }
            } else {
                //if (nowConfig.getDay().compareTo(BigDecimal.valueOf(Long.parseLong(day))) > 0) {
                String configDayTime = String.format("%02d %s", nowConfig.getDay().intValue(), nowConfig.getTime());
                String startDayTime = String.format("%02d %s", Long.parseLong(day), time);
                if (configDayTime.compareTo(startDayTime) > 0) {
                    return year + month;
                } else {
                    if (month.equals("12")) {
                        return (Long.parseLong(year) + 1) + "01";
                    }
                    return year + String.format("%02d", Integer.parseInt(month) + 1);
                }
            }
        } else {
            List<BmYmjzConfigLs> list = bmYmjzConfigLsService.lambdaQuery()
                    .eq(BmYmjzConfigLs::getJzyear, Long.parseLong(year) - 1)
                    .list();
            BmYmjzConfigLs nowConfig = list.stream().filter(item -> item.getMonth().intValue() == Integer.parseInt(month))
                    .findFirst().orElse(null);
            if (StringUtil.isNull(nowConfig)) {
                if (Long.parseLong(day) < 27) {
                    return year + month;
                } else {
                    if (month.equals("12")) {
                        return (Long.parseLong(year) + 1) + "01";
                    }
                    return year + String.format("%02d", Integer.parseInt(month) + 1);
                }
            } else {
                //if (nowConfig.getDay().compareTo(BigDecimal.valueOf(Long.parseLong(day))) > 0) {
                String configDayTime = String.format("%02d %s", nowConfig.getDay().intValue(), nowConfig.getTime());
                String startDayTime = String.format("%02d %s", Long.parseLong(day), time);
                if (configDayTime.compareTo(startDayTime) > 0) {
                    return year + month;
                } else {
                    if (month.equals("12")) {
                        return (Long.parseLong(year) + 1) + "01";
                    }
                    return year + String.format("%02d", Integer.parseInt(month) + 1);
                }
            }
        }
    }
}
