package com.szpg.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.szpg.db.dao.PgAssetInSpectionDao;
import com.szpg.db.dao.impl.PgAssetInSpectionImpl;
import com.szpg.db.data.PgAssetInSpectionPlan;
import com.szpg.db.data.PgAssetPlanJson;
import com.szpg.util.HttpRequest;
import org.apache.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

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

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

        try {
            String endTime = dateFormat.format(new Date());//当前时间

            String startTime = pgAssetInSpectionDao.findLastRecordTime();//上次同步数据最后记录时间
            if (startTime == null) {
                Date dt = new Date();
                startTime = getStartDateStr(dt, -1);
            }
            else
            {
                Date dtStart = dateFormat.parse(startTime);
                startTime=getStartDateStr(dtStart,1);
            }

            StringBuilder sb = new StringBuilder();
            sb.append("tableName=").append(tableName);
            sb.append("&page=-1");
            sb.append("&strWhere= UPDATEDATE");
            sb.append(" between '").append(startTime);
            sb.append("' and '").append(endTime);
            sb.append("'");

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

    public void insertDB(PgAssetInSpectionDao pgAssetInSpectionDao,String jsonStr)
    {
        List<PgAssetInSpectionPlan> pgAssetInSpectionPlanList= getAssetByJsonString(jsonStr);
        logger.info("同步巡检数据条数："+ String.valueOf(pgAssetInSpectionPlanList.size()));
        pgAssetInSpectionDao.addAssetInSpectionPlan(pgAssetInSpectionPlanList);
    }

    private List<PgAssetInSpectionPlan> getAssetByJsonString(String jsonStr){
        try {
            String jsonString=String.valueOf(JSON.parse(jsonStr));
            PgAssetPlanJson pgAssetPlanJson = JSONObject.parseObject(jsonString, new TypeReference<PgAssetPlanJson>() {});
            List<PgAssetInSpectionPlan> pgAssetInSpectionPlanList = pgAssetPlanJson.getRows();
            return pgAssetInSpectionPlanList;
        }
        catch(Exception ex){
            logger.error(ex.getMessage());
            return null;
        }

    }

    public String getStartDateStr(Date dt,int d) {

        String yesToday = "";
        Date dtt = (Date) dt.clone();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dtt);
        calendar.add(Calendar.DAY_OF_MONTH, d);
        yesToday = dateFormat.format(calendar.getTime());

        return yesToday;
    }

}