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.subsidyProjects.SubsidyProjectsAddRequest;
import com.ruoyi.web.domain.dto.subsidyProjects.SubsidyProjectsEditRequest;
import com.ruoyi.web.domain.dto.subsidyProjects.SubsidyProjectsQueryRequest;
import com.ruoyi.web.domain.vo.SubsidyProjectsVO;
import com.ruoyi.web.service.SubsidyProjectsService;
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.Map;

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

@Api(tags ="补贴-项目信息管理")
@RestController
@RequestMapping("/system/subsidy/projects")
public class SubsidyProjectsController {

    @Autowired
    private SubsidyProjectsService subsidyProjectsService;

    /**
     * 添加惠农补贴项目
     *
     * @param subsidyProjectsAddRequest
     * @return
     */
    @ApiOperation("添加惠农补贴项目")
    @PostMapping("/add")
    public AjaxResult addSubsidyProjects(@RequestBody SubsidyProjectsAddRequest subsidyProjectsAddRequest) {
        if (subsidyProjectsAddRequest == null) {
            return error("请求参数为空");
        }
        Long id = subsidyProjectsService.addSubsidyProjects(subsidyProjectsAddRequest);
        return success(id);
    }

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


    /**
     * 编辑惠农补贴项目
     *
     * @param subsidyProjectsEditRequest
     * @return
     */
    @ApiOperation("编辑惠农补贴项目")
    @PostMapping("/edit")
    public AjaxResult editSubsidyProjects(@RequestBody SubsidyProjectsEditRequest subsidyProjectsEditRequest) {
        if (subsidyProjectsEditRequest == null) {
            return error("请求参数为空");
        }
        subsidyProjectsService.editSubsidyProjects(subsidyProjectsEditRequest);
        return AjaxResult.success();
    }


    /**
     * 根据id获取补贴项目
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取补贴项目")
    @GetMapping("/get")
    public AjaxResult getSubsidyProjectsById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        SubsidyProjectsVO subsidyProjectsVO = subsidyProjectsService.getSubsidyProjectsById(id);
        return success(subsidyProjectsVO);
    }

    /**
     * 分页获取补贴-项目列表
     *
     * @param subsidyProjectsQueryRequest
     * @return
     */
    @ApiOperation("分页获取补贴-项目列表")
    @PostMapping("/list/page")
    public AjaxResult listSubsidyProjectsByPage(@RequestBody SubsidyProjectsQueryRequest subsidyProjectsQueryRequest) {
        if (subsidyProjectsQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<SubsidyProjectsVO> listSubsidyProjectsByPageVO = subsidyProjectsService.getListSubsidyProjectsByPage(subsidyProjectsQueryRequest);
        return success(listSubsidyProjectsByPageVO);
    }

}
