package com.xhkjedu.sapi.controller.itemtool;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.sapi.model.itemtool.TRead;
import com.xhkjedu.sapi.model.itemtool.TReadStudent;
import com.xhkjedu.sapi.service.itemtool.ReadService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.sapi.vo.itemtool.ReadStudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName ReadController
 * Description TODO 电子书
 * Author WN
 * Date 2020/6/9 18:09
 **/
@RestController
@RequestMapping("/read")
public class ReadController {
    @Autowired
    private ReadService readService;

    /**
     *功能描述  保存
     * @author WN
     * @date 2020/6/9
     * @param  * @param read
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/save")
    @Action("电子书-增加")
    public ResultVo save(@RequestBody TRead read){
        N_Utils.validation(new Object[]{read.getReadname(),"阅读标题",2,
                read.getReadpath(),"阅读文件",2});
        Integer result = readService.save(read);
        if(result>0){
            return new ResultVo(0,"保存成功");
        }else{
            return new ResultVo(1,"保存失败");
        }
    }

    /**
     *功能描述  修改
     * @author WN
     * @date 2020/6/9
     * @param  * @param read
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/up")
    @Action("电子书-修改")
    public ResultVo update(@RequestBody TRead read){
        N_Utils.validation(new Object[]{
                read.getReadid(),"阅读id",1,
                read.getReadname(),"阅读标题",2});
        Integer result = readService.update(read);
        if(result>0){
            return new ResultVo(0,"修改成功");
        }else{
            return new ResultVo(1,"修改失败");
        }
    }

    /**
     *功能描述 删除
     * @author WN
     * @date 2020/6/9
     * @param  * @param read
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/del")
    @Action("电子书-删除")
    public ResultVo del(@RequestBody TRead read){
        N_Utils.validation(new Object[]{read.getReadid(),"阅读id",1});
        Integer result = readService.del(read.getReadid());
        if(result>0){
            return new ResultVo(0,"删除成功");
        }else{
            return new ResultVo(1,"删除失败");
        }
    }

    /**
     *功能描述  分页列表（网页）
     * @author WN
     * @date 2020/6/9
     * @param  * @param read
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/list")
    public ResultVo list(@RequestBody TRead read){
        Integer page = read.getPage();
        Integer pageSize = read.getPageSize();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1});
        PageHelper.startPage(page, pageSize);
        List<TRead> list = readService.listReadsForWeb(read);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取列表成功",pageResult);
    }

    /**
     *功能描述  详情--网页
     * @author WN
     * @date 2020/6/17
     * @param  * @param read
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/detail")
    public ResultVo detailWeb(@RequestBody TRead read){
        N_Utils.validation(new Object[]{read.getReadid(),"阅读id",1});
        TRead readrtn = readService.getObjByReadId(read.getReadid());
        return new ResultVo(0,"获取阅读详情成功",readrtn);
    }

    /**
     *功能描述 学生阅读历史
     * @author WN
     * @date 2020/6/17
     * @param  * @param readStudent
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/readany")
    public ResultVo readStudents(@RequestBody TReadStudent readStudent){
        N_Utils.validation(new Object[]{readStudent.getReadid(),"阅读id",1,readStudent.getSchoolid(),"学校id",1});
        List<ReadStudentVo> lst = readService.listReadStudents(readStudent);
        return new ResultVo(0,"获取学生阅读历史成功",lst);
    }

    @PostMapping("/readsany")
    public ResultVo readSchool(@RequestBody TReadStudent readStudent){
        N_Utils.validation(new Object[]{readStudent.getReadid(),"阅读id",1});
        List<ReadStudentVo> lst = readService.listReadSchool(readStudent);
        return new ResultVo(0,"获取学校阅读历史成功",lst);
    }



    /**
     *功能描述 获取分页列表成功（android）
     * @author WN
     * @date 2020/6/9
     * @param  * @param read
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/lstandr")
    public ResultVo listAndroid(@RequestBody TRead read){
        Integer page = read.getPage();
        Integer pageSize = read.getPageSize();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1});
        read.setPage((page-1)*pageSize);

        List<TRead> list = readService.listReadsForAndr(read);
        return new ResultVo(0,"获取列表成功",list);
    }

    /**
     *功能描述 查看详情（android）
     * @author WN
     * @date 2020/6/17
     * @param  * @param read
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/detailandr")
    public ResultVo detail(@RequestBody TRead read){
        N_Utils.validation(new Object[]{read.getReadid(),"阅读id",1});
        TRead readrtn = readService.getObjByReandidForAndr(read.getReadid());
        return new ResultVo(0,"获取阅读详情成功",readrtn);
    }

    /**
     *功能描述  保存学生阅读记录
     * @author WN
     * @date 2020/6/17
     * @param  * @param readStudent
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/savers")
    public ResultVo saveReadStudent(@RequestBody TReadStudent readStudent){
        N_Utils.validation(new Object[]{
                readStudent.getReadid(),"阅读id",1,
                readStudent.getStudentid(),"学生id",1,
                readStudent.getCosttime(),"阅读时长",1});
        Integer result = readService.saveReadStudent(readStudent);
        if(result>0){
            return new ResultVo(0,"保存成功");
        }else{
            return new ResultVo(1,"保存失败");
        }
    }

    @PostMapping("/update_state")
    @Action("电子书-修改状态")
    public ResultVo updateState(@RequestBody TRead read){
        Integer readid = read.getReadid();
        Integer readstate = read.getReadstate();
        N_Utils.validation(new Object[]{readid,"阅读id",1,readstate,"状态",1});
        readService.updateState(readid, readstate);
        return new ResultVo(0,"修改成功");
    }

    /**
     *功能描述 电子书学生--网页--列表
     * @author WN
     * @date 2022/2/22
     * @param  * @param read
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/stw_read")
    public ResultVo listRead(@RequestBody TRead read){
        Integer page = read.getPage();
        Integer pageSize = read.getPageSize();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1});
        PageHelper.startPage(page, pageSize);
        List<TRead> list = readService.listReadStuWeb(read);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取电子书列表成功",pageResult);
    }

    @PostMapping("/get_readable")
    public ResultVo getReadable(@RequestBody TRead read) {
        N_Utils.validation(new Object[]{read.getReadid(), "阅读id", 1});
        Integer readable = readService.getReadable(read);
        return new ResultVo(0, "获取电子书是否可读成功", readable);
    }
}
