package org.springblade.modules.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.modules.project.pojo.dto.*;
import org.springblade.modules.project.pojo.vo.*;
import org.springblade.modules.project.service.IProjectInfoService;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Param
 * @Author wangwl
 * @Date 2024/4/8 11:28
 **/
@NonDS
@RestController
@AllArgsConstructor
@RequestMapping(  "/blade-attach/project")
@Tag(name = "项目信息接口", description = "项目信息接口")
public class ProjectInfoController extends BladeController {

    private final IProjectInfoService projectInfoService;

    private final JdbcTemplate jdbcTemplate;

    @PostMapping("/add")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "新增项目", description = "新增项目，传入项目信息和每一年的计划数组")
    public R add(@RequestBody ProjectInfoDTO dto) {
        projectInfoService.add(dto);
        return R.success("新增成功");
    }

    @PostMapping("/page")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "分页", description = "分页查询项目")
    public R<PageVO<ProjectInfoVO>> page(@RequestBody ProjectInfoPageDTO dto) {
        PageVO<ProjectInfoVO> page =  projectInfoService.page2(dto);
        return R.data(page);
    }

    @GetMapping("/detail")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "项目详情", description = "项目详情，传入项目id")
    public R<ProjectInfoDetailVO> detail(Long id) {
        ProjectInfoDetailVO vo =  projectInfoService.detail(id);
        return R.data(vo);
    }

    @PostMapping("/update")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "修改项目", description = "修改项目，传入项目信息和每一年的计划数组")
    public R update(@RequestBody ProjectInfoDetailVO dto) {
        projectInfoService.update2(dto);
        return R.success("修改成功");
    }

    @GetMapping("/project-and-plan-detail")
    @ApiOperationSupport(order = 5)
    @Operation(summary = "项目与计划详情", description = "项目与计划详情，传入项目id")
    public R<ProjectAndPlanDetailVO> detail2(Long id) {
        ProjectAndPlanDetailVO vo =  projectInfoService.detail2(id);
        return R.data(vo);
    }

    @PostMapping("/update-project-finished")
    @ApiOperationSupport(order = 6)
    @Operation(summary = "修改项目完成情况", description = "修改项目完成情况,传入项目信息与计划年份和每年12个月的详情")
    public R<String> updateFinished(@RequestBody ProjectAndPlanDetailVO vo) {
        projectInfoService.updateFinished(vo);
        return R.success("修改成功");
    }

    @PostMapping("/delete")
    @ApiOperationSupport(order = 7)
    @Operation(summary = "项目删除", description = "传入项目id拼接字符串")
    public R<String> delete(@RequestBody ProjectInfoPageDTO dto) {
        projectInfoService.delete(dto);
        return R.success("删除成功");
    }

    @GetMapping("/data-project-stat")
    @ApiOperationSupport(order = 8)
    @Operation(summary = "数据看板-项目统计", description = "传入搜索条件")
    public R<DataProjectStatVO> dataProjectStat(DataStatDTO dto) {
        DataProjectStatVO vo = projectInfoService.dataProjectStat(dto);
        return R.data(vo);
    }

    @GetMapping("/data-invest-stat")
    @ApiOperationSupport(order = 9)
    @Operation(summary = "数据看板-投资数据", description = "传入搜索条件")
    public R<DataInvestStatVO> dataInvestStat(DataStatDTO dto) {
        DataInvestStatVO vo = projectInfoService.dataInvestStat(dto);
        return R.data(vo);
    }

    @GetMapping("/data-schedule-stat")
    @ApiOperationSupport(order = 10)
    @Operation(summary = "数据看板-进度数据", description = "传入搜索条件")
    public R<DataScheduleStatVO> dataScheduleStat(DataStatDTO dto) {
        DataScheduleStatVO vo = projectInfoService.dataScheduleStat(dto);
        return R.data(vo);
    }

    @GetMapping("/data-end-date")
    @ApiOperationSupport(order = 11)
    @Operation(summary = "数据看板-截至时间", description = "没有参数")
    public R<String> dataEndDate() {
        return R.data(projectInfoService.dataEndDate());
    }

    @PostMapping("/save-warning")
    @ApiOperationSupport(order = 12)
    @Operation(summary = "项目设置保存预警", description = "传入一个数组，2个对象，每个对象type区分")
    public R<String> saveWarning(@RequestBody ProjectWarningDTO dto) {
        projectInfoService.saveWarning(dto);
        return R.success("保存成功");
    }

    @PostMapping("/detail-warning")
    @ApiOperationSupport(order = 13)
    @Operation(summary = "项目预警详情", description = "返回项目预警信息")
    public R<ProjectWarningDTO> detailWarning() {
        ProjectWarningDTO dto = projectInfoService.detailWarning();
        return R.data(dto);
    }

    @PostMapping("/page-warning")
    @ApiOperationSupport(order = 14)
    @Operation(summary = "异常管理-分页", description = "传入分页和搜索参数")
    public R<IPage<pageWarningVO>> pageWarning(@RequestBody pageWarningDTO dto) {
        IPage<pageWarningVO> page = projectInfoService.pageWarning(dto);
        return R.data(page);
    }

    @PostMapping("/repeal-warning")
    @ApiOperationSupport(order = 15)
    @Operation(summary = "异常管理-撤销", description = "传入分页和搜索参数")
    public R<String> repealWarning(@RequestBody ProjectRepealDTO dto) {
        projectInfoService.repealWarning(dto);
        return R.success("撤销成功");
    }

    @PostMapping("/import-temp")
    @ApiOperationSupport(order = 16)
    @Operation(summary = "项目导入模板", description = "")
    public R<String> importProject() {
        String url = jdbcTemplate.queryForObject("select dict_value from blade_dict where code = 'project_template' and dict_key = 1 and is_deleted = 0", String.class);
        return R.data(url);
    }

    @PostMapping("/import-project")
    @ApiOperationSupport(order = 17)
    @Operation(summary = "项目导入", description = "传入Excel")
    public R importProject(MultipartFile file) {
        return projectInfoService.importProject(file);
    }

    @GetMapping("/build-report")
    @ApiOperationSupport(order = 18)
    @Operation(summary = "项目生成报告", description = "传入年和季度，返回生成文件")
    @Parameters({
            @Parameter(name = "year", description = "年：2024",required = true),
            @Parameter(name = "quarter", description = "季度：1234",required = true)
    })
    public ResponseEntity<FileSystemResource> buildReport(Integer year, Integer quarter, HttpServletResponse response) {
        return projectInfoService.buildReport(year,quarter,response);
    }

    @GetMapping("/get-project-year")
    @ApiOperationSupport(order = 19)
    @Operation(summary = "获取项目所有年份", description = "返回所有年数组")
    public R<List<Integer>> getProjectYear() {
        return projectInfoService.getProjectYear();
    }

}
