package com.qmth.themis.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.themis.business.cache.bean.ExamCacheBean;
import com.qmth.themis.business.dao.TBOrgMapper;
import com.qmth.themis.business.dto.response.TBOrgDto;
import com.qmth.themis.business.entity.TBOrg;
import com.qmth.themis.business.service.TBOrgService;
import com.qmth.themis.business.service.TEExamService;
import com.qmth.themis.business.service.ThemisCacheService;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 机构 服务实现类
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/25
 */
@Service
public class TBOrgServiceImpl extends ServiceImpl<TBOrgMapper, TBOrg> implements TBOrgService {

    @Resource
    TBOrgMapper tbOrgMapper;

    @Resource
    TEExamService teExamService;

    @Resource
    ThemisCacheService themisCacheService;

    /**
     * 查询机构分页信息
     *
     * @param iPage
     * @param code
     * @param name
     * @param enable
     * @return
     */
    @Override
    public IPage<TBOrgDto> queryByPage(IPage<Map> iPage, String code, String name, Integer enable) {
        return tbOrgMapper.queryByPage(iPage, code, name, enable);
    }

    /**
     * 根据考试批次id集合合并orgId
     *
     * @param examIdSet
     * @return
     */
    @Override
    public LinkedMultiValueMap<Long, Long> mergeOrgId(Set<Long> examIdSet) {
        LinkedMultiValueMap<Long, Long> orgExamIdMap = new LinkedMultiValueMap<>();
        for (Long l : examIdSet) {
            ExamCacheBean examCacheBean = teExamService.getExamCacheBean(l);
            TBOrg tbOrg = themisCacheService.addOrgCache(examCacheBean.getOrgId());
            orgExamIdMap.add(tbOrg.getId(), l);
        }
        return orgExamIdMap;
    }
}
