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.module.PaperModule;
import com.qxgmat.data.constants.enums.status.PreviewStatus;
import com.qxgmat.data.dao.PreviewPaperMapper;
import com.qxgmat.data.dao.entity.PreviewPaper;
import com.qxgmat.data.dao.entity.UserPaper;
import com.qxgmat.data.dao.entity.UserReport;
import com.qxgmat.data.relation.UserPaperRelationMapper;
import com.qxgmat.data.relation.UserReportRelationMapper;
import com.qxgmat.data.relation.entity.QuestionNoRelation;
import com.qxgmat.data.relation.entity.UserPreviewPaperRelation;
import com.qxgmat.service.extend.ToolsService;
import com.qxgmat.service.inline.QuestionNoService;
import com.qxgmat.service.inline.UserReportService;
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.*;
import java.util.stream.Collectors;

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

    @Resource
    private PreviewPaperMapper previewPaperMapper;

    @Resource
    private QuestionNoService questionNoService;

    @Resource
    private UserReportService userReportService;

    @Resource
    private UserPaperService userPaperService;

    @Resource
    private UserReportRelationMapper userReportRelationMapper;

    @Resource
    private UserPaperRelationMapper userPaperRelationMapper;

    @Resource
    private ToolsService toolsService;

    // 完成一次
    public boolean finished(UserPaper userPaper){

        return true;
    }

    /**
     * 查找用户作业，并关联用户完成度最高的最后一次
     * @param page
     * @param size
     * @param category
     * @param userId
     * @param previewId
     * @param startTime
     * @param endTime
     * @return
     */
    public PageResult<UserPreviewPaperRelation> listAdmin(int page, int size, Number category, Number userId, Number previewId, String startTime, String endTime){
        Page<UserPaper> p = page(() -> {
            userPaperRelationMapper.listPreviewAdmin(category, userId, previewId, startTime, endTime);
        }, page, size);

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

        // 获取详细数据
        List<UserPaper> list = userPaperService.select(ids);
        List<UserPreviewPaperRelation> pr = Transform.convert(list, UserPreviewPaperRelation.class);

        // 获取完成度最高的作业结果
        List<UserReport> reportList = userReportRelationMapper.listFinishLast(ids, startTime, endTime);
        Collection reportIds = Transform.getIds(reportList, UserReport.class, "id");
        Transform.replace(reportList, userReportService.select(reportIds), UserReport.class, "id");

        Transform.combine(p, reportList, UserPreviewPaperRelation.class, "id", "report", UserReport.class, "paperId");

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

    /**
     * 查找用户作业, 并关联用户最后一次作业
     * @param page
     * @param size
     * @param category
     * @param userId
     * @param endTime
     * @param finish
     * @return
     */
    public PageResult<UserPreviewPaperRelation> list(int page, int size, Number category, Number userId, String endTime, Boolean finish){
        Page<UserPaper> p = page(()->{
            userPaperRelationMapper.listPreview(category, userId, endTime, finish);
        },page, size);

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

        // 获取详细数据
        List<UserPaper> list = userPaperService.select(ids);
        List<UserPreviewPaperRelation> pr = Transform.convert(list, UserPreviewPaperRelation.class);

        // 获取最后一次作业结果
        List<UserReport> reportList = userReportRelationMapper.listLast(ids);
        Collection reportIds = Transform.getIds(reportList, UserReport.class, "id");
        Transform.replace(reportList, userReportService.select(reportIds), UserReport.class, "id");

        Transform.combine(p, reportList, UserPreviewPaperRelation.class, "id", "report", UserReport.class, "paperId");

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

    /**
     * 获取用户分组作业列表，并关联用户最后一次作业
     * @param userId
     * @param top
     * @return
     */
    public Map<Object, Collection<UserPreviewPaperRelation>> groupByCategory(Number userId, Number top){
        List<UserPaper> p = userPaperRelationMapper.listPreviewGroupTop(userId, top);
        Map<Object, Collection<UserPreviewPaperRelation>> relationMap = new HashMap<>();

        if(p.size() == 0) return relationMap;

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

        // 获取详细数据
        List<UserPaper> list = userPaperService.select(ids);
        List<UserPreviewPaperRelation> pr = Transform.convert(list, UserPreviewPaperRelation.class);

        // 获取最后一次作业结果
        List<UserReport> reportList = userReportRelationMapper.listLast(ids);
        Collection reportIds = Transform.getIds(reportList, UserReport.class, "id");
        Transform.replace(reportList, userReportService.select(reportIds), UserReport.class, "id");

        Transform.combine(pr, reportList, UserPreviewPaperRelation.class, "id", "report", UserReport.class, "paperId");

        for(UserPreviewPaperRelation row: pr){
            List<UserPreviewPaperRelation> l;
            Number moduleId = row.getModuleId();
            if(!relationMap.containsKey(moduleId)){
                l = new ArrayList<>();
                relationMap.put(moduleId, l);
            }else{
                l = (List<UserPreviewPaperRelation>)relationMap.get(moduleId);
            }
            l.add(row);
        }
        return relationMap;
    }

    /**
     * 完成预习作业
     * @param report
     */
    public void Finish(UserReport report){

    }

    @Transactional
    public PreviewPaper add(PreviewPaper previewPaper){
        int result = insert(previewPaperMapper, previewPaper);
        previewPaper = one(previewPaperMapper, previewPaper.getId());
        if(previewPaper == null){
            throw new SystemException("预习作业添加失败");
        }
        Integer[] userIds = previewPaper.getUserIds();

        // 获取考题时间
        List<QuestionNoRelation> relationList = questionNoService.listWithRelationByIds(previewPaper.getQuestionNoIds());
        Integer time = toolsService.computerTime(relationList.toArray(new QuestionNoRelation[0]));
        for(Integer userId : userIds){
            userPaperService.add(UserPaper.builder()
                    .paperModule(PaperModule.PREVIEW.key)
                    .userId(userId)
                    .title(previewPaper.getTitle())
                    .moduleId(previewPaper.getId())
                    .questionNoIds(previewPaper.getQuestionNoIds())
                    .time(time)
                    .build()
            );
        }
        return previewPaper;
    }

    @Transactional
    public PreviewPaper edit(PreviewPaper previewPaper){
        PreviewPaper in = one(previewPaperMapper, previewPaper.getId());
        if(in == null){
            throw new ParameterException("预习作业不存在");
        }
        int result = update(previewPaperMapper, previewPaper);

        List<Integer> oldList = Arrays.stream(in.getUserIds()).collect(Collectors.toList());
        List<Integer> newList = Arrays.stream(previewPaper.getUserIds()).collect(Collectors.toList());

        // 移除旧用户试卷
        oldList.removeAll(newList);
        userPaperService.removeByUsersAndPaper(oldList, PaperModule.PREVIEW, previewPaper.getId());

        // 获取旧用户做为编辑试卷
        List<Integer> editList = Arrays.stream(in.getUserIds()).collect(Collectors.toList());
        editList.removeAll(oldList);

        // 获取考题时间
        List<QuestionNoRelation> relationList = questionNoService.listWithRelationByIds(previewPaper.getQuestionNoIds());
        Integer time = toolsService.computerTime(relationList.toArray(new QuestionNoRelation[0]));
        for(Integer userId : newList){
            if (!editList.contains(userId)){
                // 添加
                userPaperService.add(UserPaper.builder()
                        .paperModule(PaperModule.PREVIEW.key)
                        .userId(userId)
                        .title(previewPaper.getTitle())
                        .moduleId(previewPaper.getId())
                        .questionNoIds(previewPaper.getQuestionNoIds())
                        .time(time)
                        .build()
                );
                editList.remove(userId);
            }
        }
        // 修改
        userPaperService.editByUsersAndPaper(UserPaper.builder()
                .title(previewPaper.getTitle())
                .questionNoIds(previewPaper.getQuestionNoIds())
                .time(time)
                .build(),
                oldList,
                PaperModule.PREVIEW, previewPaper.getId());

        return previewPaper;
    }

    public boolean delete(Number id){
        PreviewPaper in = one(previewPaperMapper, id);
        if(in == null){
            throw new ParameterException("预习作业不存在");
        }
        int result = delete(previewPaperMapper, id);
        return result > 0;
    }

    public PreviewPaper get(Number id){
        PreviewPaper in = one(previewPaperMapper, id);

        if(in == null){
            throw new ParameterException("预习作业不存在");
        }
        return in;
    }

    public Page<PreviewPaper> select(int page, int pageSize){
        return select(previewPaperMapper, page, pageSize);
    }

    public Page<PreviewPaper> select(int page, int pageSize, Integer category, PreviewStatus status){
        Example example = new Example(PreviewPaper.class);
        if(category != null && category>0)
            example.and(
                    example.createCriteria().andEqualTo("category", category)
            );
        if (status!= PreviewStatus.ALL){
            tk.mybatis.mapper.entity.Example.Criteria criteria = example.createCriteria();
            switch(status){
                case NO_START:
                    criteria.andLessThan("startTime", new Date());
                    break;
                case PROCESS:
                    criteria.andGreaterThanOrEqualTo("startTime", new Date()).andLessThan("endTime", new Date());
                    break;
                case FINISH:
                    criteria.andGreaterThanOrEqualTo("endTime", new Date());
                    break;
            }
            example.and(criteria);
        }
        example.orderBy("id").desc();
        return select(previewPaperMapper, example, page, pageSize);
    }

    public List<PreviewPaper> select(Collection ids){
        return select(previewPaperMapper, ids);
    }

}
