package com.szpg.db.dao.impl;

import com.szpg.db.dao.PgAssetInSpectionDao;
import com.szpg.db.data.PgAssetInSpectionPlan;
import com.szpg.db.util.ConnectionManager;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PgAssetInSpectionImpl implements PgAssetInSpectionDao {

    private org.apache.log4j.Logger logger = Logger.getLogger(this.getClass().getName());

    @Override
    public void addAssetInSpectionPlan(List<PgAssetInSpectionPlan> pgAssetInSpectionPlanList){
        Connection conn = null;

        String insertStr = "INSERT INTO PG_ASSET_INSPECTION_PLAN " +
                "(KEY_ID, PLAN_NAME, INSPECTION_PERSON,START_DATE,END_DATE,FREQUENCY,STATE,RESPONSIBLE_DEPT,PLAN_NO," +
                "INSPECTION_TYPE,INSPECTION_MODE,INSPECTION_ROUTE,INSPECTION_START_TIME,INSPECTION_END_TIME,INSPECTION_PRACE," +
                "INSPECTION_GROUP,INSERT_TIME,FREQUENCYDATE,FREQUENCYTIME,CREATEDATE,CREATOR,PROJECT,COMLILE,START_TIME,DEVELOP_DEPT," +
                "END_TIME,START_PILE_NO,END_PILE_NO,BUILD_CABIN,MILEAGE,FREQUENCY_TIME,FREQUENCYTIME_END,KETTLE_TS,UPDATEDATE) " +
                "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        List<Object[]> paramObjectList=new ArrayList<Object[]>();
        for(PgAssetInSpectionPlan pgAssetInSpectionPlan:pgAssetInSpectionPlanList){
            Object[] params = new Object[34];
            params[0] = pgAssetInSpectionPlan.getKEY_ID();
            params[1] = pgAssetInSpectionPlan.getPLAN_NAME();
            params[2] = pgAssetInSpectionPlan.getINSPECTION_PERSON();
            params[3] = pgAssetInSpectionPlan.getSTART_DATE();
            params[4]=pgAssetInSpectionPlan.getEND_DATE();
            params[5]=pgAssetInSpectionPlan.getFREQUENCY();
            params[6]=pgAssetInSpectionPlan.getSTATE();
            params[7]=pgAssetInSpectionPlan.getRESPONSIBLE_DEPT();
            params[8]=pgAssetInSpectionPlan.getPLAN_NO();
            params[9]=pgAssetInSpectionPlan.getINSPECTION_TYPE();
            params[10]=pgAssetInSpectionPlan.getINSPECTION_MODE();
            params[11]=pgAssetInSpectionPlan.getINSPECTION_ROUTE();
            params[12]=pgAssetInSpectionPlan.getINSPECTION_START_TIME();
            params[13]=pgAssetInSpectionPlan.getINSPECTION_END_TIME();
            params[14]=pgAssetInSpectionPlan.getINSPECTION_PRACE();
            params[15]=pgAssetInSpectionPlan.getINSPECTION_GROUP();
            params[16]=pgAssetInSpectionPlan.getINSERT_TIME();
            params[17]=pgAssetInSpectionPlan.getFREQUENCYDATE();
            params[18]=pgAssetInSpectionPlan.getFREQUENCYTIME();
            params[19]=pgAssetInSpectionPlan.getCREATEDATE();
            params[20]=pgAssetInSpectionPlan.getCREATOR();
            params[21]=pgAssetInSpectionPlan.getPROJECT();
            params[22]=pgAssetInSpectionPlan.getCOMLILE();
            params[23]=pgAssetInSpectionPlan.getSTART_TIME();
            params[24]=pgAssetInSpectionPlan.getDEVELOP_DEPT();
            params[25]=pgAssetInSpectionPlan.getEND_TIME();
            params[26]=pgAssetInSpectionPlan.getSTART_PILE_NO();
            params[27]=pgAssetInSpectionPlan.getEND_PILE_NO();
            params[28]=pgAssetInSpectionPlan.getBUILD_CABIN();
            params[29]=pgAssetInSpectionPlan.getMILEAGE();
            params[30]=pgAssetInSpectionPlan.getFREQUENCY_TIME();
            params[31]=pgAssetInSpectionPlan.getFREQUENCYTIME_END();
            params[32]=pgAssetInSpectionPlan.getKETTLE_TS();
            params[33]=pgAssetInSpectionPlan.getUPDATEDATE();

            paramObjectList.add(params);
        }

        try {
            conn = ConnectionManager.getConnectionFromC3P0();
            conn.setAutoCommit(false);

            QueryRunner runner = new QueryRunner();
            for (Object[] params:paramObjectList) {
                int count = runner.update(conn, insertStr, params);
                if (count > 0)
                    logger.info("巡检数据同步成功" + pgAssetInSpectionPlanList.toString());
                else
                    logger.info("巡检数据同步失败！");
            }

        } catch (Exception ex) {
            logger.info("巡检数据同步异常", ex);
        } finally {
            try {
                DbUtils.commitAndClose(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String findLastRecordTime(){
        Connection conn = null;

        String queryStr = "SELECT * FROM PG_ASSET_INSPECTION_PLAN ORDER BY UPDATEDATE DESC";
        try {
            conn = ConnectionManager.getConnectionFromC3P0();
            conn.setAutoCommit(false);

            QueryRunner runner = new QueryRunner();
            List<PgAssetInSpectionPlan> pgAssetInSpectionPlanList = (List<PgAssetInSpectionPlan>) runner.query(conn,
                    queryStr, new BeanListHandler<PgAssetInSpectionPlan>(PgAssetInSpectionPlan.class));

            if (null != pgAssetInSpectionPlanList && pgAssetInSpectionPlanList.isEmpty() == false) {
                logger.debug("获取最新巡检记录成功[" + pgAssetInSpectionPlanList + "]");
                return pgAssetInSpectionPlanList.get(0).getUPDATEDATE();
            }
            else
                return null;
        } catch (Exception ex) {
            logger.error("获取最新巡检记录异常", ex);
        } finally {
            try {
                DbUtils.commitAndClose(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}