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.villageTradition.VillageTraditionAddRequest;
import com.ruoyi.web.domain.dto.villageTradition.VillageTraditionEditRequest;
import com.ruoyi.web.domain.dto.villageTradition.VillageTraditionQueryRequest;
import com.ruoyi.web.domain.entity.VillageTradition;
import com.ruoyi.web.service.VillageTraditionService;
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/village/tradition")
public class VillageTraditionController {

    @Autowired
    private VillageTraditionService villageTraditionService;

    /**
     * 添加传统产业
     *
     * @param villageTraditionAddRequest
     * @return
     */
    @ApiOperation("添加传统产业")
    @PostMapping("/add")
    public AjaxResult addVillageTradition(@RequestBody VillageTraditionAddRequest villageTraditionAddRequest) {
        if (villageTraditionAddRequest == null) {
            return error("请求参数为空");
        }
        Integer id = villageTraditionService.addVillageTradition(villageTraditionAddRequest);
        return success(id);
    }

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


    /**
     * 编辑传统产业
     *
     * @param villageTraditionEditRequest
     * @return
     */
    @ApiOperation("编辑传统产业")
    @PostMapping("/edit")
    public AjaxResult editVillageTradition(@RequestBody VillageTraditionEditRequest villageTraditionEditRequest) {
        if (villageTraditionEditRequest == null) {
            return error("请求参数为空");
        }
        villageTraditionService.editVillageTradition(villageTraditionEditRequest);
        return AjaxResult.success();
    }


    /**
     * 根据id获取传统产业
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取传统产业")
    @GetMapping("/get")
    public AjaxResult getVillageTraditionById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        VillageTradition villageTradition = villageTraditionService.getVillageTraditionById(id);
        return success(villageTradition);
    }

    /**
     * 分页获取传统产业列表
     *
     * @param villageTraditionQueryRequest
     * @return
     */
    @ApiOperation("分页获取传统产业列表")
    @PostMapping("/list/page")
    public AjaxResult listVillageTraditionByPage(@RequestBody VillageTraditionQueryRequest villageTraditionQueryRequest) {
        if (villageTraditionQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<VillageTradition> listVillageTraditionByPage = villageTraditionService.getListVillageTraditionByPage(villageTraditionQueryRequest);
        return success(listVillageTraditionByPage);
    }
}
