package com.ruoyi.web.controller.system;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.domain.dto.villageCommitteeRole.VillageCommitteeRoleAddRequest;
import com.ruoyi.web.domain.dto.villageCommitteeRole.VillageCommitteeRoleEditRequest;
import com.ruoyi.web.domain.dto.villageCommitteeRole.VillageCommitteeRoleQueryRequest;
import com.ruoyi.web.domain.entity.VillageCommitteeRole;
import com.ruoyi.web.service.VillageCommitteeRoleService;
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.List;
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/village/committee/role")
public class VillageCommitteeRoleController {

    @Autowired
    private VillageCommitteeRoleService villageCommitteeRoleService;

    /**
     * 添加村委类型
     *
     * @param villageCommitteeRoleAddRequest
     * @return
     */
    @ApiOperation("添加村委类型")
    @PostMapping("/add")
    public AjaxResult addVillageCommitteeRole(@RequestBody VillageCommitteeRoleAddRequest villageCommitteeRoleAddRequest) {
        if (villageCommitteeRoleAddRequest == null) {
            return error("请求参数为空");
        }
        Integer id = villageCommitteeRoleService.addVillageCommitteeRole(villageCommitteeRoleAddRequest);
        return success(id);
    }

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

    /**
     * 编辑村委类型
     *
     * @param villageCommitteeRoleEditRequest
     * @return
     */
    @ApiOperation("编辑村委类型")
    @PostMapping("/edit")
    public AjaxResult editVillageCommitteeRole(@RequestBody VillageCommitteeRoleEditRequest villageCommitteeRoleEditRequest) {
        if (villageCommitteeRoleEditRequest == null) {
            return error("请求参数为空");
        }
        villageCommitteeRoleService.editVillageCommitteeRole(villageCommitteeRoleEditRequest);
        return AjaxResult.success();
    }


    /**
     * 根据id获取村委类型
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取村委类型")
    @GetMapping("/get")
    public AjaxResult getVillageCommitteeRoleById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        VillageCommitteeRole villageCommitteeRoleById = villageCommitteeRoleService.getVillageCommitteeRoleById(id);
        return success(villageCommitteeRoleById);
    }


    /**
     * 获取村委类型列表
     *
     * @return
     */
    @ApiOperation("获取村委类型列表")
    @PostMapping("/list")
    public AjaxResult listVillageCommitteeRoleByPage(@RequestBody VillageCommitteeRoleQueryRequest villageCommitteeRoleQueryRequest) {
        if (villageCommitteeRoleQueryRequest == null) {
            throw new ServiceException("请求参数未空");
        }
        List<VillageCommitteeRole> list = villageCommitteeRoleService.getListVillageCommitteeRole(villageCommitteeRoleQueryRequest);
        return success(list);
    }
}
