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

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.Util.StringUtils;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.sale.order.model.SlmOrderLineLockEntity;

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

/**
 * 合同行锁定数据类
 * @author Administrator
 *
 */
public class SlmOrderLineLockDAL extends DBCommon{
	
	public CoreReturnObject query(String orderNo,String startTime,String endTime){
		String sql = CoreXmlSqlParsersFactory.getSql("SlmOrderLineLockDAL.query");
		if (!StringUtils.isEmpty(orderNo)) {
			sql += " AND T.ORDER_NO_SEQ LIKE '%" + orderNo + "%'";
		}
		if(!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)){
			sql += " AND T.LOCK_DATE BETWEEN to_date('" + startTime + " 00:00:00','yyyy-MM-dd HH24:MI:SS') AND to_date('" + endTime + " 23:59:59','yyyy-MM-dd HH24:MI:SS')";
		}
		return this.getData(sql);
	}

	/**
	 * 获取合同行的锁定状态
	 * @param ordLnPk
	 * @return
	 */
	public CoreReturnObject getOrderLineLock(String ordLnPk) {
		Object[] obj = new Object[] { ordLnPk };
		return this.getDataBySqlID("SlmOrderLineLockDAL.GetOrderLineLock", obj);
	}

	/**
	 * 新增
	 */
	public CoreReturnObject Insert(ArrayList<String> jsons){
		TransctionTool transction = new TransctionTool();
		try{
			Connection con = transction.getCon();
			for(String json : jsons){
				SlmOrderLineLockEntity orderLineLockEntity = EntityTool.getEntityByGson(json, SlmOrderLineLockEntity.class);
				String sqlInsert = CoreXmlSqlParsersFactory.getSql("SlmOrderLineLockDAL.insert");
				sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, orderLineLockEntity);
				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 Lock(ArrayList<String> jsons){
		TransctionTool transction = new TransctionTool();
		try{
			Connection con = transction.getCon();
			for(String json : jsons){
				SlmOrderLineLockEntity orderLineLockEntity = EntityTool.getEntityByGson(json, SlmOrderLineLockEntity.class);
				String sqlUpdate = CoreXmlSqlParsersFactory.getSql("SlmOrderLineLockDAL.lock");
				sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, orderLineLockEntity);
				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 UnLock(ArrayList<String> jsons){
		TransctionTool transction = new TransctionTool();
		try{
			Connection con = transction.getCon();
			for(String json : jsons){
				SlmOrderLineLockEntity orderLineLockEntity = EntityTool.getEntityByGson(json, SlmOrderLineLockEntity.class);
				String sqlUpdate = CoreXmlSqlParsersFactory.getSql("SlmOrderLineLockDAL.unLock");
				sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, orderLineLockEntity);
				if(!transction.ExcuteTransction(sqlUpdate)){
					return transction.rollBack();
				}
			}
		}catch(Exception ex){
			CoreReturnObject cro = transction.rollBack();
			cro.setV_errMsg(ex.getMessage());
			return cro;
		}
		return transction.commit();
	}
}
