package com.xhkjedu.sstudy.service.question;

import com.xhkjedu.sstudy.mapper.question.QuestionMapper;
import com.xhkjedu.sstudy.mapper.question.UserBasketMapper;
import com.xhkjedu.sstudy.mapper.subjectbook.SubjectMapper;
import com.xhkjedu.sstudy.model.question.TUserBasket;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.sstudy.vo.question.QuestionVo;
import com.xhkjedu.sstudy.vo.question.UserBasketVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ywx
 * @classname UserBasketService
 * @description 
 * @date 2020/6/9 10:49
 **/
@Service
public class UserBasketService {
    @Resource
    private UserBasketMapper userBasketMapper;
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private SubjectMapper subjectMapper;

    //添加
    public Integer save(TUserBasket model) {
        Integer ubid = userBasketMapper.getUbidByUserIdQuestionId(model.getUserid(),model.getQuestionid());
        if (N_Utils.isEmptyInteger(ubid)) {
            Integer ctype = userBasketMapper.getCtypeByQtypeid(model.getQtypeid());
            model.setCtype(ctype);
            model.setCreatetime(N_Utils.getSecondTimestamp());
            userBasketMapper.insertUseGeneratedKeys(model);
            questionMapper.updateCount(model.getQuestionid());//更新组题次数
            ubid = model.getId();
        }
        return ubid;
    }

    //删除
    public Integer deleteById(Integer userid,String questionid) {
       // return userBasketMapper.deleteByPrimaryKey(ubid);
        return userBasketMapper.deleteByQuestioniduid(userid,questionid);
    }

    /**
     * 清空试题篮
     * @Param [userid, subjectid]
     * @Author ywx
     * @Date 2020/6/9 10:54
     * @return java.lang.Integer
     **/
    public Integer deleteBySubjectId(Integer userid, String subjectid) {
        return userBasketMapper.deleteBySubjectId(userid, subjectid);
    }

    //获取列表
    public List <UserBasketVo> findAll(Integer userid, String subjectid) {
        List<UserBasketVo> baskets = userBasketMapper.findAll(userid, subjectid);
        List<String> questionids = new ArrayList<>();
        for (UserBasketVo basket : baskets) {
            questionids.addAll(basket.getQuestions().stream().filter(b -> b.getQlevel().equals(2)).map(
                    QuestionVo::getQuestionid).collect(Collectors.toList()));
        }
        if (N_Utils.isListNotEmpty(questionids)) {
            List<Map<String, String>> sonList = questionMapper.listSonQuestionForSelect(String.join("','", questionids));
            for (UserBasketVo basket : baskets) {
                for (QuestionVo question : basket.getQuestions()) {
                    if (question.getQlevel().equals(2)) {
                        String questionid = question.getQuestionid();
                        List<Map> sonqlist = sonList.stream().filter(
                                s -> s.get("questionpid").equals(questionid)).collect(Collectors.toList());
                        question.setSonqlist(sonqlist);
                    }
                }
            }
        }
        String sujectname = subjectMapper.getSubjectnameById(subjectid);
        //英语学科时处理听力题
        if(sujectname.contains("英语")){
            List<QuestionVo> hearqs  = new ArrayList<>();
            List<UserBasketVo> rtnbaskets = new ArrayList<>();
            for (UserBasketVo b : baskets) {
                List<QuestionVo> questions = b.getQuestions();

                List<QuestionVo> tllist = questions.stream().filter( o -> o.getHashear() ==1).collect(Collectors.toList());
                if(N_Utils.isListNotEmpty(tllist)){
                    hearqs.addAll(tllist);

                    questions = questions.stream().filter( o -> o.getHashear() ==0).collect(Collectors.toList());
                }

                if(questions.size() > 0){
                    UserBasketVo bn = new UserBasketVo();
                    bn.setQtypeid(b.getQtypeid());
                    bn.setQtypename(b.getQtypename());
                    bn.setPtnum(questions.size());
                    bn.setCtype(b.getCtype());
                    Double ptscore = questions.stream().collect(Collectors.summingDouble(QuestionVo::getScore));
                    bn.setPtscore(ptscore);
                    bn.setQuestions(questions);
                    rtnbaskets.add(bn);
                }
            }
            baskets = new ArrayList<>(rtnbaskets);

            if(N_Utils.isListNotEmpty(hearqs)){
                UserBasketVo tlvo = new UserBasketVo();
                tlvo.setQtypeid("tlt");
                tlvo.setQtypename("听力题");
                tlvo.setCtype(12);
                tlvo.setPtnum(hearqs.size());
                Double scoretotal = hearqs.stream().collect(Collectors.summingDouble(QuestionVo::getScore));
                tlvo.setPtscore(scoretotal);
                tlvo.setQuestions(hearqs);
                baskets.add(0,tlvo);
            }
        }else{
            for (UserBasketVo b : baskets) {
                List<QuestionVo> questions = b.getQuestions();
                b.setPtnum(questions.size());
                Double scoretotal = questions.stream().collect(Collectors.summingDouble(QuestionVo::getScore));
                b.setPtscore(scoretotal);
            }
        }
        return baskets;
    }

    /**
     * 删除题型下指定试题
     * @Param [userid, qtypeid]
     * @Author ywx
     * @Date 2020/6/9 11:06
     * @return java.lang.Integer
     **/
    public Integer deleteByTypeId(Integer userid, String qtypeid) {
        if(qtypeid.equals("tlt")){
            //删除用户试题栏中听力题（试题标识为听力文件的试题）
           return userBasketMapper.deleteTlt(userid);
        }else{
           return userBasketMapper.deleteByTypeId(userid, qtypeid);
        }
    }
}
