package cn.com.ctop.alarm.modules.controller;

import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.IUserAllocationService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rule/switch/")
public class RuleSwitchController {

    @Autowired
    IUserAllocationService userAllocationService;

    /**
     *  账户添加内广监测链接预警
     */
    @PostMapping(value = "/onOrOffMonitoringLink")
    public Result<Object> onOrOffMonitoringLink(@RequestParam Long accountId, @RequestParam String switchType){
        Result<Object> result=new Result<>();
        UserAllocation userAllocation=new UserAllocation();
        userAllocation.setAccountId(accountId);
        UpdateWrapper<UserAllocation> wrapper=new UpdateWrapper<>();
        //开启预警
        if(switchType.equals("on")){
            wrapper.set("monitoring_link",0);
            wrapper.setEntity(userAllocation);
            boolean update = userAllocationService.update(wrapper);
            if(update){
                result.success("监测链接预警开启成功");
            }
        }else if(switchType.equals("off")){
            wrapper.set("monitoring_link",1);
            wrapper.setEntity(userAllocation);
            boolean update = userAllocationService.update(wrapper);
            if(update){
                result.success("监测链接预警关闭成功");
            }
        }else {
            result.error500("未知操作");
        }
        return result;
    }

    /**
     *  账户添加落地页链接预警
     */
    @PostMapping(value = "/onOrOffLandingPage")
    public Result<Object> onOrOffLandingPage(@RequestParam Long accountId, @RequestParam String switchType){
        Result<Object> result=new Result<>();
        UserAllocation userAllocation=new UserAllocation();
        userAllocation.setAccountId(accountId);
        UpdateWrapper<UserAllocation> wrapper=new UpdateWrapper<>();
        //开启预警
        if(switchType.equals("on")){
            wrapper.set("landing_page",0);
            wrapper.setEntity(userAllocation);
            boolean update = userAllocationService.update(wrapper);
            if(update){
                result.success("落地页链接预警开启成功");
            }
        }else if(switchType.equals("off")){
            wrapper.set("landing_page",1);
            wrapper.setEntity(userAllocation);
            boolean update = userAllocationService.update(wrapper);
            if(update){
                result.success("落地页链接预警关闭成功");
            }
        }else {
            result.error500("未知操作");
        }
        return result;
    }

    /**
     *  开关预警配置回显
     */
    @PostMapping(value = "/echoSwitchStatus")
    public Result<JSONObject> echoSwitchStatus(Long accountId){
        Result<JSONObject> result =new Result<>();
        JSONObject switchStatus= userAllocationService.getSwitchStatusByAccount(accountId);
        if(switchStatus.isEmpty()){
            result.error500("未找到该账户");
        }else {
            result.setResult(switchStatus);
            result.setSuccess(true);
        }
        return result;
    }
}
