package com.srs.web.controller.comprehensive;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.srs.web.controller.common.RoleBool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.core.page.TableDataInfo;
import com.srs.common.enums.BusinessType;
import com.srs.comprehensive.domain.CphNewGoodClassApply;
import com.srs.comprehensive.domain.Dto.CphCancel;
import com.srs.comprehensive.domain.Vo.ExcelGoodClass;
import com.srs.comprehensive.service.ICphNewGoodClassApplyService;
import com.srs.system.domain.SysDeptMap;
import com.srs.system.service.ISysDeptMapService;
import com.srs.system.service.ISysPostService;
import com.srs.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.srs.common.annotation.Log;

/**
 * 先进班集体Controller
 *
 * @author srs
 * @date 2024-05-15
 */
@RestController
@RequestMapping("/comprehensive/newClassGoodApply")
@Api(value = "先进班集体管理", tags = "先进班集体管理")
public class CphNewGoodClassApplyController extends BaseController {
    @Autowired
    private ICphNewGoodClassApplyService cphNewGoodClassApplyService;

    @Autowired
    ISysPostService _postService;

    @Autowired
    ISysDeptMapService _deptMap;


    @PostMapping("/editStatus")
    @ApiOperation("修改审核状态")
    public AjaxResult editStatus(@RequestBody CphNewGoodClassApply param){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return cphNewGoodClassApplyService.editStatus(param);
        }else{
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @PostMapping("/addRemark")
    @ApiOperation("添加备注")
    public AjaxResult addRemark(@RequestBody CphNewGoodClassApply param){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return cphNewGoodClassApplyService.addRemark(param);
        }else{
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @PostMapping("/editManySendMoney")
    @ApiOperation("批量标记为已转账")
    public AjaxResult editManySendMoney(@RequestBody List<Long> ids){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return cphNewGoodClassApplyService.editManySendMoney(ids);
        }else{
            return AjaxResult.error(401, "你无权做这些");
        }
    }



    @PostMapping("/editAllSendMoney")
    @ApiOperation("将所有未转账标记为已转账")
    public AjaxResult editAllSendMoney(){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return cphNewGoodClassApplyService.editAllSendMoney();
        }else{
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    @PostMapping("/editSendMoney")
    @ApiOperation("标记转账")
    public AjaxResult editSendMoney(@RequestBody  CphNewGoodClassApply param){
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return cphNewGoodClassApplyService.editSendMoney(param);
        }else{
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @Log(title = "先进班集体", businessType = BusinessType.EXPORT)
    @PostMapping("/exportJwc")
    @ApiOperation("学工导出先进班集体列表")
    public void exportJwc(HttpServletResponse response, CphNewGoodClassApply param) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            List<ExcelGoodClass> list = cphNewGoodClassApplyService.exportView(param);
            ExcelUtil<ExcelGoodClass> util = new ExcelUtil<ExcelGoodClass>(ExcelGoodClass.class);
            util.exportExcel(response, list, "先进班集体数据");
        }
    }


    @Log(title = "先进班集体", businessType = BusinessType.EXPORT)
    @PostMapping("/exportXw")
    @ApiOperation("学务导出先进班集体列表")
    public void exportXw(HttpServletResponse response, CphNewGoodClassApply param) {
        boolean hasRole = RoleBool.isHigh(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<ExcelGoodClass> list = cphNewGoodClassApplyService.exportView(param);
            ExcelUtil<ExcelGoodClass> util = new ExcelUtil<ExcelGoodClass>(ExcelGoodClass.class);
            util.exportExcel(response, list, "先进班集体数据");
        }
    }


    @Log(title = "先进班集体", businessType = BusinessType.EXPORT)
    @PostMapping("/exportFdy")
    @ApiOperation("辅导员导出先进班集体列表")
    public void exportFdy(HttpServletResponse response, CphNewGoodClassApply cphNewGoodClassApply) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            cphNewGoodClassApply.fdyNo = getUsername();
            List<ExcelGoodClass> list = cphNewGoodClassApplyService.exportFdy(cphNewGoodClassApply);
            ExcelUtil<ExcelGoodClass> util = new ExcelUtil<ExcelGoodClass>(ExcelGoodClass.class);
            util.exportExcel(response, list, "先进班集体数据");
        }
    }

    @PostMapping("/manyAudit")
    @ApiOperation("批量审核")
    public AjaxResult manyAudit(@RequestBody List<Long> ids) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return cphNewGoodClassApplyService.manyAudit(ids);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @PostMapping("/doAudit")
    @ApiOperation("审核")
    public AjaxResult doAudit(@RequestBody CphNewGoodClassApply param) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            return cphNewGoodClassApplyService.doAudit(param);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }
    @PostMapping("/reApply")
    @ApiOperation("重新提交")
    public AjaxResult reApply(@RequestBody CphNewGoodClassApply param){
        return cphNewGoodClassApplyService.reApply(param);
    }

    @PostMapping("/cancelApply")
    @ApiOperation("学生取消申请")
    public AjaxResult cancelApply(@RequestBody CphCancel param) {
        return cphNewGoodClassApplyService.cancelApply(param);
    }


    @PostMapping("/doApply")
    @ApiOperation("学生提交申请")
    public AjaxResult doApply(@RequestBody CphNewGoodClassApply param) {
        param.stuNo = getUsername();
        return cphNewGoodClassApplyService.doApply(param);
    }


    @GetMapping("/getOwnClass")
    @ApiOperation("学生自己的班级信息")
    public AjaxResult getOwnClass() {
        String stuNo = getUsername();
        CphNewGoodClassApply res = cphNewGoodClassApplyService.getStuClass(stuNo);
        if (res != null) {
            res.classId = null;
            res.classCode = null;
            res.fdyNo = null;
        } else {
            res = new CphNewGoodClassApply();
        }

        return AjaxResult.success(res);
    }


    @GetMapping("/listStu")
    @ApiOperation("学生查询先进班集体列表")
    public TableDataInfo listStu(CphNewGoodClassApply param) {
        param.setStuNo(getUsername());
        startPage();
        List<CphNewGoodClassApply> list = cphNewGoodClassApplyService.listView(param);
        return getDataTable(list);
    }

    @GetMapping("/listFdy")
    @ApiOperation("辅导员查询先进班集体待审核")
    public TableDataInfo listFdy(CphNewGoodClassApply param) {
        boolean hasRole = RoleBool.isFdy(getUserId(), _postService);
        if (hasRole) {
            param.setFdyNo(getUsername());
            param.applyStatus = "1";
            startPage();
            List<CphNewGoodClassApply> list = cphNewGoodClassApplyService.listFdy(param);
            return getDataTable(list);
        } else {
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @GetMapping("/listFdyAll")
    @ApiOperation("辅导员查询先进班集体")
    public TableDataInfo listFdyAll(CphNewGoodClassApply param) {
        boolean hasRole = RoleBool.isFdy(getUserId(), _postService);
        if (hasRole) {
            param.setFdyNo(getUsername());
            startPage();
            List<CphNewGoodClassApply> list = cphNewGoodClassApplyService.listFdy(param);
            return getDataTable(list);
        } else {
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @GetMapping("/listXw")
    @ApiOperation("学务查询先进班集体待审核")
    public TableDataInfo listXw(CphNewGoodClassApply 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.applyStatus = "2";
            startPage();
            List<CphNewGoodClassApply> list = cphNewGoodClassApplyService.listView(param);
            return getDataTable(list);
        } else {
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @GetMapping("/listXwAll")
    @ApiOperation("学务查询先进班集体")
    public TableDataInfo listXwAll(CphNewGoodClassApply 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<CphNewGoodClassApply> list = cphNewGoodClassApplyService.listView(param);
            return getDataTable(list);
        } else {
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @GetMapping("/listJwc")
    @ApiOperation("学工处查询先进班集体待审核")
    public TableDataInfo listJwc(CphNewGoodClassApply param) {
        boolean hasRole = RoleBool.isJwc(getUserId(), _postService);
        if (hasRole) {
            param.applyStatus = "3";
            startPage();
            List<CphNewGoodClassApply> list = cphNewGoodClassApplyService.listView(param);
            return getDataTable(list);
        } else {
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }

    @GetMapping("/listJwcAll")
    @ApiOperation("学工处查询先进班集体")
    public TableDataInfo listJwcAll(CphNewGoodClassApply param) {
        boolean hasRole = RoleBool.isJwc(getUserId(), _postService);
        if (hasRole) {
            startPage();
            List<CphNewGoodClassApply> list = cphNewGoodClassApplyService.listView(param);
            return getDataTable(list);
        } else {
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }


    /**
     * 查询先进班集体列表
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:newClassGoodApply:list')")
    @GetMapping("/list")
    @ApiOperation("查询先进班集体列表")
    public TableDataInfo list(CphNewGoodClassApply cphNewGoodClassApply) {
        startPage();
        List<CphNewGoodClassApply> list = cphNewGoodClassApplyService.selectCphNewGoodClassApplyList(cphNewGoodClassApply);
        return getDataTable(list);
    }

    /**
     * 导出先进班集体列表
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:newClassGoodApply:export')")
    @Log(title = "先进班集体", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出先进班集体列表")
    public void export(HttpServletResponse response, CphNewGoodClassApply cphNewGoodClassApply) {
        List<CphNewGoodClassApply> list = cphNewGoodClassApplyService.selectCphNewGoodClassApplyList(cphNewGoodClassApply);
        ExcelUtil<CphNewGoodClassApply> util = new ExcelUtil<CphNewGoodClassApply>(CphNewGoodClassApply.class);
        util.exportExcel(response, list, "先进班集体数据");
    }

    /**
     * 获取先进班集体详细信息
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:newClassGoodApply:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取先进班集体详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cphNewGoodClassApplyService.selectCphNewGoodClassApplyById(id));
    }

    /**
     * 新增先进班集体
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:newClassGoodApply:add')")
    @Log(title = "先进班集体", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增先进班集体")
    public AjaxResult add(@RequestBody CphNewGoodClassApply cphNewGoodClassApply) {
        return toAjax(cphNewGoodClassApplyService.insertCphNewGoodClassApply(cphNewGoodClassApply));
    }

    /**
     * 修改先进班集体
     */
//    @PreAuthorize("@ss.hasPermi('comprehensive:newClassGoodApply:edit')")
    @Log(title = "先进班集体", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改先进班集体")
    public AjaxResult edit(@RequestBody CphNewGoodClassApply cphNewGoodClassApply) {
        return toAjax(cphNewGoodClassApplyService.updateCphNewGoodClassApply(cphNewGoodClassApply));
    }

    /**
     * 删除先进班集体
     */
    @PreAuthorize("@ss.hasPermi('comprehensive:newClassGoodApply:remove')")
    @Log(title = "先进班集体", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除先进班集体")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cphNewGoodClassApplyService.deleteCphNewGoodClassApplyByIds(ids));
    }
}
