package com.qxgmat.service.inline;

import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.PageResult;
import com.nuliji.tools.Transform;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.qxgmat.data.constants.enums.status.DirectionStatus;
import com.qxgmat.data.dao.ExercisePaperQuestionMapper;
import com.qxgmat.data.dao.entity.ExercisePaperQuestion;
import com.qxgmat.data.dao.entity.QuestionNo;
import com.qxgmat.data.relation.ExerciseQuestionRelationMapper;
import com.qxgmat.data.relation.QuestionNoRelationMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Service
public class ExerciseQuestionService extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(ExerciseQuestionService.class);

    @Resource
    private ExercisePaperQuestionMapper exerciseQuestionMapper;

    @Resource
    private ExerciseQuestionRelationMapper exerciseQuestionRelationMapper;

    @Resource
    private QuestionNoRelationMapper questionNoRelationMapper;

    @Resource
    private QuestionNoService questionNoService;


    public Page<ExercisePaperQuestion> listAdmin(int page, int pageSize, String type, Number structId, Number questionNo, Number paperId, String place, String difficult, String startTime, String endTime, String order, DirectionStatus direction){
        if(order.isEmpty()) order = "id";
        if (direction == null){
            direction = DirectionStatus.DESC;
        }
        String finalOrder = order;
        DirectionStatus finalDirection = direction;

        Page<ExercisePaperQuestion> p = page(() -> {
            exerciseQuestionRelationMapper.listAdmin(type, structId, questionNo, paperId, place, difficult, startTime, endTime, finalOrder, finalDirection.key);
        }, page, pageSize);

        Collection ids = Transform.getIds(p, ExercisePaperQuestion.class, "id");

        // 获取详细数据
        List<ExercisePaperQuestion> list = select(ids);
        Transform.replace(p, list, ExercisePaperQuestion.class, "id");

        return p;
    }

    public ExercisePaperQuestion add(ExercisePaperQuestion question){
        int result = insert(exerciseQuestionMapper, question);
        question = one(exerciseQuestionMapper, question.getId());
        if(question == null){
            throw new SystemException("题目添加失败");
        }
        return question;
    }

    public ExercisePaperQuestion edit(ExercisePaperQuestion question){
        ExercisePaperQuestion in = one(exerciseQuestionMapper, question.getId());
        if(in == null){
            throw new ParameterException("题目不存在");
        }
        int result = update(exerciseQuestionMapper, question);
        return question;
    }

    public boolean delete(Number id){
        ExercisePaperQuestion in = one(exerciseQuestionMapper, id);
        if(in == null){
            throw new ParameterException("题目不存在");
        }
        int result = delete(exerciseQuestionMapper, id);
        return result > 0;
    }

    public ExercisePaperQuestion get(Number id){
        ExercisePaperQuestion in = one(exerciseQuestionMapper, id);

        if(in == null){
            throw new ParameterException("题目不存在");
        }
        return in;
    }

    public Page<ExercisePaperQuestion> select(int page, int pageSize){
        return select(exerciseQuestionMapper, page, pageSize);
    }

    public List<ExercisePaperQuestion> select(Collection ids){
        return select(exerciseQuestionMapper, ids);
    }

}
