package com.qmth.distributed.print.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.bean.result.TemplatePrintInfoResult;
import com.qmth.distributed.print.business.entity.BasicTemplateOrg;
import com.qmth.distributed.print.business.mapper.BasicTemplateOrgMapper;
import com.qmth.distributed.print.business.service.BasicTemplateOrgService;
import com.qmth.distributed.print.business.enums.TemplateTypeEnum;
import com.qmth.teachcloud.common.entity.BasicCourse;
import com.qmth.teachcloud.common.entity.SysOrg;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.service.BasicCourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 适用学院（题卡规则、模板适用学院） 服务实现类
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Service
public class BasicTemplateOrgServiceImpl extends ServiceImpl<BasicTemplateOrgMapper, BasicTemplateOrg> implements BasicTemplateOrgService {
    @Resource
    private BasicTemplateOrgMapper basicTemplateOrgMapper;
    @Resource
    private BasicCourseService basicCourseService;

    @Override
    public List<SysOrg> listByTypeAndTemplateId(String type, Long templateId) {
        return this.baseMapper.listByTypeAndTemplateId(type, templateId);
    }

    @Override
    public void removeByRuleId(Long id) {
        UpdateWrapper<BasicTemplateOrg> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(BasicTemplateOrg::getRuleId, id);
        this.remove(updateWrapper);
    }

    @Override
    public void saveBatch(Long schoolId, TemplateTypeEnum type, Long ruleId, Long[] orgIds, Long createId) {
        List<BasicTemplateOrg> list = new ArrayList<>();
        for (Long orgId : orgIds) {
            BasicTemplateOrg basicTemplateOrg = new BasicTemplateOrg();
            basicTemplateOrg.setSchoolId(schoolId);
            basicTemplateOrg.setType(type);
            basicTemplateOrg.setRuleId(ruleId);
            basicTemplateOrg.setOrgId(orgId);
            basicTemplateOrg.insertInfo(createId);
            list.add(basicTemplateOrg);
        }
        this.saveBatch(list);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<TemplatePrintInfoResult> findTemplateInfoByOrgIds(List<Long> ids) {
        return basicTemplateOrgMapper.templateListByOrgIds(ids);
    }

    @Override
    public List<BasicTemplateOrg> findByCourseCodeAndType(String courseCode, TemplateTypeEnum templateTypeEnum, Long schoolId) {
        BasicCourse basicCourse = basicCourseService.getOne(new QueryWrapper<BasicCourse>().lambda().eq(BasicCourse::getSchoolId,schoolId).eq(BasicCourse::getCode,courseCode));
        if (Objects.isNull(basicCourse)){
            throw ExceptionResultEnum.ERROR.exception("未找到基础课程信息");
        }
        return this.list(new QueryWrapper<BasicTemplateOrg>().lambda()
                .eq(BasicTemplateOrg::getSchoolId,schoolId)
                .eq(BasicTemplateOrg::getType,templateTypeEnum)
                .eq(BasicTemplateOrg::getOrgId,basicCourse.getTeachingRoomId()));
    }
}
