package com.steering.pss.qcm.DAL;

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.ComBaseWbsEntity;

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

public class ComBaseWbsDAL extends DBCommon{

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

    /**
     * 通过主键查询
     */
    public CoreReturnObject QueryByPk(String wbsCode){
        ComBaseWbsEntity comBaseWbsEntity = new ComBaseWbsEntity();
        comBaseWbsEntity.setWbsCode(wbsCode);
        String sqlQuery = CoreXmlSqlParsersFactory.getSql("ComBaseWbsDAL.QueryByPk");
        sqlQuery = EntityTool.fillSqlByEntity(sqlQuery, comBaseWbsEntity);
        return this.getData(sqlQuery);
    }

    /**
     * 新增
     */
    public CoreReturnObject Insert(ArrayList<String> jsons){
        TransctionTool transction = new TransctionTool();
        try{
            Connection con = transction.getCon();
            for(String json : jsons){
                ComBaseWbsEntity comBaseWbsEntity = EntityTool.getEntityByGson(json, ComBaseWbsEntity.class);
                comBaseWbsEntity.setValidflag("1");
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("ComBaseWbsDAL.Insert");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, comBaseWbsEntity);
                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){
                ComBaseWbsEntity comBaseWbsEntity = EntityTool.getEntityByGson(json, ComBaseWbsEntity.class);
                String sqlUpdate = CoreXmlSqlParsersFactory.getSql("ComBaseWbsDAL.Update");
                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, comBaseWbsEntity);
                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){
                ComBaseWbsEntity comBaseWbsEntity = EntityTool.getEntityByGson(json, ComBaseWbsEntity.class);
                String sqlUpdateValidflag = "";
                if(validflag.equals("0")){
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseWbsDAL.Delete");
                }else{
                    sqlUpdateValidflag = CoreXmlSqlParsersFactory.getSql("ComBaseWbsDAL.Resume");
                }
                sqlUpdateValidflag = EntityTool.fillSqlByEntity(sqlUpdateValidflag, comBaseWbsEntity);
                if(!transction.ExcuteTransction(sqlUpdateValidflag)){
                    return transction.rollBack();
                }
            }
        }catch(Exception ex){
            CoreReturnObject cro = transction.rollBack();
            cro.setV_errMsg(ex.getMessage());
            return cro;
        }
        return transction.commit();
    }

}
