package com.srs.web.controller.comprehensive;

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

import com.srs.common.annotation.RepeatSubmit;
import com.srs.comprehensive.domain.CphMsg;
import com.srs.comprehensive.domain.Dto.CphCancel;
import com.srs.comprehensive.domain.Dto.CphResub;
import com.srs.comprehensive.domain.SrsAddRecord;
import com.srs.comprehensive.domain.SrsStudent;
import com.srs.comprehensive.domain.Vo.CheckIam;
import com.srs.comprehensive.domain.Vo.CphApply;
import com.srs.comprehensive.domain.Vo.CphSearch;
import com.srs.comprehensive.service.ICphMsgService;
import com.srs.comprehensive.service.ISrsStudentService;
import com.srs.system.service.ISysPostService;
import com.srs.system.service.ISysUserService;
import com.srs.web.controller.common.RoleBool;
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.comprehensive.domain.CphAuditDetails;
import com.srs.comprehensive.service.ICphAuditDetailsService;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.core.page.TableDataInfo;

/**
 * 审核明细Controller
 *
 * @author 邓
 * @date 2023-07-04
 */
@RestController
@RequestMapping("/comprehensive/auditDetails")
public class CphAuditDetailsController extends BaseController {
    @Autowired
    private ICphAuditDetailsService cphAuditDetailsService;

    @Autowired
    private ISysPostService sysPostService; //岗位
    @Autowired
    private ISrsStudentService _student;

    @PostMapping("/plusCheckRecord")
    public AjaxResult plusCheckRecord(@RequestBody CphAuditDetails param){
        return cphAuditDetailsService.plusCheckRecord(param);
    }


    @GetMapping("/listCheckAuditAndRecord")
    public AjaxResult listCheckAuditAndRecord(CheckIam param) {
        Map<String, Object> map = new HashMap<>();
        List<CphAuditDetails> list = cphAuditDetailsService.listCheckAudit(param);
        List<SrsAddRecord> srsAddRecordList = cphAuditDetailsService.ListCheckRecord(param);
        map.put("auditList", list);
        map.put("recordList", srsAddRecordList);
        return AjaxResult.success(map);
    }

    @GetMapping("/checkIamList")
    public AjaxResult checkIamList(){
        List<CheckIam> res = cphAuditDetailsService.checkIamList();
        return AjaxResult.success(res);
    }


    //清除审核状态不正确的加分记录
    @PostMapping("/cleanCancelRecord")
    public AjaxResult cleanCancelRecord(){
        return cphAuditDetailsService.cleanCancelRecord();
    }


    /***
     * 重新提交 （学生）
     * @author zhy
     * @date 2023-09-08 10:59:04
     * @param param
     * @return
     **/
    @PostMapping("/reSub")
    @RepeatSubmit(interval = 10000, message = "请不要重复提交")
    public AjaxResult reSub(@RequestBody CphResub param) {
        return success(cphAuditDetailsService.reSub(param));
    }

    /**
     * 取消审核的记录 (教务处)
     *
     * @param param
     * @return
     * @author zhy
     * @date 2023-09-06 11:42:59
     **/
    @PostMapping("/cancelProcess")
    @RepeatSubmit(interval = 10000, message = "请不要重复提交")
    public AjaxResult cancelProcess(@RequestBody CphCancel param) {
        Long uid = getUserId();
        boolean isJwc = RoleBool.isJwc(uid, sysPostService);
        if (!isJwc) {
            return AjaxResult.error(401, "你无权做这些");
        }
        return cphAuditDetailsService.cancelProcess(param);
    }

    /**
     * 列出自己的已处理 (辅导员)
     *
     * @return
     * @author zhy
     * @date 2023-09-09 15:53:05
     **/
    @GetMapping("/listOwnProcessed")
    public TableDataInfo listOwnProcessed(CphSearch param) {
        Long uid = getUserId();
        boolean isFdy = RoleBool.isFdy(uid, sysPostService);
        if (!isFdy) {
            return getDataTable(null);
        }
        String tNo = getUsername();
        param.setTNo(tNo);
        startPage();
        List<CphApply> list = cphAuditDetailsService.listOwnProcessed(param);
        return getDataTable(list);
    }

    /**
     * 列出自己的申请记录 (学生)
     *
     * @return
     * @author zhy
     * @date 2023-09-09 15:53:21
     **/
    @GetMapping("/listOwnApply")
    public TableDataInfo listOwnApply() {
        String stuNo = getUsername();
        startPage();
        List<CphApply> list = cphAuditDetailsService.listOwnApply(stuNo);
        return getDataTable(list);
    }

    /**
     * 审核时获取详细信息
     *
     * @return
     * @author zhy
     * @date 2023-07-06 16:01:46
     **/
    @PostMapping("/getDetail/{id}")
    public AjaxResult getDetailInfo(@PathVariable("id") Long id) {
        return cphAuditDetailsService.getDetail(id);
    }


    /**
     * 已处理查询
     *
     * @return
     * @author zhy
     * @date 2023-07-06 11:27:13
     **/
    @GetMapping("/myProcessedList")
    public TableDataInfo myProcessed(CphAuditDetails cphAuditDetails) {
        //startPage();
        List<CphAuditDetails> list = cphAuditDetailsService.listMyProcessed(cphAuditDetails, getUserId());
        return getDataTable(list);
    }

    /**
     * 查询审核完成
     *
     * @return
     */
    @GetMapping("/completed")
    public TableDataInfo getAuditCompleted() {
        CphAuditDetails cphAuditDetails = new CphAuditDetails();
        cphAuditDetails.setStatusCode(6L);
        startPage();
        List<CphAuditDetails> cphAuditDetails1 = cphAuditDetailsService.selectCphAuditDetailsList(cphAuditDetails);
        return getDataTable(cphAuditDetails1);

    }

    /**
     * 审核完成
     *
     * @return
     */
    @PostMapping("/completedFinish")
    public AjaxResult completedFinish(@RequestBody CphAuditDetails cphAuditDetails) {
        return toAjax(cphAuditDetailsService.completedFinish(cphAuditDetails));
    }

    /**
     * 查询审核明细列表
     */
    //@PreAuthorize("@ss.hasPermi('comprehensive:auditDetails:list')")
    @GetMapping("/list")
    public TableDataInfo list(CphAuditDetails cphAuditDetails) {
        startPage();
        List<CphAuditDetails> list = cphAuditDetailsService.searchCphAuditDetailsList(cphAuditDetails, getUserId());
        return getDataTable(list);
    }

    /**
     * 我的申请
     */
    //@PreAuthorize("@ss.hasPermi('comprehensive:auditDetails:list')")
    @GetMapping("/myApplyList")
    public TableDataInfo myApplyList(CphAuditDetails cphAuditDetails) {

        SrsStudent stu = _student.selectSrsStudentByStuNo(getUsername());
        if (stu != null) {
            cphAuditDetails.setSubmitterId(stu.getStuId());
        }
        startPage();
        List<CphAuditDetails> list = cphAuditDetailsService.selectCphAuditDetailsList(cphAuditDetails);
        return getDataTable(list);
    }

    /**
     * 我的审核
     */
    //@PreAuthorize("@ss.hasPermi('comprehensive:auditDetails:list')")
    @GetMapping("/myReviewList")
    public TableDataInfo myReviewList(CphAuditDetails cphAuditDetails) {
        //startPage();
        cphAuditDetails.setReviewedById(getUserId());
        List<CphAuditDetails> list = cphAuditDetailsService.searchCphAuditDetailsList(cphAuditDetails, getUserId());
        return getDataTable(list);
    }


    /**
     * 导出审核明细列表
     */
    //@PreAuthorize("@ss.hasPermi('comprehensive:auditDetails:export')")
    @Log(title = "审核明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CphAuditDetails cphAuditDetails) {
        List<CphAuditDetails> list = cphAuditDetailsService.selectCphAuditDetailsList(cphAuditDetails);
        ExcelUtil<CphAuditDetails> util = new ExcelUtil<CphAuditDetails>(CphAuditDetails.class);
        util.exportExcel(response, list, "审核明细数据");
    }

    /**
     * 获取审核明细详细信息
     */
    //@PreAuthorize("@ss.hasPermi('comprehensive:auditDetails:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cphAuditDetailsService.selectCphAuditDetailsById(id));
    }

    /**
     * 新增审核明细
     */
    //@PreAuthorize("@ss.hasPermi('comprehensive:auditDetails:add')")
    @Log(title = "审核明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CphAuditDetails cphAuditDetails) {
        return toAjax(cphAuditDetailsService.insertCphAuditDetails(cphAuditDetails));
    }

    /**
     * 修改审核明细
     */
    //@PreAuthorize("@ss.hasPermi('comprehensive:auditDetails:edit')")
    @Log(title = "审核明细", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody CphAuditDetails cphAuditDetails) {
        return toAjax(cphAuditDetailsService.updateCphAuditDetails(cphAuditDetails));
    }

    /**
     * 审核操作
     */
    //@PreAuthorize("@ss.hasPermi('comprehensive:auditDetails:edit')")
    @Log(title = "审核操作", businessType = BusinessType.UPDATE)
    @PostMapping("/auditOpera")
    @RepeatSubmit(interval = 10000, message = "请不要重复提交")
    public AjaxResult auditOpera(@RequestBody CphAuditDetails cphAuditDetails) {
        cphAuditDetailsService.sendMsg(getUserId(), cphAuditDetails);
        return toAjax(cphAuditDetailsService.auditOpera(cphAuditDetails));
    }

    /**
     * 批量审核操作
     */
    @Log(title = "批量审核操作", businessType = BusinessType.UPDATE)
    @PostMapping("/auditOperaBatch")
    @RepeatSubmit(interval = 10000, message = "请不要重复提交")
    public AjaxResult auditOperaBatch(@RequestBody Long[] ids) {
        return toAjax(cphAuditDetailsService.auditOperaBatch(ids, getUserId()));
    }

    /**
     * 删除审核明细
     */
    //@PreAuthorize("@ss.hasPermi('comprehensive:auditDetails:remove')")
    @Log(title = "审核明细", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cphAuditDetailsService.deleteCphAuditDetailsByIds(ids));
    }
    /**
     * 撤销审核
     */
    @PostMapping("/cancelAudit/{id}")
    public AjaxResult cancelAudit(@PathVariable("id") Long id) {
        return toAjax(cphAuditDetailsService.cancelAuditById(id));
    }
}
