package com.xhkjedu.sapi.service.itemtool;

import com.xhkjedu.sapi.listener.MessageSender;
import com.xhkjedu.sapi.mapper.itemtool.ReadMapper;
import com.xhkjedu.sapi.mapper.itemtool.ReadStudentMapper;
import com.xhkjedu.sapi.model.itemtool.TRead;
import com.xhkjedu.sapi.model.itemtool.TReadDate;
import com.xhkjedu.sapi.model.itemtool.TReadStudent;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.sapi.vo.itemtool.ReadDateVo;
import com.xhkjedu.sapi.vo.itemtool.ReadStudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @ClassName ReadService
 * Description TODO
 * Author WN
 * Date 2020/6/9 17:29
 **/
@Service
public class ReadService {
    @Autowired
    private ReadMapper readMapper;

    @Autowired
    private ReadStudentMapper readStudentMapper;
    @Autowired
    private MessageSender messageSender;
    @Resource
    private ReadDateService readDateService;

    /**
     * 功能描述  保存
     *
     * @param * @param read
     * @return java.lang.Integer
     * @author WN
     * @date 2020/6/9
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer save(TRead read) {
        read.setCreatetime(N_Utils.getSecondTimestamp());
        read.setReadcount(0);
        read.setReadstate(1);
        readMapper.insertUseGeneratedKeys(read);
        Integer readId = read.getId();
        saveReadDate(read, readId);
        return readId;
    }

    //保存阅读开放时间
    private void saveReadDate(TRead read, Integer readId) {
        List<ReadDateVo> dates = read.getDates();
        List<TReadDate> dateList = new ArrayList<>();
        for (ReadDateVo date : dates) {
            List<Integer> datenums = date.getDatenum();
            List<String> timestrs = date.getTimestr();
            String begintime = timestrs.get(0);
            String endtime = timestrs.get(1);
            for (Integer datenum : datenums) {
                TReadDate d = new TReadDate();
                d.setReadid(readId);
                d.setWeeknum(datenum);
                d.setBegintime(begintime);
                d.setEndtime(endtime);
                dateList.add(d);
            }
        }
        if (N_Utils.isListNotEmpty(dateList)) readDateService.insertList(dateList);
    }

    /**
     * 功能描述  修改
     *
     * @param * @param read
     * @return java.lang.Integer
     * @author WN
     * @date 2020/6/9
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer update(TRead read) {
        Integer readid = read.getReadid();
        readDateService.deleteByReadId(readid);
        saveReadDate(read, readid);
        return readMapper.updateByPrimaryKeySelective(read);
    }

    /**
     * 功能描述  删除
     *
     * @param * @param readid
     * @return java.lang.Integer
     * @author WN
     * @date 2020/6/9
     */
    public Integer del(Integer readid) {
        TRead read = readMapper.getReadPath(readid);//获取电子书文件
        Integer result = readMapper.deleteByPrimaryKey(readid);
        List<String> resourceUrls = new ArrayList<>();
        if (read != null && N_Utils.isNotEmpty(read.getReadthumb())) {
            resourceUrls.add(read.getReadthumb());
        }
        if (read != null && N_Utils.isNotEmpty(read.getReadpath())) {
            resourceUrls.add(read.getReadpath());
        }
        if(N_Utils.isNotEmpty(resourceUrls)){
            messageSender.resourceDel(resourceUrls);//给文件服务器发送mq删除文件
        }
        return result;
    }

    /**
     * 功能描述 所有阅读信息
     *
     * @param * @param schoolid
     * @return java.util.List<com.xhkjedu.model.itemtool.TRead>
     * @author WN
     * @date 2020/6/9
     */
    public List<TRead> listReadsForWeb(TRead read) {
        List<TRead> reads = readMapper.listReadsForWeb(read.getSchoolid(), read.getReadstate(), read.getReadname());
        readDateService.setReads(reads);//关联阅读开放时间
        return reads;
    }

    /**
     * 功能描述 获取阅读详情
     *
     * @param * @param readid
     * @return com.xhkjedu.model.itemtool.TRead
     * @author WN
     * @date 2020/6/17
     */
    public TRead getObjByReadId(Integer readid) {
        TRead read = readMapper.getObjByReadId(readid);
        readDateService.setRead(read);
        return read;
    }


    /**
     * 功能描述  阅读分页（android）
     *
     * @param * @param read
     * @return java.util.List<com.xhkjedu.model.itemtool.TRead>
     * @author WN
     * @date 2020/6/9
     */
    public List<TRead> listReadsForAndr(TRead read) {
        List<TRead> reads = readMapper.listReadsForAndr(read);
        readDateService.setReads(reads);//关联阅读开放时间
        return reads;
    }

    /**
     * 功能描述  阅读详情（android）
     *
     * @param * @param readid
     * @return com.xhkjedu.model.itemtool.TRead
     * @author WN
     * @date 2020/6/17
     */
    public TRead getObjByReandidForAndr(Integer readid) {
        //修改阅读次数
        readMapper.updateReadcount(readid);

        return readMapper.getObjByReadId(readid);
    }

    /**
     * 功能描述 阅读历史保存
     *
     * @param * @param readStudent
     * @return java.lang.Integer
     * @author WN
     * @date 2020/6/17
     */
    public Integer saveReadStudent(TReadStudent readStudent) {
        //修改阅读次数
        readMapper.updateReadcount(readStudent.getReadid());
        readStudent.setReadtime(N_Utils.getSecondTimestamp());
        return readStudentMapper.insertUseGeneratedKeys(readStudent);
    }

    /**
     * 功能描述  获取学生阅读历史
     *
     * @param * @param readStudent
     * @return java.util.List<com.xhkjedu.model.itemtool.TReadStudent>
     * @author WN
     * @date 2020/6/17
     */
    public List<ReadStudentVo> listReadStudents(TReadStudent readStudent) {
        Integer begintime = readStudent.getBegintime();
        Integer endtime = 0;
        if (begintime == 0) {//全部
            endtime = 0;
        } else if (begintime == 1) {//5分钟以内
            begintime = 0;
            endtime = 5 * 60;
        } else if (begintime == 2) {//5~10分钟
            begintime = 5 * 60;
            endtime = 10 * 60;
        } else if (begintime == 3) {//10~30分钟
            begintime = 10 * 60;
            endtime = 30 * 60;
        } else if (begintime == 4) {//10~1小时
            begintime = 10 * 60;
            endtime = 60 * 60;
        } else if (begintime == 5) {//1小时以上
            begintime = 60 * 60;
            endtime = 0;
        }
        return readMapper.listReadStudents(readStudent.getSchoolid(), readStudent.getReadid(), begintime, endtime);
    }

    /**
     * 修改上架状态
     *
     * @return java.lang.Integer
     * @Param [readid, readstate]
     * @Author ywx
     * @Date 2020/6/26 11:50
     **/
    public Integer updateState(Integer readid, Integer readstate) {
        return readMapper.updateState(readid, readstate);
    }

    /**
     * 定时任务修改阅读状态
     *
     * @return java.lang.Integer
     * @Param [readstate, readstate2]
     * @Author ywx
     * @Date 2020/9/1 9:52
     **/
    public Integer updateTaskState(int readstate, int readstate2) {
        return readMapper.updateTaskState(readstate, readstate2);
    }

    /**
     * 获取学校阅读历史
     *
     * @return java.util.List<com.xhkjedu.vo.itemtool.ReadStudentVo>
     * @Param [readStudent]
     * @Author ywx
     * @Date 2021/7/31 17:20
     **/
    public List<ReadStudentVo> listReadSchool(TReadStudent readStudent) {
        Integer begintime = readStudent.getBegintime();
        Integer endtime = 0;
        if (begintime == 0) {//全部
            endtime = 0;
        } else if (begintime == 1) {//5分钟以内
            begintime = 0;
            endtime = 5 * 60;
        } else if (begintime == 2) {//5~10分钟
            begintime = 5 * 60;
            endtime = 10 * 60;
        } else if (begintime == 3) {//10~30分钟
            begintime = 10 * 60;
            endtime = 30 * 60;
        } else if (begintime == 4) {//10~1小时
            begintime = 10 * 60;
            endtime = 60 * 60;
        } else if (begintime == 5) {//1小时以上
            begintime = 60 * 60;
            endtime = 0;
        }
        return readMapper.listReadSchool(readStudent.getReadid(), begintime, endtime);
    }

    /**
     * 功能描述  电子书学生--网页-列表
     *
     * @param * @param read
     * @return java.util.List<com.xhkjedu.sapi.model.itemtool.TRead>
     * @author WN
     * @date 2022/2/22
     */
    public List<TRead> listReadStuWeb(TRead read) {
        List<TRead> reads = readMapper.listReadForStuWeb(read);
        readDateService.setReads(reads);//关联阅读开放时间
        return reads;
    }

    /**
     * 获取电子书是否可读
     *
     * @return java.lang.Integer
     * @Param [read]
     * @Author ywx
     * @Date 2022/3/14 10:52
     **/
    public Integer getReadable(TRead read) {
        return readDateService.setRead(read);
    }
}
