package com.steering.jobface.lims.service;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.Util.StringUtils;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.model.lims.*;

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

public class LimsDataService extends DBCommon {

    /**
     * 查询新计量同步数据
     * @param entity
     * @return
     */
    public ArrayList<MeterWorkCarActualEntity> getCarActual(MeterWorkCarActualEntity entity) {
        Connection con = null;
        ArrayList<MeterWorkCarActualEntity> entitys = null;
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.getCarActual");
        try {
            con = this.getDao("LimsDao").getConnection();
            con.setAutoCommit(false);
            entitys = EntityTool.getEntitysBySqlNoClsCon(sql, con, MeterWorkCarActualEntity.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;
    }

    /**
     * 批量新增数据
     * @param entitys
     * @return
     * @throws Exception
     */
    public CoreReturnObject insertMeasures(ArrayList<MMeasureTEntity> entitys) throws Exception {
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.insertMeasure");
        ArrayList<String> list = new ArrayList<String>();
        for (MMeasureTEntity entity : entitys) {
            list.add(EntityTool.fillSqlByEntity(sql, entity));
        }
        return this.setData("WlsrvDaoNew", list, null);
    }

    /**
     * 批量修改数据
     * @param entitys
     * @return
     * @throws Exception
     */
    public CoreReturnObject updateCarActuals(ArrayList<MeterWorkCarActualEntity> entitys) throws Exception {
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.updateCarActual");
        ArrayList<String> list = new ArrayList<String>();
        for (MeterWorkCarActualEntity entity : entitys) {
            list.add(EntityTool.fillSqlByEntity(sql, entity));
        }
        return this.setData("LimsDao", list, null);
    }

    /**
     * 查询老计量数据
     * @param entity
     * @return
     */
    public MMeasureTEntity getMeasure(MMeasureTEntity entity) {
        Connection con = null;
        MMeasureTEntity mMeasureTEntity = null;
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.getMeasure");
        try {
            con = this.getDao("WlsrvDaoNew").getConnection();
            con.setAutoCommit(false);
            sql = EntityTool.fillSqlByEntity(sql, entity);
            mMeasureTEntity = EntityTool.getEntityBySqlNoClsCon(sql, con, MMeasureTEntity.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 mMeasureTEntity;
    }

    /**
     * 新增老计量数据
     * @param entity
     * @return
     */
    public CoreReturnObject insertMeasure(MMeasureTEntity entity) {
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.insertMeasure");
        sql = EntityTool.fillSqlByEntity(sql, entity);
        return this.setData("WlsrvDaoNew", sql);
    }

    /**
     * 删除老计量数据
     * @param entity
     * @return
     */
    public CoreReturnObject deleteMeasure(MMeasureTEntity entity) {
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.deleteMeasure");
        sql = EntityTool.fillSqlByEntity(sql, entity);
        return this.setData("WlsrvDaoNew", sql);
    }

    /**
     * 更新老计量数据
     * @param entity
     * @return
     */
    public CoreReturnObject updateMeasure(MMeasureTEntity entity) {
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.updateMeasure");
        sql = EntityTool.fillSqlByEntity(sql, entity);
        return this.setData("WlsrvDaoNew", sql);
    }

    /**
     * 修改新计量状态
     * @param entity
     * @return
     */
    public CoreReturnObject updateCarActual(MeterWorkCarActualEntity entity) {
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.updateCarActual");
        sql = EntityTool.fillSqlByEntity(sql, entity);
        return this.setData("LimsDao", sql);
    }

    /**
     * 查询一次计量
     * @return
     */
    public ArrayList<MeterWorkCarActualFirstEntity> getCarActualFirst() {
        Connection con = null;
        ArrayList<MeterWorkCarActualFirstEntity> entitys = null;
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.getCarActualFirst");
        try {
            con = this.getDao("LimsDao").getConnection();
            con.setAutoCommit(false);
            entitys = EntityTool.getEntitysBySqlNoClsCon(sql, con, MeterWorkCarActualFirstEntity.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;
    }

    /**
     * 修改一次计量状态
     * @param entity
     * @return
     */
    public CoreReturnObject updateCarActualFirst(MeterWorkCarActualFirstEntity entity) {
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.updateCarActualFirst");
        sql = EntityTool.fillSqlByEntity(sql, entity);
        return this.setData("LimsDao", sql);
    }

    /**
     * 查询产销同步的物料
     * @param entity
     * @return
     */
    public MeterBaseMatterInfoEntity getMatInfo(MeterBaseMatterInfoEntity entity){
        Connection con = null;
        MeterBaseMatterInfoEntity meterWorkCarActualEntity = null;
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.getMatInfo");
        try {
            con = this.getDao("LimsDao").getConnection();
            con.setAutoCommit(false);
            sql = EntityTool.fillSqlByEntity(sql, entity);
            meterWorkCarActualEntity = EntityTool.getEntityBySqlNoClsCon(sql, con, MeterBaseMatterInfoEntity.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 meterWorkCarActualEntity;
    }

    /**
     * 查询老计量供应商单位
     * @param entity
     * @return
     */
    public MUnitTEntity getOldUnit(MUnitTEntity entity){
        Connection con = null;
        MUnitTEntity qSteelsTEntity = null;
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.getOldUnit");
        try {
            con = this.getDao("WlsrvDaoNew").getConnection();
            con.setAutoCommit(false);
            sql = EntityTool.fillSqlByEntity(sql, entity);
            qSteelsTEntity = EntityTool.getEntityBySqlNoClsCon(sql, con, MUnitTEntity.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 qSteelsTEntity;
    }

    /**
     * 查询老计量物料
     * @param entity
     * @return
     */
    public MMaterialTEntity getMaterial(MMaterialTEntity entity){
        Connection con = null;
        MMaterialTEntity qSteelsTEntity = null;
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.getMaterial");
        try {
            con = this.getDao("WlsrvDaoNew").getConnection();
            con.setAutoCommit(false);
            sql = EntityTool.fillSqlByEntity(sql, entity);
            qSteelsTEntity = EntityTool.getEntityBySqlNoClsCon(sql, con, MMaterialTEntity.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 qSteelsTEntity;
    }

    /**
     * 查询产销同步供应商
     * @param entity
     * @return
     */
    public MeterBaseCustomerSupplierEntity getSuppInfo(MeterBaseCustomerSupplierEntity entity){
        Connection con = null;
        MeterBaseCustomerSupplierEntity qSteelsTEntity = null;
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.getSuppInfo");
        try {
            con = this.getDao("LimsDao").getConnection();
            con.setAutoCommit(false);
            sql = EntityTool.fillSqlByEntity(sql, entity);
            qSteelsTEntity = EntityTool.getEntityBySqlNoClsCon(sql, con, MeterBaseCustomerSupplierEntity.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 qSteelsTEntity;
    }

    /**
     * 查询老计量的扣杂，超长
     * @param entity
     * @return
     */
    public QSteelsTEntity getSteels(QSteelsTEntity entity){
        Connection con = null;
        QSteelsTEntity qSteelsTEntity = null;
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.getSteels");
        try {
            con = this.getDao("WlsrvDaoNew").getConnection();
            con.setAutoCommit(false);
            sql = EntityTool.fillSqlByEntity(sql, entity);
            qSteelsTEntity = EntityTool.getEntityBySqlNoClsCon(sql, con, QSteelsTEntity.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 qSteelsTEntity;
    }

    /**
     * 删除老计量的扣杂，超长
     * @param entity
     * @return
     */
    public CoreReturnObject deleteSteels(QSteelsTEntity entity) {
        if (entity == null || StringUtils.isEmpty(entity.getMatchid())) {
            new Exception("验配id为空！");
            return null;
        }
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.deleteSteels");
        sql = EntityTool.fillSqlByEntity(sql, entity);
        return this.setData("WlsrvDaoNew", sql);
    }

    /**
     * 新增老计量的扣杂，超长
     * @param entity
     * @return
     */
    public CoreReturnObject insertSteels(QSteelsTEntity entity) {
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDataTask.insertSteels");
        sql = EntityTool.fillSqlByEntity(sql, entity);
        return this.setData("WlsrvDaoNew", sql);
    }


    public CoreReturnObject deleteCarActualBks(MeterWorkCarActualBksEntity entity) {
        String sql = CoreXmlSqlParsersFactory.getSql("LimsDelDataTask.delCarActual");
        sql = EntityTool.fillSqlByEntity(sql, entity);
        return this.setData("LimsDao", sql);
    }
}
