package com.atguigu.ssyx.sys.controller;


import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.sys.RegionWare;
import com.atguigu.ssyx.sys.service.RegionWareService;
import com.atguigu.ssyx.vo.sys.RegionWareQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 城市仓库关联表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-09-14
 */
@RestController
@RequestMapping(value = "/admin/sys/regionWare")
@Api(value = "开通区域接口", tags = "开通区域接口")
public class RegionWareController {
    @Autowired
    private RegionWareService regionWareService;


    //TODO 分页查询错误
    @ApiOperation(value = "获取开通区域列表")
    @GetMapping("{page}/{limit}")
    public Result index(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
                        @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
                        @ApiParam(name = "regionWareVo", value = "查询对象", required = false) RegionWareQueryVo regionWareQueryVo) {
        try {
            Page<RegionWare> pageParam = new Page<>(page, limit);
            IPage<RegionWare> pageModel = regionWareService.selectPage(pageParam, regionWareQueryVo);
            return Result.ok(pageModel);
        } catch (Exception e) {
            throw new RuntimeException("获取开通区域列表异常", e);
        }

    }

    @ApiOperation(value = "新增开通区域")
    @PostMapping("save")
    public Result save(@RequestBody RegionWare regionWare) {
        try {
            regionWareService.saveRegionWare(regionWare);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("新增开通区域异常", e);
        }
    }

    @ApiOperation(value = "删除开通区域")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        try {
            boolean result = regionWareService.removeById(id);
            return result ? Result.ok(null) : Result.fail(null);
        } catch (Exception e) {
            throw new RuntimeException("删除开通区域异常", e);
        }
    }

    @ApiOperation(value = "取消开通区域")
    @PostMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        try {
            regionWareService.updateStatus(id, status);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("删除开通区域异常", e);
        }
    }
}

