package com.xhkjedu.sapi.controller.scantron;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.sapi.model.scantron.TScantron;
import com.xhkjedu.sapi.service.scantron.ScantronService;
import com.xhkjedu.sapi.service.system.UserService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.vo.system.UserVo;
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;

/**
 * @Description：答题卡
 * @Author: WN
 * @Date: 2024/10/25 10:15:46
 **/
@RestController
@RequestMapping("/scantron")
public class ScantronController {
    @Resource
    private ScantronService scantronService;
    @Resource
    private UserService userService;

    /*
     * @Description 答题卡列表-我的
     * @Date 2024/10/25 10:23:08
     * @Author WN
     * @Param [scantron]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_my")
    public ResultVo listMy(@RequestBody TScantron scantron) {
        Integer page = scantron.getPage();
        Integer pageSize = scantron.getPageSize();
        Integer createid = scantron.getCreateid();
        Integer schoolid = scantron.getSchoolid();
        N_Utils.validation(new Object[]{page, "显示页码", 1, pageSize, "显示条数", 1, createid, "用户id", 1, schoolid, "学校id", 1});
        PageHelper.startPage(page, pageSize);
        List<Map<String, Object>> list = scantronService.listScantrons(scantron);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "查询成功", pageResult);
    }

    /*
     * @Description 答题卡列表-全部
     * @Date 2024/10/25 10:41:09
     * @Author WN
     * @Param [scantron]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list")
    public ResultVo list(@RequestBody TScantron scantron) {
        Integer page = scantron.getPage();
        Integer pageSize = scantron.getPageSize();
        Integer createid = scantron.getCreateid();
        Integer schoolid = scantron.getSchoolid();
        N_Utils.validation(new Object[]{page, "显示页码", 1, pageSize, "显示条数", 1, createid, "教师id", 1, schoolid, "学校id", 1});

        // 获取用户类型
        UserVo userVo = userService.getUserTypeAndSubjectid(createid);
        if (userVo.getUsertype() == 1) {
            // 设置教师科目
            scantron.setSubjectid(userVo.getSubjectid());
        }
        if (userVo.getUsertype() == 0) {
            // 管理员看到学校所有
            scantron.setCreateid(null);
        }
        scantron.setPubliched(1);// 全部查看公开的

        PageHelper.startPage(page, pageSize);
        List<Map<String, Object>> list = scantronService.listScantrons(scantron);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "查询成功", pageResult);
    }

    // 答题卡-新增
    @PostMapping("/add")
    public ResultVo add(@RequestBody TScantron scantron) {
        N_Utils.validation(new Object[]{scantron.getCreateid(), "创建人id", 1, scantron.getSchoolid(), "学校id", 1,
                scantron.getSubjectid(), "科目id", 1, scantron.getSname(), "答题卡名称", 2});
        try {
            String pdfpath = scantronService.save(scantron);
            return new ResultVo(0, "保存成功", pdfpath);
        } catch (Exception e) {
            return new ResultVo(1, "保存失败");
        }
    }

    // 答题卡-修改
    @PostMapping("/update")
    public ResultVo update(@RequestBody TScantron scantron) {
        N_Utils.validation(new Object[]{scantron.getSid(), "答题卡id", 1});

        try {
            String pdfpath = scantronService.update(scantron);
            return new ResultVo(0, "保存成功", pdfpath);
        } catch (Exception e) {
            return new ResultVo(1, "保存失败");
        }
    }

    // 答题卡-删除
    @PostMapping("/delete")
    public ResultVo delete(@RequestBody TScantron scantron) {
        N_Utils.validation(new Object[]{scantron.getSid(), "答题卡id", 1});
        scantronService.delete(scantron.getSid());
        return new ResultVo(0, "删除成功");
    }

    // 答题卡-详情
    @PostMapping("/detail")
    public ResultVo detail(@RequestBody TScantron scantron) {
        N_Utils.validation(new Object[]{scantron.getSid(), "答题卡id", 1});
        TScantron st = scantronService.detail(scantron.getSid());
        return new ResultVo(0, "获取成功", st);
    }

    // 答题卡-公开
    @PostMapping("/pub")
    public ResultVo updatePubliched(@RequestBody TScantron scantron) {
        N_Utils.validation(new Object[]{scantron.getSid(), "答题卡id", 1});
        scantronService.updateScantronPubliched(scantron.getSid(), 1);
        return new ResultVo(0, "设置成功");
    }

    // 答题卡-取消公开
    @PostMapping("/unpub")
    public ResultVo updateUnPubliched(@RequestBody TScantron scantron) {
        N_Utils.validation(new Object[]{scantron.getSid(), "答题卡id", 1});
        scantronService.updateScantronPubliched(scantron.getSid(), 2);
        return new ResultVo(0, "设置成功");
    }

    // 答题卡-复用
    @PostMapping("/copy")
    public ResultVo copyScantron(@RequestBody TScantron scantron) {
        N_Utils.validation(new Object[]{scantron.getSid(), "答题卡id", 1, scantron.getCreateid(), "创建人id", 1});
        TScantron st = scantronService.copyScantron(scantron);
        return new ResultVo(0, "复用成功", st);
    }


}
