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

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.WorkReportApprovalRecordRespVO;
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.project.vo.ProjectReportContentExportReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.project.vo.ProjectReportDefaultDraftReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.project.vo.ProjectReportExportVO;
import com.njcn.rdms.module.project.controller.admin.workreport.project.vo.ProjectReportOwnerProjectOptionRespVO;
import com.njcn.rdms.module.project.controller.admin.workreport.project.vo.ProjectReportPageReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.project.vo.ProjectReportRefreshDraftReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.project.vo.ProjectReportRespVO;
import com.njcn.rdms.module.project.controller.admin.workreport.project.vo.ProjectReportSaveReqVO;
import com.njcn.rdms.module.project.service.workreport.export.WorkReportContentExportService;
import com.njcn.rdms.module.project.service.workreport.project.ProjectReportService;
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.RequestParam;
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/project")
@Validated
public class ProjectReportController {

    @Resource
    private ProjectReportService projectReportService;
    @Resource
    private WorkReportContentExportService workReportContentExportService;

    @GetMapping("/owner-project-options")
    @Operation(summary = "获取项目半月报负责人可选项目列表")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_PROJECT_OWNER + "')")
    public CommonResult<List<ProjectReportOwnerProjectOptionRespVO>> getOwnerProjectOptions() {
        return success(projectReportService.getOwnerProjectOptions());
    }

    @GetMapping("/init")
    @Operation(summary = "获取项目半月报新建默认数据")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_PROJECT_OWNER + "')")
    public CommonResult<ProjectReportRespVO> initProjectReport(@RequestParam("projectId") Long projectId) {
        return success(projectReportService.initProjectReport(projectId));
    }

    @GetMapping("/{projectId}/default-draft")
    @Operation(summary = "预览项目半月报默认稿")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_PROJECT_OWNER + "')")
    public CommonResult<ProjectReportRespVO> previewProjectDefaultDraft(@PathVariable("projectId") Long projectId,
                                                                        @Valid ProjectReportDefaultDraftReqVO reqVO) {
        return success(projectReportService.previewProjectDefaultDraft(projectId, reqVO));
    }

    @PostMapping("/{projectId}/refresh-draft")
    @Operation(summary = "手动刷新项目半月报默认稿")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_PROJECT_OWNER + "')")
    public CommonResult<ProjectReportRespVO> refreshProjectDraft(@PathVariable("projectId") Long projectId,
                                                                 @Valid @RequestBody ProjectReportRefreshDraftReqVO reqVO) {
        return success(projectReportService.refreshProjectDraft(projectId, reqVO));
    }

    @PostMapping
    @Operation(summary = "新建项目半月报草稿")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_PROJECT_OWNER + "')")
    public CommonResult<Long> createProjectReport(@Valid @RequestBody ProjectReportSaveReqVO reqVO) {
        return success(projectReportService.createProjectReport(reqVO));
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改项目半月报草稿")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_PROJECT_OWNER + "')")
    public CommonResult<Boolean> updateProjectReport(@PathVariable("id") Long id,
                                                     @Valid @RequestBody ProjectReportSaveReqVO reqVO) {
        projectReportService.updateProjectReport(id, reqVO);
        return success(true);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取项目半月报详情")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_QUERY + "')")
    public CommonResult<ProjectReportRespVO> getProjectReport(@PathVariable("id") Long id) {
        return success(projectReportService.getProjectReport(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获取我的项目半月报分页")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_QUERY + "')")
    public CommonResult<PageResult<ProjectReportRespVO>> getProjectReportPage(@Valid ProjectReportPageReqVO reqVO) {
        return success(projectReportService.getProjectReportPage(reqVO));
    }

    @GetMapping("/approval-page")
    @Operation(summary = "获取待我审批的项目半月报分页")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_APPROVE + "')")
    public CommonResult<PageResult<ProjectReportRespVO>> getProjectApprovalPage(@Valid ProjectReportPageReqVO reqVO) {
        return success(projectReportService.getProjectApprovalPage(reqVO));
    }

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

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

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

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

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

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

    @GetMapping("/export")
    @Operation(summary = "导出我的项目半月报")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_EXPORT + "')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProjectReport(@Valid ProjectReportPageReqVO reqVO, HttpServletResponse response)
            throws IOException {
        ExcelUtils.write(response, "项目半月报_" + LocalDate.now() + ".xls", "项目半月报",
                ProjectReportExportVO.class, projectReportService.getProjectExportList(reqVO));
    }

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