package com.xhkjedu.slive.service.liveplay;

import com.xhkjedu.slive.config.ConfigKey;
import com.xhkjedu.slive.mapper.liveplay.CourseSectionMapper;
import com.xhkjedu.slive.mapper.liveplay.CourseSectionStudentMapper;
import com.xhkjedu.slive.model.liveplay.LCourseSection;
import com.xhkjedu.slive.model.liveplay.LCourseSectionStudent;
import com.xhkjedu.slive.vo.liveplay.CSStudentVo;
import com.xhkjedu.slive.vo.liveplay.CourseVo;
import com.xhkjedu.utils.IpRegionUtil;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ywx
 * @classname CourseSectionStudentService
 * @description
 * @date 2021/1/15 12:34
 **/
@Service
public class CourseSectionStudentService {
    @Resource
    private CourseSectionStudentMapper courseSectionStudentMapper;
    @Resource
    private CourseSectionMapper courseSectionMapper;

    //添加
    public void save(LCourseSectionStudent model, HttpServletRequest request) {
        model.setCreatetime(N_Utils.getSecondTimestamp());
        if (model.getTotaltime() == null) {
            model.setTotaltime(0);
        }

        String ip = N_Utils.getIpAddress(request);
        String region = null;
        if(N_Utils.isNotEmpty(ip)){
            IpRegionUtil ipRegionUtil = new IpRegionUtil();
            region = ipRegionUtil.getIpForAddress(ip, ConfigKey.ipregiondb);
        }
        model.setLogip(ip);
        model.setLogaddress(region);
        courseSectionStudentMapper.save(model);
        courseSectionMapper.updateStuNum(model.getSectionid());
    }

    //更新最晚出课堂时间
    public Integer updateById(LCourseSectionStudent student) {
        return courseSectionStudentMapper.updateById(student);
    }

    //获取列表
    public List <CSStudentVo> listBySectionId(Integer sectionid) {
        return courseSectionStudentMapper.listBySectionId(sectionid);
    }

    //学生进入课节
    public ResultVo inSection(LCourseSectionStudent student,HttpServletRequest request) {
        CourseVo course = courseSectionMapper.getCourseBySectionId(student.getSectionid());
        if (course.getCoursestate() == 3 || course.getDeleted() == 10) return new ResultVo(1,"已结束");
        LCourseSectionStudent cs = courseSectionStudentMapper.getByStudent(student);//学生进入课节记录
        student.setOnlined(1);
        if (cs != null) {//有学生进入课节记录更新
            student.setCsid(cs.getCsid());
            courseSectionStudentMapper.updateByPrimaryKeySelective(student);
        } else {//没有学生进入课节记录添加
            student.setTostamp(N_Utils.getSecondTimestamp());
            student.setOutstamp(0);
            save(student,request);
        }
        return new ResultVo(0,"学生进入课节成功");
    }

    //学生退出课节
    public void outSection(LCourseSectionStudent student) {
        student.setOutstamp(N_Utils.getSecondTimestamp());
        updateById(student);
    }

    //修改学生上课时长
    public void updateTime(LCourseSectionStudent student) {
        LCourseSection section = courseSectionMapper.selectByPrimaryKey(student.getSectionid());
        //课节不是进行中的状态则不计算学生观看时长
        if (section.getSectionstate()!=2) return;
        courseSectionStudentMapper.updateTime(student);
    }
}
