package com.qxgmat.service;

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.nuliji.tools.mybatis.Example;
import com.qxgmat.data.constants.enums.QuestionType;
import com.qxgmat.data.constants.enums.module.QuestionModule;
import com.qxgmat.data.constants.enums.status.DirectionStatus;
import com.qxgmat.data.dao.UserNoteQuestionMapper;
import com.qxgmat.data.dao.entity.Question;
import com.qxgmat.data.dao.entity.QuestionNo;
import com.qxgmat.data.dao.entity.UserCollectQuestion;
import com.qxgmat.data.dao.entity.UserNoteQuestion;
import com.qxgmat.data.relation.UserNoteQuestionRelationMapper;
import com.qxgmat.data.relation.entity.UserNoteQuestionRelation;
import com.qxgmat.service.inline.QuestionNoService;
import com.qxgmat.service.inline.QuestionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

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

    @Resource
    private UserNoteQuestionMapper userNoteQuestionMapper;

    @Resource
    private UserNoteQuestionRelationMapper userNoteQuestionRelationMapper;

    @Resource
    private QuestionNoService questionNoService;

    @Resource
    private QuestionService questionService;

    /**
     * 获取用户笔记列表
     * @param page
     * @param size
     * @param userId
     * @param module
     * @param type
     * @param startTime
     * @param endTime
     * @param order
     * @param direction
     * @return
     */
    public PageResult<UserNoteQuestionRelation> list(int page, int size, Integer userId, QuestionModule module, QuestionType type, String startTime, String endTime, String order, DirectionStatus direction){
        String moduleKey = module == null ? null : module.key;
        String typeKey = type == null ? null : type.key;
        Page<UserNoteQuestion> p = page(()->{
            userNoteQuestionRelationMapper.list(userId, moduleKey, typeKey, startTime, endTime, order, direction.key);
        }, page, size);

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

        // 获取详细数据
        List<UserNoteQuestion> list = select(ids);

        List<UserNoteQuestionRelation> pr = Transform.convert(list, UserNoteQuestionRelation.class);

        // 获取题目信息
        Collection questionIds = Transform.getIds(pr, UserNoteQuestionRelation.class, "questionId");
        List<Question> questionList = questionService.select(questionIds);
        Transform.combine(pr, questionList, UserNoteQuestionRelation.class, "questionId", "question", Question.class, "id");

        // 获取题目编号信息
        Collection questionNoIds = Transform.getIds(pr, UserNoteQuestionRelation.class, "questionNoId");
        List<QuestionNo> questionNoList = questionNoService.select(questionNoIds);
        Transform.combine(pr, questionNoList, UserNoteQuestionRelation.class, "questionNoId", "questionNo", Question.class, "id");

        return new PageResult<>(pr, p.getTotal());
    }

    /**
     * 更新用户笔记：没有则添加
     * @param note
     * @return
     */
    @Transactional
    public UserNoteQuestion update(UserNoteQuestion note){
        Example example = new Example(UserCollectQuestion.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("userId", note.getUserId())
                        .andEqualTo("module", note.getModule())
        );
        example.and(
                example.createCriteria()
                        .orEqualTo("questionId", note.getQuestionId())
                        .orEqualTo("questionNoId", note.getQuestionNoId())
        );
        UserNoteQuestion in = one(userNoteQuestionMapper, example);
        if(in == null){
            return add(note);
        }else{
            note.setId(in.getId());
            return edit(note);
        }
    }

    public UserNoteQuestion add(UserNoteQuestion note){
        int result = insert(userNoteQuestionMapper, note);
        note = one(userNoteQuestionMapper, note.getId());
        if(note == null){
            throw new SystemException("笔记添加失败");
        }
        return note;
    }

    public UserNoteQuestion edit(UserNoteQuestion note){
        UserNoteQuestion in = one(userNoteQuestionMapper, note.getId());
        if(in == null){
            throw new ParameterException("笔记不存在");
        }
        int result = update(userNoteQuestionMapper, note);
        return note;
    }

    public boolean delete(Number id){
        UserNoteQuestion in = one(userNoteQuestionMapper, id);
        if(in == null){
            throw new ParameterException("笔记不存在");
        }
        int result = delete(userNoteQuestionMapper, id);
        return result > 0;
    }

    public UserNoteQuestion get(Number id){
        UserNoteQuestion in = one(userNoteQuestionMapper, id);

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

    public Page<UserNoteQuestion> select(int page, int pageSize){
        return select(userNoteQuestionMapper, page, pageSize);
    }

    public List<UserNoteQuestion> select(Collection ids){
        return select(userNoteQuestionMapper, ids);
    }

}
