package ink.wgink.mongo.module.dictionary.controller.apis;

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.dictionary.pojo.dtos.AreaDTO;
import ink.wgink.mongo.module.dictionary.service.IMongoAreaService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.ZTreeDTO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
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;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: AreaAreaController
 * @Description: 地区字典
 * @Author: WangGeng
 * @Date: 2019/11/18 14:00
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "地区字典接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/mongo/area")
public class MongoAreaController extends DefaultBaseController {

    @Autowired
    private IMongoAreaService mongoAreaService;

    @ApiOperation(value = "更新mongo", notes = "更新mongo接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update-mongo")
    public SuccessResult updateMongo() {
        mongoAreaService.updateMongo();
        return new SuccessResult();
    }

    @ApiOperation(value = "地区字典详情（ID查询）", notes = "地区字典详情（ID查询）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaId", value = "地区字典ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{areaId}")
    public AreaDTO get(@PathVariable("areaId") String areaId) {
        return mongoAreaService.get(areaId);
    }

    @ApiOperation(value = "地区字典列表（上级ID查询）", notes = "地区字典列表（上级ID查询）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaParentId", value = "地区字典上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list/parent-id/{areaParentId}")
    public List<AreaDTO> listByParentId(@PathVariable("areaParentId") String areaParentId) {
        return mongoAreaService.listByParentId(areaParentId);
    }

    @ApiOperation(value = "地区字典全部列表（上级ID查询）", notes = "地区字典全部列表（上级ID查询）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaParentId", value = "地区字典上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-all/parent-id/{areaParentId}")
    public List<AreaDTO> listAllByParentId(@PathVariable("areaParentId") String areaParentId) {
        return mongoAreaService.listAllByParentId(areaParentId);
    }

    @ApiOperation(value = "地区字典分页列表", notes = "地区字典分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "上级ID", paramType = "query", 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")
    public SuccessResultList<List<AreaDTO>> listPage(ListPage page) {
        Map<String, Object> params = requestParams();
        String parentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_PARENT_ID) == null ? null : params.get(ISystemConstant.PARAMS_PARENT_ID).toString())) {
            parentId = params.get(ISystemConstant.PARAMS_PARENT_ID).toString();
        }
        params.put("areaParentId", parentId);
        page.setParams(params);
        return mongoAreaService.listPage(page);
    }

    @ApiOperation(value = "地区字典zTree列表", notes = "地区字典zTree列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "父ID", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-ztree")
    public List<ZTreeDTO> listZTree() {
        Map<String, Object> params = requestParams();
        String parentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_ID) == null ? null : params.get(ISystemConstant.PARAMS_ID).toString())) {
            parentId = params.get(ISystemConstant.PARAMS_ID).toString();
        }
        return mongoAreaService.listZTree(parentId);
    }

    @ApiOperation(value = "地区字典列表（通过地区字典编码）", notes = "地区字典列表（通过地区字典编码）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "地区字典编码", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list/code/{areaCode}")
    public List<AreaDTO> listByCode(@PathVariable("areaCode") String areaCode) {
        return mongoAreaService.listByCode(areaCode);
    }

    @ApiOperation(value = "地区字典列表（通过地区字典编码）", notes = "地区字典列表（通过地区字典编码）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "地区字典编码", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-sub/code/{areaCode}")
    public List<AreaDTO> listSubByCode(@PathVariable("areaCode") String areaCode) {
        return mongoAreaService.listSubByCode(areaCode);
    }

}
