package com.ruoyi.web.controller.system;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.domain.dto.projects.ProjectsAddRequest;
import com.ruoyi.web.domain.dto.projects.ProjectsCompletedRequest;
import com.ruoyi.web.domain.dto.projects.ProjectsEditRequest;
import com.ruoyi.web.domain.dto.projects.ProjectsQueryRequest;
import com.ruoyi.web.domain.vo.ProjectsVO;
import com.ruoyi.web.service.ProjectsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

@Api(tags ="项目信息管理")
@RestController
@RequestMapping("/system/projects")
public class ProjectsController {

    @Autowired
    private ProjectsService projectsService;

    /**
     * 添加项目
     *
     * @param projectsAddRequest
     * @return
     */
    @ApiOperation("添加项目")
    @PostMapping("/add")
    public AjaxResult addProjects(@RequestBody ProjectsAddRequest projectsAddRequest) {
        if (projectsAddRequest == null) {
            return error("请求参数为空");
        }
        Integer id = projectsService.addProjects(projectsAddRequest);
        return success(id);
    }

    /**
     * 删除项目
     *
     * @param paramsMap
     * @return
     */
    @ApiOperation("删除项目")
    @PostMapping("/delete")
    public AjaxResult deleteProjects(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        if (StrUtil.isBlank(ids)) {
            return error("请求参数为空");
        }
        boolean b = projectsService.deleteProjects(ids);
        if (b) {
            return success("删除成功");
        }
        return error("删除失败");
    }


    /**
     * 编辑项目
     *
     * @param projectsEditRequest
     * @return
     */
    @ApiOperation("编辑项目")
    @PostMapping("/edit")
    public AjaxResult editProjects(@RequestBody ProjectsEditRequest projectsEditRequest) {
        if (projectsEditRequest == null) {
            return error("请求参数为空");
        }
        projectsService.editProjects(projectsEditRequest);
        return AjaxResult.success();
    }

    /**
     * 项目建成
     *
     * @param projectsCompletedRequest
     * @return
     */
    @ApiOperation("项目建成")
    @PostMapping("/completed")
    public AjaxResult completedProjects(@RequestBody ProjectsCompletedRequest projectsCompletedRequest) {
        Integer id = projectsCompletedRequest.getId();
        Date actualCompletionDate = projectsCompletedRequest.getActualCompletionDate();
        if (id <= 0 || actualCompletionDate == null) {
            return error("请求参数为空");
        }
        boolean b = projectsService.completedProjects(id, actualCompletionDate);
        if (b) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.error("操作失败");
        }

    }

    /**
     * 根据id获取项目
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取项目")
    @GetMapping("/get")
    public AjaxResult getProjectsById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        ProjectsVO projectsVO = projectsService.getProjectsById(id);
        return success(projectsVO);
    }

    /**
     * 分页获取项目列表  mybatis-plus
     *
     * @param projectsQueryRequest
     * @return
     */
    @ApiOperation("分页获取项目列表")
    @PostMapping("/list/page")
    public AjaxResult listProjectsByPage(@RequestBody ProjectsQueryRequest projectsQueryRequest) {
        if (projectsQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<ProjectsVO> listProjectsVOByPage = projectsService.getListProjectsByPage(projectsQueryRequest);
        return success(listProjectsVOByPage);
    }

}
