using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.Http; using WebAPI.Models; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace WebAPI.Controllers.SCGL.日计划管理 { public class JIT_DayPlanPlatFormBillController : ApiController { private json objJsonResult = new json(); public DataSet ds = new DataSet(); public WebServer webserver = new WebServer(); SQLHelper.ClsCN oCN = new SQLHelper.ClsCN(); Sc_WorkBillSortBillMain omdelMian = new Sc_WorkBillSortBillMain(); public static string BillType = "4610"; #region 生产日计划平台 查询 [Route("JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBillList")] [HttpGet] public object JIT_DayPlanPlatFormBillList(string sWhere,string user) { try { //查看权限 if (!DBUtility.ClsPub.Security_Log("Sc_WorkBillSortBill_Query", 1, false, user)) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "无查看权限!"; objJsonResult.data = null; return objJsonResult; } omdelMian = JsonConvert.DeserializeObject(sWhere); ds = oCN.RunProcReturn($"exec h_p_JIT_Sc_WorkBillSortBill_Query '{omdelMian.HPlanBeginDate}',{(omdelMian.HProdORGID==null?0: omdelMian.HProdORGID)},'{omdelMian.HSeOrderBillNo}'" + $",'{omdelMian.HPlanEndDate.AddDays(1).AddSeconds(-1)}',{omdelMian.HMaterID},'{omdelMian.HICMOBillNo}',{omdelMian.HWorkShopID}", "h_p_JIT_Sc_WorkBillSortBill_Query"); objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "Sucess!"; objJsonResult.data = ds.Tables[0]; return objJsonResult; } catch (Exception e) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "Exception!" + e.ToString(); objJsonResult.data = null; return objJsonResult; } } #endregion #region 生产日计划平台 保存 [Route("JIT_DayPlanPlatFormBill/JIT_DayPlanPlatFormBill_btnSave")] [HttpPost] public object JIT_DayPlanPlatFormBill_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 { 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); } int TrueCount = 0, SumCount = 0; for (int i = 0; i < list.Count; i++) { TrueCount = 0; SumCount = 0; long HInterID = 0; var HBillNo = ""; if (list[i]["单据号"].ToString() == "" && list[i]["hmainid"].ToString() == "") { var HICMOBillNo = list[i]["生产订单号"].ToString(); var HOrderType = list[i]["订单类型"].ToString(); var HICMOEntrySEQ = list[i]["生产订单明细行号"].ToString(); var HWorkShopID = list[i]["HWorkShopID"].ToString(); var HSourceID = list[i]["HSourceID"].ToString(); var HYX = list[i]["优先级"].ToString(); var HProdORGID = list[i]["HProdORGID"].ToString(); var HMaterID = list[i]["HMaterID"].ToString(); var HMaterName = list[i]["物料名称"].ToString(); var HMaterModel = list[i]["规格型号"].ToString(); var HUnitID = list[i]["HUnitID"].ToString(); var HSeOrderBillQty = list[i]["销售订单数量"].ToString(); var HOrderNeedQty = list[i]["订单需求数量"].ToString(); var HSplitQty = list[i]["拆单数量"].ToString(); var HDayPlanSumQty = list[i]["日计划数量总量"].ToString(); var HPlanBeginDate = list[i]["计划开始日期"].ToString(); var HSeOrderBillNo = list[i]["销售订单号"].ToString(); var HICMOBillType = list[i]["生产订单单据类型"].ToString(); var HSourceStockInQty = list[i]["产线入库数量"].ToString(); var HLeftPlanQty = list[i]["未排数量"].ToString(); var HOrderLev = list[i]["订单等级"].ToString(); var HMainSourceInterID = list[i]["源单主内码"].ToString(); var HMainSourceEntryID = list[i]["源单子内码"].ToString(); if (HSourceID=="") { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = $"第{i+1}行生产资源不能为空!"; objJsonResult.data = null; return objJsonResult; } ds = oCN.RunProcReturn($"select * from Sc_WorkBillSortBillMain where HMainSourceEntryID={HMainSourceEntryID}" + $" and HMainSourceEntryID={HMainSourceEntryID} and HICMOBillNo='{HICMOBillNo}' and HICMOEntrySEQ={HICMOEntrySEQ} and HSourceID={HSourceID}", "Sc_WorkBillSortBillMain"); 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 //主表 oCN.RunProc("insert into Sc_WorkBillSortBillMain(HInterID,HBillNo,HYear,HPeriod,HBillType," + "HBillSubType,HDate,HBillStatus,HMaker,HMakeDate,HICMOBillNo,HOrderType," + "HICMOEntrySEQ,HWorkShopID,HSourceID, HYX, HProdORGID," + " HMaterID, HMaterName, HMaterModel, HUnitID, HSeOrderBillQty," + " HOrderNeedQty, HSplitQty, HDayPlanSumQty,HPlanBeginDate," + "HSeOrderBillNo,HICMOBillType,HSourceStockInQty,HLeftPlanQty,HOrderLev,HPreparatDate)values" + $"({HInterID},'{HBillNo}',{DateTime.Now.Year},{DateTime.Now.Month},'{BillType}'," + $"'{BillType}',GETDATE(),1,'{user}',getdate(),'{HICMOBillNo}','{HOrderType}'," + $"{(HICMOEntrySEQ == "" ? 0.ToString() : HICMOEntrySEQ)},{(HWorkShopID == "" ? 0.ToString() : HWorkShopID)},{(HSourceID == "" ? 0.ToString() : HSourceID)}, {(HYX == "" ? 0.ToString() : HYX)}, {(HProdORGID == "" ? 0.ToString() : HProdORGID)}," + $" {(HMaterID == "" ? 0.ToString() : HMaterID)}, '{HMaterName}', '{HMaterModel}', {(HUnitID == "" ? 0.ToString() : HUnitID)}, {(HSeOrderBillQty == "" ? 0.ToString() : HSeOrderBillQty)}," + $" {(HOrderNeedQty == "" ? 0.ToString() : HOrderNeedQty)}, {(HSplitQty == "" ? 0.ToString() : HSplitQty)}, {(HDayPlanSumQty == "" ? 0.ToString() : HDayPlanSumQty)},'{HPlanBeginDate}'," + $"'{HSeOrderBillNo}','{HICMOBillType}',{(HSourceStockInQty == "" ? 0.ToString() : HSourceStockInQty)},{(HLeftPlanQty == "" ? 0.ToString() : HLeftPlanQty)},'{HOrderLev}',getdate())"); } int SumDay = 31; //动态两月之差 DateTime.Now.AddMonths(1).AddDays(-1).Subtract(DateTime.Now).Days; for (int j = 0; j < SumDay; j++) { //主表子表都有数据 if (list[i][DateTime.Now.AddDays(j).ToString("yyyy-MM-dd")].ToString() != "") { SumCount += 1; ////保存子表 objJsonResult = AddBillSub(HInterID == 0 ? list[i]["hmainid"].ToString() : HInterID.ToString() , HBillNo == "" ? list[i]["单据号"].ToString() : HBillNo , DateTime.Parse(DateTime.Now.AddDays(j).ToString("yyyy-MM-dd").ToString()) , int.Parse(list[i][DateTime.Now.AddDays(j).ToString("yyyy-MM-dd")].ToString())); if (objJsonResult.count == 1) { TrueCount += 1; } } //主表有数据 子表无数据 if (j == 30 && SumCount == 0 && HInterID != 0 && HBillNo != "") { objJsonResult = AddBillSub(HInterID.ToString(), HBillNo, DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd").ToString()), 0); } } } if (TrueCount != SumCount) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = objJsonResult.Message; objJsonResult.data = null; return objJsonResult; } 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; } } public json AddBillSub(string HInterID, string HBillNo, DateTime date, int HQTY) { //获取表格数据 ds = oCN.RunProcReturn($"select * from h_v_JIT_Sc_WorkBillSortBillList where 单据号='{HBillNo}'", "h_v_JIT_Sc_WorkBillSortBillList"); int count = 0; if (ds.Tables[0].Rows.Count > 0) { //判断子表是否有当日的日期以及日期所对应的数量 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if (DateTime.Parse(ds.Tables[0].Rows[i]["日计划生产日期"].ToString()) == date) { oCN.RunProc($"update Sc_WorkBillSortBillSub set HMasterDate='{date}',HQty={HQTY}" + $" where HInterID={HInterID} and HEntryID={ds.Tables[0].Rows[i]["hsubid"].ToString()}"); count += 1; } } } if (count != 1) { //插入子表 oCN.RunProc("insert into Sc_WorkBillSortBillSub(HInterID,HEntryID," + "HMasterDate,HQty)" + $"values({HInterID}, {(ds.Tables[0].Rows.Count + 1)}," + $" '{date}', {HQTY})"); } objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = null; objJsonResult.data = null; return objJsonResult; } #endregion } }