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

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.dictionary.pojo.dtos.DataDTO;
import ink.wgink.mongo.module.dictionary.service.IMongoDataService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.ZTreeDTO;
import ink.wgink.pojo.result.ErrorResult;
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: DictionaryController
 * @Description: 字典
 * @Author: WangGeng
 * @Date: 2019/11/18 14:00
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "字典管理接口")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/mongo/data")
public class MongoDataAppController extends DefaultBaseController {

    @Autowired
    private IMongoDataService mongoDataService;

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

    @ApiOperation(value = ISystemConstant.API_TAGS_RELEASE_PREFIX + "字典详情（ID查询）", notes = ISystemConstant.API_TAGS_RELEASE_PREFIX + "字典详情（ID查询）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataId", value = "字典ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/id-" + ISystemConstant.RELEASE_SUFFIX + "/{dataId}")
    public DataDTO getByIdRelease(@PathVariable("dataId") String dataId) {
        return mongoDataService.get(dataId);
    }

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

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

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

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

    @ApiOperation(value = "分页字典列表", notes = "分页字典列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @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<DataDTO>> listPage(@RequestHeader("token") String token, ListPage page) {
        return listPageRelease(page);
    }

    @ApiOperation(value = ISystemConstant.API_TAGS_RELEASE_PREFIX + "分页字典列表", notes = ISystemConstant.API_TAGS_RELEASE_PREFIX + "分页字典列表接口")
    @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-" + ISystemConstant.RELEASE_SUFFIX)
    public SuccessResultList<List<DataDTO>> listPageRelease(ListPage page) {
        Map<String, Object> params = requestParams();
        String dataParentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_PARENT_ID) == null ? null : params.get(ISystemConstant.PARAMS_PARENT_ID).toString())) {
            dataParentId = params.get(ISystemConstant.PARAMS_PARENT_ID).toString();
        }
        params.put("dataParentId", dataParentId);
        page.setParams(params);
        return mongoDataService.listPage(page);
    }

    @ApiOperation(value = "zTree列表", notes = "zTree列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "id", value = "父ID", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-ztree")
    public List<ZTreeDTO> listZTree(@RequestHeader("token") String token) {
        return listZTreeRelease();
    }

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

}
