You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
15 KiB

using System;
using System.Collections.Generic;
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 BomSetBL
{
public static List<BomSet> GetBomSetRowVersion()
{
return LocalQueryUtil.GetBomSetRowVersion();
}
public class BomSetData
{
public long BomSet_ID { get; set; }
public long BomSet_Detail_ID { get; set; }
public decimal? Number { get; set; }
public long? Goods_ID { get; set; }
public decimal? DetailNumber { get; set; }
public InputStyle? InputStyle { get; set; }
private List<Tuple<decimal?, decimal?, decimal?>> mNumbers = new List<Tuple<decimal?, decimal?, decimal?>>();
public List<Tuple<decimal?, decimal?,decimal?>> Numbers { get { return mNumbers; } set { mNumbers = value; } }
}
static List<BomSetData> resultList = new List<BomSetData>();
private static List<BomSetData> GetDetails(BomSetData setData)
{
var unitPrecentStr = string.Empty;
var sql = string.Format("select b.Goods_ID,b.InputStyle,b.Number,a.Number,b.FixedNumber from BomSet a inner join BSDetail b on a.BomSet_ID = b.BomSet_ID where a.BomSet_ID = {1}", unitPrecentStr, setData.BomSet_ID);
var table = SqlHelperEx.DoQuery(sql);
if (table.Rows.Count == 0)
return new List<BomSetData>();
var list = new List<BomSetData>();
foreach (DataRow row in table.Rows) {
var bomSetData = new BomSetData();
bomSetData.Goods_ID = (long)(int.Parse(row[0].ToString()));
var inputStyle = row[1].ToString();
if (!string.IsNullOrEmpty(inputStyle)) {
var num = int.Parse(inputStyle);
if (num == 1) {
bomSetData.InputStyle = InputStyle.;
list.Add(bomSetData);
} else if (num == 2) {
bomSetData.InputStyle = InputStyle.;
}
}
var detailNumber = DataTypeUtil.GetDecimalNullNum(row[2]);
var number = DataTypeUtil.GetDecimalNullNum(row[3]);
var fixedNumber = DataTypeUtil.GetDecimalNullNum(row[4]);
if (setData.Numbers.Count() > 0) {
foreach (var item in setData.Numbers) {
bomSetData.Numbers.Add(new Tuple<decimal?, decimal?, decimal?>(item.Item1, item.Item2,item.Item3));
}
}
bomSetData.Numbers.Add(new Tuple<decimal?, decimal?,decimal?>(number, detailNumber,fixedNumber));
resultList.Add(bomSetData);
}
return list;
}
private static void GetDetailsByPackage(List<BomSetData> datalist)
{
var sql = string.Format("select BomSet_ID,Goods_ID from BSApplyDetail where Goods_ID in ({0})",string.Join(",",datalist.Select(x=>x.Goods_ID)));
var table = SqlHelperEx.DoQuery(sql);
var list = new List<BomSetData>();
if (table.Rows.Count > 0) {
foreach (DataRow row in table.Rows) {
var bomSetData = new BomSetData();
bomSetData.BomSet_ID = (long)(int.Parse(row[0].ToString()));
bomSetData.Goods_ID = (long)(int.Parse(row[1].ToString()));
var result = datalist.Where(x => x.Goods_ID == bomSetData.Goods_ID);
if (result != null && result.Count() > 0) {
foreach (var item in result.FirstOrDefault().Numbers) {
bomSetData.Numbers.Add(new Tuple<decimal?,decimal?,decimal?>(item.Item1,item.Item2,item.Item3));
}
}
list.Add(bomSetData);
}
}
if (list.Count > 0) {
for (int i = 0; i < list.Count; i++) {
if (list[i] != null) {
var details = GetDetails(list[i]);
if (details.Count > 0) {
GetDetailsByPackage(details);
}
}
}
}
}
public static List<BomSetData> GetBomSet(long bomSet_ID, params long[] Goods_IDs)
{
var backList = new List<BomSetData>();
resultList.Clear();
if(Goods_IDs.Length <= 0)
return new List<BomSetData>();
var list = GetDetails(new BomSetData() { BomSet_ID = bomSet_ID });
if (list.Count > 0) {
GetDetailsByPackage(list);
}
for (var i = 0; i < Goods_IDs.Length; i++) {
var result = resultList.Where(x => x.Goods_ID == Goods_IDs[i]);
if (result != null && result.Count() > 0) {
backList.Add(result.FirstOrDefault());
}
}
return backList;
}
public static void Insert(List<BomSet> bomSets, List<BomSet_Detail> bomSet_Details, List<BomSet_ApplyDetail> bomSet_ApplyDetails)
{
using (var she = new SqlHelperEx()) {
she.CreateTransaction();
bool success = false;
string errorMessage;
if (bomSets != null && bomSets.Count() > 0) {
foreach (var bomSet in bomSets) {
string insertSql = InsertUtil.GetInsertSql(TableNames.BOM设置,
new string[] { "BomSet_ID", "BomSet_Date", "Number", "RowVersion" },
new string[] { bomSet.BomSet_ID.ToString(), bomSet.Date == null ? "null" : bomSet.Date.Value.ToString(), bomSet.Number == null ? "null" : bomSet.Number.ToString(), bomSet.RowVersion.ToString() });
she.ExecuteNonQuery(insertSql, out success, out errorMessage);
if (!success && !string.IsNullOrEmpty(errorMessage)) {
she.Rollback();
throw new ApplicationException(errorMessage);
}
}
}
if (bomSet_Details != null && bomSet_Details.Count() > 0) {
foreach (var bomSet_Detail in bomSet_Details) {
string insertSql = InsertUtil.GetInsertSql(TableNames.BOM清单,
new string[] { "BomSet_ID", "BomSet_Detail_ID", "Number", "Goods_ID", "InputStyle", "UnitPrecent", "FixedNumber" },
new string[] { bomSet_Detail.BomSet_ID.ToString(), bomSet_Detail.BomSet_Detail_ID.ToString(), bomSet_Detail.Number == null ? "null" : bomSet_Detail.Number.ToString(), bomSet_Detail.Goods_ID.ToString(), bomSet_Detail.InputStyle == null ? "null" : ((int)(bomSet_Detail.InputStyle)).ToString(), bomSet_Detail.UnitPrecent == null ? "null" : bomSet_Detail.UnitPrecent.ToString(), bomSet_Detail.FixedNumber == null ? "null" : bomSet_Detail.FixedNumber.ToString() });
she.ExecuteNonQuery(insertSql, out success, out errorMessage);
if (!success && !string.IsNullOrEmpty(errorMessage)) {
she.Rollback();
throw new ApplicationException(errorMessage);
}
}
}
if (bomSet_ApplyDetails != null && bomSet_ApplyDetails.Count() > 0) {
foreach (var bomSet_ApplyDetail in bomSet_ApplyDetails) {
string insertSql = InsertUtil.GetInsertSql(TableNames.BOM产品清单,
new string[] { "BomSet_ID", "BomSet_Detail_ID", "Goods_ID" },
new string[] { bomSet_ApplyDetail.BomSet_ID.ToString(), bomSet_ApplyDetail.BomSet_Detail_ID.ToString(), bomSet_ApplyDetail.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();
}
}
}
private static bool ExcuteSql(string sql)
{
bool success;
using (var she = new SqlHelperEx()) {
she.CreateTransaction();
she.ExecuteNonQuery(sql, out success);
if (!success)
she.Rollback();
else
she.Commit();
}
return success;
}
public static string Delete(long ID)
{
bool success;
string errorMessage;
using (var she = new SqlHelperEx()) {
she.CreateTransaction();
var sql = SqlUtil.GetDeleteSql(TableNames.BOM设置, " where BomSet_ID=" + ID.ToString());
she.ExecuteNonQuery(sql, out success,out errorMessage);
if (success) {
var sql2 = SqlUtil.GetDeleteSql(TableNames.BOM清单, " where BomSet_ID=" + ID.ToString());
she.ExecuteNonQuery(sql2, out success,out errorMessage);
}
if (success) {
var sql3 = SqlUtil.GetDeleteSql(TableNames.BOM产品清单, " where BomSet_ID=" + ID.ToString());
she.ExecuteNonQuery(sql3, out success, out errorMessage);
}
if (!success)
she.Rollback();
else
she.Commit();
}
return errorMessage;
}
public static string Delete()
{
bool success;
string errorMessage;
using (var she = new SqlHelperEx()) {
she.CreateTransaction();
var sql = SqlUtil.GetDeleteSql(TableNames.BOM设置);
she.ExecuteNonQuery(sql, out success, out errorMessage);
if (success) {
var sql2 = SqlUtil.GetDeleteSql(TableNames.BOM清单);
she.ExecuteNonQuery(sql2, out success, out errorMessage);
}
if (success) {
var sql3 = SqlUtil.GetDeleteSql(TableNames.BOM产品清单);
she.ExecuteNonQuery(sql3, out success, out errorMessage);
}
if (!success)
she.Rollback();
else
she.Commit();
}
return errorMessage;
}
public static List<BomSetData> GetBomSetDetailNum(long goods_ID)
{
var sql = string.Format("select b.BomSet_ID from BomSet a inner join BSAPPLYDetail b on a.BomSet_ID = b.BomSet_ID where b.Goods_ID = {0}", goods_ID);
var table = SqlHelperEx.DoQuery(sql);
if (table.Rows.Count == 0)
return new List<BomSetData>();
var list = new List<BomSetData>();
var bomSet_ID = (long)(int.Parse(table.Rows[0][0].ToString()));
var sql2 = string.Format("select Goods_ID,a.Number,b.Number,a.InputStyle from BSDetail a left outer join BomSet b on a.BomSet_ID = b.BomSet_ID where a.BomSet_ID = {0}", bomSet_ID);
var table2 = SqlHelperEx.DoQuery(sql2);
if (table2.Rows.Count == 0)
return new List<BomSetData>();
foreach (DataRow row in table2.Rows) {
var bomSetData = new BomSetData();
bomSetData.Goods_ID = (long)(int.Parse(row[0].ToString()));
var detailNumber = row[1].ToString();
if (!string.IsNullOrEmpty(detailNumber)) {
bomSetData.DetailNumber = decimal.Parse(detailNumber);
}
var number = row[2].ToString();
if (!string.IsNullOrEmpty(number)) {
bomSetData.Number = decimal.Parse(number);
}
var inputStyle = row[3].ToString();
if (!string.IsNullOrEmpty(inputStyle)) {
var num = int.Parse(inputStyle);
if (num == 1) {
bomSetData.InputStyle = InputStyle.;
} else if (num == 2) {
bomSetData.InputStyle = InputStyle.;
}
}
list.Add(bomSetData);
}
return list;
}
public static void SyncBomSet()
{
var method = "/MainSystem/B3_HaoYue/Rpcs/RpcFun/GetBomSetRowVersion";
var bomSetRowVersions = RpcFacade.Call<List<RpcObject>>(method);
var oldBomSetRowVersions = BomSetBL.GetBomSetRowVersion();
var needInsertBomSetID = new List<long?>();
var needDeleteAndInsertBomSetID = new List<long?>();
if (bomSetRowVersions.Count > 0) {
if (oldBomSetRowVersions == null || oldBomSetRowVersions.Count() <= 0) {
bomSetRowVersions.ForEach(x => needInsertBomSetID.Add(x.Get<long>("BomSet_ID")));
} else {
foreach (var bsRowVersion in bomSetRowVersions) {
var bomSet_ID = bsRowVersion.Get<long>("BomSet_ID");
var oldBomSet = oldBomSetRowVersions.Where(x => x.BomSet_ID == bomSet_ID);
if (oldBomSet != null && oldBomSet.Count() > 0 && oldBomSet.FirstOrDefault().RowVersion != bsRowVersion.Get<int>("RowVersion")) {
needDeleteAndInsertBomSetID.Add(bomSet_ID);
} else if (oldBomSet == null || oldBomSet.Count() <= 0) {
needInsertBomSetID.Add(bomSet_ID);
}
}
foreach (var oldVersion in oldBomSetRowVersions) {
if (!bomSetRowVersions.Any(x => x.Get<long>("BomSet_ID") == oldVersion.BomSet_ID)) {
BomSetBL.Delete(oldVersion.BomSet_ID);
}
}
}
} else {
BomSetBL.Delete();
}
if (needDeleteAndInsertBomSetID.Count() > 0) {
foreach (var bomSetID in needDeleteAndInsertBomSetID) {
var error = BomSetBL.Delete(bomSetID.Value);
if (!string.IsNullOrEmpty(error)) {
throw new ApplicationException(error);
}
needInsertBomSetID.Add(bomSetID);
}
}
if (needInsertBomSetID.Count > 0) {
var getBomSet = "/MainSystem/B3_HaoYue/Rpcs/RpcFun/GetBomSet";
var bomSets = RpcFacade.Call<List<RpcObject>>(getBomSet, needInsertBomSetID.ToArray());
var getBomSetDetail = "/MainSystem/B3_HaoYue/Rpcs/RpcFun/GetBomSetDetail";
var bomSetDetails = RpcFacade.Call<List<RpcObject>>(getBomSetDetail, needInsertBomSetID.ToArray());
var getBomSetApplyDetail = "/MainSystem/B3_HaoYue/Rpcs/RpcFun/GetBomSetApplyDetail";
var bomSetApplyDetails = RpcFacade.Call<List<RpcObject>>(getBomSetApplyDetail, needInsertBomSetID.ToArray());
var list = new List<BomSet>();
var detaillist = new List<BomSet_Detail>();
var applyDetaillist = new List<BomSet_ApplyDetail>();
if (bomSets.Count() > 0) {
foreach (var detail in bomSets) {
var bomSet = new BomSet();
bomSet.BomSet_ID = detail.Get<long>("BomSet_ID");
bomSet.Date = detail.Get<DateTime?>("Date");
bomSet.Number = detail.Get<decimal?>("Number");
bomSet.RowVersion = detail.Get<int>("RowVersion");
list.Add(bomSet);
}
}
if (bomSetDetails.Count() > 0) {
foreach (var detail in bomSetDetails) {
var bomSet = new BomSet_Detail();
bomSet.BomSet_ID = detail.Get<long>("BomSet_ID");
bomSet.BomSet_Detail_ID = detail.Get<long>("BomSet_Detail_ID");
bomSet.Goods_ID = detail.Get<long?>("Goods_ID");
bomSet.Number = detail.Get<decimal?>("Number");
var inputStyle = detail.Get<string>("InputStyle");
if (!string.IsNullOrEmpty(inputStyle)) {
if(inputStyle == "料包"){
bomSet.InputStyle = InputStyle.;
} else if (inputStyle == "直接") {
bomSet.InputStyle = InputStyle.;
}
}
bomSet.UnitPrecent = detail.Get<decimal?>("UnitPrecent");
bomSet.FixedNumber = detail.Get<decimal?>("FixedNumber");
detaillist.Add(bomSet);
}
}
if (bomSetApplyDetails.Count() > 0) {
foreach (var detail in bomSetApplyDetails) {
var bomSet = new BomSet_ApplyDetail();
bomSet.BomSet_ID = detail.Get<long>("BomSet_ID");
bomSet.BomSet_Detail_ID = detail.Get<long>("BomSet_Detail_ID");
bomSet.Goods_ID = detail.Get<long?>("Goods_ID");
applyDetaillist.Add(bomSet);
}
}
BomSetBL.Insert(list, detaillist, applyDetaillist);
}
}
}
}