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.localPolicy.LocalPolicyAddRequest;
import com.ruoyi.web.domain.dto.localPolicy.LocalPolicyEditRequest;
import com.ruoyi.web.domain.dto.localPolicy.LocalPolicyQueryRequest;
import com.ruoyi.web.domain.entity.LocalPolicy;
import com.ruoyi.web.service.LocalPolicyService;
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/local/policy")
public class LocalPolicyController {

    @Autowired
    private LocalPolicyService localPolicyService;

    /**
     * 添加当地政策
     * 注：文件上传应先调用通用上传接口 /common/upload 获取文件路径
     * 然后再调用此接口，传入文件路径
     *
     * @param localPolicyAddRequest
     * @return
     */
    @ApiOperation("添加当地政策")
    @PostMapping("/add")
    public AjaxResult addLocalPolicy(@RequestBody LocalPolicyAddRequest localPolicyAddRequest) {
        if (localPolicyAddRequest == null) {
            return error("请求参数为空");
        }
        Integer id = localPolicyService.addLocalPolicy(localPolicyAddRequest);
        return success(id);
    }

    /**
     * 删除设施
     *
     * @param paramsMap
     * @return
     */
    @ApiOperation("删除当地政策")
    @PostMapping("/delete")
    public AjaxResult deleteLocalPolicy(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        if (StrUtil.isBlank(ids)) {
            return error("请求参数为空");
        }
        boolean b = localPolicyService.deleteLocalPolicy(ids);
        if (b) {
            return success("删除成功");
        }
        return error("删除失败");
    }


    /**
     * 编辑当地政策
     * 注：文件上传应先调用通用上传接口 /common/upload 获取文件路径
     * 然后再调用此接口，传入文件路径
     *
     * @param localPolicyEditRequest
     * @return
     */
    @ApiOperation("编辑当地政策")
    @PostMapping("/edit")
    public AjaxResult editLocalPolicy(@RequestBody LocalPolicyEditRequest localPolicyEditRequest) {
        if (localPolicyEditRequest == null) {
            return error("请求参数为空");
        }
        localPolicyService.editLocalPolicy(localPolicyEditRequest);
        return AjaxResult.success();
    }


    /**
     * 根据id获取当地政策
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取当地政策")
    @GetMapping("/get")
    public AjaxResult getLocalPolicyById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        LocalPolicy localPolicy = localPolicyService.getLocalPolicyById(id);
        return success(localPolicy);
    }

    /**
     * 分页获取当地政策列表
     *
     * @param localPolicyQueryRequest
     * @return
     */
    @ApiOperation("分页获取当地政策列表")
    @PostMapping("/list/page")
    public AjaxResult listLocalPolicyByPage(@RequestBody LocalPolicyQueryRequest localPolicyQueryRequest) {
        if (localPolicyQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<LocalPolicy> listLocalPolicyByPage = localPolicyService.getListLocalPolicyByPage(localPolicyQueryRequest);
        return success(listLocalPolicyByPage);
    }
}
