using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Linq; using System.Text; using FireBirdUtil.SqlUtils; using Forks.JsonRpc.Client; using Forks.JsonRpc.Client.Data; using Utils.Datas; using WeighBusiness.BO; using WeighBusiness.Utils; using WeighBusiness.Utils.SqlUtils; namespace WeighBusiness.BL { public static class ProductPlanBL { public static List GetAllLocalProduceBatchDetailID() { var sql = "select ProducePlan_ID from {0}".FormatWith(TableNames.生产计划); var table = SqlHelperEx.DoQuery(sql); if (table.Rows.Count == 0) return null; var detailIds = new List(); foreach (DataRow row in table.Rows) { detailIds.Add((long)((int)row[0])); } return detailIds; } public static List GetProductPlanRowVersion() { var table = SqlHelperEx.DoQuery("select ProductPlan_ID,RowVersion from {0}".FormatWith(TableNames.生产计划)); if (table.Rows.Count == 0) return new List(); var list = new List(); foreach (DataRow row in table.Rows) { var plan = new ProductPlan(); plan.ProductPlan_ID = (long)(int.Parse(row[0].ToString())); plan.RowVersion = int.Parse(row[1].ToString()); list.Add(plan); } return list; } public static void Insert(List plans, List butcherDetails,List batchs) { using (var she = new SqlHelperEx()) { she.CreateTransaction(); bool success = false; string errorMessage; if (plans.Count() > 0) { foreach (var plan in plans) { string insertSql = InsertUtil.GetInsertSql(TableNames.生产计划, new string[] { "ProductPlan_ID", "PlanDate", "RowVersion" }, new string[] { plan.ProductPlan_ID.ToString(), plan.PlanDate.ToString(), plan.RowVersion.ToString() }); she.ExecuteNonQuery(insertSql, out success, out errorMessage); if (!success && !string.IsNullOrEmpty(errorMessage)) { she.Rollback(); throw new ApplicationException(errorMessage); } } } if (butcherDetails.Count() > 0) { foreach (var detail in butcherDetails) { string insertSql = InsertUtil.GetInsertSql(TableNames.生产计划分割明细, new string[] { "ProductPlan_ID", "DetailID", "Goods_Code", "Goods_Name", "Goods_Spec", "Goods_MainUnit", "Goods_ID", "PlanNumber", "IsFinish", "Department_Name", "Remark" }, new string[] { detail.ProductPlan_ID.ToString(), detail.DetailID.ToString(), detail.Goods_Code, detail.Goods_Name, detail.Goods_Spec, detail.Goods_MainUnit, detail.Goods_ID.ToString(), detail.PlanNumber == null ? "null" : detail.PlanNumber.Value.ToString(),detail.IsFinish?"1":"0",detail.Department_Name,detail.Remark }); she.ExecuteNonQuery(insertSql, out success, out errorMessage); if (!success && !string.IsNullOrEmpty(errorMessage)) { she.Rollback(); throw new ApplicationException(errorMessage); } } } if (batchs.Count() > 0) { foreach (var batch in batchs) { string insertSql = InsertUtil.GetInsertSql(TableNames.生产计划生产批次, new string[] { "Batch_ID", "Batch", "BillDate", "RowVersion", "ProductPlan_ID" }, new string[] { batch.Batch_ID.ToString(), batch.Batch, batch.BillDate.ToString(), batch.RowVersion.ToString(), batch.ProductPlan_ID.ToString() }); she.ExecuteNonQuery(insertSql, out success, out errorMessage); if (!success && !string.IsNullOrEmpty(errorMessage)) { she.Rollback(); throw new ApplicationException(errorMessage); } } } if (!success) { she.Rollback(); } else { she.Commit(); } } } public static void UpdateButhcerDetailFinish(long detailId) { string updateSql = string.Format("update " + TableNames.生产计划分割明细 + " set IsFinish = 1 where DetailID ={0}", detailId); bool success = true; using (var she = new SqlHelperEx()) { she.CreateTransaction(); she.ExecuteNonQuery(updateSql, out success); if (!success) { she.Rollback(); } else { she.Commit(); } } } public static void UpdateButhcerDetailFinishNum(decimal? finishNum,decimal? detailID,long? goodsId, SqlHelperEx she2 = null) { string excuteSql = string.Empty; var table = SqlHelperEx.DoQuery("select ID from {0} where DetailID = {1}".FormatWith(TableNames.生产计划分割明细完工数量, detailID)); if (table.Rows.Count > 0) { excuteSql = string.Format("update " + TableNames.生产计划分割明细完工数量 + " set FinishNumber = {0} where DetailID ={1}", finishNum, detailID); } else { excuteSql = InsertUtil.GetInsertSql(TableNames.生产计划分割明细完工数量, new string[] { "DetailID", "Goods_ID", "FinishNumber" }, new string[] { detailID.ToString(), goodsId.ToString(), (finishNum ?? 0).ToString() }); } bool success = true; if (she2 == null) { using (var she = new SqlHelperEx()) { she.CreateTransaction(); she.ExecuteNonQuery(excuteSql, out success); if (!success) { she.Rollback(); } else { she.Commit(); } } } else { she2.ExecuteNonQuery(excuteSql, out success); if (!success) { she2.Rollback(); } } } public static string Delete(long ID) { bool success; string errorMessage; using (var she = new SqlHelperEx()) { she.CreateTransaction(); var sql = SqlUtil.GetDeleteSql(TableNames.生产计划, " where ProductPlan_ID=" + ID.ToString()); she.ExecuteNonQuery(sql, out success, out errorMessage); if (success) { var sql2 = SqlUtil.GetDeleteSql(TableNames.生产计划分割明细, " where ProductPlan_ID=" + ID.ToString()); she.ExecuteNonQuery(sql2, out success, out errorMessage); } if (success) { var sql2 = SqlUtil.GetDeleteSql(TableNames.生产计划生产批次, " where ProductPlan_ID=" + ID.ToString()); she.ExecuteNonQuery(sql2, out success, out errorMessage); } if (!success) she.Rollback(); else she.Commit(); } return errorMessage; } public static void SyncProductPlan() { var domain_ID = ConfigurationManager.AppSettings["Domain_ID"]; var accountingUnit_ID = ConfigurationManager.AppSettings["AccountingUnit_ID"]; var method = "/MainSystem/B3_HaoYue/Rpcs/B3CowButcherManageRpc/GetProductPlanRowVersion"; if (domain_ID != null && !string.IsNullOrEmpty(domain_ID.ToString()) && accountingUnit_ID != null && !string.IsNullOrEmpty(accountingUnit_ID.ToString())) { var productPlanRowVersions = RpcFacade.Call>>(method, long.Parse(domain_ID), long.Parse(accountingUnit_ID)); var oldproductPlanRowVersions = GetProductPlanRowVersion(); var needInsertProductPlanID = new List(); var needDeleteAndInsertProductPlanID = new List(); if (productPlanRowVersions.Count > 0) { if (oldproductPlanRowVersions.Count() <= 0) { productPlanRowVersions.ForEach(x => needInsertProductPlanID.Add(x.Item1)); } else { foreach (var planRowVersion in productPlanRowVersions) { var plan_ID = planRowVersion.Item1; var oldProductPlans = oldproductPlanRowVersions.Where(x => x.ProductPlan_ID == plan_ID); if (oldProductPlans.Count() > 0 && oldProductPlans.FirstOrDefault().RowVersion != planRowVersion.Item2) { needDeleteAndInsertProductPlanID.Add(planRowVersion.Item1); } else if (oldProductPlans.Count() <= 0) { needInsertProductPlanID.Add(plan_ID); } } foreach (var oldVersion in oldproductPlanRowVersions) { if (!productPlanRowVersions.Any(x => x.Item1 == oldVersion.ProductPlan_ID)) { Delete(oldVersion.ProductPlan_ID); } } } } else { Delete(); } if (needDeleteAndInsertProductPlanID.Count() > 0) { foreach (var productPlanID in needDeleteAndInsertProductPlanID) { var error = Delete(productPlanID.Value); if (!string.IsNullOrEmpty(error)) { throw new ApplicationException(error); } needInsertProductPlanID.Add(productPlanID); } } if (needInsertProductPlanID.Count > 0) { var getButcherDetail = "/MainSystem/B3_HaoYue/Rpcs/B3CowButcherManageRpc/GetProducePlanButcherDetailData"; var butcherDetails = RpcFacade.Call>(getButcherDetail, needInsertProductPlanID.ToArray()); var list = new List(); var butcherList = new List(); var batchList = new List(); foreach (var planID in needInsertProductPlanID) { var plan = new ProductPlan(); plan.ProductPlan_ID = planID.Value; var result = productPlanRowVersions.Where(x => x.Item1 == planID).First(); plan.RowVersion = result.Item2.Value; plan.PlanDate = result.Item3.Value; list.Add(plan); } if (butcherDetails.Count() > 0) { foreach (var detail in butcherDetails) { var butcherDetail = new ButcherDetail(); butcherDetail.ProductPlan_ID = detail.Get("ProductPlan_ID"); butcherDetail.DetailID = detail.Get("DetailID"); butcherDetail.Goods_Code = detail.Get("Goods_Code"); butcherDetail.Goods_Name = detail.Get("Goods_Name"); butcherDetail.Goods_Spec = detail.Get("Goods_Spec"); butcherDetail.Goods_MainUnit = detail.Get("Goods_MainUnit"); butcherDetail.Goods_ID = detail.Get("Goods_ID"); butcherDetail.PlanNumber = detail.Get("PlanNumber"); butcherDetail.Department_Name = detail.Get("Department_Name"); butcherDetail.Remark = detail.Get("Remark"); butcherList.Add(butcherDetail); } } string method3 = "/MainSystem/B3_HaoYue/Rpcs/FourSplitBodyWeightClient/GetProductBatchData"; var produceBatchs = RpcFacade.Call>>(method3, needInsertProductPlanID.ToArray()); if (produceBatchs.Count() > 0) { foreach (var batch in produceBatchs) { var pBatch = new ProductBatch(); pBatch.Batch = batch.Item1; pBatch.Batch_ID = batch.Item2.Value; pBatch.RowVersion = batch.Item3.Value; pBatch.BillDate = batch.Item4.Value; pBatch.ProductPlan_ID = batch.Item5.Value; batchList.Add(pBatch); } } Insert(list, butcherList, batchList); } } } public static List GetProductBatch(DateTime date,DateTime startDate,bool isLoad) { var list = new List(); var querySql = string.Empty; if (!isLoad) { querySql = "select Batch_ID,Batch from {0} where BillDate >='{1}' and BillDate < '{2}'".FormatWith(TableNames.生产计划生产批次, startDate, date.AddDays(1)); } else { querySql = "select Batch_ID,Batch from {0}".FormatWith(TableNames.生产计划生产批次, startDate.AddDays(1)); } var table = SqlHelperEx.DoQuery(querySql); if (table.Rows.Count == 0) return new List(); foreach (DataRow row in table.Rows) { var batch = new ProductBatch(); batch.Batch_ID = DataTypeUtil.GetLongNum(row[0]); batch.Batch = row[1].ToString(); list.Add(batch); } return list; } public static string Delete() { bool success; string errorMessage; using (var she = new SqlHelperEx()) { she.CreateTransaction(); var sql = SqlUtil.GetDeleteSql(TableNames.生产计划); she.ExecuteNonQuery(sql, out success, out errorMessage); if (success) { var sql3 = SqlUtil.GetDeleteSql(TableNames.生产计划分割明细); she.ExecuteNonQuery(sql3, out success, out errorMessage); } if (success) { var sql3 = SqlUtil.GetDeleteSql(TableNames.生产计划生产批次); she.ExecuteNonQuery(sql3, out success, out errorMessage); } if (!success) she.Rollback(); else she.Commit(); } return errorMessage; } public static List GetButcherDetails(DateTime date,DateTime startDate,long planTeamID) { var list = new List(); var sql = "select a.Goods_ID, a.Goods_Name,a.Goods_Spec,a.Goods_MainUnit,a.PlanNumber,a.DetailID,e.FinishNumber,a.Department_Name,a.Remark from {0} a inner join {1} b on a.ProductPlan_ID = b.ProductPlan_ID inner join {2} c on c.Goods_ID = a.Goods_ID inner join {3} d on c.ProductTeam_ID = d.ProductTeam_ID left outer join {4} e on a.DetailID = e.DetailID where a.IsFinish = 0 and d.ProductTeam_ID = {5}".FormatWith(TableNames.生产计划分割明细, TableNames.生产计划, TableNames.生产班组产出明细, TableNames.生产班组, TableNames.生产计划分割明细完工数量,planTeamID); if (!string.IsNullOrEmpty(sql)) { var table = SqlHelperEx.DoQuery(sql); foreach (DataRow row in table.Rows) { var outDetail = new ButcherDetailData(); outDetail.Goods_ID = (long)((int)row[0]); outDetail.Goods_Name = (string)row[1]; outDetail.Goods_Spec = (string)row[2]; outDetail.Goods_MainUnit = (string)row[3]; outDetail.PlanNumber = DataTypeUtil.GetDecimalNum(row[4]); outDetail.DetailID = (long)((int)row[5]); outDetail.FinishNum = DataTypeUtil.GetDecimalNum(row[6]); outDetail.Department_Name = (string)row[7]; outDetail.Remark = (string)row[8]; list.Add(outDetail); } } return list; } } public class ButcherDetailData : INotifyPropertyChanged { public long DetailID { get; set; } public long Goods_ID { get; set; } public string Goods_Name { get; set; } public string Goods_Spec { get; set; } public string Goods_MainUnit { get; set; } public decimal? PlanNumber { get; set; } public string Department_Name { get; set; } public string Remark { get; set; } private decimal? mFinishNum; public decimal? FinishNum { get { return mFinishNum; } set { mFinishNum = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("FinishNum")); } } } private decimal? mSurplusNum; public decimal? SurplusNum { get { return mSurplusNum; } set { mSurplusNum = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("SurplusNum")); } } } public event PropertyChangedEventHandler PropertyChanged; } }