package ink.wgink.module.map.controller.api.grid;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.map.pojo.dtos.grid.GridGroupDTO;
import ink.wgink.module.map.pojo.vos.grid.GridGroupVO;
import ink.wgink.module.map.service.grid.IGridGroupService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.RegexUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: GridGroupController
 * @Description: 网格组管理
 * @Author: wanggeng
 * @Date: 2021/10/19 10:21 下午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "网格组接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/grid-group")
public class GridGroupController extends DefaultBaseController {

    @Autowired
    private IGridGroupService gridGroupService;

    @ApiOperation(value = "新增网格组", notes = "新增网格组接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult save(@RequestBody GridGroupVO gridGroupVO) throws Exception {
        if (StringUtils.isBlank(gridGroupVO.getGridGroupCode())) {
            throw new ParamsException("编码不能为空");
        }
        if (!RegexUtil.isLetterOrNumber(gridGroupVO.getGridGroupCode())) {
            throw new ParamsException("编码只能是字母或数字");
        }
        gridGroupService.save(gridGroupVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "删除网格组", notes = "删除网格组接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("remove/{ids}")
    public SuccessResult remove(@PathVariable("ids") String ids) {
        gridGroupService.remove(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "修改网格组", notes = "修改网格组接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gridGroupIdId", value = "网格组ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{gridGroupId}")
    @CheckRequestBodyAnnotation
    public SuccessResult update(@PathVariable("gridGroupId") String gridGroupId, @RequestBody GridGroupVO gridGroupVO) throws Exception {
        gridGroupService.update(gridGroupId, gridGroupVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "网格组详情", notes = "网格组详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gridGroupIdId", value = "网格组ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{gridGroupId}")
    public GridGroupDTO get(@PathVariable("gridGroupId") String gridGroupId) {
        return gridGroupService.get(gridGroupId);
    }

    @ApiOperation(value = "网格组列表", notes = "网格组列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list")
    public List<GridGroupDTO> list() {
        Map<String, Object> params = requestParams();
        return gridGroupService.list(params);
    }

    @ApiOperation(value = "网格组分页列表", notes = "网格组分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage")
    public SuccessResultList<List<GridGroupDTO>> listPage(ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return gridGroupService.listPage(page);
    }

}
