using System; using System.Collections.Generic; 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 ProductTeamBL { public static List GetProductTeamRowVersion() { var table = SqlHelperEx.DoQuery("select ProductTeam_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 ProductTeam(); plan.ProductTeam_ID = (long)(int.Parse(row[0].ToString())); plan.RowVersion = int.Parse(row[1].ToString()); list.Add(plan); } return list; } public static void Insert(List teams, List outDetails) { using (var she = new SqlHelperEx()) { she.CreateTransaction(); bool success = false; string errorMessage; if (teams.Count() > 0) { foreach (var team in teams) { string insertSql = InsertUtil.GetInsertSql(TableNames.生产班组, new string[] { "ProductTeam_ID", "Name", "ProduceType", "Employee_ID", "RowVersion", "ClientNode" }, new string[] { team.ProductTeam_ID.ToString(), team.Name, team.ProduceType, team.Employee_ID == null ? "null" : team.Employee_ID.Value.ToString(), team.RowVersion.ToString(), team.ClientNode }); she.ExecuteNonQuery(insertSql, out success, out errorMessage); if (!success && !string.IsNullOrEmpty(errorMessage)) { she.Rollback(); throw new ApplicationException(errorMessage); } } } if (outDetails.Count() > 0) { foreach (var detail in outDetails) { string insertSql = InsertUtil.GetInsertSql(TableNames.生产班组产出明细, new string[] { "ProductTeam_ID", "Goods_ID", "DetailID" }, new string[] { detail.ProductTeam_ID.ToString(), detail.Goods_ID.ToString(), detail.DetailID.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 ProductTeam_ID=" + ID.ToString()); she.ExecuteNonQuery(sql, out success, out errorMessage); if (success) { var sql3 = SqlUtil.GetDeleteSql(TableNames.生产班组产出明细, " where ProductTeam_ID=" + ID.ToString()); she.ExecuteNonQuery(sql3, out success, out errorMessage); } if (!success) she.Rollback(); else she.Commit(); } return errorMessage; } public static void SyncProductTeam() { var domain_ID = ConfigurationManager.AppSettings["Domain_ID"]; var method = "/MainSystem/B3_HaoYue/Rpcs/B3CowButcherManageRpc/GetProductTeamRowVersion2"; if (domain_ID != null && !string.IsNullOrEmpty(domain_ID)) { var productTeamRowVersions = RpcFacade.Call>>(method, long.Parse(domain_ID)); var oldProductTeamRowVersions = GetProductTeamRowVersion(); var needInsertProductTeamID = new List(); var needDeleteAndInsertProductTeamID = new List(); if (productTeamRowVersions.Count > 0) { if (oldProductTeamRowVersions.Count() <= 0) { productTeamRowVersions.ForEach(x => needInsertProductTeamID.Add(x.Item1)); } else { foreach (var teamRowVersion in productTeamRowVersions) { var team_ID = teamRowVersion.Item1; var oldProductTeams = oldProductTeamRowVersions.Where(x => x.ProductTeam_ID == team_ID); if (oldProductTeams.Count() > 0 && oldProductTeams.FirstOrDefault().RowVersion != teamRowVersion.Item2) { needDeleteAndInsertProductTeamID.Add(teamRowVersion.Item1); } else if (oldProductTeams.Count() <= 0) { needInsertProductTeamID.Add(team_ID); } } foreach (var oldVersion in oldProductTeamRowVersions) { if (!productTeamRowVersions.Any(x => x.Item1 == oldVersion.ProductTeam_ID)) { Delete(oldVersion.ProductTeam_ID); } } } } else { Delete(); } if (needDeleteAndInsertProductTeamID.Count() > 0) { foreach (var productTeamID in needDeleteAndInsertProductTeamID) { var error = Delete(productTeamID.Value); if (!string.IsNullOrEmpty(error)) { throw new ApplicationException(error); } needInsertProductTeamID.Add(productTeamID); } } if (needInsertProductTeamID.Count > 0) { var getProductTeamOutDetail = "/MainSystem/B3_HaoYue/Rpcs/B3CowButcherManageRpc/GetProductTeamOutDetailData"; var productTeamOutDetails = RpcFacade.Call>>(getProductTeamOutDetail, needInsertProductTeamID.ToArray()); var list = new List(); var outList = new List(); foreach (var teamID in needInsertProductTeamID) { var team = new ProductTeam(); team.ProductTeam_ID = teamID.Value; var result = productTeamRowVersions.Where(x => x.Item1 == teamID).First(); team.RowVersion = result.Item2.Value; team.Name = result.Item3; team.ProduceType = result.Item4; team.Employee_ID = result.Item5; team.ClientNode = result.Item6; list.Add(team); } if (productTeamOutDetails.Count() > 0) { foreach (var detail in productTeamOutDetails) { var outDetail = new ProductTeamOutDetail(); outDetail.ProductTeam_ID = detail.Item1.Value; outDetail.Goods_ID = detail.Item2.Value; outDetail.DetailID = detail.Item3.Value; outList.Add(outDetail); } } Insert(list, outList); } } } 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 sql2 = SqlUtil.GetDeleteSql(TableNames.生产班组产出明细); she.ExecuteNonQuery(sql2, out success, out errorMessage); } if (!success) she.Rollback(); else she.Commit(); } return errorMessage; } public static ProductTeam GetProductTeam(string clientNode) { var table = SqlHelperEx.DoQuery("select ProductTeam_ID,Name,ProduceType from {0} where ClientNode = {1}".FormatWith(TableNames.生产班组, clientNode == null ? "null" : string.Format("'{0}'", clientNode))); if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { var team = new ProductTeam(); team.ProductTeam_ID = (long)(int.Parse(row[0].ToString())); team.Name = row[1].ToString(); team.ProduceType = row[2].ToString(); return team; } } return null; } } }