package com.xhkjedu.sapi.controller.research;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.sapi.model.research.TResearch;
import com.xhkjedu.sapi.model.research.TResearchUser;
import com.xhkjedu.sapi.service.research.ResearchService;
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;

/**
 * @ClassName ResearchController
 * Description 教研
 * Author WN
 * Date 2020/6/11 19:01
 **/
@RestController
@RequestMapping("/research")
public class ResearchController {
    @Resource
    private ResearchService researchService;

   /**
    *功能描述  列表--管理端
    * @author WN
    * @date 2020/6/11
    * @param  * @param research
    * @return com.xhkjedu.vo.ResultVo
    */
    @PostMapping("list")
    public ResultVo list(@RequestBody TResearch research){
        N_Utils.validation(new Object[]{
                research.getPage(),"显示页码",1,research.getPageSize(),"显示条数",1,
                research.getSchoolid(),"学校id",1,research.getRorder(),"排序",2});
        if(!N_Utils.isNotEmpty(research.getRorder())){
            research.setRorder("stoptime");
        }
        PageHelper.startPage(research.getPage(), research.getPageSize());
        List<Map<String, String>> list = researchService.listResearchs(research.getSchoolid(), research.getRorder());
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取教研列表成功",pageResult);
    }

    /**
     *功能描述 保存
     * @author WN
     * @date 2020/6/11
     * @param  * @param research
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/save")
    @Action("教研管理-增加")
    public ResultVo save(@RequestBody TResearch research){
        N_Utils.validation(new Object[]{
                research.getResearchname(),"教研标题",2,
                research.getResearchcontent(),"教研内容",2
        });
        if(CollectionUtils.isEmpty(research.getTeacherids())){
            return new ResultVo(0,"未选择教师");
        }else{
            Integer id = researchService.save(research);
            return new ResultVo(0,"保存成功", id);
        }
    }

    /**
     *功能描述 修改
     * @author WN
     * @date 2020/6/11
     * @param  * @param research
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/up")
    @Action("教研管理-修改")
    public ResultVo update(@RequestBody TResearch research){
        N_Utils.validation(new Object[]{
                research.getResearchid(),"教研id",1,
                research.getResearchname(),"教研标题",2,
                research.getResearchcontent(),"教研内容",2
        });
        researchService.update(research);
        return new ResultVo(0,"修改成功");
    }

    /**
     *功能描述 删除
     * @author WN
     * @date 2020/6/11
     * @param  * @param research
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/del")
    @Action("教研管理-删除")
    public ResultVo del(@RequestBody TResearch research){
        N_Utils.validation(new Object[]{
                research.getResearchid(),"教研id",1
        });
        Integer result = researchService.del(research.getResearchid());
        if(result>0){
            return new ResultVo(0,"删除成功");
        }else{
            return new ResultVo(1,"删除失败");
        }
    }

    /**
     *功能描述  教研详情
     * @author WN
     * @date 2020/6/11
     * @param  * @param research
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/detail")
    public ResultVo detail(@RequestBody TResearch research) {
        Integer researchid = research.getResearchid();
        Integer schoolid = research.getSchoolid();
        N_Utils.validation(new Object[]{researchid, "教研id", 1, schoolid, "学校id", 1});
        research = researchService.getResearchById(researchid, schoolid);
        return new ResultVo(0, "获取教研详情成功", research);
    }

    /**
     *功能描述 修改MP4code
     * @author WN
     * @date 2020/8/25
     * @param  * @param research
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/up_code")
    public ResultVo updateMp4code(@RequestBody TResearch research){
        N_Utils.validation(new Object[]{
                research.getResearchid(),"教研id",1,
                research.getMp4code(),"mp4code",2,
                research.getResearchpath(),"教研视频地址",2
        });
        researchService.updateResourceMp4code(research);
        return new ResultVo(0,"修改成功");
    }

    /**
     * @Description 获取学校下用户教研评价
     * @Param [research]
     * @Author ywx
     * @Date 2023/7/24 9:09
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("ulist")
    public ResultVo ulist(@RequestBody TResearchUser research) {
        Integer page = research.getPage();
        Integer pageSize = research.getPageSize();
        Integer researchid = research.getResearchid();
        Integer schoolid = research.getSchoolid();
        N_Utils.validation(new Object[]{page, "显示页码", 1, pageSize, "显示条数", 1,
                researchid, "教研id", 1, schoolid, "学校id", 1});
        PageHelper.startPage(page, pageSize);
        List<Map<String, String>> list = researchService.listUserByRidAndSid(researchid, schoolid, research.getUserid());
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "获取学校下用户教研评价成功", pageResult);
    }

    /*
    * @Description 删除用户教研
    * @Author WN
    * @Date 2023/8/8 15:39:05
    */
    @PostMapping("deltr")
    @Action("教研管理-删除教师教研")
    public ResultVo delTeacherResearch(@RequestBody TResearchUser ruser){
        N_Utils.validation(new Object[]{
                ruser.getRuid(),"教师教研id",1,
        });
        Integer result = researchService.delTeacherResearch(ruser.getRuid());
        return new ResultVo(0,"删除教师教研成功");
    }

}
