package com.nantian.cloud.dbcapp.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.dbcapp.entity.SettlementDataConfig;
import com.nantian.cloud.dbcapp.mapper.record.SettlementDataConfigMapper;
import com.nantian.cloud.dbcapp.service.ISettlementDataConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 企业数据结算周期配置 服务实现类
 * </p>
 *
 * @author qqm
 * @since 2024-01-25
 */
@Service
public class SettlementDataConfigServiceImpl extends ServiceImpl<SettlementDataConfigMapper, SettlementDataConfig> implements ISettlementDataConfigService {


    @Override
    public IPage<SettlementDataConfig> getSettlementDataConfigInfo(Page page, SettlementDataConfig settlementDataConfig) {
        return baseMapper.queryAllByLimit(page,settlementDataConfig);
    }

    @Override
    public SettlementDataConfig getSettlementDataConfigInfoByOne(String enterpriseCode, String statDate, String month, String formulName) {
        QueryWrapper<SettlementDataConfig> param = new QueryWrapper<>();
        SettlementDataConfig settlementDataConfig = new SettlementDataConfig();
        List<SettlementDataConfig> settlementDataConfigList = new ArrayList<>();
        param.eq("enterprise_code", enterpriseCode);
        param.eq("formul_name", formulName);
        param.like("month", month);
        param.and( w ->{
            return w.last(" concat('"+statDate+"','-01') between use_start_date and use_end_date");
        });
        param.orderByDesc("id");
        //settlementDataConfig = baseMapper.selectOne(param);
        settlementDataConfigList = baseMapper.selectList(param);
        //if (null == settlementDataConfig){
        if (null == settlementDataConfigList || settlementDataConfigList.size() == 0){
            QueryWrapper<SettlementDataConfig> paramAll = new QueryWrapper<>();
            paramAll.eq("enterprise_code", enterpriseCode);
            paramAll.eq("formul_name", "全厂");
            paramAll.like("month", month);
            paramAll.and( w ->{
                return w.last(" concat('"+statDate+"','-01') between use_start_date and use_end_date");
            });
            paramAll.orderByDesc("id");
            //settlementDataConfig = baseMapper.selectOne(paramAll);
            settlementDataConfigList = baseMapper.selectList(paramAll);
        }
        if(settlementDataConfigList != null && settlementDataConfigList.size()>0){
            return settlementDataConfigList.get(0);
        }else{
            return null;
        }
    }

    public SettlementDataConfig isCheckEntSettlement(String enterpriseCode,String formulName,String month){
        //先查单独配置的，如果有的话就取，
        //判断企业是否配置自定义结算时间范围
        QueryWrapper<SettlementDataConfig> param = new QueryWrapper<>();
        param.eq("enterprise_code", enterpriseCode);
        param.eq("formul_name", formulName);
        param.eq("is_user", "1");
        param.like("month", month.substring(5,7));
        SettlementDataConfig settlementData = baseMapper.selectOne(param);
        if (null != settlementData){
            return settlementData;
        }
        //判断企业是否有全厂配置
        QueryWrapper<SettlementDataConfig> paramAll = new QueryWrapper<>();
        paramAll.eq("enterprise_code", enterpriseCode);
        paramAll.eq("formul_name", "全厂");
        paramAll.eq("is_user", "1");
        paramAll.like("month", month.substring(5,7));
        return baseMapper.selectOne(paramAll);
    }

    /**
     * 验证当前月份是否在结算月中
     * @Author: dengjian
     * @DATE: 2024/6/27 15:19
     * @param settlementData
     * @param month
     * @return java.lang.Boolean
     *
     */
    public Boolean isCheckSettleMonth(SettlementDataConfig settlementData, String month){
        //如果没有配置结算月，则走原逻辑
        if (StringUtils.isBlank(settlementData.getMonth())){
            return false;
        }
        //结算月份
        String[] settlementMonthList = settlementData.getMonth().split(",");
        //截取当前月份（yyyy-MM），1-9月，需要补全
        List<String> collect = Arrays.stream(settlementMonthList).collect(Collectors.toList());
        for (String s : collect) {
            if (s.equals(month.substring(5,7))){
                return true;
            }
        }
        return false;
    }


    /**
     * @Description:  根据企业信用代码获取结算月份
     * @Author: dengjian
     * @DATE: 2024/11/6 17:41
     * @param enterpriseCode
     * @return java.util.List<java.lang.String>
     *
     */
    public List<SettlementDataConfig> getSettleMonthByEnterpriseCode(String enterpriseCode,String time){
        return baseMapper.getSettleMonthByEnterpriseCode(enterpriseCode,time);
    }




}
