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.

448 lines
18 KiB

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<long> 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<long>();
foreach (DataRow row in table.Rows) {
detailIds.Add((long)((int)row[0]));
}
return detailIds;
}
public static List<ProductCatalog> GetProductCatalogRowVersion()
{
var table = SqlHelperEx.DoQuery("select ProductCatalog_ID,RowVersion from {0}".FormatWith(TableNames.));
if (table.Rows.Count == 0)
return new List<ProductCatalog>();
var list = new List<ProductCatalog>();
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<ProductCatalog> productCatalogs, List<ProductCatalog_Detail> 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<List<Tuple<long?, int?, string>>>(method, long.Parse(domain_ID));
Delete();
//var oldpCatalogRowVersion = GetProductCatalogRowVersion();
var oldpCatalogRowVersion = new List<ProductCatalog>();
var needInsertCatalogID = new List<long?>();
var needDeleteAndInsertCatalogID = new List<long?>();
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<List<RpcObject>>(method2, needInsertCatalogID.ToArray());
var list = new List<ProductCatalog>();
var detailList = new List<ProductCatalog_Detail>();
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<long>("ProductCatalog_ID");
catalogDetail.CatalogDetail_ID = detail.Get<long>("ID");
catalogDetail.Goods_Name = detail.Get<string>("Goods_Name");
catalogDetail.Goods_ID = detail.Get<long>("Goods_ID");
if (detail.Get<NamedValue>("UnitConvertDirection") != null)
catalogDetail.UnitConvertDirection = detail.Get<NamedValue>("UnitConvertDirection").ToString();
catalogDetail.MainUnitRatio = detail.Get<decimal?>("MainUnitRatio");
catalogDetail.SecondUnitRatio = detail.Get<decimal?>("SecondUnitRatio");
catalogDetail.Goods_Spec = detail.Get<string>("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<ProductCatalog> 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<ProductCatalog>();
var list = new List<ProductCatalog>();
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<ProCataGoods> 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<ProCataGoods>();
var list = new List<ProCataGoods>();
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<ProCataGoods> 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<ProCataGoods> goodslist)
{
bool success;
string errorMessage;
var selectedList = new List<ProductCatalog>();
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<ProCataGoods>();
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<ProductCatalog>();
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<ProductCatalog> 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;
}
}