package com.steering.pss.sale.base;

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.sale.base.entity.SlmBaseCustStorageEntity;

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

public class SlmBaseCustStorage extends DBCommon {

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

    /**
     * 查询仓库
     * @return
     */
    public CoreReturnObject QueryStorage(){
        return this.getData(CoreXmlSqlParsersFactory.getSql("SlmBaseCustStorage.QueryStorage"));
    }

    /**
     * 查询客户
     * @return
     */
    public CoreReturnObject QueryCustomer(){
        return this.getData(CoreXmlSqlParsersFactory.getSql("SlmBaseCustStorage.QueryCustomer"));
    }

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

}
