package com.hz.pm.api.sys.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.service.IProjectService;
import com.hz.pm.api.sys.enumeration.ProjectEarlyWarningStatusEnum;
import com.hz.pm.api.sys.mapper.ProjectEarlyWarningMapper;
import com.hz.pm.api.sys.model.entity.ProjectEarlyWarning;
import com.hz.pm.api.sys.service.IProjectEarlyWarningService;
import com.wflow.enums.WarningNoticeTypeEnum;
import com.wflow.enums.WarningRuleTypeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


/**
 * <p>
 * 业务实现类
 * 菜单
 * </p>
 *
 * @author ZPF
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ProjectEarlyWarningServiceImpl extends ServiceImpl<ProjectEarlyWarningMapper, ProjectEarlyWarning>
        implements IProjectEarlyWarningService {

    private final IProjectService projectService;

    @Override
    public Boolean earlyWarning(Project project, Integer ruleType, Integer noticeType) {
        return doEarlyWarning(project, ruleType, noticeType);
    }

    @Override
    public Boolean earlyWarning(Project project, Integer ruleType, List<Integer> noticeTypes) {
        if(CollUtil.isNotEmpty(noticeTypes)){
            if(noticeTypes.contains(WarningNoticeTypeEnum.OVER.getCode())){
                return doEarlyWarning(project, ruleType,WarningNoticeTypeEnum.OVER.getCode());
            }else{
                return doEarlyWarning(project, ruleType,WarningNoticeTypeEnum.ADVENT.getCode());
            }
        }
        return doEarlyWarning(project, ruleType, null);
    }

    @Override
    public Boolean earlyWarning(String projectCode, Integer ruleType, List<Integer> noticeTypes) {
        Project project = projectService.getProjectByCode(projectCode);
        if(Objects.isNull(project)){
            return Boolean.FALSE;
        }
        return earlyWarning(project,ruleType,noticeTypes);
    }

    private Boolean doEarlyWarning(Project project, Integer ruleType, Integer noticeType) {
        if(Objects.isNull(project) || Objects.isNull(ruleType)){
            return Boolean.FALSE;
        }
        String projectCode = project.getProjectCode();
        ProjectEarlyWarning old = getOne(Wrappers.lambdaQuery(ProjectEarlyWarning.class)
                .eq(ProjectEarlyWarning::getProjectCode, projectCode)
                .last(BizConst.LIMIT_1));
        ProjectEarlyWarning warning = new ProjectEarlyWarning();
        if(Objects.nonNull(old)){
            warning.setId(old.getId());
        }else{
            warning.setBuildOrgCode(project.getBuildOrgCode());
            warning.setAreaCode(project.getAreaCode());
        }

        switch (WarningRuleTypeEnum.checkByCode(ruleType)){
            case PROCESS_WARNING:
                warning.setProcessWarning(Boolean.TRUE);
                break;
            case DECLARED_WARNING:
                warning.setDeclaredWarning(Boolean.TRUE);
                break;
            case OPERATION_WARNING:
                warning.setOperationWarning(Boolean.TRUE);
                break;
            case RENEWAL_FUND:
                warning.setRenewalFundWarning(Boolean.TRUE);
                break;
            default:
                return Boolean.FALSE;
        }

        warning.setRuleType(ruleType);
        if(Objects.isNull(noticeType)){
            warning.setStatus(ProjectEarlyWarningStatusEnum.OVER_TIME.name());
        }else{
            switch (WarningNoticeTypeEnum.match(noticeType)){
                case OVER:
                    warning.setStatus(ProjectEarlyWarningStatusEnum.OVER_TIME.name());
                    break;
                case ADVENT:
                    warning.setStatus(ProjectEarlyWarningStatusEnum.ADVENT_TIME.name());
                    break;
                default:
                    warning.setStatus(ProjectEarlyWarningStatusEnum.OVER_TIME.name());
                    break;
            }
        }

        warning.setNoticeType(noticeType);
        warning.setNormal(Boolean.FALSE);
        return saveOrUpdate(warning);
    }
}
