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", "UnitConvertDirection", "MainUnitRatio", "SecondUnitRatio","Goods_Spec" }, new string[] { detail.ProductCatalog_ID.ToString(), detail.CatalogDetail_ID.ToString(), detail.Goods_Name, detail.Goods_ID.ToString(), detail.UnitConvertDirection == null ? "null" : detail.UnitConvertDirection.ToString(), detail.MainUnitRatio == null ? "null" : detail.MainUnitRatio.ToString(), detail.SecondUnitRatio == null ? "null" : detail.SecondUnitRatio.ToString(),detail.Goods_Spec }); 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)); Delete(); //var oldpCatalogRowVersion = GetProductCatalogRowVersion(); var oldpCatalogRowVersion = new List(); 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.Get("ProductCatalog_ID"); catalogDetail.CatalogDetail_ID = detail.Get("ID"); catalogDetail.Goods_Name = detail.Get("Goods_Name"); catalogDetail.Goods_ID = detail.Get("Goods_ID"); if (detail.Get("UnitConvertDirection") != null) catalogDetail.UnitConvertDirection = detail.Get("UnitConvertDirection").ToString(); catalogDetail.MainUnitRatio = detail.Get("MainUnitRatio"); catalogDetail.SecondUnitRatio = detail.Get("SecondUnitRatio"); catalogDetail.Goods_Spec = detail.Get("Goods_Spec"); 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,Sequence from {0} order by Sequence asc".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(); catalog.Sequence = DataTypeUtil.GetIntNullNum(row[2]); 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,a.UnitConvertDirection,a.MainUnitRatio,a.SecondUnitRatio,a.Goods_Spec from {0} a left outer join {1} b on a.ProductCatalog_ID = b.ProductCatalog_ID and 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; catalog.UnitConvertDirection = DataTypeUtil.ToStringEmptyIfNull(row[5]); catalog.MainUnitRatio = DataTypeUtil.GetDecimalNullNum(row[6]); catalog.SecondUnitRatio = DataTypeUtil.GetDecimalNullNum(row[7]); catalog.Goods_Spec = DataTypeUtil.ToStringEmptyIfNull(row[8]); list.Add(catalog); } return list; } public static void UpdateProductCatalogDetails(IEnumerable selectedList, string database = TableNames.数据库) { bool success = true; using (var she = new SqlHelperEx(database)) { she.CreateTransaction(); string deleteSql = string.Format("delete from " + TableNames.客户端产品分类配制存货); she.ExecuteNonQuery(deleteSql, out success); if (selectedList.Count() > 0) { 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 static void Backup(IEnumerable goodslist) { bool success; string errorMessage; var selectedList = new List(); var table = SqlHelperEx.DoQuery("select ProductCatalog_ID, Sequence from {0}".FormatWith(TableNames.客户端产品分类) ); if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { var catalog = new ProductCatalog(); catalog.ProductCatalog_ID = int.Parse(row[0].ToString()); catalog.Sequence = DataTypeUtil.GetIntNullNum(row[1]); selectedList.Add(catalog); } } using (var she = new SqlHelperEx(TableNames.数据库_backup)) { she.CreateTransaction(); string deleteSql = string.Format("delete from " + TableNames.客户端产品分类); she.ExecuteNonQuery(deleteSql, out success); if (selectedList.Count() > 0) { foreach (var catalog in selectedList) { string insertSql = InsertUtil.GetInsertSql(TableNames.客户端产品分类, new string[] { "ProductCatalog_ID", "Name", "Sequence", "RowVersion" }, new string[] { catalog.ProductCatalog_ID.ToString(), catalog.ProductCatalog_ID.ToString(), catalog.Sequence.ToString(),"0" }); she.ExecuteNonQuery(insertSql, out success); if (!success) she.Rollback(); } } she.Commit(); } using (var she = new SqlHelperEx(TableNames.数据库_backup)) { she.CreateTransaction(); string deleteSql = string.Format("delete from " + TableNames.客户端产品分类配制存货); she.ExecuteNonQuery(deleteSql, out success); if (goodslist.Count() > 0) { foreach (var catalogDetail in goodslist) { 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 static void Restore() { var selectedList = new List(); var table = SqlHelperEx.DoQuery("select ProductCatalog_ID, Goods_ID from {0} b ".FormatWith( TableNames.客户端产品分类配制存货 ), TableNames.数据库_backup); if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { var catalog = new ProCataGoods(); catalog.ProductCatalog_ID = int.Parse(row[0].ToString()); catalog.Goods_ID = int.Parse(row[1].ToString()); selectedList.Add(catalog); } } var selectedList2 = new List(); var table2 = SqlHelperEx.DoQuery("select ProductCatalog_ID, Sequence from {0}".FormatWith(TableNames.客户端产品分类), TableNames.数据库_backup); if (table2.Rows.Count > 0) { foreach (DataRow row in table2.Rows) { var catalog = new ProductCatalog(); catalog.ProductCatalog_ID = int.Parse(row[0].ToString()); catalog.Sequence = DataTypeUtil.GetIntNullNum(row[1]); selectedList2.Add(catalog); } } using (var she = new SqlHelperEx(TableNames.数据库)) { she.CreateTransaction(); string deleteSql = string.Format("delete from " + TableNames.客户端产品分类配制存货); bool success; she.ExecuteNonQuery(deleteSql, out success); if (selectedList.Count() > 0) { 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(); } using (var she = new SqlHelperEx()) { she.CreateTransaction(); foreach (var catalog in selectedList2) { string updateSql = string.Format("update " + TableNames.客户端产品分类 + " set Sequence = {0} where ProductCatalog_ID ={1}", catalog.Sequence == null ? "null" : catalog.Sequence.ToString(), catalog.ProductCatalog_ID); bool success; she.ExecuteNonQuery(updateSql, out success); if (!success) { she.Rollback(); } } she.Commit(); } } public static void UpdateSequence(List catalogs) { foreach (var catalog in catalogs) { string updateSql = string.Format("update " + TableNames.客户端产品分类 + " set Sequence = {0} where ProductCatalog_ID ={1}", catalog.Sequence == null?"null":catalog.Sequence.ToString(), catalog.ProductCatalog_ID); bool success = true; using (var she = new SqlHelperEx()) { she.CreateTransaction(); she.ExecuteNonQuery(updateSql, out success); if (!success) { she.Rollback(); } else { 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 string Goods_Spec { get; set; } public long Goods_ID { get; set; } public string UnitConvertDirection { get; set; } public decimal? MainUnitRatio { get; set; } public decimal? SecondUnitRatio { 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; } }