package com.szpg.db.dao.impl;

import com.szpg.db.dao.PgMaintanceDao;
import com.szpg.db.data.PgMaintance;
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 PgMaintanceDaoImpl implements PgMaintanceDao {

    private final Logger logger = Logger.getLogger(this.getClass().getName());

    @Override
    public void addMaintanceRecord(List<PgMaintance> pgMaintanceList){
        Connection conn = null;

        String insertStr = "INSERT INTO PG_MAINTANCERECORD_HEAD " +
                "(ID,KEY_ID, PLAN_KEY_ID, PLAN_NAME, RECORD_NO,REOAIRMAN,MAINTENANCE_DATE,BUILD_CABIN,PROJECT,CREATEDATE," +
                "CREATOR) VALUES (SEQ_PG_MAINTANCERECORD_HEAD_ID.nextval,?,?,?,?,?,?,?,?,?,?)";

        List<Object[]> paramObjectList=new ArrayList<Object[]>();
        for(PgMaintance pgMaintance:pgMaintanceList){
            Object[] params = new Object[10];
            params[0] = pgMaintance.getKEY_ID();
            params[1] = pgMaintance.getPLAN_KEY_ID();
            params[2] = pgMaintance.getPLAN_NAME();
            params[3] = pgMaintance.getRECORD_NO();
            params[4]=pgMaintance.getREOAIRMAN();
            params[5]=pgMaintance.getMAINTENANCE_DATE();
            params[6]=pgMaintance.getBUILD_CABIN();
            params[7]=pgMaintance.getPROJECT();
            params[8]=pgMaintance.getCREATEDATE();
            params[9]=pgMaintance.getCREATOR();

            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.debug("维护数据同步成功" + pgMaintanceList.toString());
                else
                    logger.error("维护数据同步失败！");
            }

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

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

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

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

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


}
