package com.xhkjedu.sstudy.controller.recite;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.sstudy.mapper.system.UserMapper;
import com.xhkjedu.sstudy.model.recite.TRecite;
import com.xhkjedu.sstudy.service.gradeclass.ClassService;
import com.xhkjedu.sstudy.service.recite.ReciteService;
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 ReciteController
 * Description TODO
 * Author WN
 * Date 2020/6/10 9:45
 **/
@RestController
@RequestMapping("/recite")
public class ReciteController {
    @Resource
    private ReciteService reciteService;
    @Resource
    private ClassService classService;
    @Resource
    private UserMapper userMapper;

    /**
     *功能描述  发布背诵
     * @author WN
     * @date 2020/6/10
     * @param  * @param recite
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/save")
    @Action("背诵管理-增加")
    public ResultVo save(@RequestBody TRecite recite){
        N_Utils.validation(new Object[]{
                recite.getRecitetitle(),"背诵标题",2,
                recite.getRecitecont(),"背诵内容",2
        });
        if (recite.getStarttime().compareTo(recite.getStoptime())==1) {
            return new ResultVo(1,"结束时间必须大于开始时间");
        }
        if(CollectionUtils.isEmpty(recite.getRclist()) && CollectionUtils.isEmpty(recite.getJtrclist())){
            return new ResultVo(1,"未选择班级");
        }else{
            return reciteService.save(recite);
        }
    }

    /**
     *功能描述 修改背诵
     * @author WN
     * @date 2020/6/10
     * @param  * @param recite
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/up")
    @Action("背诵管理-修改")
    public ResultVo update(@RequestBody TRecite recite){
        try {
            N_Utils.validation(new Object[]{
                    recite.getReciteid(),"背诵id",1,
                    recite.getRecitetitle(),"背诵标题",2,
                    recite.getRecitecont(),"背诵内容",2
            });
            Integer result = reciteService.updateRecite(recite);
            if(result>0){
                return new ResultVo(0,"修改成功");
            }else{
                return new ResultVo(1,"修改失败");
            }
        }catch (Exception e){
            return new ResultVo(1,"修改失败");
        }
    }

    /**
     *功能描述 删除背诵内容
     * @author WN
     * @date 2020/6/10
     * @param  * @param recite
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/del")
    @Action("背诵管理-删除")
    public ResultVo del(@RequestBody TRecite recite){
        N_Utils.validation(new Object[]{
                recite.getReciteid(),"背诵id",1
        });
        Integer result = reciteService.delRecite(recite.getReciteid());
        if(result>0){
            return new ResultVo(0,"删除成功");
        }else{
            return new ResultVo(1,"删除失败");
        }
    }

    /**
     * 功能描述 背诵教师--网页--列表
     *
     * @param * @param recite
     * @return com.xhkjedu.vo.ResultVo
     * @author WN
     * @date 2020/6/10
     */
    @PostMapping("list")
    public ResultVo list(@RequestBody TRecite recite) {
        Integer createid = recite.getCreateid();
        N_Utils.validation(new Object[]{
                recite.getPage(), "显示页码", 1, recite.getPageSize(), "显示条数", 1,
                createid, "创建人", 1,recite.getSchoolid(),"学校id",1});
        Map classmap = classService.getQxClassidsByTeacherid(createid, null);
        recite.setClassids(classmap.get("classids").toString());
        recite.setBxclassids(classmap.get("bxclassids").toString());
        Integer usertype = userMapper.getUsertypeByUserid(createid);
        PageHelper.startPage(recite.getPage(), recite.getPageSize());
        recite.setNowtime(N_Utils.getSecondTimestamp());
        List<Map> list = reciteService.listRecites(recite, usertype);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "获取列表成功", pageResult);
    }

    /*
     * @Description 背诵教师--网页--列表-我的
     * @Date 2024/3/12 9:38:23
     * @Author WN
     * @Param [recite]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("list_my")
    public ResultVo listMy(@RequestBody TRecite recite) {
        Integer createid = recite.getCreateid();
        N_Utils.validation(new Object[]{
                recite.getPage(), "显示页码", 1, recite.getPageSize(), "显示条数", 1,
                createid, "创建人", 1,recite.getSchoolid(),"学校id",1});
        Integer usertype = userMapper.getUsertypeByUserid(createid);
        PageHelper.startPage(recite.getPage(), recite.getPageSize());
        recite.setNowtime(N_Utils.getSecondTimestamp());
        List<Map> list = reciteService.listMyRecites(recite, usertype);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "获取列表成功", pageResult);
    }

    @PostMapping("/list_class")
    public ResultVo listClass(@RequestBody TRecite recite) {
        Integer reciteid = recite.getReciteid();
        Integer createid = recite.getCreateid();
        N_Utils.validation(new Object[]{reciteid, "背诵id", 1, createid,"教师id",1,recite.getSchoolid(),"学校id",1});
        Map classmap = classService.getQxClassidsByTeacherid(createid, reciteService.getYearById(reciteid));
        recite.setClassids(classmap.get("classids").toString());
        recite.setBxclassids(classmap.get("bxclassids").toString());
        Integer usertype = userMapper.getUsertypeByUserid(createid);
        Map list = reciteService.listClass(recite, usertype);
        return new ResultVo(0, "获取背诵班级成功", list);
    }

}
