using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CK.SCP.Models;
using CK.SCP.Utils;
namespace CK.SCP.Controller
{
public class SCP_BILLCODE_CONTROLLER
{
public static string MakePLAN_ASKCode()
{
return GetTablePK("E", "TB_ASK", "AskBillNum", "yyMMdd", 4);
}
public static string MakePLAN_EXTEND_ASKCode()
{
return GetTablePK("U", "TB_ASK", "AskBillNum", "yyMMdd", 4);
}
public static string MakePOCode()
{
return GetTablePK("P", "TB_PO", "PoBillNum", "yyMMdd", 4);
}
public static string MakeQualityCode()
{
return GetTablePK("Q", "TB_QUALITY", "QualityNo", "yyMMdd", 4);
}
public static string MakeASNCode()
{
return GetTablePK("N", "TB_ASN", "AsnBillNum", "yyMMdd", 4);
}
public static string MakePalletCode()
{
return GetTablePK("G", "TB_PALLET", "PalletNum", "yyMMdd", 4);
}
///
/// 生成托盘码(青岛)
///
/// 一次性生成多个号使用
///
public static string MakePalletCode_QD(int num)
{
return GetTablePK_QD("G", "TB_PALLETS", "PalletNum", "yyMMdd", 4,num);
}
public static string MakeASKCode_BJBQ(int num)
{
return GetTablePK_BJBQ("K", "TB_ASK", "AskBillNum", "yyMMdd", 4,num);
}
public static string MakeASKCode()
{
return GetTablePK("K", "TB_ASK", "AskBillNum", "yyMMdd", 4);
}
public static string MakeASKCode1()
{
return GetTablePK("K", "TB_ASK", "AskBillNum", "ddMMyy", 4);
}
///
/// 生成收货单码
///
///
public static string MakeReviceCode_ASN()
{
return GetTablePK("C", "TB_RECEIVE", "RecvBillNum", "yyMMdd", 4);
}
public static string MakeReviceCode()
{
return GetTablePK("R", "TB_RECEIVE", "RecvBillNum", "yyMMdd", 4);
}
public static string MakeRejectCode()
{
return GetTablePK("J", "TB_REJECT", "RjctBillNum", "yyMMdd", 4);
}
public static string MakeInvoiceCode()
{
return GetTablePK("F", "TB_INVOICE", "InvcBillNum", "yyMMdd", 4);
}
public static string MakeInvoiceCode(string p_vendId)
{
return GetTablePK(p_vendId,"F" ,"TB_INVOICE", "InvcBillNum", "yyMMdd", 4);
}
public static string MakeCode(string p_Header, string p_TableName,string p_ColumnName)
{
return GetTablePK(p_Header, p_TableName, p_ColumnName, "yyMMdd",4);
}
public static string MakePoNum(int num=1)
{
return GetTablePO("P", "TB_PO", "PoBillNum", 7,num);
}
public static string GetTablePO(string Ext, string TableName, string ColName, int len,int num)
{
var _result = string.Empty;
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
string sql = "select max(right(" + ColName + "," + len.ToString() + ")+"+ num + ") from " + TableName + " where " + ColName + " like '%P[0-9]%' ";
DbRawSqlQuery result = db.Database.SqlQuery(sql);
var obj = result.FirstOrDefault();
if (obj == null)
{
_result = Ext + "1".PadLeft(len, '0');
}
else
{
_result = Ext + result.FirstOrDefault().ToString().PadLeft(len, '0');
}
}
}
catch (Exception e)
{
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_BILLCODE_CONTROLLER), "GetTablePK", e.Message);
}
return _result;
}
public static string GetTablePK_BJBQ(string Ext, string TableName, string ColName, string Format, int len,int num)
{
var _result = string.Empty;
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
string time = Ext + DateTime.Now.ToString(Format);
string sql = "select max(right(" + ColName + "," + len.ToString() + ")+"+ num + ") from " + TableName + " where " + ColName + " like '" + time + "%'";
DbRawSqlQuery result = db.Database.SqlQuery(sql);
var obj = result.FirstOrDefault();
if (obj == null)
{
_result = time + num.ToString().PadLeft(len, '0');
}
else
{
_result = time + result.FirstOrDefault().ToString().PadLeft(len, '0');
}
}
}
catch (Exception e)
{
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_BILLCODE_CONTROLLER), "GetTablePK", e.Message);
}
return _result;
}
public static string GetTablePK(string Ext, string TableName, string ColName, string Format, int len)
{
var _result = string.Empty;
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
string time = Ext + DateTime.Now.ToString(Format);
string sql = "select max(right(" + ColName + "," + len.ToString() + ")+1) from " + TableName + " where " + ColName + " like '" + time + "%'";
DbRawSqlQuery result = db.Database.SqlQuery(sql);
var obj = result.FirstOrDefault();
if (obj==null)
{
_result= time + "1".PadLeft(len, '0');
}
else
{
_result= time + result.FirstOrDefault().ToString().PadLeft(len, '0');
}
}
}
catch(Exception e)
{
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_BILLCODE_CONTROLLER), "GetTablePK", e.Message);
}
return _result;
}
public static string GetTablePK(string p_vendId,string Ext, string TableName, string ColName, string Format, int len)
{
var _result = string.Empty;
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
string time =p_vendId+"_"+ Ext + DateTime.Now.ToString(Format);
string sql = "select max(right(" + ColName + "," + len.ToString() + ")+1) from " + TableName + " where " + ColName + " like '" + time + "%'";
DbRawSqlQuery result = db.Database.SqlQuery(sql);
var obj = result.FirstOrDefault();
if (obj == null)
{
_result = time + "1".PadLeft(len, '0');
}
else
{
_result = time + result.FirstOrDefault().ToString().PadLeft(len, '0');
}
}
}
catch (Exception e)
{
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_BILLCODE_CONTROLLER), "GetTablePK", e.Message);
}
return _result;
}
public static string GetTablePK_QD(string Ext, string TableName, string ColName, string Format, int len,int num)
{
var _result = string.Empty;
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
string time = Ext + DateTime.Now.ToString(Format);
string sql = "select max(right(" + ColName + "," + len.ToString() + ")+1) from " + TableName + " where " + ColName + " like '" + time + "%'";
DbRawSqlQuery result = db.Database.SqlQuery(sql);
var obj = result.FirstOrDefault();
if (obj == null)
{
_result = time + num.ToString().PadLeft(len, '0');
}
else
{
_result = time + (result.FirstOrDefault()+num).ToString().PadLeft(len, '0');
}
}
}
catch (Exception e)
{
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_BILLCODE_CONTROLLER), "GetTablePK", e.Message);
}
return _result;
}
}
}