package com.steering.jobface.cld.service;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.jobface.cld.entity.CldDataEntity;
import com.steering.model.lims.MMeasureTEntity;
import com.steering.model.lims.MeterWorkCarActualEntity;

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

public class CldDataService extends DBCommon {

    /**
     * 查询产销数据
     * @param entity
     * @return
     */
    public ArrayList<CldDataEntity> queryData(CldDataEntity entity) {
        Connection con = null;
        ArrayList<CldDataEntity> entitys = null;
        String sql = CoreXmlSqlParsersFactory.getSql("CldDataTask.QueryData");
        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            entitys = EntityTool.getEntitysBySqlNoClsCon(sql, con, CldDataEntity.class);
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                if (con != null && !con.isClosed()) {
                    con.rollback();
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return entitys;
    }

    public CoreReturnObject updateData(ArrayList<CldDataEntity> entitys) throws Exception {
        String sql = CoreXmlSqlParsersFactory.getSql("CldDataTask.updateData");
        ArrayList<String> list = new ArrayList<String>();
        for (CldDataEntity entity : entitys) {
            list.add(EntityTool.fillSqlByEntity(sql, entity));
        }
        if (list == null || list.size() <= 0) {
            return new CoreReturnObject();
        }
        return this.setData("TgDao", list, null);
    }

    /**
     * 添加到中间库表
     * @param entity
     * @return
     */
    public CoreReturnObject insertData(CldDataEntity entity) throws Exception{
        String sql = CoreXmlSqlParsersFactory.getSql("CldDataTask.insertData");
        sql = EntityTool.fillSqlByEntity(sql, entity);
        return this.setData("ZheDaPBDaoTH", sql);
    }
}
