package com.xhkjedu.sstudy.controller.resource;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.sstudy.model.resource.TResource;
import com.xhkjedu.sstudy.model.resource.TResourceCollect;
import com.xhkjedu.sstudy.model.subjectbook.TDirector;
import com.xhkjedu.sstudy.service.resource.ResourceService;
import com.xhkjedu.sstudy.service.subjectbook.DirectorService;
import com.xhkjedu.sstudy.vo.resource.ResourceCollectVo;
import com.xhkjedu.sstudy.vo.resource.ResourceVo;
import com.xhkjedu.sstudy.vo.system.CollectVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import org.springframework.util.CollectionUtils;
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 javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ywx
 * @classname ResourceController
 * @description 
 * @date 2020/6/5 9:47
 **/
@RestController
@RequestMapping("/resource")
public class ResourceController {
    @Resource
    private ResourceService resourceService;
    @Resource
    private DirectorService directorService;

    @PostMapping("/add")
    @Action("资源管理-增加")
    public ResultVo add(@RequestBody TResource tResource) {
        if (tResource.getLevel()== 3 && CollectionUtils.isEmpty(tResource.getGuideGroups())){
            return new ResultVo(1,"导学案班级集合不能为空");
        }
        String id = resourceService.save(tResource);
        return new ResultVo(0, "保存成功",id);
    }

    @PostMapping("/batch_add")
    @Action("资源管理-批量增加")
    public ResultVo batchAdd(@RequestBody List<TResource> resources) {
        resourceService.batchAdd(resources);
        return new ResultVo(0, "批量保存成功");
    }

    @PostMapping("/delete")
    @Action("资源管理-删除")
    public ResultVo delete(@RequestBody TResource tResource) {
        String resourceid = tResource.getResourceid();
        N_Utils.validation(new Object[]{resourceid,"资源id",2});
        resourceService.deleteById(resourceid);
        return new ResultVo(0,"删除成功");
    }

    @PostMapping("/update")
    @Action("资源管理-修改")
    public ResultVo update(@RequestBody TResource tResource) {
        String resourceid = tResource.getResourceid();
        N_Utils.validation(new Object[]{resourceid, "资源id", 2});
        tResource.setCreatetime(N_Utils.getSecondTimestamp());
        resourceService.update(tResource);
        return new ResultVo(0, "修改成功");
    }

    @PostMapping("/detail")
    public ResultVo detail(@RequestBody TResource tResource) {
        String resourceid = tResource.getResourceid();
        N_Utils.validation(new Object[]{resourceid,"资源id",2});
        ResourceVo resource = resourceService.findById(resourceid);
        return new ResultVo(0,"获取详情成功",resource);
    }

    @PostMapping("/get_resourceurl")
    public ResultVo getResourceUrl(@RequestBody TResource tResource) {
        String resourceid = tResource.getResourceid();
        N_Utils.validation(new Object[]{resourceid,"资源id",2});
        String resourceurl = resourceService.getResourceUrl(resourceid);
        return new ResultVo(0,"获取预览地址成功",resourceurl);
    }

    @PostMapping("/list")
    public ResultVo list(@RequestBody TResource tResource) {
        Integer page = tResource.getPage();
        Integer pageSize = tResource.getPageSize();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1});

        //获取所选章节信息
        if(N_Utils.isNotEmpty(tResource.getDirectorid()) && !"0".equals(tResource.getDirectorid())){
            TDirector director = directorService.getDirectorById(tResource.getDirectorid());
            tResource.setDirectorLeaf(director.getLeaf());
            //leaf 是否是叶子节点0不是1是
            if(director.getLeaf() == 0){
                //说明不是叶子节点，需要获取其子章节所有信息
                tResource.setDirectorall(director.getDirectorall());
            }
        }

        PageHelper.startPage(page, pageSize);
        List<Map> list = resourceService.findAll(tResource);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取列表成功",pageResult);
    }

    @PostMapping("/share_school")
    @Action("资源管理-分享校本")
    public ResultVo shareSchool(@RequestBody TResource tResource) {
        chekShareParam(tResource);
        String id = resourceService.share(tResource);
        if(id.equals("99")){
            return new ResultVo(0,"该资源已分享过校本");
        }else{
            return new ResultVo(0,"分享成功",id);
        }
    }

    /**
     * @Description 分享伙伴校(我的空间分享)
     * @Param [tResource]
     * @Author ywx
     * @Date 2023/7/21 9:34
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/share_ps")
    @Action("资源管理-分享伙伴校")
    public ResultVo sharePartnerSchool(@RequestBody TResource tResource) {
        chekShareParam(tResource);
        return resourceService.sharePartnerSchool(tResource);
    }

    //校验分享参数
    private void chekShareParam(@RequestBody TResource tResource) {
        String resourceid = tResource.getResourceid();
        String lsbid = tResource.getLsbid();
        String directorid = tResource.getDirectorid();
        Integer resourcetype = tResource.getResourcetype();
        Integer createid = tResource.getCreateid();
        N_Utils.validation(new Object[]{resourceid, "资源id", 2, lsbid, "教材关系表id", 2
                , directorid, "章节id", 2, resourcetype, "资源归类", 1, createid, "教师id", 1});
        tResource.setLevel(1);//资源级别1校本
    }

    /**
     * @Description 获取分享的伙伴校
     * @Date 2023/8/29 14:48
     * @Author YWX
     * @Param [tResource]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_share_ps")
    public ResultVo listSharePartnerSchool(@RequestBody TResource tResource) {
        String resourceid = tResource.getResourceid();
        N_Utils.validation(new Object[]{resourceid, "资源id", 2});
        List<Map<String, Object>> list=resourceService.listSharePartnerSchool(resourceid);
        return new ResultVo(0,"获取分享的伙伴校成功",list);
    }

    /**
     * @Description 分享集团校（校本资源模块分享）
     * @Date 2023/8/29 16:11
     * @Author YWX
     * @Param [tResource]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/share_gs")
    public ResultVo shareGroupSchool(@RequestBody TResource tResource) {
        N_Utils.validation(new Object[]{tResource.getResourceid(), "资源id", 2, tResource.getCreateid(), "用户id", 1});
        return resourceService.shareGroupSchool(tResource);
    }

    @PostMapping("/update_downcount")
    public ResultVo updateDownCount(@RequestBody TResource tResource) {
        String resourceid = tResource.getResourceid();
        N_Utils.validation(new Object[]{resourceid,"资源id",2});
        resourceService.updateDownCount(resourceid);
        return new ResultVo(0,"更新下载次数成功");
    }

    /**
     * @Description 我的空间（网页、app端）
     * @Date 2023/9/1 15:39
     * @Author YWX
     * @Param [tResource]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/my_list")
    public ResultVo myList(@RequestBody TResource tResource) {
        Integer page = tResource.getPage();
        Integer pageSize = tResource.getPageSize();
        Integer createid = tResource.getCreateid();
        Integer level = tResource.getLevel();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,createid,"教师id",1
                ,level,"资源级别",1});

        //获取所选章节信息
        if(N_Utils.isNotEmpty(tResource.getDirectorid()) && !"0".equals(tResource.getDirectorid())){
            TDirector director = directorService.getDirectorById(tResource.getDirectorid());
            tResource.setDirectorLeaf(director.getLeaf());
            //leaf 是否是叶子节点0不是1是
            if(director.getLeaf() == 0){
                //说明不是叶子节点，需要获取其子章节所有信息
                tResource.setDirectorall(director.getDirectorall());
            }
        }

        PageHelper.startPage(page, pageSize);
        List<Map> list = resourceService.listMyResource(tResource);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取教师备课资源列表成功",pageResult);
    }

    /**
     * @Description 我的空间（课堂端）
     * @Date 2023/9/1 15:38
     * @Author YWX
     * @Param [tResource]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/my_list2")
    public ResultVo myList2(@RequestBody TResource tResource) {
        Integer createid = tResource.getCreateid();
        N_Utils.validation(new Object[]{createid,"教师id",1});
        tResource.setLevel(2);
        //获取所选章节信息
        if(N_Utils.isNotEmpty(tResource.getDirectorid()) && !"0".equals(tResource.getDirectorid())){
            TDirector director = directorService.getDirectorById(tResource.getDirectorid());
            tResource.setDirectorLeaf(director.getLeaf());
            //leaf 是否是叶子节点0不是1是
            if(director.getLeaf() == 0){
                //说明不是叶子节点，需要获取其子章节所有信息
                tResource.setDirectorall(director.getDirectorall());
            }
        }

        List<Map> list = resourceService.listMyResource(tResource);
        return new ResultVo(0,"获取我的备课列表成功",list);
    }

    @PostMapping("/student_video_list")
    public ResultVo studentVideoList(@RequestBody TResource tResource) {
        Integer page = tResource.getPage();
        Integer pageSize = tResource.getPageSize();
        Integer schoolid = tResource.getSchoolid();
        String subjectid = tResource.getSubjectid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,schoolid,"学校id",1,subjectid,"科目id",2});
        //获取所选章节信息
        if(N_Utils.isNotEmpty(tResource.getDirectorid()) && !"0".equals(tResource.getDirectorid())){
            TDirector director = directorService.getDirectorById(tResource.getDirectorid());
            tResource.setDirectorLeaf(director.getLeaf());
            //leaf 是否是叶子节点0不是1是
            if(director.getLeaf() == 0){
                //说明不是叶子节点，需要获取其子章节所有信息
                tResource.setDirectorall(director.getDirectorall());
            }
        }
        tResource.setPage((page - 1)*pageSize);
        List<TResource> list = resourceService.studentVideoList(tResource);
        return new ResultVo(0,"获取学生同步视频列表成功",list);
    }

    @PostMapping("/update_readcount")
    public ResultVo updateReadCount(@RequestBody TResource tResource) {
        String resourceid = tResource.getResourceid();
        N_Utils.validation(new Object[]{resourceid,"资源id",2});
        resourceService.updateReadCount(resourceid);
        return new ResultVo(0,"更新阅读次数成功");
    }

    @PostMapping("/save_collect")
    public ResultVo saveCollect(@RequestBody TResourceCollect collect) {
        N_Utils.validation(new Object[]{collect.getResourceid(),"资源id",2,collect.getUserid(),"用户id",1});
        resourceService.saveCollect(collect);
        return new ResultVo(0,"资源收藏成功");
    }

    @PostMapping("/cancel_collect")
    public ResultVo cancelCollect(@RequestBody TResourceCollect collect) {
        N_Utils.validation(new Object[]{collect.getResourceid(),"资源id",2,collect.getUserid(),"用户id",1});
        resourceService.cancelCollect(collect);
        return new ResultVo(0,"资源取消收藏成功");
    }

    @PostMapping("/list_collect")
    public ResultVo listCollect(@RequestBody CollectVo collect) {
        List<String> resourceids = collect.getIds();
        Integer userid = collect.getUserid();
        if (N_Utils.isListEmpty(resourceids)){
            return new ResultVo(1,"资源id集合不能为空");
        }
        N_Utils.validation(new Object[]{userid,"用户id",1});
        List<ResourceCollectVo> list = resourceService.listCollect(resourceids, userid);
        return new ResultVo(0,"获取资源是否收藏列表成功",list);
    }

    @PostMapping("/collected")
    public ResultVo isCollected(@RequestBody TResourceCollect collect) {
        Integer userid = collect.getUserid();
        String resourceid = collect.getResourceid();
        N_Utils.validation(new Object[]{userid,"用户id",1,resourceid,"资源id",2});
        Integer collected = resourceService.isCollected(resourceid, userid);
        return new ResultVo(0,"获取资源是否收藏成功",collected);
    }

    //资源保存转pdf
    @PostMapping("/convert")
    public ResultVo convert(@RequestBody TResource tResource) {
        String resourceid = tResource.getResourceid();
        String pdfurl = tResource.getPdfurl();
        N_Utils.validation(new Object[]{resourceid,"资源id",2,pdfurl,"转换后的pdf文件地址",2});
        resourceService.convert(resourceid, pdfurl);
        return new ResultVo(0,"转换pdf成功");
    }

    /**
     *功能描述 学生网页端同步视频列表
     * @author WN
     * @date 2022/2/22
     * @param  * @param resource
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/sw_video")
    public ResultVo studentVideoListStuWeb(@RequestBody TResource resource) {
        Integer page = resource.getPage();
        Integer pageSize = resource.getPageSize();
        Integer schoolid = resource.getSchoolid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,schoolid,"学校id",1});
        //获取所选章节信息
        if(N_Utils.isNotEmpty(resource.getDirectorid()) && !"0".equals(resource.getDirectorid())){
            TDirector director = directorService.getDirectorById(resource.getDirectorid());
            resource.setDirectorLeaf(director.getLeaf());
            //leaf 是否是叶子节点0不是1是
            if(director.getLeaf() == 0){
                //说明不是叶子节点，需要获取其子章节所有信息
                resource.setDirectorall(director.getDirectorall());
            }
        }
        PageHelper.startPage(page, pageSize);
        List<TResource> list = resourceService.studentVideoWebList(resource);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取学生同步视频列表成功",pageResult);
    }
}
