using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mesnac.Codd.Session;
using System.Data;
using Mesnac.Action.Feeding.BasicInfo;
namespace Mesnac.Action.Feeding.FinishBatch
{
///
/// 架子条码
///
public class ShelfBarcode
{
DbHelper localHelper = null;
DbHelper serverHelper = null;
bool isNet = true;
private LotBarcode lotBarcode = null;
private RecipeData.PptPlanInfo planInfo = null;
private PlanExecuteData planExecuteData = null;
private int shelfLotCount = 0;
private string _shelfbarcode = string.Empty;
public string Barcode { get { return _shelfbarcode; } }
public ShelfBarcode(DbHelper _localHelper, DbHelper _serverHelper, bool _isNet, LotBarcode _lotBarcode, RecipeData.PptPlanInfo _planInfo, PlanExecuteData _planExecuteData)
{
this.localHelper = _localHelper;
this.serverHelper = _serverHelper;
this.isNet = _isNet;
this.lotBarcode = _lotBarcode;
this.planInfo = _planInfo;
this.planExecuteData = _planExecuteData;
IniShelfBarcode();
}
///
/// 获取当前计划的架子条码
///
///
private string getCurrentPptShelfBarcode()
{
try
{
DbHelper dbHelper = null;
string sqlstr = string.Empty;
if (isNet)
{
dbHelper = this.serverHelper;
sqlstr = "SELECT MAX(Barcode) AS Barcode FROM dbo.Ppt_ShiftConfig WHERE Equip_Code = @EquipCode and left(Barcode,6) = @PlanDate and Shift_ID = @ShiftID and Mater_code = @MaterialCode ";
}
else
{
dbHelper = this.localHelper;
sqlstr = "SELECT MAX(Barcode) AS Barcode FROM dbo.PptShiftConfig WHERE EquipCode = @EquipCode and left(Barcode,6) = @PlanDate and ShiftID = @ShiftID and MaterialCode = @MaterialCode ";
}
//string sqlstr = "SELECT MAX(Barcode) AS Barcode FROM dbo.PptShiftConfig WHERE Barcode LIKE @PlanID ";
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
//dbHelper.AddParameter("@PlanID", lotBarcode.PlanID.Trim() + "%");
dbHelper.AddParameter("@EquipCode", planInfo.RecipeEquipCode.Trim());
dbHelper.AddParameter("@PlanDate", String.Format("{0:yyMMdd}", Convert.ToDateTime(planInfo.PlanDate)));
dbHelper.AddParameter("@ShiftID", planInfo.ShiftID);
dbHelper.AddParameter("@MaterialCode", planInfo.RecipeMaterialCode.Trim());
object maxCode = dbHelper.ToScalar();
if (maxCode != null && maxCode != DBNull.Value)
{
return maxCode.ToString();
}
return string.Empty;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("存盘业务-获取当前计划的架子条码失败:" + ex.Message);
return String.Empty;
}
}
///
/// 生成架子信息
///
private void InsertPptShiftConfigBarCode()
{
try
{
FeedingAction action = new FeedingAction();
string sqlstr = String.Empty;
//获取架子条码:12计划号+6位SerialBatchId
//this._shelfbarcode = this.lotBarcode.PlanID.Trim() + this.lotBarcode.SerialBatchId.ToString("D6");
//获取架子条码:12计划号+1位工厂号+5位SerialBatchId
this._shelfbarcode = this.lotBarcode.PlanID.Trim() + action.GetConfigValue("FactoryID", "2") + this.lotBarcode.SerialBatchId.ToString("D5");
if (isNet)
{
try
{
sqlstr = @"INSERT INTO Ppt_ShiftConfig(
Barcode, Plan_Date, Equip_Code, Shift_ID, Shift_class, Mater_code, Mater_Name, Barcode_Start,
Barcode_End, Total_Weight, Shelf_Num, Real_Weight, Barcode_Use, Print_Date, Oper_Code,
Mem_Note, Stock_Flag, Check_Flag, Update_Flag, Used_Num, Used_Weigh, Used_Flag, Used_Date,
Used_ShiftID, Used_Class, Used_Equip, Ascend_Num, OrgOrNot, OrgBarcode, UP_ERP,
DiffTime, Plan_ID, Prod_Date, Pi_Weight
)
SELECT @Barcode,t1.Plan_Date,t1.Equip_code,t1.Shift_ID,t2.Shift_Class
,t1.Mater_code,t1.Mater_Name,@BarcodeStart,
@BarcodeEnd,0,0,0,0,'1900-01-01','',
'',0,'N',0,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,0,
NULL,t1.Plan_ID,convert(varchar,getdate(),120),NULL
FROM dbo.Ppt_Plan t1
LEFT JOIN dbo.Ppt_ShifTime t2 ON t1.Plan_Date=t2.Shift_DT AND t1.Shift_ID=t2.Shift_ID AND t2.Dept_code=1
WHERE t1.Plan_ID=@PlanID";
serverHelper.CommandText = sqlstr;
serverHelper.CommandType = CommandType.Text;
serverHelper.ClearParameter();
serverHelper.AddParameter("@Barcode", this._shelfbarcode);
serverHelper.AddParameter("@BarcodeStart", this.lotBarcode.SerialBatchId);
serverHelper.AddParameter("@BarcodeEnd", this.lotBarcode.SerialBatchId);
serverHelper.AddParameter("@PlanID", this.lotBarcode.PlanID.Trim());
serverHelper.ExecuteNonQuery();
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("网络库生成架子信息失败:" + ex.Message);
}
}
sqlstr = @"INSERT INTO PptShiftConfig(
Barcode, PlanDate, EquipCode, ShiftID, ClassID, MaterialCode, MaterialName, BarcodeStart,
BarcodeEnd, TotalWeight, ShelfNum, RealWeight, BarcodeUse, PrintDate, OperCode, ReceiveDate,
MemNote, StockFlag, CheckFlag, UpdateFlag, UsedNum, UsedWeigh, UsedFlag, UsedDate,
UsedShiftID, UsedClassID, UsedEquip, AscendNum, OrgOrNot, OrgBarcode, UPERP, DealMode,
DiffTime, MaterCode2, PlanID, ProdDate, PiWeight,IsUpFlag
)
SELECT @Barcode,t1.down_date,t1.equip_code,t1.shift,t1.shift_class,t1.mater_code,t1.recipe_code,@BarcodeStart,
@BarcodeEnd,0,0,0,0,'1900-01-01','',NULL,
'',0,'N',0,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,0,NULL,
NULL,NULL,t1.plan_id,convert(varchar,getdate(),120),NULL,@IsUpFlag
FROM ppt_plan t1
WHERE t1.plan_id=@PlanID";
localHelper.CommandText = sqlstr;
localHelper.CommandType = CommandType.Text;
localHelper.ClearParameter();
localHelper.AddParameter("@Barcode", this._shelfbarcode);
localHelper.AddParameter("@BarcodeStart", this.lotBarcode.SerialBatchId);
localHelper.AddParameter("@BarcodeEnd", this.lotBarcode.SerialBatchId);
#region 设置上传更新标志
if (isNet)
{
localHelper.AddParameter("@IsUpFlag", 1);
}
else
{
localHelper.AddParameter("@IsUpFlag", 0);
}
#endregion
localHelper.AddParameter("@PlanID", this.lotBarcode.PlanID.Trim());
localHelper.ExecuteNonQuery();
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("存盘业务-生成架子信息失败:" + ex.Message);
}
}
///
/// 初始化架子条码
///
private void IniShelfBarcode()
{
try
{
this._shelfbarcode = getCurrentPptShelfBarcode();
#region 不存在需要的架子号
if (string.IsNullOrWhiteSpace(this._shelfbarcode))
{
InsertPptShiftConfigBarCode();
return;
}
#endregion
#region 本计划与上一个计划的物料是否相同
if (this.planExecuteData.MixingFinishedCount == 1)
{
string strSql = "select top 1 mater_code from ppt_plan where ActionOrder < (select ActionOrder from ppt_plan where plan_id = @PlanID) and CONVERT(varchar,down_date,112) = @PlanDate and equip_code= @RecipeEquipCode and shift = @ShiftID order by ActionOrder desc";
this.localHelper.CommandType = CommandType.Text;
this.localHelper.CommandText = strSql;
this.localHelper.AddParameter("@PlanDate", String.Format("{0:yyyyMMdd}", Convert.ToDateTime(this.planInfo.PlanDate)));
this.localHelper.AddParameter("@RecipeEquipCode", this.planInfo.RecipeEquipCode.Trim());
this.localHelper.AddParameter("@ShiftID", this.planInfo.ShiftID);
this.localHelper.AddParameter("@PlanID", this.planInfo.PlanID.Trim());
object result = this.localHelper.ToScalar();
if (result != null && result != System.DBNull.Value)
{
if (!result.ToString().Equals(this.planInfo.RecipeMaterialCode.Trim()))
{
InsertPptShiftConfigBarCode();
return;
}
}
}
//if (!PlanCommon.IsSameMaterCode(this.lotBarcode.PlanID, planInfo.RecipeEquipCode, Convert.ToDateTime(planInfo.PlanDate), Convert.ToInt32(planInfo.ShiftID)))
//{
// InsertPptShiftConfigBarCode();
// return;
//}
#endregion
#region 判断架子号可用
string sqlstr = @"SELECT t1.shelf_num FROM pmt_recipe t1
INNER JOIN ppt_plan t2
ON t1.mater_code=t2.mater_code
AND t1.edt_code=t2.edt_code
AND t1.equip_code=substring(t2.equip_code,4,2)
WHERE t2.plan_id =@PlanID";
localHelper.CommandType = CommandType.Text;
localHelper.CommandText = sqlstr;
localHelper.ClearParameter();
localHelper.AddParameter("@PlanID", this.lotBarcode.PlanID);
object obj = localHelper.ToScalar();
int iShelfLotCount = 0;
if (obj != null && obj != System.DBNull.Value)
{
int.TryParse(obj.ToString(), out iShelfLotCount);
}
DbHelper dbHelper = null;
//if (isNet)
//{
// dbHelper = this.serverHelper;
// sqlstr = "SELECT Shelf_Num FROM dbo.Ppt_ShiftConfig WHERE Barcode=@Barcode";
//}
//else
{
dbHelper = this.localHelper;
sqlstr = "SELECT ShelfNum FROM dbo.PptShiftConfig WHERE Barcode=@Barcode";
}
dbHelper.CommandText = sqlstr;
dbHelper.CommandType = CommandType.Text;
dbHelper.ClearParameter();
dbHelper.AddParameter("@Barcode", this._shelfbarcode);
obj = dbHelper.ToScalar();
int iShelfNum = 0;
if (obj != null && obj != System.DBNull.Value)
{
int.TryParse(obj.ToString(), out iShelfNum);
}
if (iShelfNum >= iShelfLotCount)
{
InsertPptShiftConfigBarCode();
}
#endregion
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("存盘业务-初始化架子条码失败:" + ex.Message);
}
}
}
}