package com.ynfy.buss.exam.paperruledetail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynfy.buss.exam.paperruledetail.entity.PaperRuleDetail;
import com.ynfy.buss.exam.paperruledetail.mapper.PaperRuleDetailMapper;
import com.ynfy.buss.exam.paperruledetail.service.IPaperRuleDetailService;
import com.ynfy.buss.exam.paperrulegroup.entity.PaperRuleGroup;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Description: paper_rule_detail
 * @Author: jeecg-boot
 * @Date: 2023-02-22
 * @Version: V1.0
 */
@Service
public class PaperRuleDetailServiceImpl extends ServiceImpl<PaperRuleDetailMapper, PaperRuleDetail> implements IPaperRuleDetailService {

    @Override
    public void saveAll(String paperId, List<PaperRuleGroup> groupList) {
        if (!CollectionUtils.isEmpty(groupList)) {
            groupList.stream().forEach(g -> {
                List<PaperRuleDetail> ruleList = g.getRuleList();
                int sort = 1;
                if (!CollectionUtils.isEmpty(ruleList)) {
                    for (PaperRuleDetail r : ruleList) {
                        r.setGroupId(g.getId());
                        r.setPaperId(paperId);
                        r.setQuestionType(g.getQuestionType());
                        r.setSort(sort);
                        sort++;
                    }
                }
                saveBatch(ruleList);
            });
        }
    }

    /**
     * 删除已保存
     */
    @Override
    public void remove(String paperId) {
        QueryWrapper<PaperRuleDetail> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PaperRuleDetail::getPaperId, paperId);
        this.remove(wrapper);
    }

    /**
     * 根据试卷id查询规则明细
     */
    @Override
    public List<PaperRuleDetail> listByPaperId(String paperId) {
        QueryWrapper<PaperRuleDetail> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PaperRuleDetail::getPaperId, paperId);
        return this.list(wrapper);
    }

    @Override
    public void removeByQuestionIds(String[] paperIds) {
        QueryWrapper<PaperRuleDetail> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(PaperRuleDetail::getPaperId, paperIds);
        this.remove(wrapper);
    }
}
