Gb1400Controller.java 7.23 KB
package com.viontech.controller;

import com.alibaba.fastjson.JSONObject;
import com.viontech.constant.SystemConstants;
import com.viontech.service.Gb1400Service;
import com.viontech.utils.Gb1400ResponseUtil;
import com.viontech.utils.JsonMessage;
import com.viontech.vo.gb1400.SubscribeNotificationsObj;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.HandlerMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/VIID")
public class Gb1400Controller {

    @Resource
    private Gb1400Service gb1400Service;
    /**
     * 批量订阅
     * @param params
     * @return
     */
//    @RequestMapping(value = "/Subscribes" ,method = RequestMethod.POST)
//    @ResponseBody
//    public JSONObject subscribes(@RequestBody String params){
//        log.info("收到请求[Subscribes.request]:{}", params);
//        JsonMessage vo = gb1400Service.subscribes(params);
//        log.info("响应[Subscribes.request]:{}", JSONObject.toJSONString(vo));
//        if (SystemConstants.APP_CODE_SUCCESS == vo.getCode()) {
//            return Gb1400ResponseUtil.responseStatusListObject((List<JSONObject>) vo.getData());
//        } else {
//            return Gb1400ResponseUtil.error(1, vo.getMsg(),"/VIID/Subscribes");
//        }
//    }

    /**
     * 批量订阅
     * @return
     */
    @RequestMapping(value = "/Subscribes" ,method = RequestMethod.POST)
    public void subscribes(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BufferedReader br = request.getReader();
        String str;
        StringBuilder params = new StringBuilder();
        while((str = br.readLine()) != null){
            params.append(str);
        }
        log.info("收到请求[Subscribes.request]:{}", params.toString());
        JsonMessage vo = gb1400Service.subscribes(params.toString());
        log.info("响应[Subscribes.request]:{}", JSONObject.toJSONString(vo));

        JSONObject result = null;
        response.setContentType("application/VIID+JSON;charset=utf-8");
        if (SystemConstants.APP_CODE_SUCCESS == vo.getCode()) {
            result = Gb1400ResponseUtil.responseStatusListObject((List<JSONObject>) vo.getData());
        } else {
            result = Gb1400ResponseUtil.error(1, vo.getMsg(),"/VIID/Subscribes");
        }
        response.getWriter().write(result.toJSONString());
    }

    /**
     * 取消订阅
     * @param ID
     * @return
     */
//    @RequestMapping(value = "/Subscribes/{ID}" ,method = RequestMethod.PUT)
//    @ResponseBody
//    public JSONObject unSubscribes(@PathVariable String ID, @RequestBody String params){
//        log.info("收到请求[unSubscribes.request]:{}-{}", ID, params);
//        JsonMessage vo = gb1400Service.unSubscribes(ID, params);
//        log.info("响应[unSubscribes.request]:{}-{}", ID, JSONObject.toJSONString(vo));
//        if (SystemConstants.APP_CODE_SUCCESS == vo.getCode()) {
//            return Gb1400ResponseUtil.success("/VIID/Subscribes/" + ID);
//        } else {
//            return Gb1400ResponseUtil.error(1, vo.getMsg(),"/VIID/Subscribes/" + ID);
//        }
//    }

    /**
     * 取消订阅
     * @return
     */
    @RequestMapping(value = "/Subscribes/{ID}" ,method = RequestMethod.PUT)
    @ResponseBody
    public void unSubscribes(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map attribute = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        String ID = "";
        if (attribute != null && attribute.containsKey("ID")) {
            ID = (String) attribute.get("ID");
        }
        BufferedReader br = request.getReader();
        String str;
        StringBuilder params = new StringBuilder();
        while((str = br.readLine()) != null){
            params.append(str);
        }

        log.info("收到请求[unSubscribes.request]:{}-{}", ID, params.toString());
        JsonMessage vo = gb1400Service.unSubscribes(ID, params.toString());
        log.info("响应[unSubscribes.request]:{}-{}", ID, JSONObject.toJSONString(vo));

        JSONObject result = null;
        response.setContentType("application/VIID+JSON;charset=utf-8");
        if (SystemConstants.APP_CODE_SUCCESS == vo.getCode()) {
            result = Gb1400ResponseUtil.success(ID,"/VIID/Subscribes/" + ID);
        } else {
            result = Gb1400ResponseUtil.error(ID, 1, vo.getMsg(),"/VIID/Subscribes/" + ID);
        }
        response.getWriter().write(result.toJSONString());
    }

    /**
     * 更新订阅
     * @return
     */
    @RequestMapping(value = "/Subscribes" ,method = RequestMethod.PUT)
    public void updateSubscribes(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BufferedReader br = request.getReader();
        String str;
        StringBuilder params = new StringBuilder();
        while((str = br.readLine()) != null){
            params.append(str);
        }
        log.info("收到请求[put.Subscribes.request]:{}", params.toString());
        JsonMessage vo = gb1400Service.subscribes(params.toString());
        log.info("响应[put.Subscribes.request]:{}", JSONObject.toJSONString(vo));

        JSONObject result = null;
        response.setContentType("application/VIID+JSON;charset=utf-8");
        if (SystemConstants.APP_CODE_SUCCESS == vo.getCode()) {
            result = Gb1400ResponseUtil.responseStatusListObject((List<JSONObject>) vo.getData());
        } else {
            result = Gb1400ResponseUtil.error(1, vo.getMsg(),"/VIID/Subscribes");
        }
        response.getWriter().write(result.toJSONString());
    }

    /**
     * 删除通知记录
     * @param IDList
     * @return
     */
    @DeleteMapping("/SubscribeNotifications")
    public JsonMessage deleteNotificationsRecord(String IDList){
        log.info("收到请求[deleteNotificationsRecord.request]:{}", IDList);
        JsonMessage vo = gb1400Service.deleteNotifications(IDList);
        return vo;
    }

    /**
     * 查询通知记录
     * @param obj
     * @return
     */
    @GetMapping("/SubscribeNotifications")
    public JsonMessage selectNotificationsRecord(SubscribeNotificationsObj.SubscribeNotificationObject obj){
        log.info("收到请求[selectNotificationsRecord.request]:{}", JSONObject.toJSONString(obj));
        JsonMessage vo = gb1400Service.selectNotifications(obj);
        return vo;
    }


    /**
     * 注册接口
     * @param params
     * @return
     */
    @RequestMapping(value = "/System/Register" ,method = RequestMethod.POST)
    @ResponseBody
    public JSONObject Register(@RequestBody String params){
        log.info("收到请求[Register.request]:{}", params);
        JsonMessage vo = gb1400Service.registerToMySystem(params);
        log.info("响应[Register.request]:{}", JSONObject.toJSONString(vo));
        if (SystemConstants.APP_CODE_SUCCESS == vo.getCode()) {
            return Gb1400ResponseUtil.success("/VIID/System/Register");
        } else {
            return Gb1400ResponseUtil.error(1, vo.getMsg(),"/VIID/System/Register");
        }
    }
}