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.VillageService.VillageServiceAddRequest;
import com.ruoyi.web.domain.dto.VillageService.VillageServiceEditRequest;
import com.ruoyi.web.domain.dto.VillageService.VillageServiceQueryRequest;
import com.ruoyi.web.domain.vo.VillageServiceVO;
import com.ruoyi.web.service.VillageServiceService;
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/service")
public class VillageServiceController   {

    @Autowired
    private VillageServiceService villageServiceService;

    /**
     * 添加服务与支持
     *
     * @param villageServiceAddRequest
     * @return
     */
    @ApiOperation("添加服务与支持")
    @PostMapping("/add")
    public AjaxResult addVillageService(@RequestBody VillageServiceAddRequest villageServiceAddRequest) {
        if (villageServiceAddRequest == null) {
            return error("请求参数为空");
        }
        System.out.println(villageServiceAddRequest.getContent());

        Integer id = villageServiceService.addVillageService(villageServiceAddRequest);
        return success(id);
    }

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


    /**
     * 编辑服务与支持
     *
     * @param villageServiceEditRequest
     * @return
     */
    @ApiOperation("编辑服务与支持")
    @PostMapping("/edit")
    public AjaxResult editVillageService(@RequestBody VillageServiceEditRequest villageServiceEditRequest) {
        if (villageServiceEditRequest == null) {
            return error("请求参数为空");
        }
        villageServiceService.editVillageService(villageServiceEditRequest);
        return AjaxResult.success();
    }

    /**
     * 发布服务与支持
     *
     * @param id
     * @return
     */
    @ApiOperation("发布服务与支持")
    @GetMapping("/publish")
    public AjaxResult publishVillageService(@RequestParam int id) {
        if (id <= 0) {
            return error("请求参数为空");
        }
        boolean b = villageServiceService.publishVillageService(id);
        if (b) {
            return AjaxResult.success("发布成功");
        } else {
            return AjaxResult.error("发布失败");
        }
    }

    /**
     * 下架服务与支持
     *
     * @param id
     * @return
     */
    @ApiOperation("下架服务与支持")
    @GetMapping("/remove")
    public AjaxResult removeVillageService(@RequestParam int id) {
        if (id <= 0) {
            return error("请求参数为空");
        }
        boolean b = villageServiceService.removeVillageService(id);
        if (b) {
            return AjaxResult.success("下架成功");
        } else {
            return AjaxResult.error("下架失败");
        }
    }


    /**
     * 根据id获取服务与支持
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取服务与支持")
    @GetMapping("/get")
    public AjaxResult getVillageServiceById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        VillageServiceVO villageServiceById = villageServiceService.getVillageServiceById(id);
        return success(villageServiceById);
    }

    /**
     * 分页获取服务与支持列表
     *
     * @param villageServiceQueryRequest
     * @return
     */
    @ApiOperation("分页获取服务与支持列表")
    @PostMapping("/list/page")
    public AjaxResult listVillageServiceByPage(@RequestBody VillageServiceQueryRequest villageServiceQueryRequest) {
        if (villageServiceQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<VillageServiceVO> listVillageServiceByPageVO = villageServiceService.getListVillageServiceByPage(villageServiceQueryRequest);
        return success(listVillageServiceByPageVO);
    }
}
