package com.srs.web.controller.stureg;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.srs.stureg.domain.SrsStuReg;
import com.srs.stureg.domain.SrsStuTodo;
import com.srs.stureg.domain.SrsTaskList;
import com.srs.stureg.domain.VO.SrsStuTodoVo;
import com.srs.stureg.service.ISrsStuRegService;
import com.srs.stureg.service.ISrsStuTodoService;
import com.srs.system.service.ISysPostService;
import com.srs.web.controller.common.RoleBool;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.srs.common.annotation.Log;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.enums.BusinessType;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.core.page.TableDataInfo;

/**
 * 学生待办表Controller
 *
 * @author srs
 * @date 2023-08-18
 */
@RestController
@RequestMapping("/system/todo")
public class SrsStuTodoController extends BaseController {
    @Autowired
    private ISrsStuTodoService srsStuTodoService;

    @Autowired
    private ISrsStuRegService _srsStuReg;

    @Autowired
    ISysPostService _postService;


    @ApiOperation("获取自己的任务状态")
    @GetMapping("/getOwnTaskStatusByCode/{code}")
    public AjaxResult getOwnTaskStatusByCode(@PathVariable("code") String taskCode) {
        String ksh = getUsername();
        SrsStuTodo param = new SrsStuTodo();
        param.setTaskCode(taskCode);
        param.setKsh(ksh);
        return srsStuTodoService.getOwnTaskStatusByCode(param);
    }

    @ApiOperation("号码确认")
    @PostMapping("/confirmHmqr")
    public AjaxResult confirmHmqr() {
        String ksh = getUsername();
        return srsStuTodoService.confirmTask(ksh, "HMQR");
    }

    @ApiOperation("报到核验")
    @PostMapping("/confirmBdhy")
    public AjaxResult confirmBdhy() {
        String ksh = getUsername();
        return srsStuTodoService.confirmTask(ksh, "BDHY");
    }

    @ApiOperation("专业确认")
    @PostMapping("/confirmZyqr")
    public AjaxResult confirmZyqr() {
        String ksh = getUsername();
        return srsStuTodoService.confirmTask(ksh, "ZYQR");
    }

    @ApiOperation("入学须知确认")
    @PostMapping("/confirmRxxz")
    public AjaxResult confirmRxxz() {
        String ksh = getUsername();
        return srsStuTodoService.confirmTask(ksh, "RXXZ");
    }

    @ApiOperation("宿舍确认")
    @PostMapping("/confirmZxxs")
    public AjaxResult confirmZxxs() {
        String ksh = getUsername();
        return srsStuTodoService.confirmTask(ksh, "ZXXS");
    }

    @GetMapping("/listOwnTask")
    public AjaxResult listOwnTask() {
        String ksh = getUsername();
        SrsStuTodo param = new SrsStuTodo();
        param.setKsh(ksh);
        SrsStuReg stureg=_srsStuReg.getOwnInfoByKsh(ksh);
        List<SrsStuTodo> list = srsStuTodoService.selectSrsStuTodoList(param);
        if(stureg.getIsDirection().equals("1")){
            for(SrsStuTodo sst:list){
                if(sst.taskCode.equals("ZXJF")){
                    list.remove(sst);
                }
            }
        }
        return AjaxResult.success(list);
    }

    @GetMapping("/listStuTodo/{ksh}")
    public AjaxResult listStuTodo(@PathVariable("ksh") String ksh) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            SrsStuTodo param = new SrsStuTodo();
            param.setKsh(ksh);
            List<SrsStuTodoVo> list = srsStuTodoService.selectSrsStuTodoListJoin(param);
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    /**
     * 列出自己的待办
     */
    @GetMapping("/listowntodo")
    public TableDataInfo listOwnTodo() {
        startPage();
        String ksh = getUsername();
        SrsStuReg temp = _srsStuReg.getOwnInfoByKsh(ksh);
        SrsStuTodo param = new SrsStuTodo();
        param.setStuNo(temp.getXH());
        List<SrsStuTodoVo> list = srsStuTodoService.selectSrsStuTodoListJoin(param);
        return getDataTable(list);
    }

    /**
     * 通过id查询自己待办的信息
     *
     * @return
     */
    @GetMapping(value = "/getInfoByStudent/{id}")
    public AjaxResult getInfoByStudent(@PathVariable("id") Long id) {
        SrsStuTodo srsStuTodo = srsStuTodoService.selectSrsStuTodoById(id);
        SrsStuReg stuReg = _srsStuReg.getOwnInfoByKsh(getUsername());
        if (stuReg.getXH().equals(srsStuTodo.getStuNo())) {
            return success(srsStuTodo);
        } else {
            return error("信息不一致");
        }
    }

    /**
     * 查询学生待办表列表
     */
//    @PreAuthorize("@ss.hasPermi('system:todo:list')")
    @GetMapping("/listjoin")
    public TableDataInfo listJoin(SrsStuTodo srsStuTodo) {
        startPage();
        List<SrsStuTodoVo> list = srsStuTodoService.selectSrsStuTodoListJoin(srsStuTodo);
        return getDataTable(list);
    }

    /**
     * 查询学生待办表列表
     */
//    @PreAuthorize("@ss.hasPermi('system:todo:list')")
    @GetMapping("/list")
    public TableDataInfo list(SrsStuTodo srsStuTodo) {
        startPage();
        List<SrsStuTodo> list = srsStuTodoService.selectSrsStuTodoList(srsStuTodo);
        return getDataTable(list);
    }

    /**
     * 导出学生待办表列表
     */
//    @PreAuthorize("@ss.hasPermi('system:todo:export')")
    @Log(title = "学生待办表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SrsStuTodo srsStuTodo) {
        List<SrsStuTodo> list = srsStuTodoService.selectSrsStuTodoList(srsStuTodo);
        ExcelUtil<SrsStuTodo> util = new ExcelUtil<SrsStuTodo>(SrsStuTodo.class);
        util.exportExcel(response, list, "学生待办表数据");
    }

    /**
     * 获取学生待办表详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:todo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(srsStuTodoService.selectSrsStuTodoById(id));
    }

    /**
     * 新增学生待办表
     */
//    @PreAuthorize("@ss.hasPermi('system:todo:add')")
    @Log(title = "学生待办表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SrsStuTodo srsStuTodo) {
        return toAjax(srsStuTodoService.insertSrsStuTodo(srsStuTodo));
    }

    /**
     * 修改学生待办表
     */
//    @PreAuthorize("@ss.hasPermi('system:todo:edit')")
    @Log(title = "学生待办表", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody SrsStuTodo srsStuTodo) {
        return toAjax(srsStuTodoService.updateSrsStuTodo(srsStuTodo));
    }

    /**
     * 删除学生待办表
     */
//    @PreAuthorize("@ss.hasPermi('system:todo:remove')")
    @Log(title = "学生待办表", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(srsStuTodoService.deleteSrsStuTodoByIds(ids));
    }
}
