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

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

/**
 * @description:
 * @auther: Wanghs
 * @date: 2022-05-24
 */

@Slf4j
@RestController
@RequestMapping("/userset")
@CrossOrigin
@Tag(name = "用户设置")
public class UserSetController {

    @Autowired
    private DatadictionaryService datadictionaryService;

    @Operation(description = "是否故障弹窗提示")
    @PostMapping("/jumpwindow")
    public ResponseWrapper saveJumpWindow(@Parameter(name = "是否弹窗") @RequestParam(value = "isjump", required = true) boolean isJump) {

        boolean b = datadictionaryService.saveOrUpdateUserSet("userset", "jumpwindow", isJump);
        String msg = isJump?"故障弹窗开启":"故障弹窗关闭";
        ResponseWrapper resultWrapper=null;
        if(b)
            resultWrapper=  ResponseWrapper.success(msg+"设置成功");
        else
            resultWrapper=ResponseWrapper.error(msg+"设置失败");
        return  resultWrapper;
    }

    @Operation(description = "是否故障语音播报")
    @PostMapping("/faultread")
    public ResponseWrapper saveFaultRead(@Parameter(name = "是否播报") @RequestParam(value = "isread", required = true) boolean isread) {
        boolean b = datadictionaryService.saveOrUpdateUserSet("userset", "faultread", isread);
        String msg = isread?"语音播报开启":"语音播报关闭";
        ResponseWrapper resultWrapper=null;
        if(b)
            resultWrapper=  ResponseWrapper.success(msg+"设置成功");
        else
            resultWrapper=ResponseWrapper.error(msg+"设置失败");
        return  resultWrapper;
    }


    @Operation(description = "获取是否故障弹窗提示")
    @GetMapping("/jumpwindow")
    public ResponseWrapper getJumpWindow() {
        Datadictionary userSet = datadictionaryService.getUserSet("userset", "jumpwindow");
        if(userSet!=null){
            boolean isJump=userSet.getEnabled()==0?false:true;
            return ResponseWrapper.success("获取成功", isJump);

        }else{
            return ResponseWrapper.error("获取失败");

        }
    }

    @Operation(description = "获取是否故障语音播报")
    @GetMapping("/faultread")
    public ResponseWrapper getFaultRead() {
        Datadictionary userSet = datadictionaryService.getUserSet("userset", "faultread");
        if(userSet!=null){
            boolean isRead=userSet.getEnabled()==0?false:true;
            return ResponseWrapper.success("获取成功", isRead);

        }else{
            return ResponseWrapper.error("获取失败");
        }
    }


}
