package com.qxgmat.service.inline;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
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.dao.UserQuestionMapper;
import com.qxgmat.data.dao.UserReportMapper;
import com.qxgmat.data.dao.entity.UserPaper;
import com.qxgmat.data.dao.entity.UserQuestion;
import com.qxgmat.data.dao.entity.UserReport;
import com.qxgmat.data.relation.UserReportRelationMapper;
import com.qxgmat.data.relation.entity.UserHomeworkPreviewRelation;
import com.qxgmat.data.relation.entity.UserReportRelation;
import com.qxgmat.service.annotation.InitPaper;
import com.qxgmat.service.annotation.InitReport;
import com.qxgmat.util.shiro.UserRealm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

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

    @Resource
    private UserReportMapper userReportMapper;
    @Resource
    private UserReportRelationMapper userReportRelationMapper;

    /**
     * 获取所属组卷的所有报告
     * @param paperIds
     * @return
     */
    public Map<Object, Collection<UserReport>> mapByPaper(Collection paperIds){
        Map<Object, Collection<UserReport>> relationMap = new HashMap<>();
        if(paperIds.size() == 0) return relationMap;
        Example example = new Example(UserReport.class);
        example.and(
                example.createCriteria()
                        .andIn("paperId", paperIds)
        );
        List<UserReport> userClassList = select(userReportMapper, example);
        if(userClassList.size() == 0) return relationMap;
        for(UserReport row: userClassList){
            List<UserReport> l;
            Number paperId = row.getPaperId();
            if(!relationMap.containsKey(paperId)){
                l = new ArrayList<>();
                relationMap.put(paperId, l);
            }else{
                l = (List<UserReport>)relationMap.get(paperId);
            }
            l.add(row);
        }
        return relationMap;
    }

    /**
     * 根据用户paper生成一份新的report记录
     * @param paper
     * @param setting
     * @param IInitReport
     * @return
     */
    public UserReport addByPaper(UserPaper paper, JSONObject setting, InitReport IInitReport){
        UserReport report = UserReport.builder()
                .module(paper.getModule())
                .moduleId(paper.getModuleId())
                .setting(setting)
                .userId(paper.getUserId())
                .paperId(paper.getId())
                .questionNoIds(paper.getQuestionNoIds())
                .questionNumber(paper.getQuestionNoIds().length)
                .time(paper.getTime())
                .build();
        // 回调，根据模块更新设置
        IInitReport.callback(report, paper);
        return add(report);
    }

    /**
     * 累加做题记录到report
     * @param question
     */
    public void accumulation(UserQuestion question){
        userReportRelationMapper.accumulation(question.getReportId(), 1, question.getTime(), question.getIsCorrect());
    }

    public UserReport add(UserReport report){
        int result = insert(userReportMapper, report);
        report = one(userReportMapper, report.getId());
        if(report == null){
            throw new SystemException("用户报告添加失败");
        }
        return report;
    }

    public UserReport edit(UserReport report){
        UserReport in = one(userReportMapper, report.getId());
        if(in == null){
            throw new ParameterException("用户报告不存在");
        }
        int result = update(userReportMapper, report);
        return report;
    }

    public boolean delete(Number id){
        UserReport in = one(userReportMapper, id);
        if(in == null){
            throw new ParameterException("用户报告不存在");
        }
        int result = delete(userReportMapper, id);
        return result > 0;
    }

    public UserReport get(Number id){
        UserReport in = one(userReportMapper, id);

        if(in == null){
            throw new ParameterException("用户报告不存在");
        }
        return in;
    }

    public Page<UserReport> select(int page, int pageSize){
        return select(userReportMapper, page, pageSize);
    }

    public List<UserReport> select(Collection ids){
        return select(userReportMapper, ids);
    }

}
