package ink.wgink.module.dictionary.controller.wechat;

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.dictionary.pojo.dtos.DataDTO;
import ink.wgink.module.dictionary.service.IDataService;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: DataWechatController
 * @Description: 微信数据字典
 * @Author: WangGeng
 * @Date: 2020/3/8 9:47 上午
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_WECHAT_PREFIX + "字典管理接口")
@RestController
@RequestMapping(ISystemConstant.WECHAT_PREFIX + "/datadictionary")
public class DataWechatController extends DefaultBaseController {

    @Autowired
    private IDataService dataService;

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

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

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

    @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<DataDTO>> listPage(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 dataService.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("listztree")
    public List<ZTreeDTO> listZTree() {
        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();
        }
        params.put("dataParentId", dataParentId);
        return dataService.listZTree(params);
    }

}
