package com.njcn.rdms.module.project.controller.admin.workreport.monthly;

import com.njcn.rdms.framework.apilog.core.annotation.ApiAccessLog;
import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.framework.excel.core.util.ExcelUtils;
import com.njcn.rdms.module.project.constant.WorkReportConstants;
import com.njcn.rdms.module.project.controller.admin.workreport.common.WorkReportExportResponseUtils;
import com.njcn.rdms.module.project.controller.admin.workreport.common.vo.WorkReportStatusActionReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.common.vo.WorkReportStatusLogRespVO;
import com.njcn.rdms.module.project.controller.admin.workreport.monthly.vo.MonthlyReportApprovalRecordRespVO;
import com.njcn.rdms.module.project.controller.admin.workreport.monthly.vo.MonthlyReportApproveReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.monthly.vo.MonthlyReportContentExportReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.monthly.vo.MonthlyReportDefaultDraftReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.monthly.vo.MonthlyReportExportVO;
import com.njcn.rdms.module.project.controller.admin.workreport.monthly.vo.MonthlyReportPageReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.monthly.vo.MonthlyReportRefreshDraftReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.monthly.vo.MonthlyReportRespVO;
import com.njcn.rdms.module.project.controller.admin.workreport.monthly.vo.MonthlyReportSaveReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.monthly.vo.MonthlyReportSignOffConfirmReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.monthly.vo.MonthlyReportSignOffDetailRespVO;
import com.njcn.rdms.module.project.service.workreport.export.WorkReportContentExportService;
import com.njcn.rdms.module.project.service.workreport.monthly.MonthlyReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

import static com.njcn.rdms.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.njcn.rdms.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 个人月报")
@RestController
@RequestMapping("/project/work-reports/monthly")
@Validated
public class MonthlyReportController {

    @Resource
    private MonthlyReportService monthlyReportService;
    @Resource
    private WorkReportContentExportService workReportContentExportService;

    @GetMapping("/init")
    @Operation(summary = "获取月报新建默认数据")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_CREATE + "')")
    public CommonResult<MonthlyReportRespVO> initMonthlyReport() {
        return success(monthlyReportService.initMonthlyReport());
    }

    @GetMapping("/default-draft")
    @Operation(summary = "预览月报默认稿")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_CREATE + "')")
    public CommonResult<MonthlyReportRespVO> previewMonthlyDefaultDraft(@Valid MonthlyReportDefaultDraftReqVO reqVO) {
        return success(monthlyReportService.previewMonthlyDefaultDraft(reqVO));
    }

    @PostMapping("/refresh-draft")
    @Operation(summary = "手动刷新月报默认稿")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_CREATE + "')")
    public CommonResult<MonthlyReportRespVO> refreshMonthlyDraft(@Valid @RequestBody MonthlyReportRefreshDraftReqVO reqVO) {
        return success(monthlyReportService.refreshMonthlyDraft(reqVO));
    }

    @PostMapping
    @Operation(summary = "新建月报草稿")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_CREATE + "')")
    public CommonResult<Long> createMonthlyReport(@Valid @RequestBody MonthlyReportSaveReqVO reqVO) {
        return success(monthlyReportService.createMonthlyReport(reqVO));
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改月报草稿")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_UPDATE + "')")
    public CommonResult<Boolean> updateMonthlyReport(@PathVariable("id") Long id,
                                                     @Valid @RequestBody MonthlyReportSaveReqVO reqVO) {
        monthlyReportService.updateMonthlyReport(id, reqVO);
        return success(true);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取月报详情")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_QUERY + "')")
    public CommonResult<MonthlyReportRespVO> getMonthlyReport(@PathVariable("id") Long id) {
        return success(monthlyReportService.getMonthlyReport(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获取我的月报分页")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_QUERY + "')")
    public CommonResult<PageResult<MonthlyReportRespVO>> getMonthlyReportPage(@Valid MonthlyReportPageReqVO reqVO) {
        return success(monthlyReportService.getMonthlyReportPage(reqVO));
    }

    @GetMapping("/approval-page")
    @Operation(summary = "获取待我审批的月报分页")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_APPROVE + "')")
    public CommonResult<PageResult<MonthlyReportRespVO>> getMonthlyApprovalPage(@Valid MonthlyReportPageReqVO reqVO) {
        return success(monthlyReportService.getMonthlyApprovalPage(reqVO));
    }

    @GetMapping("/sign-off-page")
    @Operation(summary = "获取待我确认的月报分页")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_SIGN_OFF_QUERY + "')")
    public CommonResult<PageResult<MonthlyReportRespVO>> getMonthlySignOffPage(@Valid MonthlyReportPageReqVO reqVO) {
        return success(monthlyReportService.getMonthlySignOffPage(reqVO));
    }

    @GetMapping("/{id}/sign-off-detail")
    @Operation(summary = "获取月报下签详情")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_SIGN_OFF_QUERY + "')")
    public CommonResult<MonthlyReportSignOffDetailRespVO> getMonthlySignOffDetail(@PathVariable("id") Long id) {
        return success(monthlyReportService.getMonthlySignOffDetail(id));
    }

    @PostMapping("/{id}/submit")
    @Operation(summary = "提交月报")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_UPDATE + "')")
    public CommonResult<Boolean> submitMonthlyReport(@PathVariable("id") Long id) {
        monthlyReportService.submitMonthlyReport(id);
        return success(true);
    }

    @PostMapping("/{id}/approve")
    @Operation(summary = "审批通过月报")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_APPROVE + "')")
    public CommonResult<Boolean> approveMonthlyReport(@PathVariable("id") Long id,
                                                      @Valid @RequestBody MonthlyReportApproveReqVO reqVO) {
        monthlyReportService.approveMonthlyReport(id, reqVO);
        return success(true);
    }

    @PostMapping("/{id}/reject")
    @Operation(summary = "退回月报")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_APPROVE + "')")
    public CommonResult<Boolean> rejectMonthlyReport(@PathVariable("id") Long id,
                                                     @Valid @RequestBody WorkReportStatusActionReqVO reqVO) {
        monthlyReportService.rejectMonthlyReport(id, reqVO);
        return success(true);
    }

    @PostMapping("/{id}/confirm-sign-off")
    @Operation(summary = "确认月报下签")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_SIGN_OFF_CONFIRM + "')")
    public CommonResult<Boolean> confirmMonthlySignOff(@PathVariable("id") Long id,
                                                       @Valid @RequestBody MonthlyReportSignOffConfirmReqVO reqVO) {
        monthlyReportService.confirmMonthlySignOff(id, reqVO);
        return success(true);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除月报")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_DELETE + "')")
    public CommonResult<Boolean> deleteMonthlyReport(@PathVariable("id") Long id) {
        monthlyReportService.deleteMonthlyReport(id);
        return success(true);
    }

    @GetMapping("/{id}/status-logs")
    @Operation(summary = "获取月报状态日志")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_QUERY + "')")
    public CommonResult<List<WorkReportStatusLogRespVO>> getMonthlyStatusLogs(@PathVariable("id") Long id) {
        return success(monthlyReportService.getMonthlyStatusLogs(id));
    }

    @GetMapping("/{id}/approval-records")
    @Operation(summary = "获取月报审批记录")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_QUERY + "')")
    public CommonResult<List<MonthlyReportApprovalRecordRespVO>> getMonthlyApprovalRecords(@PathVariable("id") Long id) {
        return success(monthlyReportService.getMonthlyApprovalRecords(id));
    }

    @GetMapping("/export")
    @Operation(summary = "导出我的月报")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_EXPORT + "')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMonthlyReport(@Valid MonthlyReportPageReqVO reqVO, HttpServletResponse response)
            throws IOException {
        ExcelUtils.write(response, "个人月报_" + LocalDate.now() + ".xls", "个人月报",
                MonthlyReportExportVO.class, monthlyReportService.getMonthlyExportList(reqVO));
    }

    @PostMapping("/content-export")
    @Operation(summary = "导出月报内容 Word")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_EXPORT + "')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMonthlyReportContent(@Valid @RequestBody MonthlyReportContentExportReqVO reqVO,
                                           HttpServletResponse response) throws IOException {
        WorkReportExportResponseUtils.write(response, workReportContentExportService.exportMonthly(reqVO));
    }
}
