using CK.SCP.Models ;
using CK.SCP.Models.AppBoxEntity ;
using CK.SCP.Models.Enums ;
using CK.SCP.Models.ScpEntity ;
using CK.SCP.Utils ;
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.Entity.Core ;
using System.Data.Entity.Migrations ;
using System.Data.SqlClient ;
using System.Linq ;
using System.Text ;
using System.Text.RegularExpressions ;
using System.Threading.Tasks ;
namespace CK.SCP.Controller
{
public class SCP_PLAN_CONTROLLER
{
public static void Get_V_TB_PO_PLAN_EXTEND_List ( V_TB_PO_PLAN_EXTEND p_entity , Action < ResultObject < IQueryable < V_TB_PO_PLAN_EXTEND > > > p_action )
{
ResultObject < IQueryable < V_TB_PO_PLAN_EXTEND > > _ ret = new ResultObject < IQueryable < V_TB_PO_PLAN_EXTEND > > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
IQueryable < V_TB_PO_PLAN_EXTEND > q = db . V_TB_PO_PLAN_EXTEND ;
if ( ! string . IsNullOrEmpty ( p_entity . Domain ) )
{
q = q . Where ( p = > p . Domain . Contains ( p_entity . Domain ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . PartCode ) )
{
q = q . Where ( p = > p . PartCode . Contains ( p_entity . PartCode ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . PoBillNum ) )
{
q = q . Where ( p = > p . PoBillNum . Contains ( p_entity . PoBillNum ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . Site ) )
{
q = q . Where ( p = > p . Site . Contains ( p_entity . Site ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . AskBillNum ) )
{
q = q . Where ( p = > p . AskBillNum . Contains ( p_entity . AskBillNum ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . Version ) )
{
q = q . Where ( p = > p . Version . Contains ( p_entity . Version ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . PartDesc ) )
{
q = q . Where ( p = > p . PartDesc . Contains ( p_entity . PartDesc ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . VendId ) )
{
q = q . Where ( p = > p . VendId . Contains ( p_entity . VendId ) ) ;
}
if ( p_entity . EndTime ! = null )
{
q = q . Where ( p = > p . EndTime = = p_entity . EndTime . Value ) ;
}
if ( p_entity . UserInAddress ! = null & & p_entity . UserInAddress . Count > 0 )
{
q = q . Where ( p = > p_entity . UserInAddress . Contains ( p . Domain ) ) ;
}
if ( p_entity . UserInVendIds ! = null & & p_entity . UserInVendIds . Count > 0 )
{
q = q . Where ( p = > p_entity . UserInVendIds . Contains ( p . VendId ) ) ;
}
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = q ;
p_action ( _ ret ) ;
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
_ ret . ErrorList . Add ( e ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( V_TB_PO_PLAN ) , "Get_V_TB_PO_PLAN_EXTEND_List" , e . Message ) ;
throw e ;
}
}
public static void Get_V_TB_PO_PLAN_List ( V_TB_PO_PLAN p_entity , Action < ResultObject < IQueryable < V_TB_PO_PLAN > > > p_action )
{
ResultObject < IQueryable < V_TB_PO_PLAN > > _ ret = new ResultObject < IQueryable < V_TB_PO_PLAN > > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
IQueryable < V_TB_PO_PLAN > q = db . V_TB_PO_PLAN ;
if ( ! string . IsNullOrEmpty ( p_entity . Domain ) )
{
q = q . Where ( p = > p . Domain . Contains ( p_entity . Domain ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . PartCode ) )
{
q = q . Where ( p = > p . PartCode . Contains ( p_entity . PartCode ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . PoBillNum ) )
{
q = q . Where ( p = > p . PoBillNum . Contains ( p_entity . PoBillNum ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . Site ) )
{
q = q . Where ( p = > p . Site . Contains ( p_entity . Site ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . AskBillNum ) )
{
q = q . Where ( p = > p . AskBillNum . Contains ( p_entity . AskBillNum ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . Version ) )
{
q = q . Where ( p = > p . Version . Contains ( p_entity . Version ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . PartDesc ) )
{
q = q . Where ( p = > p . PartDesc . Contains ( p_entity . PartDesc ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . VendId ) )
{
q = q . Where ( p = > p . VendId . Contains ( p_entity . VendId ) ) ;
}
if ( p_entity . EndTime ! = null )
{
q = q . Where ( p = > p . EndTime = = p_entity . EndTime . Value ) ;
}
if ( p_entity . UserInAddress ! = null & & p_entity . UserInAddress . Count > 0 )
{
q = q . Where ( p = > p_entity . UserInAddress . Contains ( p . Domain ) ) ;
}
if ( p_entity . UserInVendIds ! = null & & p_entity . UserInVendIds . Count > 0 )
{
q = q . Where ( p = > p_entity . UserInVendIds . Contains ( p . VendId ) ) ;
}
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = q ;
p_action ( _ ret ) ;
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
_ ret . ErrorList . Add ( e ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( V_TB_PO_PLAN ) , "Get_V_TB_PO_PLAN_List" , e . Message ) ;
throw e ;
}
}
public static DataTable Get_TB_PLAN_REPORT ( V_TB_PO_PLAN p_plan )
{
DataTable dt = new DataTable ( ) ;
DataTable dt1 = new DataTable ( ) ;
DataTable dt2 = new DataTable ( ) ;
DataTable dt3 = new DataTable ( ) ;
string _ sqlString = "SELECT * FROM ( SELECT partcode,endtime,planqty,partdesc,pounit,domain,pobillnum,poline FROM V_TB_PO_PLAN where {0}\n " +
") A\n" +
" pivot ( sum(planQty) for a.endtime in (\n" +
" {1}\n" +
")\n" +
") as ourpivot " ;
string _ sql = "SELECT DISTINCT(ENDTIME) FROM V_TB_PO_PLAN where {0} ORDER BY ENDTIME" ;
string _ askBillSql = "SELECT * FROM V_TB_ASK_DETAIL where VENDID = '{0}' AND EndTime IN ({1}) AND AskBillNum like 'E%' " ;
string _ asn_recive = "SELECT A.PartCode, A.PoBillNum, A.PoLine, SUM(A.Qty) AS AsnQty, SUM(ISNULL(B.Qty, 0)) AS ReciveQty,\n" +
" SUM(ISNULL(C.AskQty, 0)) AS AskQty\n" +
"FROM V_TB_ASN_DETAIL AS A LEFT OUTER JOIN\n" +
" V_TB_RECEIVE_DETAIL AS B ON A.AsnBillNum = B.AsnBillNum AND A.PartCode = B.PartCode AND\n" +
" A.PoBillNum = B.PoBillNum AND A.PoLine = B.PoLine INNER JOIN\n" +
" TB_ASK_DETAIL AS C ON A.AskBillNum = C.AskBillNum AND A.PartCode = C.PartCode AND\n" +
" A.PoBillNum = C.PoBillNum AND A.PoLine = C.PoLine\n" +
"WHERE (A.AskBillNum LIKE 'E%') AND (A.State > 0) and {0}\n" +
"GROUP BY A.PartCode, A.PoBillNum, A.PoLine" ;
var dbSetting = GlobalConfig . ScpDatabase ;
try
{
var strConn = EntitiesFactory . GetEfConnectionString ( dbSetting ) ;
SqlConnection conn = new System . Data . SqlClient . SqlConnection ( ) ;
conn . ConnectionString = strConn ;
if ( conn . State ! = ConnectionState . Open )
{
conn . Open ( ) ;
}
#region 结束时间范围
SqlCommand cmd = new SqlCommand ( ) ;
cmd . Connection = conn ;
string where1 = " 1=1 " ;
if ( ! string . IsNullOrEmpty ( p_plan . PartCode ) )
{
where1 + = string . Format ( " and partcode='{0}' " , p_plan . PartCode ) ;
}
if ( ! string . IsNullOrEmpty ( p_plan . VendId ) )
{
where1 + = string . Format ( " and VendId='{0}' " , p_plan . VendId ) ;
}
if ( ! string . IsNullOrEmpty ( p_plan . PoBillNum ) )
{
where1 + = string . Format ( " and PoBillNum='{0}' " , p_plan . PoBillNum ) ;
}
if ( p_plan . EndTime_Begin ! = null )
{
where1 + = string . Format ( " and EndTime>='{0}'" , p_plan . EndTime_Begin . Value . ToString ( "yyyy-MM-dd" ) ) ;
}
else
{
where1 + = string . Format ( " and EndTime>='{0}'" , DateTime . Now . ToString ( "yyyy-MM-dd" ) ) ;
}
if ( p_plan . EndTime_End ! = null )
{
where1 + = string . Format ( " and EndTime<='{0}'" , p_plan . EndTime_End . Value . ToString ( "yyyy-MM-dd" ) ) ;
}
else
{
where1 + = string . Format ( " and EndTime<='{0}'" , DateTime . Now . AddMonths ( 1 ) . ToString ( "yyyy-MM-dd" ) ) ;
}
cmd . CommandText = string . Format ( _ sql , where1 ) ;
SqlDataAdapter adapter = new SqlDataAdapter ( cmd ) ;
adapter . Fill ( dt ) ;
#endregion
List < string > _l s = new List < string > ( ) ;
List < string > _l s1 = new List < string > ( ) ;
foreach ( DataRow row in dt . Rows )
{
_l s . Add ( string . Format ( "[{0}]" , row [ "EndTime" ] . ToString ( ) ) ) ;
_l s1 . Add ( string . Format ( "'{0}'" , row [ "EndTime" ] . ToString ( ) ) ) ;
}
if ( _l s . Count > 0 )
{
#region 计划行转列
SqlCommand cmd1 = new SqlCommand ( ) ;
cmd1 . Connection = conn ;
string where = "1=1 " ;
if ( ! string . IsNullOrEmpty ( p_plan . PartCode ) )
{
where + = string . Format ( " and partcode='{0}' " , p_plan . PartCode ) ;
}
where + = string . Format ( " and VendId='{0}' " , p_plan . VendId ) ;
if ( ! string . IsNullOrEmpty ( p_plan . PoBillNum ) )
{
where + = string . Format ( " and PoBillNum='{0}' " , p_plan . PoBillNum ) ;
}
cmd1 . CommandText = string . Format ( _ sqlString , where , string . Join ( "," , _l s . ToArray ( ) ) ) ;
var adapter1 = new SqlDataAdapter ( cmd1 ) ;
adapter1 . Fill ( dt1 ) ;
#endregion
#region 此范围内的看板号
SqlCommand cmd2 = new SqlCommand ( ) ;
cmd2 . Connection = conn ;
cmd2 . CommandText = string . Format ( _ askBillSql , p_plan . VendId , string . Join ( "," , _l s1 . ToArray ( ) ) ) ;
SqlDataAdapter adapter2 = new SqlDataAdapter ( cmd2 ) ;
adapter2 . Fill ( dt2 ) ;
#endregion
List < string > _ askList = new List < string > ( ) ;
foreach ( DataRow row in dt2 . Rows )
{
_ askList . Add ( string . Format ( "'{0}'" , row [ "AskBillNum" ] . ToString ( ) ) ) ;
}
if ( _ askList . Count > 0 )
{
SqlCommand cmd3 = new SqlCommand ( ) ;
cmd3 . Connection = conn ;
cmd3 . CommandText = string . Format ( _ asn_recive , string . Format ( " A.askBillNum in ({0}) " , string . Join ( "," , _ askList . ToArray ( ) ) ) ) ;
SqlDataAdapter adapter3 = new SqlDataAdapter ( cmd3 ) ;
adapter3 . Fill ( dt3 ) ;
}
var column0 = dt1 . Columns . Add ( "要货数量" ) ;
var column1 = dt1 . Columns . Add ( "收货数量" ) ;
var column2 = dt1 . Columns . Add ( "发货数量" ) ;
var column3 = dt1 . Columns . Add ( "未发数量" ) ;
var column4 = dt1 . Columns . Add ( "未收数量" ) ;
foreach ( DataRow dr in dt1 . Rows )
{
foreach ( DataRow row in dt3 . Rows )
{
if (
dr [ "PartCode" ] . ToString ( ) = = row [ "PartCode" ] . ToString ( )
& & dr [ "PoBillNum" ] . ToString ( ) = = row [ "PoBillNum" ] . ToString ( )
& & dr [ "PoLine" ] . ToString ( ) = = row [ "PoLine" ] . ToString ( ) )
{
dr [ "要货数量" ] = row [ "ReciveQty" ] . ToString ( ) ;
dr [ "收货数量" ] = row [ "ReciveQty" ] . ToString ( ) ;
dr [ "发货数量" ] = row [ "AsnQty" ] . ToString ( ) ;
dr [ "未收数量" ] = decimal . Parse ( row [ "AsnQty" ] . ToString ( ) ) - decimal . Parse ( row [ "ReciveQty" ] . ToString ( ) ) ;
dr [ "未发数量" ] = decimal . Parse ( row [ "AskQty" ] . ToString ( ) ) - decimal . Parse ( row [ "AsnQty" ] . ToString ( ) ) ;
}
}
}
foreach ( DataColumn column in dt1 . Columns )
{
try
{
var _d t1 = DateTime . Parse ( column . ColumnName ) ;
var _ curDate = _d t1 . ToString ( "yyyy-MM-dd" ) ;
column . ColumnName = _ curDate + "(预测)" ; //要货预测头
foreach ( DataRow row in dt2 . Rows )
{
var _ cur = DateTime . Parse ( row [ "EndTime" ] . ToString ( ) ) . ToString ( "yyyy-MM-dd" ) ; //看板时间
StringBuilder _ buf = new StringBuilder ( ) ;
if ( _ curDate = = _ cur )
{
var _ ask = row [ "AskBillNum" ] . ToString ( ) ;
column . ColumnName = _ cur + "(" + _ ask + ")" ;
switch ( int . Parse ( row [ "state" ] . ToString ( ) ) )
{
case ( int ) AskState . New :
column . ColumnName = _ cur + "(" + _ ask + ")" + "(计划)" ;
break ;
case ( int ) AskState . Ship :
column . ColumnName = _ cur + "(" + _ ask + ")" + "(已发货)" ;
break ;
case ( int ) AskState . Confirm :
column . ColumnName = _ cur + "(" + _ ask + ")" + "(已确认)" ;
break ;
case ( int ) AskState . Release :
column . ColumnName = _ cur + "(" + _ ask + ")" + "(已发布)" ;
break ;
case ( int ) AskState . Receive :
column . ColumnName = _ cur + "(" + _ ask + ")" + "(已收货)" ;
break ;
}
}
}
}
catch
{
continue ;
}
}
dt1 . Columns [ "PartCode" ] . ColumnName = "零件编码" ;
dt1 . Columns [ "PartDesc" ] . ColumnName = "零件描述" ;
dt1 . Columns [ "PoUnit" ] . ColumnName = "单位" ;
dt1 . Columns [ "PoBillNum" ] . ColumnName = "订单编号" ;
dt1 . Columns [ "PoLine" ] . ColumnName = "行号" ;
dt1 . Columns [ "Domain" ] . ColumnName = "域" ;
}
conn . Close ( ) ;
}
catch ( SqlException ex )
{
throw new Exception ( $"系统无法连接到数据库:{dbSetting},请检查配置的服务器,数据库,用户名和密码等信息是否正确。{Environment.NewLine}{ex}" ) ;
}
return dt1 ;
}
public static DataTable Get_TB_PLAN_REPORT_EXTEND ( V_TB_PO_PLAN_EXTEND p_plan , bool IsOutput )
{
DataTable dt = new DataTable ( ) ;
DataTable dt1 = new DataTable ( ) ;
DataTable dt2 = new DataTable ( ) ;
DataTable dt3 = new DataTable ( ) ;
string _ sqlString = "SELECT a.partcode,a.PartDesc1+a.PartDesc2 as PartDesc,a.pounit,a.Site as domain,a.pobillnum,a.poline,a.Vendid , {0} FROM V_TB_PO_DETAIL A LEFT JOIN\n" +
"(\n" +
"SELECT * FROM ( SELECT partcode,endtime,planqty,partdesc,pounit,domain,pobillnum,poline,Vendid FROM V_TB_PO_PLAN_EXTEND where {1}\n" +
" ) A\n" +
" pivot ( sum(planQty) for a.endtime in ({0})\n" +
") as ourpivot) B ON A.PoBillNum=B.PoBillNum AND A.PoLine=B.PoLine AND A.PartCode=B.PartCode and a.Site=b.Domain WHERE {2} ORDER BY A.PoLine" ;
/*计划时间长度*/
string _ sql = "SELECT convert(varchar(10),EndTime ,23) EndTime FROM (\n" +
"SELECT DISTINCT(EndTime) EndTime FROM V_TB_PO_PLAN_EXTEND where {0}) A ORDER BY ENDTIME" ;
//string _sql = "SELECT convert(varchar(10),DISTINCT(ENDTIME) ,23) FROM V_TB_PO_PLAN_EXTEND where {0} ORDER BY ENDTIME";
/*导出预测导入模板*/
if ( IsOutput = = true )
{
_ sql = "select convert(varchar(10),DATEADD(day,number,GETDATE()) ,23) endtime\n" +
"from master.dbo.spt_values\n" +
"where type='p'\n" +
"AND number<=DATEDIFF(day,GETDATE(),DATEADD(DAY,-1,DATEADD(MM,DATEDIFF(MM,0,GETDATE())+6,0)))" ;
}
/*查询结束的看遍内容*/
string _ askBillSql = "SELECT * FROM V_TB_ASK_DETAIL where PoBillNum='{0}' and EndTime IN ({1}) AND AskBillNum like 'U%' and State>-1 " ;
/*要货收发货数量统计*/
//string _asn_recive = "SELECT C.PartCode, C.PoBillNum, C.PoLine, SUM(ISNULL(A.Qty, 0)) AS AsnQty, SUM(ISNULL(B.qty, 0)) AS ReciveQty,\n" +
//" SUM(ISNULL(C.AskQty, 0)) AS AskQty\n" +
//"FROM TB_ASK_DETAIL AS C LEFT OUTER JOIN\n" +
//" V_TB_ASN_DETAIL AS A ON A.AskBillNum = C.AskBillNum AND A.PartCode = C.PartCode AND\n" +
//" A.PoBillNum = C.PoBillNum AND A.PoLine = C.PoLine AND A.STATE>-1 LEFT OUTER JOIN\n" +
//" V_TB_RECEIVE_DETAIL AS B ON A.AsnBillNum = B.AsnBillNum AND A.PartCode = B.PartCode AND\n" +
//" A.PoBillNum = B.PoBillNum AND A.PoLine = B.PoLine\n" +
//"WHERE (C.AskBillNum LIKE 'U%') AND (C.State >= 0) AND (C.AskBillNum IN ({0}))\n" +
//"GROUP BY C.PartCode, C.PoBillNum, C.PoLine";
string _ asn_recive = "\n" +
"SELECT p1.PartCode, p1.PoBillNum, p1.PoLine,AskQty,ReciveQty,AsnQty FROM\n" +
"(\n" +
"\n" +
"\n" +
"SELECT sum(c.AskQty) AskQty, C.PartCode, C.PoBillNum, C.PoLine\n" +
"FROM TB_ASK_DETAIL AS C\n" +
"WHERE (C.AskBillNum LIKE 'U%') AND (C.State >= 0) AND (C.AskBillNum IN ({0}))\n" +
"GROUP BY C.PartCode, C.PoBillNum, C.PoLine\n" +
") p1\n" +
"left join\n" +
"(SELECT A.PartCode, A.PoBillNum, A.PoLine, SUM(ISNULL(A.Qty, 0)) AS AsnQty, SUM(ISNULL(B.qty, 0)) AS ReciveQty\n" +
"FROM\n" +
" V_TB_ASN_DETAIL AS A\n" +
" LEFT OUTER JOIN\n" +
" V_TB_RECEIVE_DETAIL AS B ON A.AsnBillNum = B.AsnBillNum AND A.PartCode = B.PartCode AND\n" +
" A.PoBillNum = B.PoBillNum AND A.PoLine = B.PoLine\n" +
"\n" +
"WHERE (A.AskBillNum LIKE 'U%') AND (A.State > 1) AND (A.AskBillNum IN ({0}))\n" +
"GROUP BY A.PartCode, A.PoBillNum, A.PoLine) P2\n" +
"\n" +
"on P1.PartCode=p2.PartCode and p1.PoBillNum=p2.PoBillNum and p1.PoLine=p2.PoLine" ;
/*初始化预测模板填写*/
string _ posql = "SELECT * FROM ( SELECT partcode, convert(varchar(10),EndTime ,23) endtime,0 as planqty, isnull(PartDesc1,'') +isnull(PartDesc2,'') as partdesc,pounit,Site as domain,pobillnum,poline,vendid FROM V_TB_PO_DETAIL where {0}\n" +
" ) A\n" +
" pivot ( sum(planQty) for a.endtime in (\n" +
"{1}\n" +
" )\n" +
" ) as ourpivot" ;
var dbSetting = GlobalConfig . ScpDatabase ;
SqlConnection conn = new System . Data . SqlClient . SqlConnection ( ) ;
try
{
var strConn = EntitiesFactory . GetEfConnectionString ( dbSetting ) ;
conn . ConnectionString = strConn ;
if ( conn . State ! = ConnectionState . Open )
{
conn . Open ( ) ;
}
#region 结束时间范围
SqlCommand cmd = new SqlCommand ( ) ;
cmd . Connection = conn ;
string where1 = " 1=1 " ;
if ( ! string . IsNullOrEmpty ( p_plan . PartCode ) )
{
where1 + = string . Format ( " and partcode='{0}' " , p_plan . PartCode ) ;
}
if ( ! string . IsNullOrEmpty ( p_plan . VendId ) )
{
where1 + = string . Format ( " and VendId='{0}' " , p_plan . VendId ) ;
}
if ( ! string . IsNullOrEmpty ( p_plan . PoBillNum ) )
{
where1 + = string . Format ( " and PoBillNum='{0}' " , p_plan . PoBillNum ) ;
}
if ( p_plan . EndTime_Begin ! = null )
{
where1 + = string . Format ( " and EndTime>='{0}'" , p_plan . EndTime_Begin . Value . ToString ( "yyyy-MM-dd" ) ) ;
}
else
{
where1 + = string . Format ( " and EndTime>='{0}'" , DateTime . Now . ToString ( "yyyy-MM-dd" ) ) ;
}
if ( p_plan . EndTime_End ! = null )
{
where1 + = string . Format ( " and EndTime<='{0}'" , p_plan . EndTime_End . Value . ToString ( "yyyy-MM-dd" ) ) ;
}
else
{
where1 + = string . Format ( " and EndTime<='{0}'" , DateTime . Now . AddMonths ( 6 ) . ToString ( "yyyy-MM-dd" ) ) ;
}
cmd . CommandText = string . Format ( _ sql , where1 ) ;
SqlDataAdapter adapter = new SqlDataAdapter ( cmd ) ;
adapter . Fill ( dt ) ;
#endregion
List < string > _l s = new List < string > ( ) ;
List < string > _l s1 = new List < string > ( ) ;
foreach ( DataRow row in dt . Rows )
{
if ( IsOutput = = true )
{
var _d ate = DateTime . Parse ( row [ "EndTime" ] . ToString ( ) ) ; //去掉周六周日
if ( ( int ) _d ate . DayOfWeek ! = 0 | | ( int ) _d ate . DayOfWeek ! = 6 )
{
_l s . Add ( string . Format ( "[{0}]" , row [ "EndTime" ] . ToString ( ) ) ) ;
}
_l s1 . Add ( string . Format ( "'{0}'" , row [ "EndTime" ] . ToString ( ) ) ) ;
}
else
{
_l s . Add ( string . Format ( "[{0}]" , row [ "EndTime" ] . ToString ( ) ) ) ;
_l s1 . Add ( string . Format ( "'{0}'" , row [ "EndTime" ] . ToString ( ) ) ) ;
}
}
if ( _l s . Count > 0 )
{
#region 计划行转列
SqlCommand cmd1 = new SqlCommand ( ) ;
cmd1 . Connection = conn ;
string Bwhere = "1=1 " ;
string Awhere = "1=1" ;
if ( ! string . IsNullOrEmpty ( p_plan . PartCode ) )
{
Bwhere + = string . Format ( " and PartCode='{0}' " , p_plan . PartCode ) ;
Awhere + = string . Format ( " and A.partcode='{0}' " , p_plan . PartCode ) ;
}
if ( ! string . IsNullOrEmpty ( p_plan . VendId ) )
{
Bwhere + = string . Format ( " and VendId='{0}' " , p_plan . VendId ) ;
Awhere + = string . Format ( " and A.VendId='{0}' " , p_plan . VendId ) ;
}
if ( ! string . IsNullOrEmpty ( p_plan . PoBillNum ) )
{
Bwhere + = string . Format ( " and PoBillNum='{0}' " , p_plan . PoBillNum ) ;
Awhere + = string . Format ( " and A.PoBillNum='{0}' " , p_plan . PoBillNum ) ;
}
cmd1 . CommandText = string . Format ( _ sqlString , string . Join ( "," , _l s . ToArray ( ) ) , Bwhere , Awhere ) ;
var adapter1 = new SqlDataAdapter ( cmd1 ) ;
adapter1 . Fill ( dt1 ) ;
#endregion
if ( dt1 . Rows . Count = = 0 )
{
#region 是否使用订单生成模板
DataTable poTable = new DataTable ( ) ;
SqlCommand pocmd = new SqlCommand ( ) ;
pocmd . Connection = conn ;
string poWhere = "1=1 " ;
//poWhere += string.Format(" and VendId='{0}' ", p_plan.VendId);
if ( ! string . IsNullOrEmpty ( p_plan . PoBillNum ) )
{
poWhere + = string . Format ( " and PoBillNum='{0}' " , p_plan . PoBillNum ) ;
}
pocmd . CommandText = string . Format ( _ posql , poWhere , string . Join ( "," , _l s . ToArray ( ) ) ) ;
var poAdapter = new SqlDataAdapter ( pocmd ) ;
poAdapter . Fill ( poTable ) ;
dt1 = poTable ;
foreach ( DataColumn itm in dt1 . Columns )
{
itm . DataType = System . Type . GetType ( "System.String" ) ;
}
DataColumn _ column = new DataColumn ( "计划天数" ) ;
dt1 . Columns . Add ( _ column ) ;
DataColumn _ column1 = new DataColumn ( "版本号" ) ;
dt1 . Columns . Add ( _ column1 ) ;
foreach ( DataRow row in dt1 . Rows )
{
row [ "版本号" ] = DateTime . Now . ToString ( "yyyy-MM-dd" ) . Replace ( "-" , string . Empty ) . Replace ( "/" , string . Empty ) + "0001" ;
}
#endregion
}
else
{
#region 此范围内的看板号
SqlCommand cmd2 = new SqlCommand ( ) ;
cmd2 . Connection = conn ;
cmd2 . CommandText = string . Format ( _ askBillSql , p_plan . PoBillNum , string . Join ( "," , _l s1 . ToArray ( ) ) ) ;
SqlDataAdapter adapter2 = new SqlDataAdapter ( cmd2 ) ;
adapter2 . Fill ( dt2 ) ;
#endregion
List < string > _ askList = new List < string > ( ) ;
foreach ( DataRow row in dt2 . Rows )
{
_ askList . Add ( string . Format ( "'{0}'" , row [ "AskBillNum" ] . ToString ( ) ) ) ;
}
if ( _ askList . Count > 0 )
{
SqlCommand cmd3 = new SqlCommand ( ) ;
cmd3 . Connection = conn ;
cmd3 . CommandText = string . Format ( _ asn_recive , string . Join ( "," , _ askList . ToArray ( ) ) ) ;
SqlDataAdapter adapter3 = new SqlDataAdapter ( cmd3 ) ;
adapter3 . Fill ( dt3 ) ;
}
var column1 = dt1 . Columns . Add ( "收货数量" ) ;
var column2 = dt1 . Columns . Add ( "发货数量" ) ;
var column3 = dt1 . Columns . Add ( "要货数量" ) ;
var column5 = dt1 . Columns . Add ( "版本号" ) ;
var column6 = dt1 . Columns . Add ( "计划天数" ) ;
string _ maxCode = string . Empty ;
if ( IsOutput = = true )
{
DataTable dt5 = new DataTable ( ) ;
string _ max = string . Format ( "SELECT MAX(Version) as max FROM [TB_PO_PLAN_EXTEND] where PoBillNum = '{0}' and site='{1}'" , p_plan . PoBillNum , p_plan . Domain ) ;
SqlCommand cmd5 = new SqlCommand ( ) ;
cmd5 . Connection = conn ;
cmd5 . CommandText = _ max ;
var adapter5 = new SqlDataAdapter ( cmd5 ) ;
adapter5 . Fill ( dt5 ) ;
if ( dt5 . Rows [ 0 ] [ "max" ] ! = null & & dt5 . Rows [ 0 ] [ "max" ] . ToString ( ) ! = string . Empty & & string . IsNullOrEmpty ( Regex . Replace ( dt5 . Rows [ 0 ] [ "max" ] . ToString ( ) , @"\d{11,12}" , string . Empty ) ) )
{
if ( dt5 . Rows [ 0 ] [ "max" ] . ToString ( ) . Length = = 1 1 )
{
string _ oldDate = dt5 . Rows [ 0 ] [ "max" ] . ToString ( ) . Substring ( 0 , 7 ) ;
string _ newDate = DateTime . Now . ToString ( "yyyy-MM-dd" ) . Replace ( "/" , string . Empty ) ;
if ( long . Parse ( _ oldDate ) = = long . Parse ( _ newDate ) )
{
_ maxCode = ( long . Parse ( dt5 . Rows [ 0 ] [ "max" ] . ToString ( ) ) + 1 ) . ToString ( ) ;
}
if ( long . Parse ( _ newDate ) > long . Parse ( _ oldDate ) )
{
_ maxCode = _ newDate + "0001" ;
}
}
if ( dt5 . Rows [ 0 ] [ "max" ] . ToString ( ) . Length = = 1 2 )
{
string _ oldDate = dt5 . Rows [ 0 ] [ "max" ] . ToString ( ) . Substring ( 0 , 8 ) ;
string _ newDate = DateTime . Now . ToString ( "yyyy-MM-dd" ) . Replace ( "-" , string . Empty ) ;
if ( long . Parse ( _ oldDate ) = = long . Parse ( _ newDate ) )
{
_ maxCode = ( long . Parse ( dt5 . Rows [ 0 ] [ "max" ] . ToString ( ) ) + 1 ) . ToString ( ) ;
}
if ( long . Parse ( _ newDate ) > long . Parse ( _ oldDate ) )
{
_ maxCode = _ newDate + "0001" ;
}
}
}
else
{
string _ newDate = DateTime . Now . ToString ( "yyyy-MM-dd" ) . Replace ( "-" , string . Empty ) . Replace ( "/" , string . Empty ) ;
_ maxCode = _ newDate + "0001" ;
}
}
foreach ( DataRow dr in dt1 . Rows )
{
foreach ( DataRow row in dt3 . Rows )
{
if (
dr [ "PartCode" ] . ToString ( ) = = row [ "PartCode" ] . ToString ( )
& & dr [ "PoBillNum" ] . ToString ( ) = = row [ "PoBillNum" ] . ToString ( )
& & dr [ "PoLine" ] . ToString ( ) = = row [ "PoLine" ] . ToString ( ) )
{
dr [ "收货数量" ] = row [ "ReciveQty" ] . ToString ( ) ;
dr [ "发货数量" ] = row [ "AsnQty" ] . ToString ( ) ;
dr [ "要货数量" ] = row [ "AskQty" ] . ToString ( ) ;
//dr["未收数量"] = decimal.Parse(row["AsnQty"].ToString()) - decimal.Parse(row["ReciveQty"].ToString());
//dr["未发数量"] = decimal.Parse(row["AskQty"].ToString()) - decimal.Parse(row["AsnQty"].ToString());
}
}
}
if ( IsOutput = = true )
{
foreach ( DataRow row in dt1 . Rows )
{
row [ "版本号" ] = _ maxCode ;
}
}
foreach ( DataColumn column in dt1 . Columns )
{
try
{
var _d t1 = DateTime . Parse ( column . ColumnName ) ;
var _ curDate = _d t1 . ToString ( "yyyy-MM-dd" ) ;
column . ColumnName = _ curDate + "(预测)" ;
foreach ( DataRow row in dt2 . Rows )
{
var _ cur = DateTime . Parse ( row [ "EndTime" ] . ToString ( ) ) . ToString ( "yyyy-MM-dd" ) ;
StringBuilder _ buf = new StringBuilder ( ) ;
if ( _ curDate = = _ cur )
{
var _ ask = row [ "AskBillNum" ] . ToString ( ) ;
column . ColumnName = _ cur + "(" + _ ask + ")" ;
switch ( int . Parse ( row [ "state" ] . ToString ( ) ) )
{
case ( int ) AskState . New :
column . ColumnName = _ cur + "(" + _ ask + ")" + "(计划)" ;
break ;
case ( int ) AskState . Ship :
column . ColumnName = _ cur + "(" + _ ask + ")" + "(已发货)" ;
break ;
case ( int ) AskState . Confirm :
column . ColumnName = _ cur + "(" + _ ask + ")" + "(已确认)" ;
break ;
case ( int ) AskState . Release :
column . ColumnName = _ cur + "(" + _ ask + ")" + "(已发布)" ;
break ;
case ( int ) AskState . Receive :
column . ColumnName = _ cur + "(" + _ ask + ")" + "(已收货)" ;
break ;
}
}
}
}
catch
{
continue ;
}
}
}
dt1 . Columns [ "VendID" ] . ColumnName = "供应商编码" ;
dt1 . Columns [ "PartCode" ] . ColumnName = "零件编码" ;
dt1 . Columns [ "PartDesc" ] . ColumnName = "零件描述" ;
dt1 . Columns [ "PoUnit" ] . ColumnName = "单位" ;
dt1 . Columns [ "PoBillNum" ] . ColumnName = "订单编号" ;
dt1 . Columns [ "PoLine" ] . ColumnName = "行号" ;
dt1 . Columns [ "Domain" ] . ColumnName = "域" ;
if ( IsOutput = = true )
{
dt1 . Columns [ "版本号" ] . SetOrdinal ( 0 ) ;
dt1 . Columns [ "计划天数" ] . SetOrdinal ( 1 ) ;
}
}
conn . Close ( ) ;
}
catch ( Exception ex )
{
conn . Close ( ) ;
throw new Exception ( $"系统无法连接到数据库:{dbSetting},请检查配置的服务器,数据库,用户名和密码等信息是否正确。{Environment.NewLine}{ex}" ) ;
}
return dt1 ;
}
public static DataTable EXPORT_TB_PO_PLAN_EXTEND ( V_TB_PO_PLAN_EXTEND p_plan )
{
V_TB_PO_PLAN_EXTEND _ plan = new V_TB_PO_PLAN_EXTEND ( ) ;
_ plan . VendId = p_plan . VendId ;
_ plan . PoBillNum = p_plan . PoBillNum ;
_ plan . Domain = p_plan . Domain ;
DataTable tb = Get_TB_PLAN_REPORT_EXTEND ( _ plan , true ) ;
return tb ;
}
public class TB_PO_PLAN_EXTEND_TEMP
{
public string AskBillNum { set ; get ; }
public string AsnBillNum { set ; get ; }
}
public static ResultObject < bool > IMPORT_TB_PO_PLAN_EXTEND ( DataTable p_table , User p_user , bool IsAutoRelse )
{
string _ sqlString = "SELECT askbillNum FROM TB_ASK where TB_ASK.AskBillNum LIKE 'U%' and TB_ASK.ENDTIME in ({0}) and TB_ASK.pobillnum='{1}' and TB_ASK.Site='{2}'" ;
string _ asnSqlString = "select CONVERT(varchar(100), TB_ASK.EndTime, 23) as EndTime from TB_ASK inner join tb_asn on TB_ASK.AskBillNum=TB_ASN.AskBillNum \n" +
"where tb_asn.State>-1 and TB_ASK.AskBillNum like 'U%' AND TB_ASK.ENDTIME in ({0}) and TB_ASK.pobillnum='{1}' and tb_ask.site='{2}'" ;
string _d elPlan = "delete from TB_PO_PLAN_EXTEND where pobillnum ='{0}' and domain='{1}' and endtime in ({2}) " ;
string _d elString = "delete from tb_ask where askbillnum in ({0}) " ;
string _d elDetailString = "delete from tb_ask_detail where askbillnum in ({0}) " ;
ResultObject < bool > _ ret = new ResultObject < bool > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
List < string > _ versionList = new List < string > ( ) ;
List < TB_PO_PLAN_EXTEND > _l s = new List < TB_PO_PLAN_EXTEND > ( ) ;
List < string > _ partcodeList = new List < string > ( ) ;
List < string > _ vendcodeList = new List < string > ( ) ;
List < string > _ poList = new List < string > ( ) ;
List < string > _ siteList = new List < string > ( ) ;
List < string > _d ayList = new List < string > ( ) ;
List < string > _ endTimelist = new List < string > ( ) ;
List < string > _ askbillList = new List < string > ( ) ;
#region 列校验
List < string > _ checkDay = new List < string > ( ) ;
string [ ] _ checkColumnAry = new string [ ] {
"版本号" ,
"计划天数" ,
"零件编码" ,
"订单编号" ,
"行号" ,
"供应商编码" ,
"域" ,
"单位"
} ;
int _ checkNum = 0 ;
foreach ( DataColumn column in p_table . Columns )
{
if ( ! column . ColumnName . Contains ( "版本号" ) & &
! column . ColumnName . Contains ( "计划天数" ) & &
! column . ColumnName . Contains ( "零件编码" ) & &
! column . ColumnName . Contains ( "零件描述" ) & &
! column . ColumnName . Contains ( "订单编号" ) & &
! column . ColumnName . Contains ( "行号" ) & &
! column . ColumnName . Contains ( "供应商编码" ) & &
! column . ColumnName . Contains ( "域" ) & &
! column . ColumnName . Contains ( "收货数量" ) & &
! column . ColumnName . Contains ( "要货数量" ) & &
! column . ColumnName . Contains ( "发货数量" ) & &
! column . ColumnName . Contains ( "单位" )
)
{
var _ reg1 = Regex . Match ( column . ColumnName , @"\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}\([a-zA-Z]{1}\d{10}\)\([\u4e00-\u9fa5]+\)" ) ;
var _ reg2 = Regex . Match ( column . ColumnName , @"\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}\([\u4e00-\u9fa5]+\)" ) ;
var _ reg3 = Regex . Match ( column . ColumnName , @"\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}" ) ;
if ( ! string . IsNullOrEmpty ( _ reg3 . Value ) & & ! _ endTimelist . Contains ( _ reg3 . Value ) )
{
_ endTimelist . Add ( string . Format ( "'{0}'" , DateTime . Parse ( _ reg3 . Value ) . ToString ( "yyyy-MM-dd" ) ) ) ;
}
else
{
_ ret . MessageList . Add ( _ reg3 . Value + "交货日期有重复日期!" ) ;
}
if ( string . IsNullOrEmpty ( _ reg1 . Value ) & & string . IsNullOrEmpty ( _ reg2 . Value ) )
{
_ ret . MessageList . Add ( string . Format ( "{0}列格式不对" , column . ColumnName ) ) ;
}
}
else
{
foreach ( DataRow dr in p_table . Rows ) //导入计划EXCEL行转列
{
if ( column . ColumnName = = "订单编号" )
{
if ( ! _ poList . Contains ( dr [ "订单编号" ] . ToString ( ) . Trim ( ) ) )
{
_ poList . Add ( dr [ "订单编号" ] . ToString ( ) . Trim ( ) ) ;
}
}
if ( column . ColumnName = = "版本号" )
{
if ( ! _ versionList . Contains ( dr [ "版本号" ] . ToString ( ) . Trim ( ) ) )
{
_ versionList . Add ( dr [ "版本号" ] . ToString ( ) . Trim ( ) ) ;
}
}
if ( column . ColumnName = = "零件编码" )
{
if ( ! _ partcodeList . Contains ( dr [ "零件编码" ] . ToString ( ) . Trim ( ) ) )
{
_ partcodeList . Add ( dr [ "零件编码" ] . ToString ( ) . Trim ( ) ) ;
}
}
if ( column . ColumnName = = "供应商编码" )
{
if ( ! _ vendcodeList . Contains ( dr [ "供应商编码" ] . ToString ( ) . Trim ( ) ) )
{
_ vendcodeList . Add ( dr [ "供应商编码" ] . ToString ( ) . Trim ( ) ) ;
}
}
if ( column . ColumnName = = "域" )
{
if ( ! _ siteList . Contains ( dr [ "域" ] . ToString ( ) . Trim ( ) ) )
{
_ siteList . Add ( dr [ "域" ] . ToString ( ) . Trim ( ) ) ;
}
}
if ( column . ColumnName = = "计划天数" )
{
if ( dr [ "计划天数" ] ! = null )
{
_d ayList . Add ( dr [ "计划天数" ] . ToString ( ) . Trim ( ) ) ;
}
}
}
if ( _ checkColumnAry . Contains ( column . ColumnName . Trim ( ) ) )
{
_ checkNum + + ;
}
}
}
string _ pobillNum = p_table . Rows [ 0 ] [ "订单编号" ] . ToString ( ) ;
string _ site = p_table . Rows [ 0 ] [ "域" ] . ToString ( ) ;
string _ sql = string . Format ( _ asnSqlString , string . Join ( "," , _ endTimelist . ToArray ( ) ) , _ pobillNum , _ site ) ;
var _ asnCurList = db . Database . SqlQuery < string > ( _ sql ) . ToList ( ) ;
if ( _ asnCurList . Count > 0 )
{
_ endTimelist = _ endTimelist . Where ( p = > ! _ asnCurList . Contains ( p . Replace ( "'" , string . Empty ) ) ) . ToList ( ) ;
}
if ( _ versionList . Distinct ( ) . Count ( ) > 1 )
{
_ ret . MessageList . Add ( "导入模板中多个版本号!" ) ;
}
if ( _ vendcodeList . Distinct ( ) . Count ( ) > 1 )
{
_ ret . MessageList . Add ( "导入模板中有多个供应商编码!" ) ;
}
if ( _ versionList . Distinct ( ) . Count ( ) = = 1 )
{
var _ version = _ versionList . FirstOrDefault ( ) . Replace ( "/" , string . Empty ) ;
var _f irst = db . TB_PO_PLAN_EXTEND . Where ( p = > p . VendId = = _ vendcodeList . FirstOrDefault ( ) & & p . PoBillNum = = _ poList . FirstOrDefault ( ) & & p . Version = = _ version ) . FirstOrDefault ( ) ;
if ( _f irst ! = null )
{
if ( long . Parse ( _ version ) < = long . Parse ( _f irst . Version ) )
{
_ ret . MessageList . Add ( "版本号必须大于上期版本号" ) ;
}
}
}
if ( _ checkNum ! = _ checkColumnAry . Length )
{
_ ret . MessageList . Add ( "版本号、计划天数、零件编码、订单编号、行号、供应商编码、域、单位为必填列" ) ;
}
#endregion
if ( _ ret . MessageList . Count = = 0 )
{
if ( _ endTimelist . Count > 0 )
{
var _ sql1 = string . Format ( _ sqlString , string . Join ( "," , _ endTimelist . ToArray ( ) ) , _ poList . FirstOrDefault ( ) , _ siteList . FirstOrDefault ( ) ) ;
var _ askList = db . Database . SqlQuery < string > ( _ sql1 ) . ToList ( ) ;
#region 发货单位空的看板
if ( _ askList . Count > 0 )
{
var _l ist = new List < string > ( ) ;
_ askList . ForEach ( p = >
{
_l ist . Add ( string . Format ( "'{0}'" , p ) ) ;
} ) ;
string _ askStr = string . Format ( _d elString , string . Join ( "," , _l ist . ToArray ( ) ) ) ;
int _ askdel = db . Database . ExecuteSqlCommand ( _ askStr ) ;
if ( _ askdel > 0 )
{
string _ askDetailStr = string . Format ( _d elDetailString , string . Join ( "," , _l ist . ToArray ( ) ) , _ siteList . FirstOrDefault ( ) ) ;
int _ askdelDetail = db . Database . ExecuteSqlCommand ( _ askDetailStr ) ;
}
}
int delPlanState = db . Database . ExecuteSqlCommand ( string . Format ( _d elPlan , _ pobillNum , _ site , string . Join ( "," , _ endTimelist . ToArray ( ) ) ) ) ;
#endregion
}
foreach ( DataColumn colum in p_table . Columns )
{
if ( colum . ColumnName . Contains ( "(预测)" ) )
{
var _ reg3 = Regex . Match ( colum . ColumnName , @"\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}" ) ;
colum . ColumnName = DateTime . Parse ( _ reg3 . Value ) . ToString ( "yyyy-MM-dd" ) ;
}
if ( colum . ColumnName . Contains ( "(计划)" ) )
{
var _ reg3 = Regex . Match ( colum . ColumnName , @"\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}" ) ;
colum . ColumnName = DateTime . Parse ( _ reg3 . Value ) . ToString ( "yyyy-MM-dd" ) ;
}
if ( colum . ColumnName . Contains ( "(已确认)" ) )
{
var _ reg3 = Regex . Match ( colum . ColumnName , @"\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}" ) ;
colum . ColumnName = DateTime . Parse ( _ reg3 . Value ) . ToString ( "yyyy-MM-dd" ) ;
//_endTimelist.Add(string.Format("'{0}'", colum.ColumnName));
}
if ( colum . ColumnName . Contains ( "(已发布)" ) )
{
var _ reg3 = Regex . Match ( colum . ColumnName , @"\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}" ) ;
colum . ColumnName = DateTime . Parse ( _ reg3 . Value ) . ToString ( "yyyy-MM-dd" ) ;
}
if ( _ endTimelist . Contains ( string . Format ( "'{0}'" , colum . ColumnName ) ) ) //没有被发货单占用的要货看板结束时间
{
foreach ( DataRow dr in p_table . Rows ) //导入计划EXCEL行转列
{
DateTime _d t = new DateTime ( ) ;
if ( DateTime . TryParse ( colum . ColumnName , out _d t ) )
{
if ( dr [ colum . ColumnName ] ! = null & & ! string . IsNullOrEmpty ( dr [ colum . ColumnName ] . ToString ( ) ) )
{
try
{
var _ planQty = decimal . Parse ( dr [ colum . ColumnName ] . ToString ( ) ) ;
if ( _ planQty > 0 )
{
TB_PO_PLAN_EXTEND _ extend = new TB_PO_PLAN_EXTEND ( ) ;
_ extend . PartCode = dr [ "零件编码" ] . ToString ( ) ;
_ extend . Version = _ versionList . FirstOrDefault ( ) . Replace ( "/" , string . Empty ) ;
_ extend . PoBillNum = dr [ "订单编号" ] . ToString ( ) ;
_ extend . PoLine = int . Parse ( dr [ "行号" ] . ToString ( ) ) ;
_ extend . PlanQty = decimal . Parse ( dr [ colum . ColumnName ] . ToString ( ) ) ;
_ extend . BeginTime = DateTime . Parse ( DateTime . Now . ToString ( "yyyy-MM-dd" ) ) ;
_ extend . EndTime = DateTime . Parse ( colum . ColumnName ) ;
_ extend . Domain = dr [ "域" ] . ToString ( ) ;
_ extend . VendId = dr [ "供应商编码" ] . ToString ( ) ;
_ extend . CreateTime = DateTime . Now ;
_ extend . Site = dr [ "域" ] . ToString ( ) ;
_ extend . PoUnit = dr [ "单位" ] . ToString ( ) ;
_l s . Add ( _ extend ) ;
}
}
catch ( Exception e )
{
throw e ;
}
}
}
}
}
}
db . TB_PO_PLAN_EXTEND . AddOrUpdate ( _l s . ToArray ( ) ) ;
if ( db . SaveChanges ( ) ! = - 1 )
{
if ( _d ayList . Count > 0 )
{
var _ askday = int . Parse ( _d ayList . FirstOrDefault ( ) ) ;
var _ planDayList = _l s . Select ( p = > p . EndTime ) . Distinct ( ) ;
if ( _ askday > _ planDayList . Count ( ) )
{
_ ret . MessageList . Add ( "计划天数不能大于导入天数!" ) ;
}
else
{
var _ askEndTime = _ planDayList . OrderBy ( p = > p ) . Take ( _ askday ) ;
foreach ( var itm in _ askEndTime )
{
var list = _l s . Where ( p = > p . EndTime = = itm & & p . VendId = = _ vendcodeList . FirstOrDefault ( ) & & p . PoBillNum = = _ poList . FirstOrDefault ( ) ) . ToList ( ) ;
var rsult = PLAN_EXTEND_TO_ASK ( list , p_user , IsAutoRelse ) ;
if ( rsult . State = = ReturnStatus . Succeed )
{
}
else
{
_ ret . MessageList . AddRange ( rsult . MessageList . ToArray ( ) ) ;
}
}
}
}
else
{
}
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = true ;
}
else
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
}
}
if ( _ ret . MessageList . Count = = 0 )
{
}
else
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
}
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( TB_PO_PLAN_EXTEND ) , "IMPORT_TB_PO_PLAN_EXTEND" , e . Message ) ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( e ) ;
throw e ;
}
return _ ret ;
}
public static void Get_TB_PO_PLAN ( TB_PO_PLAN p_entity , Action < ResultObject < IQueryable < TB_PO_PLAN > > > p_action )
{
ResultObject < IQueryable < TB_PO_PLAN > > _ ret = new ResultObject < IQueryable < TB_PO_PLAN > > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
IQueryable < TB_PO_PLAN > q = db . TB_PO_PLAN ;
if ( ! string . IsNullOrEmpty ( p_entity . Domain ) )
{
q = q . Where ( p = > p . Domain . Contains ( p_entity . Domain ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . PartCode ) )
{
q = q . Where ( p = > p . PartCode . Contains ( p_entity . PartCode ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . PoBillNum ) )
{
q = q . Where ( p = > p . PoBillNum . Contains ( p_entity . PoBillNum ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . Site ) )
{
q = q . Where ( p = > p . Site . Contains ( p_entity . Site ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . Version ) )
{
q = q . Where ( p = > p . Version . Contains ( p_entity . Version ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . VendId ) )
{
q = q . Where ( p = > p . VendId . Contains ( p_entity . VendId ) ) ;
}
if ( p_entity . EndTime ! = null )
{
q = q . Where ( p = > p . EndTime = = p_entity . EndTime ) ;
}
//if (p_entity.UserInAddress != null && p_entity.UserInAddress.Count > 0)
//{
// q = q.Where(p => p_entity.UserInAddress.Contains(p.Address));
//}
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = q ;
p_action ( _ ret ) ;
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
_ ret . ErrorList . Add ( e ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( V_TB_PO_PLAN ) , "Get_TB_PO_PLAN" , e . Message ) ;
throw e ;
}
}
public ResultObject < bool > Save_V_TB_PO_PLAN ( List < V_TB_PO_PLAN > p_entitys )
{
ResultObject < bool > _ ret = new ResultObject < bool > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
foreach ( var itm in p_entitys )
{
db . V_TB_PO_PLAN . AddOrUpdate ( itm ) ;
}
if ( db . SaveChanges ( ) ! = - 1 )
{
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = true ;
}
else
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
}
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( V_TB_PO_PLAN ) , "Save_V_TB_PO_PLAN" , e . Message ) ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( e ) ;
throw e ;
}
return _ ret ;
}
public ResultObject < bool > Del_V_TB_PO_PLAN ( List < V_TB_PO_PLAN > p_entitys )
{
ResultObject < bool > _ ret = new ResultObject < bool > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
foreach ( var itm in p_entitys )
{
db . V_TB_PO_PLAN . Remove ( itm ) ;
}
if ( db . SaveChanges ( ) ! = - 1 )
{
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = true ;
}
else
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
}
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( e ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( V_TB_PO_PLAN ) , "Del_V_TB_PO_PLAN" , e . Message ) ; throw e ;
}
return _ ret ;
}
public static void Get_V_TB_PO_PLAN_DETAIL_List ( V_TB_PO_PLAN_DETAIL p_entity , Action < ResultObject < IQueryable < V_TB_PO_PLAN_DETAIL > > > p_action )
{
ResultObject < IQueryable < V_TB_PO_PLAN_DETAIL > > _ ret = new ResultObject < IQueryable < V_TB_PO_PLAN_DETAIL > > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
IQueryable < V_TB_PO_PLAN_DETAIL > q = db . V_TB_PO_PLAN_DETAIL ;
if ( ! string . IsNullOrEmpty ( p_entity . PoBillNum ) )
{
q = q . Where ( p = > p . PoBillNum . Contains ( p_entity . PoBillNum ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . VendId ) )
{
q = q . Where ( p = > p . VendId . Contains ( p_entity . VendId ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . PartCode ) )
{
q = q . Where ( p = > p . PartCode . Contains ( p_entity . PartCode ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . AskBillNum ) )
{
q = q . Where ( p = > p . AskBillNum . Contains ( p_entity . AskBillNum ) ) ;
}
if ( p_entity . EndTime ! = null )
{
q = q . Where ( p = > p . EndTime = = p_entity . EndTime . Value ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . Version ) )
{
q = q . Where ( p = > p . Version . Contains ( p_entity . Version ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . Domain ) )
{
q = q . Where ( p = > p . Domain . Contains ( p_entity . Domain ) ) ;
}
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = q ;
p_action ( _ ret ) ;
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
_ ret . ErrorList . Add ( e ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( V_TB_PO_PLAN_DETAIL ) , "Get_V_TB_PO_PLAN_DETAIL_List" , e . Message ) ;
throw e ;
}
}
public ResultObject < bool > Save_V_TB_PO_PLAN_DETAIL ( List < V_TB_PO_PLAN_DETAIL > p_entitys )
{
ResultObject < bool > _ ret = new ResultObject < bool > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
foreach ( var itm in p_entitys )
{
db . V_TB_PO_PLAN_DETAIL . AddOrUpdate ( itm ) ;
}
if ( db . SaveChanges ( ) ! = - 1 )
{
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = true ;
}
else
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
}
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( V_TB_PO_PLAN_DETAIL ) , "Save_V_TB_PO_PLAN_DETAIL" , e . Message ) ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( e ) ;
throw e ;
}
return _ ret ;
}
public ResultObject < bool > Del_V_TB_PO_PLAN_DETAIL ( List < V_TB_PO_PLAN_DETAIL > p_entitys )
{
ResultObject < bool > _ ret = new ResultObject < bool > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
foreach ( var itm in p_entitys )
{
db . V_TB_PO_PLAN_DETAIL . Remove ( itm ) ;
}
if ( db . SaveChanges ( ) ! = - 1 )
{
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = true ;
}
else
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
}
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( e ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( V_TB_PO_PLAN_DETAIL ) , "Del_V_TB_PO_PLAN_DETAIL" , e . Message ) ; throw e ;
}
return _ ret ;
}
public static ResultObject < bool > PLAN_TO_ASK ( List < V_TB_PO_PLAN > p_order_list , User p_user , bool IsAutoPublish = false )
{
ResultObject < bool > _ ret = new ResultObject < bool > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
//p_order_list.ForEach(p =>
//{
// if (string.IsNullOrEmpty(p.PlanQty))
// {
// p.PlanQty = 0;
// }
//});
//int number = 1;
List < string > lineError = new List < string > ( ) ;
List < string > ErrorList = new List < string > ( ) ;
var _l st = p_order_list . Where ( p = > p . PlanQty ! = 0 ) . ToList ( ) ;
//_lst.ForEach
//(p =>
//{
// //var _ls = CheckExcelMode(db, p);
// if (_ls.Count > 0)
// {
// lineError.Add(number.ToString());
// ErrorList.Add(string.Join("<br>", _ls.ToArray()));
// }
// number++;
//});
if ( lineError . Count > 0 )
{
_ ret . State = ReturnStatus . Failed ;
_ ret . MessageList . AddRange ( ErrorList ) ;
_ ret . Result = false ;
}
else
{
var VenderList = _l st . Select ( p = > p . VendId ) . Distinct ( ) ;
var PartCodeList = _l st . Select ( p = > p . PartCode ) . Distinct ( ) ;
var OrderList = _l st . GroupBy ( p = > p . PoBillNum ) . ToList ( ) ;
OrderList . ForEach ( ( p ) = >
{
var list = p . ToList ( ) ;
if ( list . Count > 0 )
{
var _ entity = list . FirstOrDefault ( ) ;
TB_ASK _ ask = new TB_ASK ( ) ;
_ ask . AskBillNum = SCP_BILLCODE_CONTROLLER . MakePLAN_ASKCode ( ) ;
_ ask . VendId = _ entity . VendId ;
_ ask . State = ( int ) AskState . New ;
_ ask . PoBillNum = _ entity . PoBillNum ;
_ ask . ModType = ( int ) BillModType . Contract ;
_ ask . Site = _ entity . Domain ;
_ ask . Remark = _ entity . Version ;
if ( IsAutoPublish )
{
_ ask . State = ( int ) AskState . Release ;
}
else
{
_ ask . State = ( int ) AskState . New ;
}
_ ask . BeginTime = DateTime . Now ;
_ ask . Buyer = p_user . ChineseName ;
_ ask . BuyerPhone = p_user . CellPhone ;
_ ask . ErpBillNum = _ entity . PoBillNum ;
_ ask . EndTime = _ entity . EndTime ;
_ ask . CreateTime = DateTime . Now ;
_ ask . CreateUser = p_user . Name ;
_ ask . VendId = _ entity . VendId ;
_ ask . GUID = Guid . NewGuid ( ) ;
_ ask . ReceivedPort = string . Empty ;
_ ask . IsDeleted = false ;
db . TB_ASK . Add ( _ ask ) ;
#region 供应商新任务代办接口
TB_PENING_ITEMS _ item = new TB_PENING_ITEMS ( ) ;
_ item . ROLE_NAME = "供应商" ;
_ item . ITEM_NO = _ ask . AskBillNum ;
_ item . ITEM_CONTENT = "要货看盘有信息" ;
_ item . ITEM_TYPE = ( int ) PENDING_MESSAGE_TYPE . NEW_ASK ;
_ item . VEND_ID = _ ask . VendId ;
_ item . ITEM_STATE = ( int ) PENDING_STATE . WAITING ;
_ item . SENDER = p_user . ChineseName ;
_ item . SENDING_TIME = DateTime . Now ;
db . TB_PENING_ITEMS . Add ( _ item ) ;
#endregion
// int _number = 1;
List < TB_ASK_DETAIL > _l s = new List < TB_ASK_DETAIL > ( ) ;
list . ForEach ( ( itm ) = >
{
var _ number = _l s . Count ( p1 = > p1 . PoBillNum = = _ ask . PoBillNum
& & p1 . PoLine = = itm . PoLine & & p1 . PartCode = = itm . PartCode ) ;
if ( _ number > 0 )
{
_ ret . MessageList . Add ( string . Format ( "已经存在订单为{0}行号为{1}零件编码为{2}" , itm . PoBillNum , itm . PoLine . ToString ( ) , itm . PartCode ) ) ;
}
TB_ASK_DETAIL _ t_ASK_Detail = new TB_ASK_DETAIL ( ) ;
//_t_ASK_Detail.PoLine = _number;
_ t_ASK_Detail . PoLine = itm . PoLine ;
_ t_ASK_Detail . Currency = "CNY" ;
_ t_ASK_Detail . AskBillNum = _ ask . AskBillNum ;
_ t_ASK_Detail . PoBillNum = _ ask . PoBillNum ;
_ t_ASK_Detail . PoUnit = itm . PoUnit ;
_ t_ASK_Detail . CreateTime = DateTime . Now ;
_ t_ASK_Detail . CreateUser = _ ask . CreateUser ;
_ t_ASK_Detail . PartCode = itm . PartCode ;
_ t_ASK_Detail . BeginTime = _ ask . BeginTime ;
_ t_ASK_Detail . EndTime = itm . EndTime ;
_ t_ASK_Detail . TempQty = itm . PlanQty ;
_ t_ASK_Detail . AskQty = itm . PlanQty ;
_ t_ASK_Detail . IsDeleted = false ;
_ t_ASK_Detail . LocUnit = itm . PoUnit ;
_ t_ASK_Detail . UnConv = 1 ;
_ t_ASK_Detail . Remark = itm . Version ;
_ t_ASK_Detail . State = _ ask . State ;
// _t_ASK_Detail.Price = string.IsNullOrEmpty(itm.) ? 0 : decimal.Parse(itm.价格);
_ t_ASK_Detail . GUID = Guid . NewGuid ( ) ;
_ t_ASK_Detail . Remark = _ ask . Site ;
db . TB_ASK_DETAIL . Add ( _ t_ASK_Detail ) ;
_l s . Add ( _ t_ASK_Detail ) ;
// _number++;
} ) ;
}
else
{
_ ret . MessageList . Add ( "ERP订单编号已存在,请重新导入!" ) ;
}
if ( _ ret . MessageList . Count = = 0 )
{
int state = db . SaveChanges ( ) ;
if ( state ! = - 1 )
{
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = true ;
}
else
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
}
}
else
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
}
}
) ;
}
}
}
catch ( System . Data . Entity . Validation . DbEntityValidationException dbEx ) //捕获实体验证异常
{
var sb = new StringBuilder ( ) ;
foreach ( var error in dbEx . EntityValidationErrors . ToList ( ) )
{
error . ValidationErrors . ToList ( ) . ForEach ( i = >
{
sb . AppendFormat ( "表:{0},字段:{1},信息:{2}\r\n" , error . Entry . Entity . GetType ( ) . Name , i . PropertyName , i . ErrorMessage ) ;
} ) ;
}
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( dbEx ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( SCP_PO_CONTROLLER ) , "EXCEL_PO_TO_ASK" , sb . ToString ( ) ) ;
throw new ScpException ( ResultCode . DbEntityValidationException , sb . ToString ( ) , "字段验证失败" + sb . ToString ( ) ) ;
}
catch ( OptimisticConcurrencyException ex ) //并发冲突异常
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( ex ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( SCP_PO_CONTROLLER ) , "EXCEL_PO_TO_ASK" , ex . ToString ( ) ) ;
throw new ScpException ( ResultCode . Exception , "9999" , ex . ToString ( ) ) ;
}
catch ( ScpException ex )
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( ex ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( SCP_PO_CONTROLLER ) , "EXCEL_PO_TO_ASK" , ex . ToString ( ) ) ;
if ( ex . InnerException ! = null & & ex . InnerException . GetType ( ) = = typeof ( UpdateException ) )
{
var inner = ( UpdateException ) ex . InnerException ;
throw new ScpException ( ResultCode . Exception , "0000" , ex . ToString ( ) ) ;
}
else
{
if ( ex . InnerException ! = null ) throw ex . InnerException ;
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( SCP_PO_CONTROLLER ) , "EXCEL_PO_TO_ASK" , e . Message ) ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( e ) ;
throw e ;
}
return _ ret ;
}
public static ResultObject < bool > PLAN_EXTEND_TO_ASK ( List < TB_PO_PLAN_EXTEND > p_order_list , User p_user , bool IsAutoPublish = false )
{
ResultObject < bool > _ ret = new ResultObject < bool > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
//p_order_list.ForEach(p =>
//{
// if (string.IsNullOrEmpty(p.PlanQty))
// {
// p.PlanQty = 0;
// }
//});
//int number = 1;
List < string > lineError = new List < string > ( ) ;
List < string > ErrorList = new List < string > ( ) ;
var _l st = p_order_list . Where ( p = > p . PlanQty > 0 ) . ToList ( ) ;
//_lst.ForEach
//(p =>
//{
// //var _ls = CheckExcelMode(db, p);
// if (_ls.Count > 0)
// {
// lineError.Add(number.ToString());
// ErrorList.Add(string.Join("<br>", _ls.ToArray()));
// }
// number++;
//});
if ( lineError . Count > 0 )
{
_ ret . State = ReturnStatus . Failed ;
_ ret . MessageList . AddRange ( ErrorList ) ;
_ ret . Result = false ;
}
else
{
var VenderList = _l st . Select ( p = > p . VendId ) . Distinct ( ) ;
var PartCodeList = _l st . Select ( p = > p . PartCode ) . Distinct ( ) ;
var OrderList = _l st . GroupBy ( p = > p . PoBillNum ) . ToList ( ) ;
OrderList . ForEach ( ( p ) = >
{
var list = p . ToList ( ) ;
if ( list . Count > 0 )
{
var _ entity = list . FirstOrDefault ( ) ;
TB_ASK _ ask = new TB_ASK ( ) ;
_ ask . AskBillNum = SCP_BILLCODE_CONTROLLER . MakePLAN_EXTEND_ASKCode ( ) ;
_ ask . VendId = _ entity . VendId ;
_ ask . State = ( int ) AskState . New ;
_ ask . PoBillNum = _ entity . PoBillNum ;
_ ask . ModType = ( int ) BillModType . Contract ;
_ ask . Site = _ entity . Domain ;
_ ask . Remark = _ entity . Domain ;
if ( IsAutoPublish )
{
_ ask . State = ( int ) AskState . Release ;
}
else
{
_ ask . State = ( int ) AskState . New ;
}
_ ask . BeginTime = DateTime . Now ;
_ ask . Buyer = p_user . ChineseName ;
_ ask . BuyerPhone = p_user . CellPhone ;
_ ask . ErpBillNum = _ entity . PoBillNum ;
_ ask . EndTime = _ entity . EndTime ;
_ ask . SubSite = _ entity . Site ;
_ ask . CreateTime = DateTime . Now ;
_ ask . CreateUser = p_user . Name ;
_ ask . VendId = _ entity . VendId ;
_ ask . GUID = Guid . NewGuid ( ) ;
_ ask . ReceivedPort = string . Empty ;
_ ask . IsDeleted = false ;
db . TB_ASK . Add ( _ ask ) ;
#region 供应商新任务代办接口
TB_PENING_ITEMS _ item = new TB_PENING_ITEMS ( ) ;
_ item . ROLE_NAME = "供应商" ;
_ item . ITEM_NO = _ ask . AskBillNum ;
_ item . ITEM_CONTENT = "要货看盘有信息" ;
_ item . ITEM_TYPE = ( int ) PENDING_MESSAGE_TYPE . NEW_ASK ;
_ item . VEND_ID = _ ask . VendId ;
_ item . ITEM_STATE = ( int ) PENDING_STATE . WAITING ;
_ item . SENDER = p_user . ChineseName ;
_ item . SENDING_TIME = DateTime . Now ;
db . TB_PENING_ITEMS . Add ( _ item ) ;
#endregion
// int _number = 1;
List < TB_ASK_DETAIL > _l s = new List < TB_ASK_DETAIL > ( ) ;
list . ForEach ( ( itm ) = >
{
var _ number = _l s . Count ( p1 = > p1 . PoBillNum = = _ ask . PoBillNum
& & p1 . PoLine = = itm . PoLine & & p1 . PartCode = = itm . PartCode ) ;
if ( _ number > 0 )
{
_ ret . MessageList . Add ( string . Format ( "已经存在订单为{0}行号为{1}零件编码为{2}" , itm . PoBillNum , itm . PoLine . ToString ( ) , itm . PartCode ) ) ;
}
TB_ASK_DETAIL _ t_ASK_Detail = new TB_ASK_DETAIL ( ) ;
//_t_ASK_Detail.PoLine = _number;
_ t_ASK_Detail . PoLine = itm . PoLine ;
_ t_ASK_Detail . Currency = "CNY" ;
_ t_ASK_Detail . AskBillNum = _ ask . AskBillNum ;
_ t_ASK_Detail . PoBillNum = _ ask . PoBillNum ;
_ t_ASK_Detail . PoUnit = itm . PoUnit ;
_ t_ASK_Detail . CreateTime = DateTime . Now ;
_ t_ASK_Detail . CreateUser = _ ask . CreateUser ;
_ t_ASK_Detail . PartCode = itm . PartCode ;
_ t_ASK_Detail . BeginTime = _ ask . BeginTime ;
_ t_ASK_Detail . EndTime = itm . EndTime ;
_ t_ASK_Detail . TempQty = itm . PlanQty ;
_ t_ASK_Detail . AskQty = itm . PlanQty ;
_ t_ASK_Detail . IsDeleted = false ;
_ t_ASK_Detail . LocUnit = itm . PoUnit ;
_ t_ASK_Detail . UnConv = 1 ;
_ t_ASK_Detail . Site = _ ask . Site ;
_ t_ASK_Detail . SubSite = _ ask . SubSite ;
_ t_ASK_Detail . State = _ ask . State ;
// _t_ASK_Detail.Price = string.IsNullOrEmpty(itm.) ? 0 : decimal.Parse(itm.价格);
_ t_ASK_Detail . GUID = Guid . NewGuid ( ) ;
db . TB_ASK_DETAIL . Add ( _ t_ASK_Detail ) ;
_l s . Add ( _ t_ASK_Detail ) ;
// _number++;
} ) ;
}
else
{
_ ret . MessageList . Add ( "ERP订单编号已存在,请重新导入!" ) ;
}
if ( _ ret . MessageList . Count = = 0 )
{
int state = db . SaveChanges ( ) ;
if ( state ! = - 1 )
{
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = true ;
}
else
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
}
}
else
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
}
}
) ;
}
}
}
catch ( System . Data . Entity . Validation . DbEntityValidationException dbEx ) //捕获实体验证异常
{
var sb = new StringBuilder ( ) ;
foreach ( var error in dbEx . EntityValidationErrors . ToList ( ) )
{
error . ValidationErrors . ToList ( ) . ForEach ( i = >
{
sb . AppendFormat ( "表:{0},字段:{1},信息:{2}\r\n" , error . Entry . Entity . GetType ( ) . Name , i . PropertyName , i . ErrorMessage ) ;
} ) ;
}
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( dbEx ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( SCP_PO_CONTROLLER ) , "EXCEL_PO_TO_ASK" , sb . ToString ( ) ) ;
throw new ScpException ( ResultCode . DbEntityValidationException , sb . ToString ( ) , "字段验证失败" + sb . ToString ( ) ) ;
}
catch ( OptimisticConcurrencyException ex ) //并发冲突异常
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( ex ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( SCP_PO_CONTROLLER ) , "EXCEL_PO_TO_ASK" , ex . ToString ( ) ) ;
throw new ScpException ( ResultCode . Exception , "9999" , ex . ToString ( ) ) ;
}
catch ( ScpException ex )
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( ex ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( SCP_PO_CONTROLLER ) , "EXCEL_PO_TO_ASK" , ex . ToString ( ) ) ;
if ( ex . InnerException ! = null & & ex . InnerException . GetType ( ) = = typeof ( UpdateException ) )
{
var inner = ( UpdateException ) ex . InnerException ;
throw new ScpException ( ResultCode . Exception , "0000" , ex . ToString ( ) ) ;
}
else
{
if ( ex . InnerException ! = null ) throw ex . InnerException ;
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( SCP_PO_CONTROLLER ) , "EXCEL_PO_TO_ASK" , e . Message ) ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( e ) ;
throw e ;
}
return _ ret ;
}
public static void Get_TB_PLAN_CUSTOM_List ( TB_PLAN_CUSTOM p_entity , Action < ResultObject < IQueryable < TB_PLAN_CUSTOM > > > p_action )
{
ResultObject < IQueryable < TB_PLAN_CUSTOM > > _ ret = new ResultObject < IQueryable < TB_PLAN_CUSTOM > > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
IQueryable < TB_PLAN_CUSTOM > q = db . TB_PLAN_CUSTOM ;
if ( p_entity . UID ! = 0 )
{
q = q . Where ( p = > p . UID = = p_entity . UID ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . VendID ) )
{
q = q . Where ( p = > p . VendID . Contains ( p_entity . VendID ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . PlanData ) )
{
q = q . Where ( p = > p . PlanData . Contains ( p_entity . PlanData ) ) ;
}
if ( ! string . IsNullOrEmpty ( p_entity . Version ) )
{
q = q . Where ( p = > p . Version . Contains ( p_entity . Version ) ) ;
}
//if (p_entity.UserInAddress != null && p_entity.UserInAddress.Count > 0)
//{
// q = q.Where(p => p_entity.UserInAddress.Contains(p.Address));
//}
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = q ;
p_action ( _ ret ) ;
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
_ ret . ErrorList . Add ( e ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( TB_PLAN_CUSTOM ) , "Get_TB_PLAN_CUSTOM_List" , e . Message ) ;
throw e ;
}
}
public static ResultObject < bool > Save_TB_PLAN_CUSTOM ( List < TB_PLAN_CUSTOM > p_entitys )
{
ResultObject < bool > _ ret = new ResultObject < bool > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
foreach ( var itm in p_entitys )
{
db . TB_PLAN_CUSTOM . AddOrUpdate ( itm ) ;
}
if ( db . SaveChanges ( ) ! = - 1 )
{
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = true ;
}
else
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
}
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( TB_PLAN_CUSTOM ) , "Save_TB_PLAN_CUSTOM" , e . Message ) ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( e ) ;
throw e ;
}
return _ ret ;
}
public ResultObject < bool > Del_TB_PLAN_CUSTOM ( List < TB_PLAN_CUSTOM > p_entitys )
{
ResultObject < bool > _ ret = new ResultObject < bool > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
foreach ( var itm in p_entitys )
{
db . TB_PLAN_CUSTOM . Remove ( itm ) ;
}
if ( db . SaveChanges ( ) ! = - 1 )
{
_ ret . State = ReturnStatus . Succeed ;
_ ret . Result = true ;
}
else
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
}
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( e ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( TB_PLAN_CUSTOM ) , "Del_TB_PLAN_CUSTOM" , e . Message ) ; throw e ;
}
return _ ret ;
}
public ResultObject < bool > PLAN_TO_ASN ( DataTable p_table )
{
ResultObject < bool > _ ret = new ResultObject < bool > ( ) ;
try
{
using ( ScpEntities db = EntitiesFactory . CreateScpInstance ( ) )
{
//foreach (var itm in p_entitys)
//{
// db.TB_PLAN_CUSTOM.Remove(itm);
//}
//if (db.SaveChanges() != -1)
//{
// _ret.State = ReturnStatus.Succeed;
// _ret.Result = true;
//}
//else
//{
// _ret.State = ReturnStatus.Failed;
// _ret.Result = false;
//}
}
}
catch ( Exception e )
{
_ ret . State = ReturnStatus . Failed ;
_ ret . Result = false ;
_ ret . ErrorList . Add ( e ) ;
LogHelper . Writlog ( LogHelper . LogType . Error , typeof ( TB_PLAN_CUSTOM ) , "PLAN_TO_ASN" , e . Message ) ; throw e ;
}
return _ ret ;
}
}
}