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.Alertrule2;
import com.gyee.wisdom.alarm.sharding.entity.Scadabj;
import com.gyee.wisdom.alarm.sharding.service.Alertrule2Service;
import com.gyee.wisdom.alarm.sharding.service.ScadabjService;
import com.gyee.wisdom.alarm.sharding.util.TokenUtil;
import com.gyee.wisdom.alarm.sharding.util.UserLoginToken;
import com.gyee.wisdom.alarm.sharding.util.ResponseWrapper;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/scadabj")
@CrossOrigin
public class ScadabjController {

    @Autowired
    private ScadabjService scadabjService;

    //@UserLoginToken
    @PostMapping(value = "/save")
    @ResponseBody
    @UserLoginToken
    public ResponseWrapper<Scadabj> saveScadabj(HttpServletRequest request, @RequestBody Scadabj scadabj) {
        String token=request.getHeader("token");
        Claims claims = TokenUtil.parseJWT(token);
        String userName=claims.get("jti").toString();
        ResponseWrapper<Scadabj> wrapper = dataCheck(scadabj);
        if (wrapper.getSuccess()) {
            //mybatis 操作数据库成功会返回影响的条数,如果成功则为1,失败为0
            int result = scadabjService.saveAndUpdateScadabj(scadabj,userName);
            if (result <= 0) {
                wrapper.setSuccess(false);
                wrapper.setMsg("操作数据库失败");
            }
        }
        return wrapper;

    }

    @DeleteMapping(value = "/delete")
    public int deleteScadabj(Scadabj scadabj) {
        return scadabjService.deleteScadabj(scadabj);
    }


    //@UserLoginToken
    @GetMapping(value = "/page")
    public IPage<Scadabj> queryByPage(
            @RequestParam(value = "pagenum") Integer pageNum,
            @RequestParam(value = "pagesize") Integer pageSize,
            @RequestParam(value = "pointKey", required = false) String pointKey,
            @RequestParam(value = "stationId", required = false) String stationId,
            @RequestParam(value = "enable", required = false) String enable,
            @RequestParam(value = "description", required = false) String description

    ) {
        Page<Scadabj> page = new Page(pageNum, pageSize);
        IPage<Scadabj> pg = scadabjService.pageQueryAll(page, pointKey, stationId, enable, description);
        return pg;
    }

    //@UserLoginToken
    @PostMapping(value = "/save-batch")
    @ResponseBody
    @UserLoginToken
    public ResponseWrapper<Scadabj> saveScadabjBatch(HttpServletRequest request,@RequestBody List<Scadabj> lst) {
        String token=request.getHeader("token");
        Claims claims = TokenUtil.parseJWT(token);
        String userName=claims.get("jti").toString();
        boolean allCheck = true;
        ResponseWrapper<Scadabj> wrapper = null;
        for (Scadabj scadabj : lst) {
            wrapper = dataCheck(scadabj);
            if (wrapper.getSuccess() != true) {
                allCheck = false;
                String msg = wrapper.getMsg();
                wrapper.setMsg(scadabj.getDescription() + "：" + msg);
                break;
            }
        }
        if (allCheck) {
            for (Scadabj scadabj : lst) {
                int result = scadabjService.saveAndUpdateScadabj(scadabj,userName);
                if (result <= 0) {
                    wrapper.setMsg(scadabj.getDescription() + ":" + "操作数据库失败");
                    wrapper.setSuccess(false);
                    break;
                }
            }
            return wrapper;
        } else {
            return wrapper;
        }

    }


    private ResponseWrapper<Scadabj> dataCheck(Scadabj scadabj) {

        ResponseWrapper<Scadabj> wrapper = null;
        String msg = "";
        boolean result = true;
        if (scadabj.getPointKey() != null && scadabj.getPointKey().equals("")) {
            msg = "测点不能为空";
            result = false;
        } else if (scadabj.getDescription() != null && scadabj.getDescription().equals("")) {
            msg = "描述不能为空";
            result = false;
        } else if (scadabj.getRank() != null && scadabj.getRank().equals("")) {
            msg = "报警级别不能为空";
            result = false;
        } else if (scadabj.getEnabled() == -1) {
            msg = "是否可用不能为空";
            result = false;
        } else if (scadabj.getAlarmType() == -1) {
            msg = "报警逻辑不能为空";
            result = false;
        }
        if (result) {
            wrapper = ResponseWrapper.success(msg, scadabj);
        } else {
            wrapper = ResponseWrapper.error(msg, scadabj);
        }
        return wrapper;
    }
}
