package com.dcit.authority.controller;

import com.dcit.authority.exception.AuthException;
import com.dcit.authority.model.SysDict;
import com.dcit.authority.model.SysDictExample;
import com.dcit.authority.model.SysDictType;
import com.dcit.authority.model.SysDictTypeExample;
import com.dcit.authority.service.SysDictService;
import com.dcit.authority.service.SysDictTypeService;
import com.dcit.common.enums.ResultEnum;
import com.dcit.common.utils.ResultVOUtil;
import com.dcit.common.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
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;

@Api(value="系统资源管理API")
@RestController
@RequestMapping("/dict")
@Slf4j
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @ApiOperation(value = "根据dictTypeId获取字典信息")
    @GetMapping("/getSysDictByDictTypeCode/{dictTypeCode}")
    public ResultVO<Object> getSysDictByDictTypeCode(@PathVariable String dictTypeCode){
        if(StringUtils.isEmpty(dictTypeCode)){
            throw new AuthException(ResultEnum.PARAM_ERROR.getCode(),ResultEnum.PARAM_ERROR.getMessage());
        }
        SysDictTypeExample dictTyeExample = new SysDictTypeExample();
        SysDictTypeExample.Criteria dicTypeCriteria = dictTyeExample.createCriteria();
        dicTypeCriteria.andCodeEqualTo(dictTypeCode);
        List<SysDictType> typeDicts = sysDictTypeService.selectByExample(dictTyeExample);
        SysDictExample example = new SysDictExample();
        SysDictExample.Criteria criteria = example.createCriteria();
        criteria.andDictTypeIdEqualTo(typeDicts.get(0).getDictTypeId());
        List<SysDict> sysDictList =sysDictService.selectByExample(example);
        return ResultVOUtil.success(sysDictList);
    }
}
