package com.zd.exam.service.impl;

import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.exam.domain.ElExamDept;
import com.zd.exam.enums.ScopeType;
import com.zd.exam.mapper.ElExamDeptMapper;
import com.zd.exam.service.ElExamDeptService;
import com.zd.system.api.RemoteDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 考试部门(ElExamDept)表服务实现类
 *
 * @author liubo
 * @since 2021-08-31 09:25:37
 */
@Service("elExamDeptService")
public class ElExamDeptServiceImpl implements ElExamDeptService {
    @Resource
    private ElExamDeptMapper elExamDeptMapper;
    @Autowired
    private RemoteDeptService remoteDeptService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ElExamDept queryById(Long id) {
        return this.elExamDeptMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ElExamDept> queryAllByLimit(int offset, int limit) {
        return this.elExamDeptMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param elExamDept 实例对象
     * @return 实例对象
     */
    @Override
    public ElExamDept insert(ElExamDept elExamDept) {
        this.elExamDeptMapper.insert(elExamDept);
        return elExamDept;
    }

    /**
     * 修改数据
     *
     * @param elExamDept 实例对象
     * @return 实例对象
     */
    @Override
    public ElExamDept update(ElExamDept elExamDept) {
        this.elExamDeptMapper.update(elExamDept);
        return this.queryById(elExamDept.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.elExamDeptMapper.deleteById(id) > 0;
    }

    @Override
    public void saveAll(Long examId, List<Long> deptIds) {
        // 先删除
        elExamDeptMapper.deleteByExamId(examId);

        // 再增加
        if (CollectionUtils.isEmpty(deptIds)) {
            throw new ServiceException("请至少选择选择一个部门！！");
        }
        List<ElExamDept> list = new ArrayList<>();

        Date now = new Date();
        ElExamDept depart;
        for (Long id : deptIds) {
            depart = new ElExamDept();
            depart.setDeptId(id);
            depart.setExamId(examId);
            depart.setCreateTime(now);
            list.add(depart);
        }

        elExamDeptMapper.insertBatch(list);
    }

    @Override
    public List<Long> queryByExamId(Long examId) {

        ElExamDept elExamDept = new ElExamDept();
        elExamDept.setExamId(examId);
        List<ElExamDept> examDepts = elExamDeptMapper.queryAll(elExamDept);

        List<Long> departIds = new ArrayList<>();
        for (ElExamDept examDept : examDepts) {
            departIds.add(examDept.getDeptId());
        }

        return departIds;
    }

    @Override
    public int deleteByExamId(Long examId) {
        return elExamDeptMapper.deleteByExamId(examId);
    }

    /**
     * 校验 安全准入，负面清单，黑名单 类型的考试是否存在
     * @param scopeType 类型
     * @param examId 需要排除的考试
     * @param deptIds 组织架构 同一个组织架构该类型的考试只能创建一个
     */
    @Override
    public void checkOnlyOneExamByScopeType(Integer scopeType, Long examId, List<Long> deptIds) {
        // 查询出 各部门的父级
        AjaxResult result = remoteDeptService.allParentId(deptIds);
        if(result.get("code").equals(500)){
            throw new ServiceException((String) result.get("msg"));
        }
        deptIds = (List<Long>) result.get("data");
        int num = elExamDeptMapper.checkOnlyOneExamByScopeType(scopeType, examId, deptIds);
        if(num > 0){
            String typeValue = "";
            for (ScopeType type : ScopeType.values()) {
                if(Objects.equals(type.getType(), scopeType)){
                    typeValue = type.getDesc();
                    break;
                }
            }
            throw new ServiceException(typeValue + "类型的考试已经存在，不能重复！");
        }
    }
}
