package ink.wgink.module.map.controller.app.api.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_APP_PREFIX + "网格接口")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/grid")
public class GridAppController extends DefaultBaseController {

    @Autowired
    private IGridService gridService;

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

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

    @ApiOperation(value = "网格列表", notes = "网格列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @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 = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "gridId", value = "网格ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list/except/{gridId}")
    public List<GridDTO> listExcept(@RequestHeader("token") String token,
                                    @PathVariable("gridId") String gridId) {
        Map<String, Object> params = requestParams();
        return gridService.listExcept(gridId, params);
    }

    @ApiOperation(value = "网格分页列表", notes = "网格分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @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(@RequestHeader("token") String token, @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 = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "areaCode", value = "地区编码", paramType = "path", dataType = "String"),
            @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(@RequestHeader("token") String token, @PathVariable("areaCode") String areaCode, ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return gridService.listPageByAreaCode(areaCode, page);
    }

    @ApiOperation(value = "关联网格列表", notes = "关联网格列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "relationId", value = "关联ID，如用户时为userId", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-with-point/relation-id/{relationId}")
    public List<GridDTO> listWithPointByRelationId(@RequestHeader("token") String token, @PathVariable("relationId") String relationId) {
        return gridService.listWithPointByRelationId(relationId);
    }

    @ApiOperation(value = "我的网格列表", notes = "我的网格列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-of-mine")
    public List<GridDTO> listOfMine(@RequestHeader("token") String token, @RequestParam(name = "areaCodeLike", required = false) String areaCodeLike) {
        if (!StringUtils.isBlank(areaCodeLike)) {
            areaCodeLike = WStringUtil.cutContinuityRepeatCharDesc(areaCodeLike, '0', 3);
        }
        Map<String, Object> params = requestParams();
        params.put("areaCodeLike", areaCodeLike);
        return gridService.listOfMine(token, 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-of-mine")
    public SuccessResultList<List<GridDTO>> listPageOfMine(@RequestHeader("token") String token, @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.listPageOfMine(token, page);
    }

    @ApiOperation(value = "不是我的网格列表", notes = "不是我的网格列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-not-mine")
    public List<GridDTO> listNotMine(@RequestHeader("token") String token, @RequestParam(name = "areaCodeLike", required = false) String areaCodeLike) {
        if (!StringUtils.isBlank(areaCodeLike)) {
            areaCodeLike = WStringUtil.cutContinuityRepeatCharDesc(areaCodeLike, '0', 3);
        }
        Map<String, Object> params = requestParams();
        params.put("areaCodeLike", areaCodeLike);
        return gridService.listNotMine(token, 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-not-mine")
    public SuccessResultList<List<GridDTO>> listPageNotMine(@RequestHeader("token") String token, @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.listPageNotMine(token, page);
    }

}
