All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.telewave.logger.controller.SoftwareLogController Maven / Gradle / Ivy

The newest version!
package com.telewave.logger.controller;

import com.alibaba.fastjson.JSONArray;
import com.telewave.logger.common.ResponseVO;
import com.telewave.logger.common.Utils;
import com.telewave.logger.config.TwloggerServerconfig;
import com.telewave.logger.entity.SoftwareLog;
import com.telewave.logger.page.PageResult;
import com.telewave.logger.service.SoftwareLogService;
import com.telewave.logger.service.SqlUitls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

@RestController(value = "twloggerjdbcSoftwareLog")
@RequestMapping(value = "twloggerjdbc/SoftwareLog")
public class SoftwareLogController {


    private static final int REPORT_FAIL = 0;
    private static final int REPORT_ERROR = 1;

    private final SoftwareLogService softwareLogService;

    private final TwloggerServerconfig twloggerServerconfig;

    @Autowired
    public SoftwareLogController(SoftwareLogService softwareLogService, TwloggerServerconfig twloggerServerconfig) {
        this.softwareLogService = softwareLogService;
        this.twloggerServerconfig = twloggerServerconfig;
    }

    /**
     * 分页查询
     * @param pageNum  分页页数
     * @param pageSize  分页条数
     * @return  SoftwareLog集合
     * @throws Exception  错误
     */
    @RequestMapping(value="selectByPage/{pageNum}/{pageSize}",method = RequestMethod.GET)
    @ResponseBody
    public List selectByPage(@PathVariable("pageNum") int pageNum,
                                           @PathVariable("pageSize") int pageSize) throws Exception{
        PageResult pageResult = softwareLogService.selectByPage(SqlUitls.SOFTWARELOG_QUERY_PAGE,new ArrayList<>(),pageNum,pageSize);
        List list = (List) pageResult.getRows();
        return list;
    }

    /**
     * 本地H2数据库插入数据后,上报到日志服务,成功后上传该数据,不成功更新该数据状态
     * @param softwareLog softwareLog对象
     * @return 成功或失败
     * @throws Exception 错误
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
    public ResponseVO insert(SoftwareLog softwareLog) throws Exception{

        return log4j2insert(softwareLog,null);
    }

    public ResponseVO log4j2insert(SoftwareLog softwareLog, String path) throws Exception{
        String serverPath="";
        if(path == null){
            serverPath = twloggerServerconfig.getSoftwarelogPath();
        }
        ResponseVO vo = new ResponseVO();
        vo.setData(false);
        //
        if(softwareLog.getSoftware()==null){
            softwareLog.setSoftware(twloggerServerconfig.appName);
        }
        if(softwareLog.getVersion()==null){
            softwareLog.setVersion(twloggerServerconfig.appVersion);
        }
        //判断对象是否合法
        if(! Utils.checkNotNull(softwareLog)){
            return vo;
        }
        //先上报,失败后存库
        try{
            Utils.TWLOGGER_SOFTWARELOG.info("\r\n" + "SoftwareLog对象:" + JSONArray.toJSONString(softwareLog) + "\r\n");
            vo = softwareLogService.report(twloggerServerconfig.getSoftwarelogPath(),softwareLog);
            if(!vo.getData()){
                softwareLog.setReportflag(REPORT_ERROR);
                softwareLog.setReportcode(vo.getCode());
                softwareLogService.insert(softwareLog);
            }
        }catch (Exception e){
            softwareLog.setReportflag(REPORT_ERROR);
            softwareLog.setReportcode(e.getMessage());
            softwareLogService.insert(softwareLog);
            Utils.saveLoggerToFileError(e);
        }
        return vo;
    }


    /**
     * 根据ids,批量删除
     * @param ids ids,已逗号分隔
     * @return 成功条数
     * @throws Exception 错误
     */
    @ResponseBody
    @RequestMapping(value = "/deleteBatch",method = RequestMethod.DELETE)
    public ResponseVO deleteBatch(@RequestParam("ids") String ids) throws Exception {
        ResponseVO vo = new ResponseVO();
        String[] idsArray = ids.split(",");
        int num = softwareLogService.deleteBatch(idsArray);
        vo.setData(num);
        return vo;
    }
    /**
     * 根据id删除
     * @param id id
     * @return 成功或失败
     * @throws Exception 错误
     */
    @ResponseBody
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResponseVO delete(@PathVariable("id") String id) throws Exception{
        ResponseVO vo = new ResponseVO();
        int num = softwareLogService.delete(id);
        if(num > 0){
            vo.setData(true);
        }
        return vo;
    }
    /**
     * 根据id查询
     * @param id id
     * @return softwarelog
     * @throws Exception 错误
     */
    @ResponseBody
    @RequestMapping(value="select/{id}",method = RequestMethod.GET)
    public ResponseVO selectById(@PathVariable("id") String id) throws Exception{
        ResponseVO vo = new ResponseVO();
        SoftwareLog softwareLog = softwareLogService.selectByPrimary(id);
        vo.setData(softwareLog);
        return vo;
    }
    /**
     * 删除所有
     * @return 成功或失败
     * @throws Exception 错误
     */
    @ResponseBody
    @RequestMapping(value = "deleteAll",method = RequestMethod.GET)
    public ResponseVO deleteAll() throws Exception{
        ResponseVO vo = new ResponseVO();
        int num = softwareLogService.deleteAll();
        vo.setData(num);
        return vo;
    }

    /**
     * 删除上报失败,且标记为1的数据
     * @return 成功条数
     * @throws Exception 错误
     */
    @ResponseBody
    @RequestMapping(value = "deleteReportFail",method = RequestMethod.GET)
    public ResponseVO deleteReportFail() throws Exception{
        ResponseVO vo = new ResponseVO();
        int num = softwareLogService.deleteAll();
        vo.setData(num);
        return vo;
    }

    /**
     * 将上报失败标记为1的数据,且上报错误码为null的数据,修改为待上报
     * @return 成功条数
     * @throws Exception 错误
     */
    @ResponseBody
    @RequestMapping(value = "initReportFail",method = RequestMethod.GET)
    public ResponseVO initReportFail() throws Exception{
        ResponseVO vo = new ResponseVO();
        int num = softwareLogService.updateReportInit();
        vo.setData(num);
        return vo;
    }
    /**
     * 将上报失败的数据,修改为待上报
     * @return 成功条数
     * @throws Exception 错误
     */
    @ResponseBody
    @RequestMapping(value = "initReportFailAll",method = RequestMethod.GET)
    public ResponseVO initReportFailAll() throws Exception{
        ResponseVO vo = new ResponseVO<>();
        int num = softwareLogService.updateReportInitAll();
        vo.setData(num);
        return vo;
    }
    /**
     * 将上报失败的数据,修改为待上报,并全部上报
     * @return 返回成功和失败条数
     * @throws Exception 错误
     */
    @ResponseBody
    @RequestMapping(value = "reportFail",method = RequestMethod.GET)
    public ResponseVO> reportFail() throws Exception{
        initReportFailAll();
        return reportAll();
    }

    /**
     * 将上报失败标记为0的数据,再次上报,一次1000条,暂停30秒再继续。
     * @return 成功条数
     * @throws Exception 错误
     */
    @ResponseBody
    @RequestMapping(value = "reportAll",method = RequestMethod.GET)
    public ResponseVO> reportAll() throws Exception{
        int SUCCESS_NUM = 0;
        int FAIL_NUM = 0;
        int sum_num = softwareLogService.selectTotleReport();
        int num = 1000;
        int time = 0;
        if(sum_num % num != 0){
            time = sum_num/num + 1;
        }else{
            time = sum_num/num;
        }
        for (int i = 0; i < time; i++) {
            List list = reportByFail(num);
            SUCCESS_NUM = SUCCESS_NUM + list.get(0);
            FAIL_NUM = FAIL_NUM + list.get(1);
        }

        ResponseVO> responseVO = new ResponseVO>();
        List integerList = new ArrayList<>();
        integerList.add(SUCCESS_NUM);
        integerList.add(FAIL_NUM);
        responseVO.setData(integerList);
        responseVO.setMsg("成功:"+SUCCESS_NUM+"。失败:"+FAIL_NUM+"。");
        return responseVO;
    }

    /**
     * 根据指定数量上报
     * @param pagesize 上报的数量
     * @return 成功失败数量
     * @throws Exception 错误
     */
    @ResponseBody
    @RequestMapping(value = "reportByFail/{pagesize}",method = RequestMethod.GET)
    public List reportByFail(@PathVariable("pagesize") Integer pagesize) throws Exception{
        List retList = new ArrayList<>();
        if(pagesize <= 0){
            return retList;
        }
        int SUCCESS_NUM = 0;
        int FAIL_NUM = 0;
        //分页查询1
        List lists = new ArrayList<>();
        lists.add(0);
        PageResult results = softwareLogService.selectByPage(SqlUitls.SOFTWARELOG_QUERY_PAGE_REPORT,lists,1,pagesize);

        if(results!=null && results.getRows().size()>0){
            for (Object softwareLogTmp : results.getRows()) {
                SoftwareLog softwareLog = (SoftwareLog)softwareLogTmp;
                //先上报,失败后存库
                try{
                    ResponseVO vo = softwareLogService.report(twloggerServerconfig.getSoftwarelogPath(),softwareLog);
                    if(!vo.getData()){
                        softwareLog.setReportflag(REPORT_FAIL);
                        softwareLog.setReportcode(vo.getCode());
                        softwareLogService.updateReportState(vo.getCode(),REPORT_ERROR,softwareLog.getId());
                        FAIL_NUM = FAIL_NUM + 1;
                    }else{
                        SUCCESS_NUM = SUCCESS_NUM + 1;
                        softwareLogService.delete(softwareLog.getId());
                    }
                }catch (Exception e){
                    int length = e.getMessage().toString().length();
                    if(length > 100){
                        length = 100;
                    }
                    String code = e.getMessage().toString().substring(0,length);
                    softwareLogService.updateReportState(code,REPORT_ERROR,softwareLog.getId());
                    FAIL_NUM = FAIL_NUM + 1;
                    e.printStackTrace();
                }

            }

        }
        retList.add(SUCCESS_NUM);
        retList.add(FAIL_NUM);
        return retList;
    }

}