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

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.map.pojo.dtos.grid.GridDTO;
import ink.wgink.module.map.service.grid.IGridService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.string.WStringUtil;
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.List;
import java.util.Map;

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

    @Autowired
    private IGridService gridService;

    @ApiOperation(value = "网格详情", notes = "网格详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query"),
            @ApiImplicitParam(name = "gridIdId", value = "网格ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{gridId}")
    public GridDTO get(@PathVariable("gridId") String gridId) {
        return gridService.get(gridId, true, true);
    }

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

    @ApiOperation(value = "网格列表", notes = "网格列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query"),
            @ApiImplicitParam(name = "areaCode", value = "地区编码", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list/area-code/{areaCode}")
    public List<GridDTO> listByAreaCode(@PathVariable("areaCode") String areaCode) {
        Map<String, Object> params = requestParams();
        return gridService.listByAreaCode(areaCode, params);
    }

    @ApiOperation(value = "全部网格列表", notes = "全部网格列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query"),
            @ApiImplicitParam(name = "areaCode", value = "地区编码", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-all/area-code/{areaCode}")
    public List<GridDTO> listAllByAreaCode(@PathVariable("areaCode") String areaCode) {
        Map<String, Object> params = requestParams();
        return gridService.listAllByAreaCode(areaCode, params);
    }

    @ApiOperation(value = "子网格列表", notes = "网格列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query"),
            @ApiImplicitParam(name = "areaCode", value = "地区编码", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-sub/area-code/{areaCode}")
    public List<GridDTO> listSubByAreaCode(@PathVariable("areaCode") String areaCode) {
        Map<String, Object> params = requestParams();
        return gridService.listSubByAreaCode(areaCode, params);
    }

    @ApiOperation(value = "网格（排除查询网格）列表", notes = "网格（排除查询网格）列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list/except/{gridId}")
    public List<GridDTO> listExcept(@PathVariable("gridId") String gridId) {
        Map<String, Object> params = requestParams();
        return gridService.listExcept(gridId, params);
    }

    @ApiOperation(value = "网格分页列表", notes = "网格分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query"),
            @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"),
            @ApiImplicitParam(name = "areaCodeLike", value = "地区编码", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage")
    public SuccessResultList<List<GridDTO>> listPage(@RequestParam(name = "areaCodeLike", required = false) String areaCodeLike, ListPage page) {
        if (!StringUtils.isBlank(areaCodeLike)) {
            areaCodeLike = WStringUtil.cutContinuityRepeatCharDesc(areaCodeLike, '0', 3);
        }
        Map<String, Object> params = requestParams();
        params.put("areaCodeLike", areaCodeLike);
        page.setParams(params);
        return gridService.listPage(page);
    }

    @ApiOperation(value = "网格分页列表", notes = "网格分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query"),
            @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/area-code/{areaCode}")
    public SuccessResultList<List<GridDTO>> listPageByAreaCode(@PathVariable("areaCode") String areaCode, ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return gridService.listPageByAreaCode(areaCode, page);
    }

}
