package com.ruoyi.dz.service.impl;

import java.util.List;
import java.util.Optional;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.ExamType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dz.mapper.DzControlMapper;
import com.ruoyi.dz.domain.DzControl;
import com.ruoyi.dz.service.IDzControlService;

import javax.validation.ValidationException;

/**
 * 单招省份状态Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-12
 */
@Service
public class DzControlServiceImpl implements IDzControlService 
{
    @Autowired
    private DzControlMapper dzControlMapper;

    /**
     * 查询单招省份状态
     * 
     * @param id 单招省份状态主键
     * @return 单招省份状态
     */
    @Override
    public DzControl selectDzControlById(Long id)
    {
        return dzControlMapper.selectDzControlById(id);
    }

    /**
     * 查询单招省份状态列表
     * 
     * @param dzControl 单招省份状态
     * @return 单招省份状态
     */
    @Override
    public List<DzControl> selectDzControlList(DzControl dzControl)
    {
        return dzControlMapper.selectDzControlList(dzControl);
    }

    /**
     * 新增单招省份状态
     * 
     * @param dzControl 单招省份状态
     * @return 结果
     */
    @Override
    public int insertDzControl(DzControl dzControl)
    {
        return dzControlMapper.insertDzControl(dzControl);
    }

    /**
     * 修改单招省份状态
     * 
     * @param dzControl 单招省份状态
     * @return 结果
     */
    @Override
    public int updateDzControl(DzControl dzControl)
    {
        return dzControlMapper.updateDzControl(dzControl);
    }

    /**
     * 批量删除单招省份状态
     * 
     * @param ids 需要删除的单招省份状态主键
     * @return 结果
     */
    @Override
    public int deleteDzControlByIds(Long[] ids)
    {
        return dzControlMapper.deleteDzControlByIds(ids);
    }

    /**
     * 删除单招省份状态信息
     * 
     * @param id 单招省份状态主键
     * @return 结果
     */
    @Override
    public int deleteDzControlById(Long id)
    {
        return dzControlMapper.deleteDzControlById(id);
    }

    @Override
    public DzControl selectDzControl(SysUser user) {
        return selectDzControl(user.getLocation(), user.getExamType());
    }

    public DzControl selectDzControl(String location, ExamType type) {
        DzControl cond = new DzControl();
        cond.setLocation(location);
        String examType = type.name();
        Optional<DzControl> optional = dzControlMapper.selectDzControlList(cond).stream().filter(t -> StringUtils.isBlank(t.getExamTypes()) || t.getExamTypes().contains(examType)).findFirst();
        if(optional.isPresent()) {
            return optional.get();
        }
        throw new ValidationException("未初始化控制状态: " + location + "," + examType);
    }

}
