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

import ink.wgink.annotation.CheckRequestBodyAnnotation;
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.pojo.vos.grid.GridVO;
import ink.wgink.module.map.service.grid.IGridService;
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.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.Arrays;
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_SYSTEM_PREFIX + "网格接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/grid")
public class GridController extends DefaultBaseController {

    @Autowired
    private IGridService gridService;

    @ApiOperation(value = "新增网格", notes = "新增网格接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult save(@RequestBody GridVO gridVO) throws Exception {
        gridService.save(gridVO);
        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) {
        gridService.remove(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

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

    @ApiOperation(value = "网格详情", notes = "网格详情接口")
    @ApiImplicitParams({
            @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 = "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 = "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 = "areaCode", value = "地区编码", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-all-with-point/area-code/{areaCode}")
    public List<GridDTO> listAllWithPointByAreaCode(@PathVariable("areaCode") String areaCode) {
        Map<String, Object> params = requestParams();
        return gridService.listAllWithPointByAreaCode(areaCode, params);
    }

    @ApiOperation(value = "子网格列表", notes = "网格列表接口")
    @ApiImplicitParams({
            @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 = "网格（排除查询网格）列表接口")
    @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 = "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 = "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);
    }

}
