Newer
Older
stockTray / EastLineServer / src / main / java / me / bello / eastline / controller / NotifyController.java
bello on 30 Oct 2020 3 KB 新增code详情
package me.bello.eastline.controller;

import me.bello.eastline.entify.RspData;
import me.bello.eastline.utils.TextTool;
import org.attoparser.util.TextUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.TextUtils;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

import static me.bello.eastline.utils.AppConst.RSP_FAIL;
import static me.bello.eastline.utils.AppConst.RSP_OK;

@RestController
public class NotifyController extends BaseController {

    /**
     * 查询设置的通知规则列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = {"/queryNotifyList"}, method = RequestMethod.POST)
    public RspData index() throws Exception{
        RspData rspData = new RspData();

        List<Map<String, Object>> list = notifyService.doQueryNotifyList();
        rspData.setCode(RSP_OK);
        rspData.setMsg("OK");
        rspData.setData(list);
        return rspData;
    }


    /**
     * 新增规则
     *
     * @param req
     * @return
     * @throws Exception
     */
    @RequestMapping(value = {"/addNotify"}, method = RequestMethod.POST)
    public RspData addNotify(ServletRequest req) throws Exception {
        RspData rspData = new RspData();

        String code = req.getParameter("code");
        String name = req.getParameter("name");
        String max = req.getParameter("max");
        String min = req.getParameter("min");

        if (TextTool.isEmpty(code)){
            rspData.setCode(RSP_FAIL);
            rspData.setMsg("Code is not null.");
            return rspData;
        } else if (TextTool.isEmpty(name)){
            rspData.setCode(RSP_FAIL);
            rspData.setMsg("Name is not null.");
            return rspData;
        } else if (TextTool.isEmpty(max)){
            rspData.setCode(RSP_FAIL);
            rspData.setMsg("Max is not null.");
            return rspData;
        } else if (TextTool.isEmpty(min)){
            rspData.setCode(RSP_FAIL);
            rspData.setMsg("Min is not null.");
            return rspData;
        } else if (Float.parseFloat(max) < Float.parseFloat(min)){
            rspData.setCode(RSP_FAIL);
            rspData.setMsg("Max must more bigger than Min.");
            return rspData;
        }

        // 新增规则
        int result = notifyService.AddNotify(code, name, max, min);
        logger.error("result: " + result);
        if (result != 0){
            rspData.setCode(RSP_OK);
            rspData.setMsg("OK");
        } else {
            rspData.setCode(RSP_FAIL);
            rspData.setMsg("Add failed.");
        }
        return rspData;
    }


    /**
     * 删除规则
     *
     * @param req
     * @return
     * @throws Exception
     */
    @RequestMapping(value = {"/deleteNotify"}, method = RequestMethod.POST)
    public RspData deleteNotify(ServletRequest req) throws Exception {
        RspData rspData = new RspData();

        String code = req.getParameter("code");

        if (TextTool.isEmpty(code)){
            rspData.setCode(RSP_FAIL);
            rspData.setMsg("Code is not null.");
            return rspData;
        }

        // 删除规则
        int result = notifyService.deleteNotify(code);
        logger.error("result: " + result);
        if (result != 0){
            rspData.setCode(RSP_OK);
            rspData.setMsg("OK");
        } else {
            rspData.setCode(RSP_FAIL);
            rspData.setMsg("Delete failed.");
        }
        return rspData;
    }

}