package com.gyee.alarm.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gyee.alarm.model.auto.ProBasicDataDictionary;
import com.gyee.alarm.service.auto.impl.ProBasicDataDictionaryServiceImpl;
import com.gyee.common.config.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 数据字典表 前端控制器
 * </p>
 *
 * @author shilin
 * @since 2023-08-16
 */
@RestController
@RequestMapping("/datadictionary")
@CrossOrigin
@Api(value = "字典接口", tags = "字典接口")
public class ProBasicDataDictionaryController {
        /*
   报警部件： alertrule_category CLX 齿轮箱
   预警类型：	early_category	performance	性能下降
   用户设置	userset	jumpwindow	是否故障弹窗 faultread	是否语音播报
   风机型号   windtrubinemodel
    * */

    @Resource
    private ProBasicDataDictionaryServiceImpl proBasicDataDictionaryService;

    @GetMapping()
    public List<ProBasicDataDictionary> getAllDatadictionary() {
        List<ProBasicDataDictionary> datadictionaries = proBasicDataDictionaryService.getAllDatadictionary();
        return datadictionaries;
    }
    /**
     * 查询
     * @param category
     * @param name
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/page")
    @ApiOperation(value = "列表", notes = "列表")
    public R findList(@RequestParam(value = "category", required = false) String category,
                      @RequestParam(value = "name", required = false) String name,
                      @RequestParam(value = "pageNum", required = true) String pageNum,
                      @RequestParam(value = "pageSize", required = true) String pageSize) {
        IPage<ProBasicDataDictionary> list = proBasicDataDictionaryService.list(category, name, pageNum, pageSize);
        if (null != list) {
            return R.ok().data(list);
        } else {
            return R.error().data("查询失败！");
        }
    }

    /**
     * 添加
     * @param datadictionary
     * @return
     */
    @PostMapping(value = "/saveorupdate")
    @ApiOperation(value = "新增or修改", notes = "新增or修改")
    public R addAll(@RequestBody ProBasicDataDictionary datadictionary) {

        int b = proBasicDataDictionaryService.saveOrUpdateDatadictionary(datadictionary);
        if (b>=0) {
            return R.ok().data(b);
        } else {
            return R.error().data("操作数据库失败！");
        }
    }

    /**
     * 添加
     * @param proBasicDataDictionary
     * @return
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "新增or修改", notes = "新增or修改")
    public R addAllw(@RequestBody ProBasicDataDictionary proBasicDataDictionary) {

        boolean b = proBasicDataDictionaryService.saveOrUpdate(proBasicDataDictionary);
        if (b) {
            return R.ok().data(b);
        } else {
            return R.error().data("操作数据库失败！");
        }
    }


    /**
     * 根据字典类型获取字典集合
     * @param category
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "根据字典类型获取字典集合", notes = "根据字典类型获取字典集合")
    public R findList(@RequestParam(value = "category", required = false) String category){
        List<ProBasicDataDictionary> list = proBasicDataDictionaryService.getDatadictionary(category);
        if (null != list) {
            return R.ok().data(list);
        } else {
            return R.error().data("查询失败！");
        }
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/{ids}")
    @ApiOperation(value = "删除", notes = "删除")
    public R deleteAll(@PathVariable("ids") String ids) {
        String[] strings = ids.split(",");
        boolean b = proBasicDataDictionaryService.removeByIds(Arrays.asList(strings));
        if (b) {
            return R.ok().data(b);
        } else {
            return R.error().data("删除失败！");
        }
    }

}
