SSM +常用流程记载

package com.eastwit.business.custom.service.changzhou.impl;

import cn.hutool.setting.Setting;
import com.eastwit.business.custom.beans.syncItem;
import com.eastwit.business.custom.dao.changzhou.CZSyncDataDao;
import com.eastwit.business.custom.service.changzhou.CZSyncDataService;
import com.eastwit.common.datasync.OracleJDBC;
import com.github.pagehelper.PageHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: cyy
 **/
@Service
public class CZSyncDataServiceImpl implements CZSyncDataService {

    private static final Logger LOGGER = LogManager.getLogger(CZSyncDataServiceImpl.class);

    @Autowired
    private CZSyncDataDao czSyncDataDao;

    @Value("#{properties.changhzhou_lastSyncTime}")
    private String LAST_TIME;


    /**
     * 最后一次日志同步时间, LAST_TIME注入到spring的值初始值为2021-10-01
     */
    private static String LAST_NEW_TIME = "";

    @Override
    public void start() {
        LOGGER.info("get logInfo start :" + LocalDateTime.now().toString());
        List<syncItem> infoLIst = infolist();
        //日志数据条数
        int size = infoLIst.size();
        LOGGER.info("infoLIst size:" + infoLIst.size() + "====== LAST_NEW_TIME :" + LAST_NEW_TIME);
        if (size > 0) {
            String userIndate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(infoLIst.get(size - 1).getCreate_time());
            //存在数据写入oracle
            //insertOracle(infoLIst);
            boolean flag = OracleJDBC.insert(infoLIst);
            //更新最后一次拉取时间
            if (flag){
                LAST_NEW_TIME = userIndate;
                updateLastSyncDate(userIndate);
            }
        }

        LOGGER.info("get logInfo end :" + LAST_NEW_TIME);
    }

    /**
     * 查询日志数据
     */
    private List<syncItem> infolist() {
        List<syncItem> result = new ArrayList<>();
        boolean flag = true;
        int curPage = 1;
        try {
            Date date;
            if (LAST_NEW_TIME.equals("")) {
                //配置文件中时间
                date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(LAST_TIME);
            } else {
                //上一次拉取最新时间
                date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(LAST_NEW_TIME);
            }
            LOGGER.info("search date:" + date);
            while (flag) {
                //分页日志结果
                PageHelper.startPage(curPage, 1000, false);
                List<syncItem> logInfo = czSyncDataDao.getLogInfo(date);
                //没有结果 结束while
                flag = logInfo != null && !logInfo.isEmpty();
                if (!flag) {
                    break;
                }
                result.addAll(logInfo);
                curPage++;
            }
            return result;
        } catch (Exception e) {
            LOGGER.info("get info failed ,please check your code" + e.getMessage() +  "cause" + e.getCause());
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 更新配置文件中最后一次日志同步的时间
     * 格式为2000-01-01
     */
    private void updateLastSyncDate(String userIndate) {
        Setting setting = new Setting("custom.properties");
        LOGGER.info("old:" + setting.getStr("changhzhou_lastSyncTime"));
        setting.set("changhzhou_lastSyncTime", userIndate);
        setting.store(this.getClass().getClassLoader().getResource("custom.properties").getPath());
        LOGGER.info("new:" + setting.getStr("changhzhou_lastSyncTime"));
    }



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值