Newer
Older
pgdsc / src / com / szpg / task / ReadAssetInSpectionTask.java
T440 on 9 Sep 2019 3 KB 修改巡检表
package com.szpg.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.szpg.db.dao.PgAssetInSpectionDao;
import com.szpg.db.dao.impl.PgAssetInSpectionImpl;
import com.szpg.db.data.PgAssetInSpection;
import com.szpg.db.data.PgAssetJson;
import com.szpg.util.HttpRequest;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

public class ReadAssetInSpectionTask implements Runnable{
    private final Logger logger = Logger.getLogger(this.getClass().getName());
    final String tableName="V_ASSET_INSPECTION_RECORD";
    final String url="http://10.10.2.31:9056/GLYW/api/Common/LoadIndexData";
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void run(){
        PgAssetInSpectionDao pgAssetInSpectionDao=new PgAssetInSpectionImpl();

        String startTime=pgAssetInSpectionDao.findLastRecordTime();//上次同步数据最后记录时间
        String endTime= dateFormat.format(new Date());//当前时间

        if(startTime==null)
            startTime=getYestodayStr();

        StringBuilder sb = new StringBuilder();
        sb.append("tableName = ").append(tableName);
        sb.append("&page=-1");
        sb.append("&CREATE_DATE=采集时间");
        sb.append(" between '").append(startTime);
        sb.append("' and '").append(endTime);
        sb.append("'");

        logger.info("开始请求巡检数据");
        String jsonStr = HttpRequest.sendPostByHttp(url, sb.toString());
        saveAsFileWriter(jsonStr);
        if (jsonStr.equals(""))
            logger.info("巡检数据请求失败");
        else {
            insertDB(pgAssetInSpectionDao, jsonStr);
            logger.info("巡检数据存储成功");
        }
    }

    private String savefile = "D:\\test.txt";
    private void saveAsFileWriter(String content) {

        FileWriter fwriter = null;
        try {
            fwriter = new FileWriter(savefile);
            fwriter.write(content);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                fwriter.flush();
                fwriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void insertDB(PgAssetInSpectionDao pgAssetInSpectionDao,String jsonStr)
    {
        List<PgAssetInSpection> pgAssetInSpectionList= getAssetByJsonString(jsonStr);
        pgAssetInSpectionDao.addAssetInSpectionRecord(pgAssetInSpectionList);
    }

    private List<PgAssetInSpection> getAssetByJsonString(String jsonStr){
        PgAssetJson pgAssetJson = JSON.parseObject(jsonStr, new TypeReference<PgAssetJson>() {});
        List<PgAssetInSpection> pgAssetInSpectionList=pgAssetJson.getRows();
        return pgAssetInSpectionList;
    }

    public String getYestodayStr() {
        Date dt = new Date();
        String yesToday = "";
        Date dtt = (Date) dt.clone();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dtt);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        yesToday = dateFormat.format(calendar.getTime());

        return yesToday;
    }
}