package com.steering.pss.sale.order.DAL;

import java.sql.Connection;
import java.util.ArrayList;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.StringTool;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.qcm.model.GenericEntity;
import com.steering.pss.sale.order.model.CraftFileLv2CEntity;
import com.steering.pss.sale.order.model.CraftFileLv2Entity;
import com.steering.pss.sale.order.model.DesignMscPlGzEntity;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

/**
 * 数据库表CRAFT_FILE_LV2所对应的Java类（生成工具：代码生成工具4.0   访问地址：http://172.16.2.128/tool/）
 * @author tgcx-test   date 2016-07-04
 */
public class CraftFileLv2DAL extends DBCommon{

 /**
     * 通用查询
     */
    public CoreReturnObject Query(String craftNoLv2, String validflag, String msc, String processCode){
        SqlBuilder b = new SqlBuilder("CraftFileLv2DAL.Query");
        b.AppendLn("AND T.VALIDFLAG IN ('1', {0})");
        b.AppendLn("AND T.CRAFT_NO_LV2 LIKE '%'||{1}||'%'", !craftNoLv2.equals(""));
        b.AppendLn("AND T.MSC = {2}");
        b.AppendLn("AND T.PROCESS_CODE = {3}");
        b.Fill(new String[]{validflag, craftNoLv2, msc, processCode});
        return this.getData(b.toString());
    }
    
    /**
     * 通用查询
     */
    public CoreReturnObject QueryAll(){
        SqlBuilder b = new SqlBuilder("CraftFileLv2DAL.QueryAll");
        return this.getData(b.toString());
    }
    
    public CoreReturnObject FilterCraftFileLv2(String designKey, String processCode, String craftNoLv2, 
    		String validflag, String craftNo, String msc)
    {
    	String sql = CoreXmlSqlParsersFactory.getSql("CraftFileLv2DAL.FilterCraftFileLv2");
    	String sqlGetGrade = CoreXmlSqlParsersFactory.getSql("CraftFileLv2DAL.QueryCraftGz");
    	sqlGetGrade = StringTool.ReplaceAll(sqlGetGrade, "#DESGIN_KEY#", designKey);
    	Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    	ArrayList<DesignMscPlGzEntity> grades = EntityTool.getEntitysBySql(sqlGetGrade, con, DesignMscPlGzEntity.class);
    	String sqlGrade = "";
    	for(DesignMscPlGzEntity grade : grades){
    		sqlGrade += " AND (INSTR(B.GRADECODE, '"+ grade.getGradecode() +"') > 0 OR B.GRADECODE IS NULL)";
    	}
    	sql = sql.replaceAll("#GRADECODE#", sqlGrade);
    	sql = StringTool.ReplaceAll(sql, "#DESGIN_KEY#", designKey);
    	sql = StringTool.ReplaceAll(sql, "#PROCESS_CODE#", processCode);
    	sql = StringTool.ReplaceAll(sql, "#MSC#", msc);
    	sql = StringTool.ReplaceAll(sql, "#CRAFT_NO_LV2#", craftNoLv2);
    	sql = StringTool.ReplaceAll(sql, "#CRAFT_NO#", craftNo);
    	sql = StringTool.ReplaceAll(sql, "#VALIDFALG#", validflag);
    	return this.getData(sql);
    }

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(String pk){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("CraftFileLv2DAL.QueryByPk");
        return this.getData(sqlQuery);
    }

    /**
     * 新增
     */
    public CoreReturnObject Insert(ArrayList<String> jsons){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            GenericEntity maxPk = null;
            for(String json : jsons){
                CraftFileLv2Entity craftFileLv2Entity = EntityTool.getEntityByGson(json, CraftFileLv2Entity.class);
                String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("CraftFileLv2DAL.GetMaxPk");
                sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, craftFileLv2Entity);
                maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                //craftFileLv2Entity.setPk(maxPk.getMaxValue());
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("CraftFileLv2DAL.Insert");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, craftFileLv2Entity);
                if(!transction.ExcuteTransction(sqlInsert)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }

    /**
     * 修改
     */
    public CoreReturnObject Update(ArrayList<String> jsons){
    TransctionTool transction = new TransctionTool();
    try{
            Connection con = transction.getCon();
            for(String json : jsons){
                CraftFileLv2Entity craftFileLv2Entity = EntityTool.getEntityByGson(json, CraftFileLv2Entity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("CraftFileLv2DAL.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, craftFileLv2Entity);
                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 Save(ArrayList<String> jsons, ArrayList<String> jsons2){
    TransctionTool transction = new TransctionTool();
    try{
            Connection con = transction.getCon();
            GenericEntity maxPk = null;
            String fileLv2Pk = "";
            for(String json : jsons){
                CraftFileLv2Entity craftFileLv2Entity = EntityTool.getEntityByGson(json, CraftFileLv2Entity.class);
                String sqlSave = "";
                if(craftFileLv2Entity.getValidflag().equals("")){
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftFileLv2DAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("CraftFileLv2DAL.GetMaxPk");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, craftFileLv2Entity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    craftFileLv2Entity.setPk(maxPk.getMaxValue());
                }else{
                    sqlSave = CoreXmlSqlParsersFactory.getSql("CraftFileLv2DAL.Update");
                }
                fileLv2Pk = craftFileLv2Entity.getPk();
                sqlSave = EntityTool.fillSqlByEntity(sqlSave, craftFileLv2Entity);
                if(!transction.ExcuteTransction(sqlSave)){
                    return transction.rollBack();
                }
            }
            
            String fileLv2CPk = "";
            for(String json2 : jsons2){
                CraftFileLv2CEntity craftFileLv2CEntity = EntityTool.getEntityByGson(json2, CraftFileLv2CEntity.class);
                String sqlSave2 = "";
                if(craftFileLv2CEntity.getValidflag().equals("")){
                	sqlSave2 = CoreXmlSqlParsersFactory.getSql("CraftFileLv2CDAL.Insert");
                    String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("CraftFileLv2CDAL.GetMaxPk");
                    sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, craftFileLv2CEntity);
                    maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                    craftFileLv2CEntity.setPk(maxPk.getMaxValue());
                    if(!fileLv2Pk.equals("")){
                    	craftFileLv2CEntity.setFileLv2Pk(fileLv2Pk);
                    }
                    
                }else{
                	sqlSave2 = CoreXmlSqlParsersFactory.getSql("CraftFileLv2CDAL.Update");
                }
                fileLv2CPk = craftFileLv2CEntity.getPk();
                sqlSave2 = EntityTool.fillSqlByEntity(sqlSave2, craftFileLv2CEntity);
                if(!transction.ExcuteTransction(sqlSave2)){
                    return transction.rollBack();
                }
            }
            
            String[] results = new String[]{fileLv2Pk, fileLv2CPk};
            transction.getReturnObject().setResult(results);
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }

    /**
     * 修改有效标志
     */
    public CoreReturnObject UpdateValidflag(ArrayList<String> jsons, ArrayList<String> jsons2, String validflag){
        TransctionTool transction = new TransctionTool();
    try{
            Connection con = transction.getCon();
            for(String json : jsons){
                CraftFileLv2Entity craftFileLv2Entity = EntityTool.getEntityByGson(json, CraftFileLv2Entity.class);
                String sqlUpdateValidflag = "";
                String sqlUpdateValidflag2 = "";
                if(validflag.equals("0")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("CraftFileLv2DAL.Delete");
                    sqlUpdateValidflag2 = CoreXmlSqlParsersFactory.getSql("CraftFileLv2CDAL.DeleteByFileLv2Pk");
                    sqlUpdateValidflag2 = EntityTool.fillSqlByEntity(sqlUpdateValidflag2, craftFileLv2Entity);
                    if(!transction.ExcuteTransction(sqlUpdateValidflag2)){
                        return transction.rollBack();
                    }
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("CraftFileLv2DAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, craftFileLv2Entity);
                
                if(!transction.ExcuteTransction(sqlUpdateValidflag)){
                    return transction.rollBack();
                }
            }
            
            for(String json2 : jsons2){
                CraftFileLv2CEntity craftFileLv2CEntity = EntityTool.getEntityByGson(json2, CraftFileLv2CEntity.class);
                String sqlUpdateValidflag = "";
                if(validflag.equals("0")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("CraftFileLv2CDAL.Delete");
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("CraftFileLv2CDAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, craftFileLv2CEntity);
                if(!transction.ExcuteTransction(sqlUpdateValidflag)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
    
    public CoreReturnObject IsExistCraftLv2(String cratNoLv2, String pk){
    	SqlBuilder b = new SqlBuilder("CraftFileLv2DAL.IsExistCraftLv2");
    	b.Fill("CRAFT_NO_LV2", cratNoLv2);
    	b.Fill("PK", pk);
    	return this.getData(b.toString());
    }
}
