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 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;

import java.util.List;

import javax.annotation.Resource;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

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

    @Resource
    private DictService dictService;

    @ApiOperation("分页查询字典类型")
    @GetMapping("/listDictTypePage")
    public ResultBean<IPage<DictTypePageVO>> listDictGroup(@RequestParam(required = false) String typeName,
        @RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "10") Integer pageSize) {
        return ResultBean.buildSuccess(dictService.listDictTypePage(typeName, pageNum, pageSize));
    }


    @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)));
    }

    @PostMapping("/insertDict")
    @OperationLog(module = ModuleName.SYSTEM, methods = "添加字典", type = OperationLogTypeEnum.SAVE)
    public ResultBean<Dict> insertDict(@RequestBody Dict dict) {
        dictService.saveOrUpdate(dict);
        return ResultBean.buildSuccess();
    }


    @GetMapping("/delDict")
    @OperationLog(module = ModuleName.SYSTEM, methods = "删除字典", type = OperationLogTypeEnum.DELETE)
    public ResultBean delDict(@ApiParam("字典值id") @RequestParam Long dictId) {
        dictService.removeById(dictId);
        return ResultBean.buildSuccess();
    }

    @GetMapping("/delDictByType")
    public ResultBean delDictByType(String type) {
        dictService.remove(Wrappers.<Dict>lambdaQuery()
            .eq(Dict::getType, type));
        return ResultBean.buildSuccess();
    }

    @GetMapping("/getDictByTypeAndValueOrLabel")
    public ResultBean getDictCode(String type, String value, String 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();
    }

}
