package com.ruoyi.ccdi.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectImportHistoryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiProjectSaveDTO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectHistoryListItemVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectStatusCountsVO;
import com.ruoyi.ccdi.project.domain.vo.CcdiProjectVO;
import com.ruoyi.ccdi.project.service.ICcdiProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 纪检初核项目管理Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/ccdi/project")
@Tag(name = "纪检初核项目管理")
public class CcdiProjectController extends BaseController {

    @Resource
    private ICcdiProjectService projectService;

    /**
     * 创建项目
     */
    @PostMapping
    @Operation(summary = "创建项目")
    @PreAuthorize("@ss.hasPermi('ccdi:project:add')")
    public AjaxResult createProject(@Validated @RequestBody CcdiProjectSaveDTO dto) {
        CcdiProjectVO vo = projectService.createProject(dto);
        return AjaxResult.success("项目创建成功", vo);
    }

    /**
     * 更新项目
     */
    @PutMapping
    @Operation(summary = "更新项目")
    @PreAuthorize("@ss.hasPermi('ccdi:project:edit')")
    public AjaxResult updateProject(@Validated @RequestBody CcdiProjectSaveDTO dto) {
        CcdiProjectVO vo = projectService.updateProject(dto);
        return AjaxResult.success("项目更新成功", vo);
    }

    /**
     * 归档项目
     */
    @PostMapping("/{projectId}/archive")
    @Operation(summary = "归档项目")
    @PreAuthorize("@ss.hasPermi('ccdi:project:edit')")
    public AjaxResult archiveProject(@PathVariable Long projectId) {
        projectService.archiveProject(projectId, SecurityUtils.getUsername());
        return AjaxResult.success("项目归档成功");
    }

    /**
     * 删除项目
     */
    @DeleteMapping("/{projectId}")
    @Operation(summary = "删除项目")
    @PreAuthorize("@ss.hasPermi('ccdi:project:list')")
    public AjaxResult deleteProject(@PathVariable Long projectId) {
        boolean success = projectService.deleteProject(projectId, SecurityUtils.getUsername());
        return success ? AjaxResult.success("项目删除成功") : AjaxResult.error("项目删除失败");
    }

    /**
     * 恢复项目
     */
    @PostMapping("/{projectId}/restore")
    @Operation(summary = "恢复项目")
    @PreAuthorize("@ss.hasPermi('ccdi:project:edit')")
    public AjaxResult restoreProject(@PathVariable Long projectId) {
        projectService.restoreProject(projectId, SecurityUtils.getUsername());
        return AjaxResult.success("项目恢复成功");
    }

    /**
     * 查询项目详情
     */
    @GetMapping("/{projectId}")
    @Operation(summary = "查询项目详情")
    public AjaxResult getProject(@PathVariable Long projectId) {
        CcdiProjectVO vo = projectService.getProjectById(projectId);
        return AjaxResult.success(vo);
    }

    /**
     * 查询项目列表（分页）
     */
    @GetMapping("/list")
    @Operation(summary = "查询项目列表")
    public TableDataInfo listProject(CcdiProjectQueryDTO queryDTO) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<CcdiProjectVO> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        Page<CcdiProjectVO> result = projectService.selectProjectPage(page, queryDTO);
        return getDataTable(result.getRecords(), result.getTotal());
    }

    /**
     * 查询历史项目列表
     */
    @GetMapping("/history")
    @Operation(summary = "查询历史项目列表")
    public AjaxResult listHistoryProjects(CcdiProjectQueryDTO queryDTO) {
        List<CcdiProjectHistoryListItemVO> result = projectService.listHistoryProjects(queryDTO);
        return AjaxResult.success(result);
    }

    /**
     * 从历史项目导入
     */
    @PostMapping("/import")
    @Operation(summary = "导入历史项目")
    @PreAuthorize("@ss.hasPermi('ccdi:project:add')")
    public AjaxResult importFromHistory(@Validated @RequestBody CcdiProjectImportHistoryDTO dto) {
        CcdiProjectVO vo = projectService.importFromHistory(dto, SecurityUtils.getUsername());
        return AjaxResult.success("项目创建成功", vo);
    }

    /**
     * 查询项目状态统计
     */
    @GetMapping("/statusCounts")
    @Operation(summary = "查询项目状态统计")
    public AjaxResult getStatusCounts() {
        CcdiProjectStatusCountsVO counts = projectService.getStatusCounts();
        return AjaxResult.success(counts);
    }
}
