package com.yfd.platform.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yfd.platform.annotation.Log;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.system.domain.SysDictionary;
import com.yfd.platform.system.service.ISysDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 数据字典表 前端控制器
 * </p>
 *
 *
 * @since 2023-03-08
 */
@RestController
@RequestMapping("/system/dictionary")
@Api(tags = "数据字典")
public class SysDictionaryController {

    @Resource
    private ISysDictionaryService sysDictionaryService;

    /**********************************
     * 用途说明: 获取数据字典列表
     * 参数说明  dictType 字典类型
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回分页查询结果
     ***********************************/
    @GetMapping("/dictList")
    @ApiOperation("获取数据字典列表")
    public ResponseResult getDictList(String dictType) {
        if (StrUtil.isBlank(dictType)) {
            return ResponseResult.error("参数为空");
        }
        List<SysDictionary> sysDictionaries =
                sysDictionaryService.getDictList(dictType);
        return ResponseResult.successData(sysDictionaries);
    }

    /**********************************
     * 用途说明: 根据ID删除字典
     * 参数说明  id 字典ID
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回删除结果成功或者失败
     ***********************************/
    @Log(module = "数据字典", value = "根据ID删除字典",type = "0")
    @PostMapping("/deleteById")
    @ApiOperation("根据ID删除字典")
    public ResponseResult deleteDictById(@RequestParam String id) {
        boolean ok = sysDictionaryService.deleteDictById(id);
        if (ok) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

    /**********************************
     * 用途说明: 新增字典
     * 参数说明  sysDictionary 字典对象
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回增加成功或者失败
     ***********************************/
    @Log(module = "数据字典", value = "新增数据字典",type = "0")
    @PostMapping("/addDict")
    @ApiOperation("新增字典")
    public ResponseResult addDict(@RequestBody SysDictionary sysDictionary) {
        if (sysDictionary == null) {
            return ResponseResult.error("参数为空");
        }
        LambdaQueryWrapper<SysDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictionary::getDictCode, sysDictionary.getDictCode()).or().eq(SysDictionary::getDictName, sysDictionary.getDictName());
        int count = sysDictionaryService.count(queryWrapper);
        if (count > 0) {
            return ResponseResult.error("当前字典编号或名称已经存在！");
        }
        boolean ok = sysDictionaryService.addDict(sysDictionary);
        if (ok) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

    /**********************************
     * 用途说明: 修改字典
     * 参数说明  sysDictionary 字典对象
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回修改成功或者失败
     ***********************************/
    @Log(module = "数据字典", value = "修改数据字典",type = "0")
    @PostMapping("/updateDict")
    @ApiOperation("修改字典")
    public ResponseResult updateDict(@RequestBody SysDictionary sysDictionary) {
        if (sysDictionary == null) {
            return ResponseResult.error("参数为空");
        }
        LambdaQueryWrapper<SysDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(SysDictionary::getId,sysDictionary.getId()).and(i->i.eq(SysDictionary::getDictCode, sysDictionary.getDictCode()).or().eq(SysDictionary::getDictName, sysDictionary.getDictName()));
        int count = sysDictionaryService.count(queryWrapper);
        if (count > 0) {
            return ResponseResult.error("当前字典编号或名称已经存在！");
        }
        boolean ok = sysDictionaryService.updateById(sysDictionary);
        if (ok) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

    /**********************************
     * 用途说明: 根据ID查询字典
     * 参数说明  sysDictionary 字典对象
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回查询结果
     ***********************************/
    @PostMapping("/getDictById")
    @ApiOperation("根据ID查询字典")
    public ResponseResult getDictById(String id) {
        if (StrUtil.isBlank(id)) {
            return ResponseResult.error("参数为空");
        }
        SysDictionary sysDictionary = sysDictionaryService.getById(id);
        return ResponseResult.successData(sysDictionary);
    }

    /**********************************
     * 用途说明: 拖动修改字典顺序
     * 参数说明  fromID 当前ID  toID 到达ID
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回拖动成功或者失败
     ***********************************/
    @Log(module = "数据字典", value = "拖动修改字典顺序",type = "0")
    @PostMapping("/changeDictOrder")
    @ApiOperation("拖动修改字典顺序")
    public ResponseResult changeDictOrder(@RequestParam String fromID,
                                          @RequestParam String toID) {

        boolean ok = sysDictionaryService.changeDictOrder(fromID, toID);
        if (ok) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }

    }

}
