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 ProductCatalogBL { 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 GetProductCatalogRowVersion() { var table = SqlHelperEx.DoQuery("select ProductCatalog_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 catalog = new ProductCatalog(); catalog.ProductCatalog_ID = (long)(int.Parse(row[0].ToString())); catalog.RowVersion = int.Parse(row[1].ToString()); list.Add(catalog); } return list; } public static void Insert(List productCatalogs, List catalogDetails) { using (var she = new SqlHelperEx()) { she.CreateTransaction(); bool success = false; string errorMessage; if (productCatalogs.Count() > 0) { foreach (var catalog in productCatalogs) { string insertSql = InsertUtil.GetInsertSql(TableNames.客户端产品分类, new string[] { "ProductCatalog_ID", "Name", "RowVersion" }, new string[] { catalog.ProductCatalog_ID.ToString(), catalog.Name, catalog.RowVersion.ToString() }); she.ExecuteNonQuery(insertSql, out success, out errorMessage); if (!success && !string.IsNullOrEmpty(errorMessage)) { she.Rollback(); throw new ApplicationException(errorMessage); } } } if (catalogDetails.Count() > 0) { foreach (var detail in catalogDetails) { string insertSql = InsertUtil.GetInsertSql(TableNames.客户端产品分类明细, new string[] { "ProductCatalog_ID", "CatalogDetail_ID", "Goods_Name", "Goods_ID" }, new string[] { detail.ProductCatalog_ID.ToString(), detail.CatalogDetail_ID.ToString(), detail.Goods_Name, detail.Goods_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 string Delete(long ID) { bool success; string errorMessage; using (var she = new SqlHelperEx()) { she.CreateTransaction(); var sql = SqlUtil.GetDeleteSql(TableNames.客户端产品分类, " where ProductCatalog_ID=" + ID.ToString()); she.ExecuteNonQuery(sql, out success, out errorMessage); if (success) { var sql2 = SqlUtil.GetDeleteSql(TableNames.客户端产品分类明细, " where ProductCatalog_ID=" + ID.ToString()); she.ExecuteNonQuery(sql2, out success, out errorMessage); } if (!success) she.Rollback(); else she.Commit(); } return errorMessage; } public static void SyncProductCatalog() { var domain_ID = ConfigurationManager.AppSettings["Domain_ID"]; var method = "/MainSystem/B3_HaoYue/Rpcs/B3CowButcherManageRpc/GetProductCatalogRowVersion"; if (domain_ID != null && !string.IsNullOrEmpty(domain_ID.ToString())) { var productCatalogRowVersion = RpcFacade.Call>>(method, long.Parse(domain_ID)); var oldpCatalogRowVersion = GetProductCatalogRowVersion(); var needInsertCatalogID = new List(); var needDeleteAndInsertCatalogID = new List(); if (productCatalogRowVersion.Count > 0) { if (oldpCatalogRowVersion.Count() <= 0) { productCatalogRowVersion.ForEach(x => needInsertCatalogID.Add(x.Item1)); } else { foreach (var catalogRowVersion in productCatalogRowVersion) { var catalog_ID = catalogRowVersion.Item1; var oldCatalogs = oldpCatalogRowVersion.Where(x => x.ProductCatalog_ID == catalog_ID); if (oldCatalogs.Count() > 0 && oldCatalogs.FirstOrDefault().RowVersion != catalogRowVersion.Item2) { needDeleteAndInsertCatalogID.Add(catalog_ID); } else if (oldCatalogs.Count() <= 0) { needInsertCatalogID.Add(catalog_ID); } } foreach (var oldVersion in oldpCatalogRowVersion) { if (!productCatalogRowVersion.Any(x => x.Item1 == oldVersion.ProductCatalog_ID)) { Delete(oldVersion.ProductCatalog_ID); } } } } else { Delete(); } if (needDeleteAndInsertCatalogID.Count() > 0) { foreach (var catalogID in needDeleteAndInsertCatalogID) { var error = Delete(catalogID.Value); if (!string.IsNullOrEmpty(error)) { throw new ApplicationException(error); } needInsertCatalogID.Add(catalogID); } } if (needInsertCatalogID.Count > 0) { var method2 = "/MainSystem/B3_HaoYue/Rpcs/B3CowButcherManageRpc/GetProductCatalogDetailData"; var catalogDetails = RpcFacade.Call>>(method2, needInsertCatalogID.ToArray()); var list = new List(); var detailList = new List(); foreach (var catalogID in needInsertCatalogID) { var catalog = new ProductCatalog(); catalog.ProductCatalog_ID = catalogID.Value; var result = productCatalogRowVersion.Where(x => x.Item1 == catalogID).First(); catalog.ProductCatalog_ID = result.Item1.Value; catalog.RowVersion = result.Item2.Value; catalog.Name = result.Item3; list.Add(catalog); } if (catalogDetails.Count() > 0) { foreach (var detail in catalogDetails) { var catalogDetail = new ProductCatalog_Detail(); catalogDetail.ProductCatalog_ID = detail.Item1.Value; catalogDetail.CatalogDetail_ID = detail.Item2.Value; catalogDetail.Goods_Name = detail.Item3; catalogDetail.Goods_ID = detail.Item4.Value; detailList.Add(catalogDetail); } } Insert(list, detailList); } } } 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) she.Rollback(); else she.Commit(); } return errorMessage; } public static List GetProductCatalogs() { var table = SqlHelperEx.DoQuery("select ProductCatalog_ID,Name from {0}".FormatWith(TableNames.客户端产品分类)); if (table.Rows.Count == 0) return new List(); var list = new List(); foreach (DataRow row in table.Rows) { var catalog = new ProductCatalog(); catalog.ProductCatalog_ID = (long)(int.Parse(row[0].ToString())); catalog.Name = row[1].ToString(); list.Add(catalog); } return list; } public static List GetProductCatalogDetails(bool? isSelected = null) { var table = SqlHelperEx.DoQuery("select a.ProductCatalog_ID,a.CatalogDetail_ID,a.Goods_Name,a.Goods_ID,b.Goods_ID from {0} a left outer join {1} b on a.Goods_ID = b.Goods_ID where 1=1 {2}".FormatWith(TableNames.客户端产品分类明细,TableNames.客户端产品分类配制存货, isSelected == true ? "and b.Goods_ID is not null" : string.Empty)); if (table.Rows.Count == 0) return new List(); var list = new List(); foreach (DataRow row in table.Rows) { var catalog = new ProCataGoods(); catalog.ProductCatalog_ID = (long)(int.Parse(row[0].ToString())); catalog.CatalogDetail_ID = (long)(int.Parse(row[1].ToString())); catalog.Goods_Name = row[2].ToString(); catalog.Goods_ID = (long)(int.Parse(row[3].ToString())); catalog.IsSelected = DataTypeUtil.GetLongNum(row[4])==0?false:true; list.Add(catalog); } return list; } public static void UpdateProductCatalogDetails(List selectedList) { bool success = true; using (var she = new SqlHelperEx()) { she.CreateTransaction(); string deleteSql = string.Format("delete from " + TableNames.客户端产品分类配制存货); she.ExecuteNonQuery(deleteSql, out success); foreach (var catalogDetail in selectedList) { string insertSql = InsertUtil.GetInsertSql(TableNames.客户端产品分类配制存货, new string[] { "ProductCatalog_ID","Goods_ID" }, new string[] { catalogDetail.ProductCatalog_ID.ToString(), catalogDetail.Goods_ID.ToString() }); she.ExecuteNonQuery(insertSql, out success); if (!success) she.Rollback(); } she.Commit(); } } } public class ProCataGoods : INotifyPropertyChanged { public long ProductCatalog_ID { get; set; } public long CatalogDetail_ID { get; set; } public string Goods_Name { get; set; } public long Goods_ID { get; set; } private bool mIsSelected = false; public bool IsSelected { get { return mIsSelected; } set { mIsSelected = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("IsSelected")); } } } public event PropertyChangedEventHandler PropertyChanged; } }