package com.tofly.feesapi.xtgl.service.impl;

import cn.hutool.core.collection.CollectionUtil;
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.toolkit.Wrappers;
import com.tofly.feesapi.common.exception.FeesErrorCode;
import com.tofly.feesapi.common.exception.FeesException;
import com.tofly.feesapi.xtgl.entity.SysGdcsb;
import com.tofly.feesapi.xtgl.mapper.SysGdcsbMapper;
import com.tofly.feesapi.xtgl.service.ISysGdcsbService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Date;
import java.util.List;

@Service
public class SysGdcsbServiceImpl extends ServiceImpl<SysGdcsbMapper, SysGdcsb> implements ISysGdcsbService {

    @Override
    public String selectCwrq() {
        SysGdcsb gdcs = getGdcs();
        return gdcs.getCwrq();
    }

    private SysGdcsb getGdcs() {
        LambdaQueryWrapper<SysGdcsb> wrapper = Wrappers.<SysGdcsb>lambdaQuery()
                .eq(SysGdcsb::getGdbz, "0");
        return getOne(wrapper);
    }

    @Override
    public String autoUpdateCwrq(String selectCwrq) {
        // 生成新的财务日期
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .appendPattern("yyyyMM")
                .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
                .toFormatter();
        LocalDate localDate = LocalDate.parse(selectCwrq, formatter);
        String newCwrq = localDate.plusMonths(1).format(formatter);

        if (StrUtil.isEmpty(newCwrq)) {
            log.error("自动更新财务周期失败 : ============================>");
            throw new FeesException("自动更新财务周期失败", FeesErrorCode.BUSINESS_ERROR);
        }

        // 更新上月财务日期状态
        List<SysGdcsb> sysGdcsbList = this.lambdaQuery()
                .eq(SysGdcsb::getSsgs, "1")
                .eq(SysGdcsb::getCwrq, selectCwrq)
                .eq(SysGdcsb::getGdbz, "0")
                .list();

        if (sysGdcsbList.size() > 0) {
            this.lambdaUpdate()
                    .set(SysGdcsb::getGdrq, new Date())
                    .set(SysGdcsb::getGdbz, "1")
                    .eq(SysGdcsb::getSsgs, "1")
                    .eq(SysGdcsb::getCwrq, selectCwrq)
                    .eq(SysGdcsb::getGdbz, "0")
                    .update();
        }

        // 新增新的财务日期
        List<SysGdcsb> newGdList = this.lambdaQuery()
                .eq(SysGdcsb::getSsgs, "1")
                .eq(SysGdcsb::getCwrq, newCwrq)
                .eq(SysGdcsb::getGdbz, "0")
                .list();
        if (CollectionUtil.isEmpty(newGdList)) {
            SysGdcsb gdcsb = new SysGdcsb();
            gdcsb.setSsgs("1");
            gdcsb.setGdbz("0");
            gdcsb.setZwrq(LocalDateTime.now().format(DateTimeFormatter.ISO_DATE));
            gdcsb.setCwrq(newCwrq);
            save(gdcsb);
        }

        return newCwrq;
    }

}
