package com.srs.web.controller.comprehensive;

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

import com.srs.comprehensive.domain.Dto.CphAuditMany;
import com.srs.comprehensive.domain.Dto.CphCancel;
import com.srs.comprehensive.domain.Vo.CphLakeApplyVo;
import com.srs.comprehensive.domain.Vo.CphSearch;
import com.srs.system.domain.SysDeptMap;
import com.srs.system.service.ISysDeptMapService;
import com.srs.system.service.ISysPostService;
import com.srs.web.controller.common.RoleBool;
import liquibase.pro.packaged.A;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;

import com.srs.comprehensive.domain.CphLakeApply;
import com.srs.comprehensive.service.ICphLakeApplyService;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.enums.BusinessType;
import com.srs.common.annotation.Log;
import com.srs.common.core.page.TableDataInfo;

import static com.srs.common.utils.DateUtils.getNowDate;

/**
 * 静湖之星申请Controller
 *
 * @author srs
 * @date 2023-10-31
 */
@RestController
@RequestMapping("/comprehensive/lakeapply")
@Api(value = "静湖之星申请管理", tags = "静湖之星申请管理")
public class CphLakeApplyController extends BaseController {
    @Autowired
    private ICphLakeApplyService cphLakeApplyService;

    @Autowired
    ISysPostService _postService;

    @Autowired
    ISysDeptMapService _deptMap;

    @PostMapping("/changeType")
    @ApiOperation("修改奖项")
    public AjaxResult changeType(@RequestBody CphLakeApply param){
        boolean hasRole = RoleBool.isJwc(getUserId(),_postService);
        if(hasRole){
            param.setUpdateTime(getNowDate());
            param.setUpdateBy(getUsername());
            return AjaxResult.success( cphLakeApplyService.updateCphLakeApply(param));
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    @PostMapping("/manyAudit")
    @ApiOperation("一键审核通过")
    public AjaxResult manyAudit(@RequestBody CphAuditMany param){
        boolean hasRole = RoleBool.isJwc(getUserId(),_postService);
        if(hasRole){
            return cphLakeApplyService.manyAudit(param);
        }else{
            return  AjaxResult.error(401,"你无权做这些");
        }
    }

    @GetMapping("/jwcExport")
    @ApiOperation("学工导出")
    public AjaxResult jwcExport(CphSearch param){
        boolean hasRole = RoleBool.isJwc(getUserId(),_postService);
        if(hasRole){
            List<CphLakeApplyVo> list = cphLakeApplyService.exportView(param);
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    @PostMapping("/jwcAudit")
    @ApiOperation("学工审核")
    public AjaxResult jwcAudit(@RequestBody CphLakeApply param){
        boolean hasRole = RoleBool.isJwc(getUserId(),_postService);
        if(hasRole){
            CphLakeApply temp = new CphLakeApply();
            temp.setId(param.getId());
            temp.setApplyStatus(param.getApplyStatus());
            temp.setJwcComment(param.getJwcComment());
            temp.setXgNo(getUsername());
            temp.setXgTime(getNowDate());
            return cphLakeApplyService.doAudit(param);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    @GetMapping("/listJwcAll")
    @ApiOperation("学工所有审核记录")
    public TableDataInfo listJwcAll(CphSearch param){
        boolean hasRole = RoleBool.isJwc(getUserId(),_postService);
        if(hasRole){
            startPage();
            List<CphLakeApplyVo> list = cphLakeApplyService.listView(param);
            return getDataTable(list);
        }else{
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @GetMapping("/listJwc")
    @ApiOperation("查询学工待审核")
    public TableDataInfo listJwc(CphSearch param){
        boolean hasRole = RoleBool.isJwc(getUserId(),_postService);
        if(hasRole){
            param.setStatus("3");
            startPage();
            List<CphLakeApplyVo> list = cphLakeApplyService.listView(param);
            return getDataTable(list);
        }else{
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @GetMapping("/xwExport")
    @ApiOperation("学务导出")
    public AjaxResult xwExport(CphSearch param){
        boolean hasRole = RoleBool.isXw(getUserId(),_postService);
        if(hasRole){
            Long oldId = getDeptId();
            SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
            Long deptId = 0L;
            if(dept != null){
                deptId = dept.getOldDeptId();
            }
            param.setDeptId(deptId);
            List<CphLakeApplyVo> list = cphLakeApplyService.exportView(param);
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    @PostMapping("/xwAudit")
    @ApiOperation("学务审核")
    public AjaxResult xwAudit(@RequestBody CphLakeApply param){
        boolean hasRole = RoleBool.isXw(getUserId(),_postService);
        if(hasRole){
            CphLakeApply temp = new CphLakeApply();
            temp.setId(param.getId());
            temp.setApplyStatus(param.getApplyStatus());
            temp.setXwComment(param.getXwComment());
            temp.setXwNo(getUsername());
            temp.setXwTime(getNowDate());
            return cphLakeApplyService.doAudit(temp);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    @GetMapping("/listXwAll")
    @ApiOperation("查询学务所有审核记录")
    public TableDataInfo listXwAll(CphSearch param){
        boolean hasRole = RoleBool.isXw(getUserId(),_postService);
        if(hasRole){
            Long oldId = getDeptId();
            SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
            Long deptId = 0L;
            if(dept != null){
                deptId = dept.getOldDeptId();
            }
            param.setDeptId(deptId);
            startPage();
            List<CphLakeApplyVo> list = cphLakeApplyService.listView(param);
            return getDataTable(list);
        }else{
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @GetMapping("/listXw")
    @ApiOperation("查询学务待审核")
    public TableDataInfo listXw(CphSearch param){
        boolean hasRole = RoleBool.isXw(getUserId(),_postService);
        if(hasRole){
            Long oldId = getDeptId();
            SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
            Long deptId = 0L;
            if(dept != null){
                deptId = dept.getOldDeptId();
            }
            param.setDeptId(deptId);
            param.setStatus("2");
            startPage();
            List<CphLakeApplyVo> list = cphLakeApplyService.listView(param);
            return getDataTable(list);
        }else{
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @GetMapping("/fdyExport")
    @ApiOperation("辅导员导出")
    public AjaxResult fdyExport(CphSearch param){
        boolean hasRole = RoleBool.isFdy(getUserId(),_postService);
        if(hasRole){
            param.setTNo(getUsername());
            List<CphLakeApplyVo> list = cphLakeApplyService.exportView(param);
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    @PostMapping("/fdyAudit")
    @ApiOperation("辅导员审核")
    public AjaxResult fdyAudit(@RequestBody CphLakeApply param){
        boolean hasRole = RoleBool.isFdy(getUserId(),_postService);
        if(hasRole){
            CphLakeApply temp = new CphLakeApply();
            temp.setId(param.getId());
            temp.setApplyStatus(param.getApplyStatus());
            temp.setFdyComment(param.getFdyComment());
            temp.setFdyNo(getUsername());
            temp.setFdyTime(getNowDate());
            return cphLakeApplyService.doAudit(temp);
        }else{
            return AjaxResult.error(401,"你无权做这些");
        }
    }

    @GetMapping("/listFdyAll")
    @ApiOperation("查询辅导所有审核")
    public TableDataInfo listFdyAll(CphSearch param){
        boolean hasRole = RoleBool.isFdy(getUserId(),_postService);
        if(hasRole){
            param.setTNo(getUsername());
            startPage();
            List<CphLakeApplyVo> list = cphLakeApplyService.listView(param);
            return getDataTable(list);
        }else{
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @GetMapping("/listFdy")
    @ApiOperation("查询辅导员待审核")
    public TableDataInfo listFdy(CphSearch param){
        boolean hasRole = RoleBool.isFdy(getUserId(),_postService);
        if(hasRole){
            param.setTNo(getUsername());
            param.setStatus("1");
            startPage();
            List<CphLakeApplyVo> list = cphLakeApplyService.listView(param);
            return getDataTable(list);
        }else{
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }


    @PostMapping("/reApplyLake")
    @ApiOperation("重新提交")
    public AjaxResult reApplyLake(@RequestBody CphLakeApply param){
        return cphLakeApplyService.reApplyLake(param);
    }

    @PostMapping("/applyTen")
    @ApiOperation("申请十佳大学生")
    public AjaxResult applyTen(@RequestBody CphLakeApply param){
        return cphLakeApplyService.applyTen(param);
    }


    @PostMapping("/applyLake")
    @ApiOperation("静湖之星申请")
    public AjaxResult applyLake(@RequestBody CphLakeApply param){
        return cphLakeApplyService.applyLake(param);
    }



    @PostMapping("/cancelOwnApply")
    @ApiOperation("取消静湖之星申请")
    public AjaxResult cancelOwnApply(@RequestBody CphCancel param){
        return cphLakeApplyService.cancelOwnApply(param);
    }

    @GetMapping("/listOwnApply")
    @ApiOperation("连表查询")
    public TableDataInfo listOwnApply(CphSearch param){
        CphSearch temp = new CphSearch();
        temp.setStatus(param.getStatus());
        temp.setCode(param.getCode());
        temp.setStuNo(getUsername());
        startPage();
        List<CphLakeApplyVo> list = cphLakeApplyService.listView(temp);
        return getDataTable(list);
    }



    @GetMapping("/listView")
    @ApiOperation("连表查询")
    public TableDataInfo listView(CphSearch param){
        boolean hasRole = RoleBool.isJwc(getUserId(),_postService);
        if(hasRole){
            startPage();
            List<CphLakeApplyVo> list = cphLakeApplyService.listView(param);
            return getDataTable(list);
        }else{
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }


    /**
     * 查询静湖之星申请列表
     */
@PreAuthorize("@ss.hasPermi('comprehensive:lakeapply:list')")
@GetMapping("/list")
@ApiOperation("查询静湖之星申请列表")
    public TableDataInfo list(CphLakeApply cphLakeApply)
    {
        startPage();
        List<CphLakeApply> list = cphLakeApplyService.selectCphLakeApplyList(cphLakeApply);
        return getDataTable(list);
    }

    /**
     * 导出静湖之星申请列表
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:lakeapply:export')")
    @Log(title = "静湖之星申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出静湖之星申请列表")
    public void export(HttpServletResponse response, CphLakeApply cphLakeApply)
    {
        List<CphLakeApply> list = cphLakeApplyService.selectCphLakeApplyList(cphLakeApply);
        ExcelUtil<CphLakeApply> util = new ExcelUtil<CphLakeApply>(CphLakeApply.class);
        util.exportExcel(response, list, "静湖之星申请数据");
    }

    /**
     * 获取静湖之星申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:lakeapply:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取静湖之星申请详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cphLakeApplyService.selectCphLakeApplyById(id));
    }

    /**
     * 新增静湖之星申请
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:lakeapply:add')")
    @Log(title = "静湖之星申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增静湖之星申请")
    public AjaxResult add(@RequestBody CphLakeApply cphLakeApply)
    {
        return toAjax(cphLakeApplyService.insertCphLakeApply(cphLakeApply));
    }

    /**
     * 修改静湖之星申请
     */
//    @PreAuthorize("@ss.hasPermi('comprehensive:lakeapply:edit')")
    @Log(title = "静湖之星申请", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改静湖之星申请")
    public AjaxResult edit(@RequestBody CphLakeApply cphLakeApply)
    {
        return toAjax(cphLakeApplyService.updateCphLakeApply(cphLakeApply));
    }

    /**
     * 删除静湖之星申请
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:lakeapply:remove')")
    @Log(title = "静湖之星申请", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除静湖之星申请")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cphLakeApplyService.deleteCphLakeApplyByIds(ids));
    }
}
