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

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.weekly.vo.WeeklyReportContentExportReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.weekly.vo.WeeklyReportDefaultDraftReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.weekly.vo.WeeklyReportExportVO;
import com.njcn.rdms.module.project.controller.admin.workreport.weekly.vo.WeeklyReportPageReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.weekly.vo.WeeklyReportRefreshDraftReqVO;
import com.njcn.rdms.module.project.controller.admin.workreport.weekly.vo.WeeklyReportRespVO;
import com.njcn.rdms.module.project.controller.admin.workreport.weekly.vo.WeeklyReportSaveReqVO;
import com.njcn.rdms.module.project.service.workreport.export.WorkReportContentExportService;
import com.njcn.rdms.module.project.service.workreport.weekly.WeeklyReportService;
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/weekly")
@Validated
public class WeeklyReportController {

    @Resource
    private WeeklyReportService weeklyReportService;
    @Resource
    private WorkReportContentExportService workReportContentExportService;

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

    @GetMapping("/default-draft")
    @Operation(summary = "预览周报默认稿")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_CREATE + "')")
    public CommonResult<WeeklyReportRespVO> previewWeeklyDefaultDraft(@Valid WeeklyReportDefaultDraftReqVO reqVO) {
        return success(weeklyReportService.previewWeeklyDefaultDraft(reqVO));
    }

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

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获取我的周报分页")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_QUERY + "')")
    public CommonResult<PageResult<WeeklyReportRespVO>> getWeeklyReportPage(@Valid WeeklyReportPageReqVO reqVO) {
        return success(weeklyReportService.getWeeklyReportPage(reqVO));
    }

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

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

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

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

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

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

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

    @GetMapping("/export")
    @Operation(summary = "导出我的周报")
    @PreAuthorize("@ss.hasPermission('" + WorkReportConstants.PERMISSION_EXPORT + "')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWeeklyReport(@Valid WeeklyReportPageReqVO reqVO, HttpServletResponse response)
            throws IOException {
        ExcelUtils.write(response, "个人周报_" + LocalDate.now() + ".xls", "个人周报",
                WeeklyReportExportVO.class, weeklyReportService.getWeeklyExportList(reqVO));
    }

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