package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dto.request.wrongbook.AddWrongBookStemRequest;
import com.example.demo.dto.request.wrongbook.WrongBookRequest;
import com.example.demo.dto.response.feign.FeignResponse;
import com.example.demo.dto.response.StemDto;
import com.example.demo.dto.response.WrongBookDto;
import com.example.demo.entity.WrongBook;
import com.example.demo.entity.WrongBookItem;
import com.example.demo.mapper.WrongBookItemMapper;
import com.example.demo.mapper.WrongBookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author hxh
 * @Date 2020-09-16 17:45
 * @Descrption TODO
 */
@Service
public class WrongBookService {

    @Autowired
    private FeignService feignService;
    @Autowired
    private WrongBookMapper wrongBookMapper;
    @Autowired
    private WrongBookItemMapper wrongBookItemMapper;

    public WrongBookDto getWrongBook(WrongBookRequest request){
        //条件查询，根据request获得数据库中相应试卷
        QueryWrapper<WrongBook> wrapper = new QueryWrapper<>();
        wrapper.eq("groupname",request.getGroupName())
                .eq("gradename",request.getGradeName())
                .eq("subject",request.getSubject())
                .eq("uid",request. getUid());
        WrongBook book = wrongBookMapper.selectOne(wrapper);
        WrongBookDto wrongBookDto = new WrongBookDto(book);

        //得到WrongBookItems
        QueryWrapper<WrongBookItem> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("groupname",request.getGroupName())
                .eq("gradename",request.getGradeName())
                .eq("subject",request.getSubject())
                .eq("uid",request. getUid());
        List<WrongBookItem> wrongBookItems = wrongBookItemMapper.selectList(wrapper2);

        //调用getWrongBookStems得到stems
        List<StemDto> stems = getWrongBookStems(wrongBookItems);
        wrongBookDto.setStems(stems);
        return wrongBookDto;
    }

    public List<StemDto> getWrongBookStems(List<WrongBookItem> wrongBookItems){
        //用feign获取题目信息（根据题目id到老师接口调用）
        List<StemDto> stems = new LinkedList<>();
        for(WrongBookItem item : wrongBookItems){
            FeignResponse response = feignService.getResponse(item.getSid());
            if(response.getCode().compareTo("200")==0){
                StemDto stem = response.getData();
                stem.setAns();
                stems.add(stem);
            }
        }
        return stems;
    }

    public boolean add(AddWrongBookStemRequest request){
        return wrongBookItemMapper.insert(new WrongBookItem(null,request.getSid(),request.getGroupName(),request.getGradeName(),request.getSubject(),request.getUid()))==0?false:true;
    }

    public int count(WrongBookRequest request){
        QueryWrapper<WrongBookItem> wrapper = new QueryWrapper<>();
        wrapper.eq("groupname",request.getGroupName())
                .eq("gradename",request.getGradeName())
                .eq("subject",request.getSubject())
                .eq("uid",request. getUid());
        return wrongBookItemMapper.selectCount(wrapper);
    }

}
