package com.yida.data.system.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.Dict;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.system.service.DictService;
import com.yida.data.system.vo.DictTypePageVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 字典crud
 *
 * @return
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/in/dict")
public class InDictController {

    @Resource
    private DictService dictService;

    @GetMapping("/selectDict")
    @OperationLog(module = ModuleName.SYSTEM, methods = "查询字典")
    public ResultBean<List<Dict>> selectDict(@ApiParam("类型") @RequestParam String type) {
        return ResultBean.buildSuccess(dictService.list(Wrappers.lambdaQuery(new Dict())
            .eq(Dict::getType, type).orderByAsc(Dict::getSort)));
    }

    @GetMapping("/getDictByTypeAndValueOrLabel")
    public ResultBean getDictCode(String type, String value, String label) {
        log.info("参数信息: {},{},{}", type, value, label);
        if (ObjectUtil.isNotNull(value)) {
            return ResultBean
                .buildSuccess(
                    dictService.getOne(Wrappers.lambdaQuery(new Dict()).eq(Dict::getType, type).eq(Dict::getValue, value)));
        }
        if (ObjectUtil.isNotNull(label)) {
            return ResultBean
                .buildSuccess(
                    dictService.getOne(Wrappers.lambdaQuery(new Dict()).eq(Dict::getType, type).eq(Dict::getLabel, label)));
        }
        return ResultBean.buildSuccess();
    }

}
