package com.xhkjedu.sapi.service.research;

import com.xhkjedu.sapi.listener.MessageSender;
import com.xhkjedu.sapi.mapper.research.ResearchMapper;
import com.xhkjedu.sapi.mapper.research.ResearchUserMapper;
import com.xhkjedu.sapi.model.research.TResearch;
import com.xhkjedu.sapi.model.research.TResearchUser;
import com.xhkjedu.sapi.vo.research.CompareUserVo;
import com.xhkjedu.sapi.vo.research.ResearchUserVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName ResearchUserService
 * Description TODO
 * Author WN
 * Date 2020/6/11 19:34
 **/
@Service
public class ResearchUserService {
    @Autowired
    private ResearchUserMapper researchUserMapper;
    @Autowired
    private ResearchMapper researchMapper;
    @Autowired
    private MessageSender messageSender;

    /**
     * 功能描述  参与评论集合
     *
     * @param * @param researchid
     * @return java.util.List<com.xhkjedu.model.research.TResearchUser>
     * @author WN
     * @date 2020/6/11
     */
    public List<Map> listRusersByReaearchid(Integer researchid, Integer schoolid) {
        return researchUserMapper.listRusersByReaearchid(researchid, schoolid);
    }

    /**
     * 功能描述 教师--参与教研集合
     *
     * @param *      @param userid
     * @param rorder
     * @return java.util.List<com.xhkjedu.vo.research.ResearchVo>
     * @author WN
     * @date 2020/6/12
     */
    public List<Map> listUserResearchs(Integer userid, String rorder, Integer rustate) {
        return researchUserMapper.listUserResearchs(userid, rorder, rustate, N_Utils.getSecondTimestamp());
    }

    /**
     * 功能描述 教师--获取教研详情
     *
     * @param * @param researchid
     * @return com.xhkjedu.model.research.TResearch
     * @author WN
     * @date 2020/6/12
     */
    public ResearchUserVo getResearchUserDetail(Integer researchid, Integer userid) {
        ResearchUserVo researchUserVo = researchUserMapper.getResearchUserDetail(researchid, userid);
        // 更改阅读次数
        researchMapper.updateReadnum(researchid);

        // 视频转换mq
        if (!"h264".equals(researchUserVo.getMp4code())) {
            messageSender.convertVideo(researchUserVo.getResearchid(), researchUserVo.getResearchpath());
        }

        int publiced = researchUserVo.getPubliced();
        // 如果教研是公开的，则获取所有已提交教研publiced：是否公开0不公开1校内公开2全部公开
        if (publiced > 0) {
            List<Map> schools = researchUserMapper.listReaearchSchools(researchid,publiced, researchUserVo.getSchoolid());
            researchUserVo.setSchools(schools);
        }

        return researchUserVo;
    }

    /**
     * 功能描述 教师--保存教研评价
     *
     * @param * @param ru
     * @return java.lang.Integer
     * @author WN
     * @date 2020/6/12
     */
    public Integer updateUserResearch(TResearchUser ru) {
        ru.setRustate(1);// 状态0未评1已评
        ru.setReviewtime(N_Utils.getSecondTimestamp());
        return researchUserMapper.updateUserResearch(ru);
    }

    /**
     * 其他教研活动列表
     *
     * @return java.util.List<com.xhkjedu.vo.research.ResearchVo>
     * @Param [research]
     * @Author ywx
     * @Date 2020/6/23 14:24
     **/
    public List<Map> otherList(TResearchUser research) {
        research.setCreatetime(N_Utils.getSecondTimestamp());
        return researchUserMapper.otherList(research);
    }

    /**
     * 教师未评价教研数量
     *
     * @return java.lang.Integer
     * @Param [userid]
     * @Author ywx
     * @Date 2020/7/24 10:45
     **/
    public Integer noEvaluatedNum(Integer userid) {
        return researchUserMapper.noEvaluatedNum(userid, N_Utils.getSecondTimestamp());
    }

    //判断修改1教研是否为自己发布的2在截至时间之前
    private CompareUserVo getReachStoptimeCompareNow(Integer ruid, Integer userid) {
        CompareUserVo compareUserVo = new CompareUserVo();

        // 获取教研截至时间和教师id
        TResearch research = researchUserMapper.getResearchStoptime(ruid);

        //先获取当前登录用户和要修改的用户是否为同一个
        if(userid.equals(research.getUserid())){
            compareUserVo.setSameuser(1);
        }else{
            compareUserVo.setSameuser(0);
        }

        Integer nowtime = N_Utils.getSecondTimestamp();// 当前时间
        Integer stoptime = research.getStoptime();//教研截至时间
        int compresult = nowtime.compareTo(stoptime);
        if (compresult == -1 || compresult == 0) {
            compareUserVo.setComparetime(1);
        }else {
            compareUserVo.setComparetime(0);
        }

        return compareUserVo;
    }

    // 删除教研
    public ResultVo delTeacherResearch(TResearchUser ruser){
        CompareUserVo compareUserVo = getReachStoptimeCompareNow(ruser.getRuid(),ruser.getUserid());
        if(compareUserVo.getSameuser() != 1){
            return new ResultVo(1,"只能删除自己发布的教研");
        }

        if(compareUserVo.getComparetime() != 1){
            return new ResultVo(1,"教研已结束禁止操作");
        }
        Integer rtn = researchUserMapper.delTeacherResearch(ruser.getRuid());
        if(rtn > 0){
            return new ResultVo(0,"删除成功");
        }else {
            return new ResultVo(1,"删除失败");
        }
    }

    // 修改教研
    public ResultVo updateTeacherResearch(TResearchUser ruser)  {
        CompareUserVo compareUserVo = getReachStoptimeCompareNow(ruser.getRuid(),ruser.getUserid());
        if(compareUserVo.getSameuser() != 1){
            return new ResultVo(1,"只能编辑自己发布的教研");
        }

        if(compareUserVo.getComparetime() != 1){
            return new ResultVo(1,"教研已结束禁止操作");
        }
        ruser.setReviewtime(N_Utils.getSecondTimestamp());
        Integer rtn = researchUserMapper.updateTeacherResearch(ruser);
        if(rtn > 0){
            return new ResultVo(0,"保存成功",ruser);
        }else {
            return new ResultVo(1,"保存失败");
        }
    }

}
