package com.steering.pss.qcm.DAL;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.judge.model.QcmZgJugdeApplyEntity;

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

public class QcmZgJugdeXs extends DBCommon {

    public CoreReturnObject query(String judgeStoveNo, String timeB, String timeE,String arr[],String orderNo,String xsFlag){
        ArrayList<Object> parms = new ArrayList<Object>();
        parms.add(judgeStoveNo);
        parms.add(timeB);
        parms.add(timeE);
        parms.add(orderNo);
        parms.add(xsFlag);
        SqlBuilder b = new SqlBuilder("QcmZgJugdeXs.query");
        b.Fill(parms.toArray());
        String sql = b.toString();
        if(!arr.equals("")){
        	String str = CoreConverter.toSqlConditionString("T.PLINE_NAME", arr);
        	sql += str;
        }
        return this.getData(sql);
    }


    /**
     * 修改
     */
    public CoreReturnObject update(String json) {
        ArrayList<String> jsons = new ArrayList<String>();
        jsons.add(json);
        return update(jsons,"1");
    }
    
    public CoreReturnObject update(ArrayList<String> jsons,String xsFlag){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            for(String json : jsons){
                QcmZgJugdeApplyEntity qcmZgJugdeApplyEntity = EntityTool.getEntityByGson(json, QcmZgJugdeApplyEntity.class);
                qcmZgJugdeApplyEntity.setXsFlag(xsFlag);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("QcmZgJugdeXs.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, qcmZgJugdeApplyEntity);
                if(!transction.ExcuteTransction(sqlUpdate)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
    
    public CoreReturnObject UpdateRemark(ArrayList<String> jsons) {
    	TransctionTool transction = new TransctionTool();
    	try {
    		 //Connection con = transction.getCon();
    		 for(String json : jsons){
    			 QcmZgJugdeApplyEntity qcmZgJugdeApplyEntity = EntityTool.getEntityByGson(json, QcmZgJugdeApplyEntity.class);
                 String sqlUpdate = CoreXmlSqlParsersFactory.getSql("QcmZgJugdeXs.UpdateRemark");
                 sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, qcmZgJugdeApplyEntity);
                 if(!transction.ExcuteTransction(sqlUpdate)){
                     return transction.rollBack();
                 }
    		 }
    	}catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
    
    //你这样把 预估计一下我的钱多久可以给我，
    //
    
    /***
   	 * 获取生产厂
   	 * @return
   	 */
   	public CoreReturnObject getPlineCode()
   	{
   		String sql = "SELECT DISTINCT C.BASECODE PLINE_CODE, C.BASENAME PLINE_NAME\r\n" +
   				"  FROM COM_BASE_PLINE T, COM_BASE_INFO C\r\n" +
   				" WHERE T.VALIDFLAG = '1'\r\n" +
   				"   AND T.FACTORY_CODE = C.BASECODE\r\n" +
   				"   AND T.FACTORY_CODE IS NOT NULL";
   		sql += " ORDER BY C.BASENAME";
   		return this.getData(sql);
   	}

   	public static String toSqlConditionString(String column, String[] args) {
        StringBuffer str = new StringBuffer();
        if (args != null && args.length > 0) {
            str.append(" AND ");
            str.append(column.toUpperCase());
            str.append(" IN(");
            for (String arg : args) {
                if (arg != null) {
                    str.append("'");
                    str.append(arg);
                    str.append("',");
                }
            }
            str.deleteCharAt(str.lastIndexOf(","));
            str.append(")");
        }
        return str.toString();
    }



    public CoreReturnObject queryInList(String judgeStoveNo, String timeB, String timeE, String bathYear,String inMold,String inType){
        ArrayList<Object> parms = new ArrayList<Object>();
        parms.add(judgeStoveNo);
        parms.add(timeB);
        parms.add(timeE);
        parms.add(bathYear);
        parms.add(inMold);
        parms.add(inType);
        SqlBuilder b = new SqlBuilder("QcmZgJugdeXs.queryInList");
        b.Fill(parms.toArray());
        return this.getData(b.toString());
    }

    public CoreReturnObject queryMatList(String judgeStoveNo, String timeB, String timeE, String bathYear,String inMold,String inType){
        ArrayList<Object> parms = new ArrayList<Object>();
        parms.add(judgeStoveNo);
        parms.add(timeB);
        parms.add(timeE);
        parms.add(bathYear);
        parms.add(inMold);
        parms.add(inType);
        SqlBuilder b = new SqlBuilder("QcmZgJugdeXs.queryMatList");
        b.Fill(parms.toArray());
        return this.getData(b.toString());
    }

    public CoreReturnObject queryOutList(String judgeStoveNo, String timeB, String timeE, String bathYear,String inMold,String inType){
        ArrayList<Object> parms = new ArrayList<Object>();
        parms.add(judgeStoveNo);
        parms.add(timeB);
        parms.add(timeE);
        parms.add(bathYear);
        parms.add(inMold);
        parms.add(inType);
        SqlBuilder b = new SqlBuilder("QcmZgJugdeXs.queryOutList");
        b.Fill(parms.toArray());
        return this.getData(b.toString());
    }

    /**
     * 修改
     */
    public CoreReturnObject updateInList(ArrayList<ArrayList<Object>> list){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            for(ArrayList<Object> parms : list){
                SqlBuilder sqlUpdate = new SqlBuilder("QcmZgJugdeXs.UpdateInList");
                sqlUpdate.Fill(parms.toArray());
                if(!transction.ExcuteTransction(sqlUpdate.toString())){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }

    public CoreReturnObject updateMatList(ArrayList<ArrayList<Object>> list){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            for(ArrayList<Object> parms : list){
                SqlBuilder sqlUpdate = new SqlBuilder("QcmZgJugdeXs.UpdateMatList");
                sqlUpdate.Fill(parms.toArray());
                if(!transction.ExcuteTransction(sqlUpdate.toString())){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }

    public CoreReturnObject updateOutList(ArrayList<ArrayList<Object>> list){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            for(ArrayList<Object> parms : list){
                SqlBuilder sqlUpdate = new SqlBuilder("QcmZgJugdeXs.UpdateOutList");
                sqlUpdate.Fill(parms.toArray());
                if(!transction.ExcuteTransction(sqlUpdate.toString())){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
}
