using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Script.Serialization; using WebAPI.Models; using System.Globalization; using System.Threading; using System.Diagnostics; namespace WebAPI.Controllers.SCGL.日计划管理 { public class Xs_SendGoodsPlanBillController : ApiController { private json objJsonResult = new json(); public DataSet ds = new DataSet(); public WebServer webserver = new WebServer(); SQLHelper.ClsCN oCN = new SQLHelper.ClsCN(); Xs_SendGoodsPlanBillMain omdelMian = new Xs_SendGoodsPlanBillMain(); string BillType = "1423"; //List omodelsub = new List(); Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter(); #region 发货计划交期(三升导入) 文件上传 [Route("Xs_SendGoodsPlanBill/ExcelImport")] [HttpPost] public object ExcelImport() { try { //获取文件名称 var file = HttpContext.Current.Request.Files[0]; //获取文件物理路径 string ExcelPath = HttpContext.Current.Server.MapPath("~/" + file.FileName); //保存文件 file.SaveAs(ExcelPath); NpoiHelper np = new NpoiHelper(); DataSet ExcelDs = np.ReadExcel(ExcelPath, 1, 1, "0"); //删除文件 File.Delete(ExcelPath); //创建临时表 DataTable tb2 = new DataTable("dt2"); //添加列名 for (int i = 0; i < ExcelDs.Tables[0].Columns.Count; i++) { tb2.Columns.Add(ExcelDs.Tables[0].Rows[0][i].ToString()); } //模板缺少列 但需要从数据库中查询出来显示在页面的字段 tb2.Columns.Add("hmainid", typeof(Int32)); tb2.Columns.Add("HMaterID", typeof(Int32)); tb2.Columns.Add("SeOrderHInterID", typeof(Int32)); tb2.Columns.Add("SeOrderHEntryID", typeof(Int32)); //获取系统参数 string Ret = ""; oSystemParameter.ShowBill(ref Ret); LogService.Write("excel值:" + ExcelDs.Tables[0].Rows[1][1].ToString());//2024-11-26 //添加数据 for (int i = 1; i < ExcelDs.Tables[0].Rows.Count; i++) { DataRow row = tb2.NewRow(); for (int j = 0; j < ExcelDs.Tables[0].Columns.Count; j++) { row[j] = ExcelDs.Tables[0].Rows[i][j].ToString(); } //如果表格第i行的第一列为空,则判断为这一行的数据为空,跳出循环并且不把数据写入 tb2 if (ExcelDs.Tables[0].Rows[i][0].ToString() == "" && ExcelDs.Tables[0].Rows[i][1].ToString() == "") { continue; } else { tb2.Rows.Add(row); } } var error = ""; var ErrorResult = ""; //查询销售表判断有没有销售订单信息 for (int i = 0; i < tb2.Rows.Count; i++) { if (tb2.Rows[i]["发货日期"].ToString() == "") error += "发货日期不能为空;"; if (tb2.Rows[i]["客户订单号"].ToString() == "") error += "客户订单号不能为空;"; if (tb2.Rows[i]["物料编码"].ToString() == "") error += "物料编码不能为空;"; if (error.Length > 0) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = ErrorResult += $"Excel模板存在错误,行数{i + 1}:{error}\r\n"; ; objJsonResult.data = null; return objJsonResult; } //查找是否存在对应销售订单 string sql1 = string.Format("select * from h_v_Xs_SendGoodsPlanSANSTEXTList where F_SANS_TEXT='{0}' and HNumber='{1}'", tb2.Rows[i]["客户订单号"].ToString(), tb2.Rows[i]["物料编码"].ToString()); ds = oCN.RunProcReturn(sql1, "h_v_Xs_SendGoodsPlanSANSTEXTList"); if (ds.Tables[0].Rows.Count == 0) { error += $"没有符合条件得销售订单信息,客户订单号:{ tb2.Rows[i]["客户订单号"].ToString()},产品代码:{tb2.Rows[i]["物料编码"].ToString()}"; } if (error.Length > 0) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = ErrorResult += $"Excel模板存在错误,行数{i + 1}:{error}\r\n"; ; objJsonResult.data = null; return objJsonResult; } else { tb2.Rows[i]["HMaterID"] = ds.Tables[0].Rows[0]["HMaterID"].ToString(); tb2.Rows[i]["SeOrderHInterID"] = ds.Tables[0].Rows[0]["FID"].ToString();//销售订单主id tb2.Rows[i]["SeOrderHEntryID"] = ds.Tables[0].Rows[0]["FENTRYID"].ToString();//销售订单子id } //查找是否存在对应发货计划工单 var dt = oCN.RunProcReturn($"select * from Xs_SendGoodsPlanBillMain where HSeOrderInterID in({ds.Tables[0].Rows[0]["FID"].ToString()}) and HSeOrderEntryID in({ds.Tables[0].Rows[0]["FENTRYID"].ToString()})", "Xs_SendGoodsPlanBillMain"); if (dt.Tables[0].Rows.Count > 0) { tb2.Rows[i]["hmainid"] = dt.Tables[0].Rows[0]["HInterID"].ToString(); } } objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = ErrorResult; objJsonResult.data = tb2; return objJsonResult; } catch (Exception e) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "Exception!" + e.ToString(); objJsonResult.data = null; return objJsonResult; } } #endregion #region 发货计划三升交期 导入(保存) [Route("Xs_SendGoodsPlanBill/ExcelSave")] [HttpPost] public object ExcelSave([FromBody] JObject sMainSub) { var _value = sMainSub["sMainSub"].ToString(); string msg1 = _value.ToString(); oCN.BeginTran(); //保存主表 objJsonResult = AddBillMain_SS(msg1); if (objJsonResult.code == "0") { oCN.RollBack(); objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = objJsonResult.Message; objJsonResult.data = null; return objJsonResult; } oCN.Commit(); objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "单据保存成功!"; objJsonResult.data = null; return objJsonResult; } public json AddBillMain_SS(string msg1) { string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); string msg2 = sArray[0].ToString(); string msg3 = sArray[1].ToString(); string user = sArray[2].ToString(); try { //if (!DBUtility.ClsPub.Security_Log("Sc_WorkBillSortBill_Edit", 1, false, user)) //{ // objJsonResult.code = "0"; // objJsonResult.count = 0; // objJsonResult.Message = "无保存权限!"; // objJsonResult.data = null; // return objJsonResult; //} //表头字段 omdelMian = Newtonsoft.Json.JsonConvert.DeserializeObject(msg2); //JSON序列化转换字典集合 List> list = new List>(); List jb = JsonConvert.DeserializeObject>(msg3); foreach (JObject item in jb) { Dictionary dic = new Dictionary(); foreach (var itm in item.Properties()) { dic.Add(itm.Name, itm.Value.ToString()); } list.Add(dic); } for (int i = 0; i < list.Count; i++) { long HInterID = 0; var HBillNo = ""; if (list[i]["hmainid"].ToString() == "") { HBillNo = DBUtility.ClsPub.CreateBillCode_Prod("1423", ref DBUtility.ClsPub.sExeReturnInfo, true);//获得一个新的单据号 HInterID = DBUtility.ClsPub.CreateBillID_Prod("1423", ref DBUtility.ClsPub.sExeReturnInfo);//获得一个新的id var HMainSourceInterID = list[i]["SeOrderHInterID"].ToString(); var HMainSourceEntryID = list[i]["SeOrderHEntryID"].ToString(); var HMaterID = list[i]["HMaterID"].ToString(); var HSendDate = list[i]["发货日期"].ToString(); //获取对应订单信息 ds = oCN.RunProcReturn($"select * from h_v_Xs_SendGoodsPlanSANSTEXTList where FID='{HMainSourceInterID}' and FENTRYID='{HMainSourceEntryID}'", "h_v_Xs_SendGoodsPlanSANSTEXTList"); if (ds.Tables[0].Rows.Count == 0) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = $"没找到对应符合条件得订单信息,行数{i + 1}\r\n"; ; objJsonResult.data = null; return objJsonResult; } var HUnitID = ds.Tables[0].Rows[0]["HUnitID"].ToString(); var HQty = ds.Tables[0].Rows[0]["HQty"].ToString(); var HOrgID = ds.Tables[0].Rows[0]["FSALEORGID"].ToString(); var HMainSourceBillNo = ds.Tables[0].Rows[0]["FBILLNO"].ToString(); //主表 oCN.RunProc("insert into Xs_SendGoodsPlanBillMain(HInterID,HBillNo,HYear,HPeriod,HBillType," + "HBillSubType,HDate,HBillStatus,HMaker,HMakeDate,HOrgID,HMainSourceInterID,HMainSourceEntryID,HMainSourceBillNo,HSeOrderInterID," + "HSeOrderEntryID,HMaterID,HUnitID,HQty,HSendDate"+ ")values" + $"({HInterID},'{HBillNo}',{DateTime.Now.Year},{DateTime.Now.Month},'{BillType}'," + $"'{BillType}',GETDATE(),1,'{user}',GETDATE(),'{HOrgID}','{HMainSourceInterID}'," + $"{HMainSourceEntryID},'{HMainSourceBillNo}',{HMainSourceInterID}, {HMainSourceEntryID},{HMaterID}, {HUnitID}," + $" {(HQty == "" ? 0.ToString() : HQty)}, '{HSendDate}')"); }//如果已存在发货计划工单更新对应计划结束日期 else { var hmainid = list[i]["hmainid"].ToString(); var HSendDate = list[i]["发货日期"].ToString(); //更新交期 if (HSendDate != "") { oCN.RunProc($"update Xs_SendGoodsPlanBillMain set HSendDate = '{HSendDate}' where HInterID ={hmainid}"); } } } objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = null; objJsonResult.data = null; return objJsonResult; } catch (Exception e) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "Exception!" + e.ToString(); objJsonResult.data = null; return objJsonResult; } } #endregion #region 发货计划 删除 [Route("Xs_SendGoodsPlanBill/Delete")] [HttpGet] public object Delete(string HInterID,string User) { try { //删除权限 //if (!DBUtility.ClsPub.Security_Log_second("Sc_WorkBillSortBill_Drop", 1, false, User)) //{ // objJsonResult.code = "0"; // objJsonResult.count = 0; // objJsonResult.Message = "无权限删除!"; // objJsonResult.data = null; // return objJsonResult; //} //DataType 1=发货计划工单 多选删除 2=发货计划平台多选删除 var NumData = HInterID.Split(','); for (int i = 0; i < NumData.Length; i++) { var NumData_T = NumData[i].Split('_'); ds = oCN.RunProcReturn("select * from h_v_Xs_SendGoodsPlanBillList where 1=1 and hmainid=" + NumData_T[0], "h_v_Xs_SendGoodsPlanBillList"); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["单据状态"].ToString() != "1") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "当前单据不能删除!"; objJsonResult.data = null; return objJsonResult; } oCN.BeginTran();//开启事务 if (ds.Tables[0].Rows.Count == 1) { oCN.RunProc($"delete from Xs_SendGoodsPlanBillMain where HInterID={ NumData_T[0]}"); } oCN.Commit();//结束事务 } } objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "删除成功!"; objJsonResult.data = null; return objJsonResult; //else if (DataType == 2) //{ // var NumData = HInterID.Split(','); // //获取系统参数 // string sReturn = ""; // oSystemParameter.ShowBill(ref sReturn); // for (int i = 0; i < NumData.Length; i++) // { // ds = oCN.RunProcReturn("select * from h_v_JIT_Sc_WorkBillSortBillList_Left where 1=1 and hmainid=" + NumData[i], "h_v_JIT_Sc_WorkBillSortBillList"); // if (ds.Tables[0].Rows.Count > 0) // { // if (ds.Tables[0].Rows[0]["单据状态"].ToString() != "1") // { // objJsonResult.code = "0"; // objJsonResult.count = 0; // objJsonResult.Message = "当前单据不能删除!"; // objJsonResult.data = null; // return objJsonResult; // } // oCN.BeginTran();//开启事务 // oCN.RunProc($"delete from Sc_WorkBillSortBillMain where HInterID={ NumData[i]}"); // oCN.Commit();//结束事务 // } // } // objJsonResult.code = "1"; // objJsonResult.count = 1; // objJsonResult.Message = "删除成功!"; // objJsonResult.data = null; // return objJsonResult; //} objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "当前单据不存在,无法删除!"; objJsonResult.data = null; return objJsonResult; } catch (Exception e) { oCN.RollBack();//回滚事务 objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = e.ToString(); objJsonResult.data = null; return objJsonResult; } } #endregion #region 发货计划平台 保存 [Route("Xs_SendGoodsPlanBill/btnSave")] [HttpPost] public object btnSave([FromBody] JObject sMainSub) { var _value = sMainSub["sMainSub"].ToString(); string msg1 = _value.ToString(); oCN.BeginTran(); //保存主表 objJsonResult = AddBillMain(msg1); if (objJsonResult.code == "0") { oCN.RollBack(); objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = objJsonResult.Message; objJsonResult.data = null; return objJsonResult; } oCN.Commit(); objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "单据保存成功!"; objJsonResult.data = null; return objJsonResult; } public json AddBillMain(string msg1) { string[] sArray = msg1.Split(new string[] { "&和" }, StringSplitOptions.RemoveEmptyEntries); string msg2 = sArray[0].ToString(); string user = sArray[1].ToString(); try { Stopwatch sw = new Stopwatch();//计时器 sw.Start();//开始计时 //if (!DBUtility.ClsPub.Security_Log("Sc_WorkBillSortBill_Edit", 1, false, user)) //{ // objJsonResult.code = "0"; // objJsonResult.count = 0; // objJsonResult.Message = "无保存权限!"; // objJsonResult.data = null; // return objJsonResult; //} //JSON序列化转换字典集合 List> list = new List>(); List jb = JsonConvert.DeserializeObject>(msg2); foreach (JObject item in jb) { Dictionary dic = new Dictionary(); foreach (var itm in item.Properties()) { dic.Add(itm.Name, itm.Value.ToString()); } list.Add(dic); } string sReturn = ""; oSystemParameter.ShowBill(ref sReturn); for (int i = 0; i < list.Count; i++) { long HInterID = 0; var HBillNo = ""; if (list[i]["单据号"].ToString() == "" && list[i]["hmainid"].ToString() == "") { ds = oCN.RunProcReturn($"select * from Xs_SendGoodsPlanBillMain where HSeOrderInterID in({list[i]["HSeOrderInterID"].ToString()}) and HSeOrderEntryID in({list[i]["HSeOrderEntryID"].ToString()})", "Xs_SendGoodsPlanBillMain"); if (ds.Tables[0].Rows.Count > 0) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = $"第{i + 1}行销售订单有重复,请修改!"; objJsonResult.data = null; return objJsonResult; } //新增单据号 id HBillNo = DBUtility.ClsPub.CreateBillCode_Prod(BillType, ref DBUtility.ClsPub.sExeReturnInfo, true);//获得一个新的单据号 HInterID = DBUtility.ClsPub.CreateBillID_Prod(BillType, ref DBUtility.ClsPub.sExeReturnInfo);//获得一个新的id var HMainSourceInterID = list[i]["HSeOrderEntryID"].ToString(); var HMainSourceEntryID = list[i]["HSeOrderInterID"].ToString(); var HMaterID = list[i]["HMaterID"].ToString(); var HSendDate = list[i]["发货日期"].ToString(); var HOrgID = list[i]["HOrgID"].ToString(); var HUnitID = list[i]["HUnitID"].ToString(); var HQty = list[i]["发货数量"].ToString(); var HMainSourceBillNo = ds.Tables[0].Rows[0]["销售订单号"].ToString(); //主表 oCN.RunProc("insert into Xs_SendGoodsPlanBillMain(HInterID,HBillNo,HYear,HPeriod,HBillType," + "HBillSubType,HDate,HBillStatus,HMaker,HMakeDate,HOrgID,HMainSourceInterID,HMainSourceEntryID,HMainSourceBillNo,HSeOrderInterID," + "HSeOrderEntryID,HMaterID,HUnitID, HQty,HSendDate" + ")values" + $"({HInterID},'{HBillNo}',{DateTime.Now.Year},{DateTime.Now.Month},'{BillType}'," + $"'{BillType}',GETDATE(),1,'{user}',GETDATE(),'{HOrgID}','{HMainSourceInterID}'," + $"{HMainSourceEntryID},{HMainSourceBillNo},{HMainSourceInterID}, {HMainSourceEntryID},{HMaterID}, {HUnitID}," + $" {(HQty == "" ? 0.ToString() : HQty)}, '{HSendDate}')"); //LogService.Write("sql:" + sql); //主表 //oCN.RunProc(sql); } else { var hmainid = list[i]["hmainid"].ToString(); var HSendDate = list[i]["发货日期"].ToString(); var HQty = list[i]["发货数量"].ToString(); oCN.RunProc($"update Xs_SendGoodsPlanBillMain set HSendDate = '{HSendDate}',HQty={HQty} where HInterID ={hmainid}"); } } sw.Stop();//结束计时 objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = null; objJsonResult.data = null; return objJsonResult; } catch (Exception e) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "Exception!" + e.ToString(); objJsonResult.data = null; return objJsonResult; } } #endregion } }