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.notice.TNotice;
import com.xhkjedu.sapi.model.research.TResearch;
import com.xhkjedu.sapi.model.research.TResearchUser;
import com.xhkjedu.sapi.service.notice.NoticeService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.sapi.vo.notice.NoticeTeacherVo;
import com.xhkjedu.sapi.vo.research.ResearchVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName ResearchService
 * Description TODO
 * Author WN
 * Date 2020/6/11 18:06
 **/
@Slf4j
@Service
public class ResearchService {
    @Autowired
    private ResearchMapper researchMapper;
    @Autowired
    private ResearchUserMapper researchUserMapper;
    @Resource
    private NoticeService noticeService;
    @Autowired
    private MessageSender messageSender;

    /**
     * 功能描述  列表--所有教研
     *
     * @param *      @param schoolid
     * @param rorder
     * @return java.util.List<com.xhkjedu.vo.research.ResearchVo>
     * @author WN
     * @date 2020/6/11
     */
    public List<ResearchVo> listResearchs(Integer schoolid, String rorder) {
        return researchMapper.listResearchs(schoolid, rorder);
    }

    /**
     * 功能描述  保存信息
     *
     * @param * @param research
     * @return void
     * @author WN
     * @date 2020/6/11
     */
    public Integer save(TResearch research) {
        research.setCreatetime(N_Utils.getSecondTimestamp());
        research.setReadnum(0);

        researchMapper.insertUseGeneratedKeys(research);

        List<TResearchUser> rslist = new ArrayList<>();

        List<Integer> teacherids = research.getTeacherids();
        //集合去重
        List<Integer> userids = teacherids.stream().distinct().collect(Collectors.toList());
        for (Integer uid : userids) {
            TResearchUser ru = new TResearchUser();
            ru.setResearchid(research.getId());
            ru.setUserid(uid);
            ru.setRustate(0);
            ru.setSchoolid(research.getSchoolid());
            ru.setCreatetime(N_Utils.getSecondTimestamp());

            rslist.add(ru);
        }

        if( research.getNotice() == 1){//发送通知
            List<NoticeTeacherVo> teachers = new ArrayList<>();
            TNotice notice = new TNotice();
            notice.setNoticetitle(research.getResearchname());
            notice.setNoticecontent(research.getResearchcontent());
            notice.setNoticetxt(research.getResearchcontent());
            notice.setNoticetype(2);
            notice.setCreateid(research.getCreateid());
            notice.setSchoolid(research.getSchoolid());
            userids.forEach(uid->{
                NoticeTeacherVo nt = new NoticeTeacherVo();
                nt.setTeacherid(uid);
                teachers.add(nt);
            });
            notice.setTeachers(teachers);
            noticeService.save(notice);
        }

        //批量保存教研老师关联信息
        researchUserMapper.saveBatchResearchUser(rslist);
        research.setResearchid(research.getId());
        convertVideo(research);//视频转换mq
        return research.getId();
    }

    /**
     * 功能描述  修改教研信息
     *
     * @param * @param research
     * @return void
     * @author WN
     * @date 2020/6/11
     */
    public void update(TResearch research) {
        //researchMapper.updateByPrimaryKeySelective(research);
        researchMapper.updateResourch(research);

        /*List<TResearchUser> rslist = new ArrayList<>();
        List<Integer> userids = research.getTeacherids();
        for (Integer uid : userids) {
            TResearchUser ru = new TResearchUser();
            ru.setResearchid(research.getResearchid());
            ru.setUserid(uid);
            ru.setRustate(0);
            ru.setSchoolid(research.getSchoolid());
            ru.setCreatetime(N_Utils.getSecondTimestamp());
            rslist.add(ru);
        }

        //先删除教研用户
        researchUserMapper.delByResearchid(research.getResearchid());
        //批量保存教研老师关联信息
        researchUserMapper.saveBatchResearchUser(rslist);*/
    }

    /**
     * 功能描述 删除
     *
     * @param * @param researchid
     * @return java.lang.Integer
     * @author WN
     * @date 2020/6/11
     */
    public Integer del(Integer researchid) {
        List<String> resourceUrls = researchMapper.listFolder(researchid);//获取教研文件夹
        messageSender.resourceDel(resourceUrls);//给文件服务器发送mq删除文件
        return researchMapper.deleteByPrimaryKey(researchid);
    }

    /**
     *功能描述  详情
     * @author WN
     * @date 2020/6/11
     * @param  * @param researchid
     * @return com.xhkjedu.model.research.TResearch
     */
    public TResearch getResearchById(Integer researchid) {
        List<TResearchUser> teachers = researchUserMapper.listRuserIdByResearchid(researchid);
        TResearch research = researchMapper.getResearchById(researchid);
        research.setTeachers(teachers);
        convertVideo(research);//视频转换mq
        return research;
    }

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

    /**
     *功能描述  修改教研mp4code
     * @author WN
     * @date 2020/8/25
     * @param  * @param research
     * @return java.lang.Integer
     */
    public Integer updateResourceMp4code(TResearch research){
        return researchMapper.updateResourceMp4code(research);
    }
}
