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>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable 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_EXTEND), "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>> p_action) //{ // ResultObject> _ret = new ResultObject>(); // try // { // using (ScpEntities db = EntitiesFactory.CreateScpInstance()) // { // IQueryable 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 _ls = new List(); // List _ls1 = new List(); // foreach (DataRow row in dt.Rows) // { // _ls.Add(string.Format("[{0}]", row["EndTime"].ToString())); // _ls1.Add(string.Format("'{0}'", row["EndTime"].ToString())); // } // if (_ls.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(",", _ls.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(",", _ls1.ToArray())); // SqlDataAdapter adapter2 = new SqlDataAdapter(cmd2); // adapter2.Fill(dt2); // #endregion // List _askList = new List(); // 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 _dt1 = DateTime.Parse(column.ColumnName); // var _curDate = _dt1.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.subsite as site,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,site,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 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 'U%') AND (A.State > 0) and {0}\n" + //"GROUP BY A.PartCode, A.PoBillNum, A.PoLine"; 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 _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 _ls = new List(); List _ls1 = new List(); foreach (DataRow row in dt.Rows) { if (IsOutput == true) { var _date = DateTime.Parse(row["EndTime"].ToString());//去掉周六周日 if ((int)_date.DayOfWeek != 0 || (int)_date.DayOfWeek != 6) { _ls.Add(string.Format("[{0}]", row["EndTime"].ToString())); } _ls1.Add(string.Format("'{0}'", row["EndTime"].ToString())); } else { _ls.Add(string.Format("[{0}]", row["EndTime"].ToString())); _ls1.Add(string.Format("'{0}'", row["EndTime"].ToString())); } } if (_ls.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(",", _ls.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(",", _ls.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(",", _ls1.ToArray())); SqlDataAdapter adapter2 = new SqlDataAdapter(cmd2); adapter2.Fill(dt2); #endregion List _askList = new List(); 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 == 11) { 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 == 12) { 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 _dt1 = DateTime.Parse(column.ColumnName); var _curDate = _dt1.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["Site"].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 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 _delPlan = "delete from TB_PO_PLAN_EXTEND where pobillnum ='{0}' and domain='{1}' and endtime in ({2}) "; string _delString = "delete from tb_ask where askbillnum in ({0}) "; string _delDetailString = "delete from tb_ask_detail where askbillnum in ({0}) "; ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { List _versionList = new List(); List _ls = new List(); List _partcodeList = new List(); List _vendcodeList = new List(); List _poList = new List(); List _siteList = new List(); List _dayList = new List(); List _endTimelist = new List(); List _askbillList = new List(); #region 列校验 List _checkDay = new List(); 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("单位") && !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) { _dayList.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(_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 _first = db.TB_PO_PLAN_EXTEND.Where(p => p.VendId == _vendcodeList.FirstOrDefault() && p.PoBillNum == _poList.FirstOrDefault() && p.Version == _version).FirstOrDefault(); if (_first != null) { if (long.Parse(_version) <= long.Parse(_first.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(_sql1).ToList(); #region 发货单位空的看板 if (_askList.Count > 0) { var _list = new List(); _askList.ForEach(p => { _list.Add(string.Format("'{0}'", p)); }); string _askStr = string.Format(_delString, string.Join(",", _list.ToArray())); int _askdel = db.Database.ExecuteSqlCommand(_askStr); if (_askdel > 0) { string _askDetailStr = string.Format(_delDetailString, string.Join(",", _list.ToArray()), _siteList.FirstOrDefault()); int _askdelDetail = db.Database.ExecuteSqlCommand(_askDetailStr); } } int delPlanState = db.Database.ExecuteSqlCommand(string.Format(_delPlan, _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 _dt = new DateTime(); if (DateTime.TryParse(colum.ColumnName, out _dt)) { 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(); _ls.Add(_extend); } } catch (Exception e) { throw e; } } } } } } db.TB_PO_PLAN_EXTEND.AddOrUpdate(_ls.ToArray()); if (db.SaveChanges() != -1) { if (_dayList.Count > 0) { var _askday = int.Parse(_dayList.FirstOrDefault()); var _planDayList = _ls.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 = _ls.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>> p_action) //{ // ResultObject> _ret = new ResultObject>(); // try // { // using (ScpEntities db = EntitiesFactory.CreateScpInstance()) // { // IQueryable 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 Save_V_TB_PO_PLAN(List p_entitys) //{ // ResultObject _ret = new ResultObject(); // 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 Del_V_TB_PO_PLAN(List p_entitys) //{ // ResultObject _ret = new ResultObject(); // 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>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable 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 Save_V_TB_PO_PLAN_DETAIL(List p_entitys) { ResultObject _ret = new ResultObject(); 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 Del_V_TB_PO_PLAN_DETAIL(List p_entitys) { ResultObject _ret = new ResultObject(); 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 PLAN_TO_ASK(List p_order_list, User p_user, bool IsAutoPublish = false) //{ // ResultObject _ret = new ResultObject(); // try // { // using (ScpEntities db = EntitiesFactory.CreateScpInstance()) // { // //p_order_list.ForEach(p => // //{ // // if (string.IsNullOrEmpty(p.PlanQty)) // // { // // p.PlanQty = 0; // // } // //}); // //int number = 1; // List lineError = new List(); // List ErrorList = new List(); // var _lst = 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("
", _ls.ToArray())); // // } // // number++; // //}); // if (lineError.Count > 0) // { // _ret.State = ReturnStatus.Failed; // _ret.MessageList.AddRange(ErrorList); // _ret.Result = false; // } // else // { // var VenderList = _lst.Select(p => p.VendId).Distinct(); // var PartCodeList = _lst.Select(p => p.PartCode).Distinct(); // var OrderList = _lst.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 _ls = new List(); // list.ForEach((itm) => // { // var _number = _ls.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); // _ls.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 PLAN_EXTEND_TO_ASK(List p_order_list, User p_user, bool IsAutoPublish = false) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { //p_order_list.ForEach(p => //{ // if (string.IsNullOrEmpty(p.PlanQty)) // { // p.PlanQty = 0; // } //}); //int number = 1; List lineError = new List(); List ErrorList = new List(); var _lst = 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("
", _ls.ToArray())); // } // number++; //}); if (lineError.Count > 0) { _ret.State = ReturnStatus.Failed; _ret.MessageList.AddRange(ErrorList); _ret.Result = false; } else { var VenderList = _lst.Select(p => p.VendId).Distinct(); var PartCodeList = _lst.Select(p => p.PartCode).Distinct(); var OrderList = _lst.GroupBy(p => p.PoBillNum).ToList(); OrderList.ForEach((p) => { var list = p.ToList(); if (list.Count > 0) { var _entity = list.FirstOrDefault(); var _priceList= db.V_TB_PRICE.Where(price => price.StartTime <= _entity.EndTime && _entity.EndTime <= price.EndTime &&price.VendId== _entity.VendId); if (_priceList != null && _priceList.Count() > 0) { 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.SubSite = _entity.Site; 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 _ls = new List(); list.ForEach((itm) => { var _number = _ls.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.SubSite = _ask.SubSite; _t_ASK_Detail.Site = _ask.Site; _t_ASK_Detail.State = _ask.State; var _askPrice = _priceList.Where(t => t.PartCode == itm.PartCode && t.VendId == itm.VendId).FirstOrDefault(); if (_askPrice != null) { _t_ASK_Detail.Price = _askPrice.Amt; } else { _ret.MessageList.Add(string.Format("供应商编号{0}零件{1}计划到货时间{2}内无价格表!",_ask.VendId,_t_ASK_Detail.PartCode,_ask.EndTime)); } _t_ASK_Detail.GUID = Guid.NewGuid(); db.TB_ASK_DETAIL.Add(_t_ASK_Detail); _ls.Add(_t_ASK_Detail); // _number++; }); } else { _ret.MessageList.Add(string.Format("供应商编号{0}计划到货时间{1}范围内无价格表!",_entity.VendId,_entity.EndTime)); } } 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>> p_action) //{ // ResultObject> _ret = new ResultObject>(); // try // { // using (ScpEntities db = EntitiesFactory.CreateScpInstance()) // { // IQueryable 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 Save_TB_PLAN_CUSTOM(List p_entitys) //{ // ResultObject _ret = new ResultObject(); // 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 Del_TB_PLAN_CUSTOM(List p_entitys) //{ // ResultObject _ret = new ResultObject(); // 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 PLAN_TO_ASN(DataTable p_table) //{ // ResultObject _ret = new ResultObject(); // 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; //} } }