package com.ozs.pm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ozs.common.core.domain.entity.SysDept;
import com.ozs.common.core.domain.entity.SysProcurementStandard;
import com.ozs.common.exception.ServiceException;
import com.ozs.common.exception.base.BaseException;
import com.ozs.plan.mapper.PlanYearsMapper;
import com.ozs.pm.mapper.PmAuditDeptRefMapper;
import com.ozs.pm.doman.PmAuditDeptRef;
import com.ozs.pm.service.PmAuditDeptRefService;
import com.ozs.system.mapper.SysDeptMapper;
import com.ozs.system.mapper.SysProcurementStandardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.validation.Validator;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 审核关联表(PmAuditDeptRef)è¡¨æå¡å®ç°ç±»
 *
 * @author makejava
 * @since 2023-02-22 22:10:08
 */
@Service
public class PmAuditDeptRefServiceImpl extends ServiceImpl<PmAuditDeptRefMapper, PmAuditDeptRef> implements PmAuditDeptRefService {

    @Autowired
    PlanYearsMapper planYearsMapper;
    @Autowired
    protected Validator validator;
    @Autowired
    private SysDeptMapper deptMapper;
    @Resource
    private SysProcurementStandardMapper sysProcurementStandardMapper;
    @Autowired
    private PmAuditDeptRefService pmAuditDeptRefService;

    @Override
    @Transactional
    public Boolean insertPmAuditDeptRefs(Long refId, String refType, Long deptId, BigDecimal evaluation, Long userId, String projectType) {
        Date now = new Date();
        SysDept sysDept = deptMapper.selectDeptById(deptId);
        String[] ancestors = sysDept.getAncestors().split(",");
        //当前用户的部门，是否允许提交（不在规定等级范围不可提交）
        //查询当前用户的部门是否有上级，是否需要上级及祖级进行审核
        Long cdeptId = null;
        Long bdeptId = null;
        String deptLevel = null;
        if (ancestors.length == 2) { //C级
            deptLevel = "C";
        } else if (ancestors.length == 3) { //B级
            deptLevel = "B";
            cdeptId = Long.valueOf(ancestors[2]);
        } else if (ancestors.length == 4) { //A级
            deptLevel = "A";
            cdeptId = Long.valueOf(ancestors[2]);
            bdeptId = Long.valueOf(ancestors[3]);
        }
        if (!Arrays.asList("A", "B", "C").contains(deptLevel)) {
            throw new BaseException("当前用户单位不属于ABC三级，无权提交");
        }
        //判断每个级别是否允许审核，并插入审核关联表
        PmAuditDeptRef refA = new PmAuditDeptRef(null, refId, refType, deptId,
                "A", 0, userId + "", now,
                userId + "", now);

        PmAuditDeptRef refB = new PmAuditDeptRef(null, refId, refType, bdeptId,
                "B", 0, userId + "",
                now, userId + "", now);

        PmAuditDeptRef refC = null;
        //审核关联表--PmAuditDeptRef
        switch (deptLevel) {
            case "A":
                // A的判断  最少插入1条。最多插入3条
                //获取各个项目类型设定的概算金额阈值
                LambdaQueryWrapper<SysProcurementStandard> queryWrapperA = new LambdaQueryWrapper<>();
                queryWrapperA.eq(SysProcurementStandard::getDeptId, deptId);
                queryWrapperA.eq(SysProcurementStandard::getCategory, projectType.equals("2") ? "1" : projectType.equals("1") ? "2" : projectType);
                List<SysProcurementStandard> sysProcurementStandardsListA = sysProcurementStandardMapper.selectList(queryWrapperA);
                if (ObjectUtils.isEmpty(sysProcurementStandardsListA)) {
                    throw new ServiceException("部门id为" + deptId + "的部门采购标准未进行初始化！");
                }
                //A不限额就自己审核
                SysProcurementStandard sA = sysProcurementStandardsListA.get(0);
                if (sA.getState().equals(Integer.valueOf(0))) {
                    //refA采用初始化数据
                } else {
                    //A限额了判断是否超额，不超额就自己审。超额了追加上级
                    BigDecimal maximum = sA.getMaximum();
                    if (maximum.compareTo(evaluation) == 1) {
                        //refA采用初始化数据
                    } else {
                        //A限额了，找B
                        refA = null;
                        LambdaQueryWrapper<SysProcurementStandard> queryWrapperB = new LambdaQueryWrapper<>();
                        queryWrapperB.eq(SysProcurementStandard::getDeptId, bdeptId);
                        queryWrapperB.eq(SysProcurementStandard::getCategory, projectType.equals("2") ? "1" : projectType.equals("1") ? "2" : projectType);
                        List<SysProcurementStandard> sysProcurementStandardsListB = sysProcurementStandardMapper.selectList(queryWrapperB);
                        //B不限额，B审
                        if (ObjectUtils.isEmpty(sysProcurementStandardsListB)) {
                            throw new ServiceException("部门id为" + bdeptId + "的部门采购标准未进行初始化！");
                        }
                        SysProcurementStandard sB = sysProcurementStandardsListB.get(0);
                        if (sB.getState().equals(Integer.valueOf(0))) {
                            //refB采用初始化数据
                            pmAuditDeptRefService.save(refB);
                        } else {
                            //B限额了判断是否超额，不超额就自己审。超额了追加C级
                            BigDecimal maximumB = sB.getMaximum();
                            //B超额
                            if (maximumB.compareTo(evaluation) == -1) {
                                //C不限额，
                                refC = new PmAuditDeptRef(null, refId, refType, cdeptId, "C", 0, userId + "", now, userId + "", now);
                                pmAuditDeptRefService.save(refC);
                            }
                            //refB采用初始化数据
                            pmAuditDeptRefService.save(refB);
                        }
                    }
                }
                if (!ObjectUtils.isEmpty(refA)) {
                    pmAuditDeptRefService.save(refA);
                }

                break;
            case "B":
                // B的判断  最少插入1条。最多插入2条
                LambdaQueryWrapper<SysProcurementStandard> queryWrapperB = new LambdaQueryWrapper<>();
                queryWrapperB.eq(SysProcurementStandard::getDeptId, deptId);
                queryWrapperB.eq(SysProcurementStandard::getCategory, projectType.equals("2") ? "1" : projectType.equals("1") ? "2" : projectType);
                List<SysProcurementStandard> sysProcurementStandardsListB = sysProcurementStandardMapper.selectList(queryWrapperB);
                //B不限额，B审
                if (ObjectUtils.isEmpty(sysProcurementStandardsListB)) {
                    throw new ServiceException("部门id为" + deptId + "的部门采购标准未进行初始化！");
                }
                SysProcurementStandard sB = sysProcurementStandardsListB.get(0);
                refB = new PmAuditDeptRef(null, refId, refType, deptId, "B", 0, userId + "", now, userId + "", now);
                if (sB.getState() == 0) {
                } else {
                    //B限额了判断是否超额，不超额就自己审。超额了追加C级
                    BigDecimal maximumB = sB.getMaximum();
                    //B不超额B审
                    if (maximumB.compareTo(evaluation) == 1) {
                    } else {
                        //C不限额C审，
                        refB = null;
//                        refB = new PmAuditDeptRef(null, refId, refType, deptId, "B", 1, userId + "", now, userId + "", now);
                        refC = new PmAuditDeptRef(null, refId, refType, cdeptId, "C", 0, userId + "", now, userId + "", now);
                        pmAuditDeptRefService.save(refC);
                    }
                }
                if (!ObjectUtils.isEmpty(refB)) {
                    pmAuditDeptRefService.save(refB);
                }
                break;
            default:
                //C不限额C审，
                // 插入1条
                refC = new PmAuditDeptRef(null, refId, refType, deptId, "C", 0, userId + "", now, userId + "", now);
                pmAuditDeptRefService.save(refC);
        }
        return true;
    }
}
