package com.gyee.wisdom.alarm.sharding.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.wisdom.alarm.sharding.entity.Datadictionary;
import com.gyee.wisdom.alarm.sharding.service.DatadictionaryService;
import com.gyee.wisdom.alarm.sharding.util.ResponseWrapper;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @descrition:
 * @author:Xugp
 * @date:2022-05-12
 */
@Slf4j
@RestController
@RequestMapping("/datadictionary")
@CrossOrigin
@Tag(name = "字典接口")
public class DatadictionaryController {

    /*
   报警部件： alertrule_category CLX 齿轮箱
   预警类型：	early_category	performance	性能下降
   用户设置	userset	jumpwindow	是否故障弹窗 faultread	是否语音播报
   风机型号   windtrubinemodel
    * */

    @Autowired
    private DatadictionaryService datadictionaryService;

    @GetMapping()
    public List<Datadictionary> getAllDatadictionary() {
        List<Datadictionary> datadictionaries = datadictionaryService.getAllDatadictionary();
        return datadictionaries;
    }

    @Operation(description = "分页查询字典")
    @GetMapping(value = "/page")
    public ResponseWrapper queryByPage(
            @Parameter(name = "pagenum", description = "当前页") @RequestParam(value = "pagenum") Integer pageNum,
            @Parameter(name = "pagesize", description = "分页大小") @RequestParam(value = "pagesize") Integer pageSize,
            @Parameter(name = "name", description = "字典名称") @RequestParam(value = "name", required = false) String name,
            @Parameter(name = "category", description = "字典类型") @RequestParam(value = "category", required = false) String category
    ) {
        Page<Datadictionary> page = new Page(pageNum, pageSize);
        IPage<Datadictionary> pageResult = datadictionaryService.pageQueryAll(page, name, category);

        return ResponseWrapper.success("请求成功", pageResult);
    }

    @Operation(description = "新增或修改字典")
    @PostMapping(value = "/saveorupdate")
    @ResponseBody
    public ResponseWrapper saveDatadictionary(@Parameter(description = "字典对象") @RequestBody Datadictionary datadictionary) {
        ResponseWrapper<Datadictionary> wrapper = null;
        int result = datadictionaryService.saveOrUpdateDatadictionary(datadictionary);
        if (result <= 0) {
            wrapper = ResponseWrapper.error("操作数据库失败");
        } else {
            wrapper = ResponseWrapper.success("操作数据库成功");
        }
        return wrapper;
    }

    @Operation(description = "根据字典类型获取字典集合")
    @GetMapping("/list")
    @ResponseBody
    public ResponseWrapper getDictionary(@Parameter(name = "category",description = "字典类型")String category) {

        List<Datadictionary> datadictionary = datadictionaryService.getDatadictionary(category);
        ResponseWrapper wrapper = ResponseWrapper.success("获取成功", datadictionary);
        return wrapper;
    }

    @Operation(description = "删除字典")
    @DeleteMapping(value = "/{id}")
    @ResponseBody
    public ResponseWrapper deleteDatadictionary(
            @Parameter(name = "字典编号") @PathVariable("id") String id) {
        ResponseWrapper wrapper = null;
        int s = datadictionaryService.deleteDatadictionary(id);
        if (s > 0) {
            wrapper = ResponseWrapper.success("删除成功!", s);
            return wrapper;
        } else {
            wrapper = ResponseWrapper.error("删除失败!");
            return wrapper;
        }

    }
}
