package com.cm.common.plugin.controller.wechat.datadictionary;

import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.pojo.dtos.datadictionary.DataDictionaryDTO;
import com.cm.common.plugin.service.datadictionary.IDataDictionaryService;
import com.cm.common.pojo.ListPage;
import com.cm.common.pojo.dtos.ZTreeDTO;
import com.cm.common.result.ErrorResult;
import com.cm.common.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: DataDictionaryWechatController
 * @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 DataDictionaryWechatController extends AbstractController {

    @Autowired
    private IDataDictionaryService dataDictionaryService;

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

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

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

    @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("listpagedictionary")
    public SuccessResultList<List<DataDictionaryDTO>> listPageDictionary(ListPage page) {
        Map<String, Object> params = requestParams();
        String dictionaryParentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_PARENT_ID) == null ? null : params.get(ISystemConstant.PARAMS_PARENT_ID).toString())) {
            dictionaryParentId = params.get(ISystemConstant.PARAMS_PARENT_ID).toString();
        }
        params.put("dictionaryParentId", dictionaryParentId);
        page.setParams(params);
        return dataDictionaryService.listPageDictionary(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("listztreedictionary")
    public List<ZTreeDTO> listZTreeDictionary() throws SearchException {
        Map<String, Object> params = requestParams();
        String dictionaryParentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_ID) == null ? null : params.get(ISystemConstant.PARAMS_ID).toString())) {
            dictionaryParentId = params.get(ISystemConstant.PARAMS_ID).toString();
        }
        params.put("dictionaryParentId", dictionaryParentId);
        return dataDictionaryService.listZTreeDictionary(params);
    }

}
