package com.steering.pss.qcm.DAL;

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

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

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.qcm.model.ComBaseCcmEntity;
import com.steering.pss.qcm.model.GenericEntity;

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

 /**
     * 通用查询
     */
    public CoreReturnObject Query(String plineName, String flag){
        SqlBuilder b = new SqlBuilder("ComBaseCcmDAL.Query");
        b.AppendLn("AND T.PLINE_NAME LIKE '%' || {0} || '%'", !plineName.equals(""));
        b.AppendLn("AND T.VALIDFLAG IN ('1', {1})", !flag.equals(""));
        b.Fill(new String[]{ plineName, flag });
        return this.getData(b.toString());
    }

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(String ccmCode){
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("ComBaseCcmDAL.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){
                ComBaseCcmEntity comBaseCcmEntity = EntityTool.getEntityByGson(json, ComBaseCcmEntity.class);
                String sqlMaxPk = CoreXmlSqlParsersFactory.getSql("ComBaseCcmDAL.GetMaxPk");
                sqlMaxPk = EntityTool.fillSqlByEntity(sqlMaxPk, comBaseCcmEntity);
                maxPk = EntityTool.getEntityBySqlNoClsCon(sqlMaxPk, con, GenericEntity.class);
                comBaseCcmEntity.setCcmCode(maxPk.getMaxValue());
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("ComBaseCcmDAL.Insert");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, comBaseCcmEntity);
                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){
                ComBaseCcmEntity comBaseCcmEntity = EntityTool.getEntityByGson(json, ComBaseCcmEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("ComBaseCcmDAL.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, comBaseCcmEntity);
                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 UpdateValidflag(ArrayList<String> jsons, String validflag){
        TransctionTool transction = new TransctionTool();
    try{
            Connection con = transction.getCon();
            for(String json : jsons){
                ComBaseCcmEntity comBaseCcmEntity = EntityTool.getEntityByGson(json, ComBaseCcmEntity.class);
                String sqlUpdateValidflag = "";
                if(validflag.equals("0")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseCcmDAL.Delete");
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseCcmDAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, comBaseCcmEntity);
                if(!transction.ExcuteTransction(sqlUpdateValidflag)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }
    
    /**
     * 获取产线基础数据
     * @return
     */
    public CoreReturnObject GetPline(){
    	SqlBuilder b = new SqlBuilder("ComBaseCcmDAL.GetPline");
    	return this.getData(b.toString());
    }
    
    /**
     * 是否存在重复数据
     * @param plineCode
     * @param dimater
     * @param waterNo
     * @return
     */
    public CoreReturnObject Exists(String plineCode, String dimater, String waterNo,
    		String gradeCode, String cic, String ccmCode){
    	SqlBuilder b = new SqlBuilder("ComBaseCcmDAL.Exists");
    	b.AppendLn("AND CCM_CODE <> {5}", !ccmCode.equals(""));
    	b.Fill(new String[]{ plineCode, dimater, waterNo, gradeCode, cic, ccmCode });
    	return this.getData(b.toString());
    }
    
    /**
     * 获取内控索引码
     * @return
     */
    public CoreReturnObject getNkCic(){
    	SqlBuilder b= new SqlBuilder("ComBaseCcmDAL.getNkCic");
    	
    	return this.getData(b.toString());
    }
}
