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.

376 lines
15 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 ProductPlanBL
{
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<ProductPlan> GetProductPlanRowVersion()
{
var table = SqlHelperEx.DoQuery("select ProductPlan_ID,RowVersion from {0}".FormatWith(TableNames.));
if (table.Rows.Count == 0)
return new List<ProductPlan>();
var list = new List<ProductPlan>();
foreach (DataRow row in table.Rows) {
var plan = new ProductPlan();
plan.ProductPlan_ID = (long)(int.Parse(row[0].ToString()));
plan.RowVersion = int.Parse(row[1].ToString());
list.Add(plan);
}
return list;
}
public static void Insert(List<ProductPlan> plans, List<ButcherDetail> butcherDetails,List<ProductBatch> batchs)
{
using (var she = new SqlHelperEx()) {
she.CreateTransaction();
bool success = false;
string errorMessage;
if (plans.Count() > 0) {
foreach (var plan in plans) {
string insertSql = InsertUtil.GetInsertSql(TableNames.,
new string[] { "ProductPlan_ID", "PlanDate", "RowVersion" },
new string[] { plan.ProductPlan_ID.ToString(), plan.PlanDate.ToString(), plan.RowVersion.ToString() });
she.ExecuteNonQuery(insertSql, out success, out errorMessage);
if (!success && !string.IsNullOrEmpty(errorMessage)) {
she.Rollback();
throw new ApplicationException(errorMessage);
}
}
}
if (butcherDetails.Count() > 0) {
foreach (var detail in butcherDetails) {
string insertSql = InsertUtil.GetInsertSql(TableNames.,
new string[] { "ProductPlan_ID", "DetailID", "Goods_Code", "Goods_Name", "Goods_Spec", "Goods_MainUnit", "Goods_ID", "PlanNumber", "IsFinish", "Department_Name", "Remark" },
new string[] { detail.ProductPlan_ID.ToString(), detail.DetailID.ToString(), detail.Goods_Code, detail.Goods_Name, detail.Goods_Spec, detail.Goods_MainUnit, detail.Goods_ID.ToString(), detail.PlanNumber == null ? "null" : detail.PlanNumber.Value.ToString(),detail.IsFinish?"1":"0",detail.Department_Name,detail.Remark });
she.ExecuteNonQuery(insertSql, out success, out errorMessage);
if (!success && !string.IsNullOrEmpty(errorMessage)) {
she.Rollback();
throw new ApplicationException(errorMessage);
}
}
}
if (batchs.Count() > 0) {
foreach (var batch in batchs) {
string insertSql = InsertUtil.GetInsertSql(TableNames.,
new string[] { "Batch_ID", "Batch", "BillDate", "RowVersion", "ProductPlan_ID" },
new string[] { batch.Batch_ID.ToString(), batch.Batch, batch.BillDate.ToString(), batch.RowVersion.ToString(), batch.ProductPlan_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 void UpdateButhcerDetailFinish(long detailId)
{
string updateSql = string.Format("update " + TableNames. + " set IsFinish = 1 where DetailID ={0}", detailId);
bool success = true;
using (var she = new SqlHelperEx()) {
she.CreateTransaction();
she.ExecuteNonQuery(updateSql, out success);
if (!success) {
she.Rollback();
} else {
she.Commit();
}
}
}
public static void UpdateButhcerDetailFinishNum(decimal? finishNum,decimal? detailID,long? goodsId, SqlHelperEx she2 = null)
{
string excuteSql = string.Empty;
var table = SqlHelperEx.DoQuery("select ID from {0} where DetailID = {1}".FormatWith(TableNames., detailID));
if (table.Rows.Count > 0) {
excuteSql = string.Format("update " + TableNames. + " set FinishNumber = {0} where DetailID ={1}", finishNum, detailID);
} else {
excuteSql = InsertUtil.GetInsertSql(TableNames.,
new string[] { "DetailID", "Goods_ID", "FinishNumber" },
new string[] { detailID.ToString(), goodsId.ToString(), (finishNum ?? 0).ToString() });
}
bool success = true;
if (she2 == null) {
using (var she = new SqlHelperEx()) {
she.CreateTransaction();
she.ExecuteNonQuery(excuteSql, out success);
if (!success) {
she.Rollback();
} else {
she.Commit();
}
}
} else {
she2.ExecuteNonQuery(excuteSql, out success);
if (!success) {
she2.Rollback();
}
}
}
public static string Delete(long ID)
{
bool success;
string errorMessage;
using (var she = new SqlHelperEx()) {
she.CreateTransaction();
var sql = SqlUtil.GetDeleteSql(TableNames., " where ProductPlan_ID=" + ID.ToString());
she.ExecuteNonQuery(sql, out success, out errorMessage);
if (success) {
var sql2 = SqlUtil.GetDeleteSql(TableNames., " where ProductPlan_ID=" + ID.ToString());
she.ExecuteNonQuery(sql2, out success, out errorMessage);
}
if (!success)
she.Rollback();
else
she.Commit();
}
return errorMessage;
}
public static void SyncProductPlan()
{
var domain_ID = ConfigurationManager.AppSettings["Domain_ID"];
var accountingUnit_ID = ConfigurationManager.AppSettings["AccountingUnit_ID"];
var method = "/MainSystem/B3_HaoYue/Rpcs/B3CowButcherManageRpc/GetProductPlanRowVersion";
if (domain_ID != null && !string.IsNullOrEmpty(domain_ID.ToString()) && accountingUnit_ID != null && !string.IsNullOrEmpty(accountingUnit_ID.ToString())) {
var productPlanRowVersions = RpcFacade.Call<List<Tuple<long?, int?, DateTime?>>>(method, long.Parse(domain_ID), long.Parse(accountingUnit_ID));
var oldproductPlanRowVersions = GetProductPlanRowVersion();
var needInsertProductPlanID = new List<long?>();
var needDeleteAndInsertProductPlanID = new List<long?>();
if (productPlanRowVersions.Count > 0) {
if (oldproductPlanRowVersions.Count() <= 0) {
productPlanRowVersions.ForEach(x => needInsertProductPlanID.Add(x.Item1));
} else {
foreach (var planRowVersion in productPlanRowVersions) {
var plan_ID = planRowVersion.Item1;
var oldProductPlans = oldproductPlanRowVersions.Where(x => x.ProductPlan_ID == plan_ID);
if (oldProductPlans.Count() > 0 && oldProductPlans.FirstOrDefault().RowVersion != planRowVersion.Item2) {
needDeleteAndInsertProductPlanID.Add(planRowVersion.Item1);
} else if (oldProductPlans.Count() <= 0) {
needInsertProductPlanID.Add(plan_ID);
}
}
foreach (var oldVersion in oldproductPlanRowVersions) {
if (!productPlanRowVersions.Any(x => x.Item1 == oldVersion.ProductPlan_ID)) {
Delete(oldVersion.ProductPlan_ID);
}
}
}
} else {
Delete();
}
if (needDeleteAndInsertProductPlanID.Count() > 0) {
foreach (var productPlanID in needDeleteAndInsertProductPlanID) {
var error = Delete(productPlanID.Value);
if (!string.IsNullOrEmpty(error)) {
throw new ApplicationException(error);
}
needInsertProductPlanID.Add(productPlanID);
}
}
if (needInsertProductPlanID.Count > 0) {
var getButcherDetail = "/MainSystem/B3_HaoYue/Rpcs/B3CowButcherManageRpc/GetProducePlanButcherDetailData";
var butcherDetails = RpcFacade.Call<List<RpcObject>>(getButcherDetail, needInsertProductPlanID.ToArray());
var list = new List<ProductPlan>();
var butcherList = new List<ButcherDetail>();
var batchList = new List<ProductBatch>();
foreach (var planID in needInsertProductPlanID) {
var plan = new ProductPlan();
plan.ProductPlan_ID = planID.Value;
var result = productPlanRowVersions.Where(x => x.Item1 == planID).First();
plan.RowVersion = result.Item2.Value;
plan.PlanDate = result.Item3.Value;
list.Add(plan);
}
if (butcherDetails.Count() > 0) {
foreach (var detail in butcherDetails) {
var butcherDetail = new ButcherDetail();
butcherDetail.ProductPlan_ID = detail.Get<long>("ProductPlan_ID");
butcherDetail.DetailID = detail.Get<long>("DetailID");
butcherDetail.Goods_Code = detail.Get<string>("Goods_Code");
butcherDetail.Goods_Name = detail.Get<string>("Goods_Name");
butcherDetail.Goods_Spec = detail.Get<string>("Goods_Spec");
butcherDetail.Goods_MainUnit = detail.Get<string>("Goods_MainUnit");
butcherDetail.Goods_ID = detail.Get<long>("Goods_ID");
butcherDetail.PlanNumber = detail.Get<decimal?>("PlanNumber");
butcherDetail.Department_Name = detail.Get<string>("Department_Name");
butcherDetail.Remark = detail.Get<string>("Remark");
butcherList.Add(butcherDetail);
}
}
string method3 = "/MainSystem/B3_HaoYue/Rpcs/FourSplitBodyWeightClient/GetProductBatchData";
var produceBatchs = RpcFacade.Call<List<Tuple<string, long?, int?, DateTime?, long?>>>(method3, needInsertProductPlanID.ToArray());
if (produceBatchs.Count() > 0) {
foreach (var batch in produceBatchs) {
var pBatch = new ProductBatch();
pBatch.Batch = batch.Item1;
pBatch.Batch_ID = batch.Item2.Value;
pBatch.RowVersion = batch.Item3.Value;
pBatch.BillDate = batch.Item4.Value;
pBatch.ProductPlan_ID = batch.Item5.Value;
batchList.Add(pBatch);
}
}
Insert(list, butcherList, batchList);
}
}
}
public static List<ProductBatch> GetProductBatch(DateTime date)
{
var list = new List<ProductBatch>();
var querySql = "select Batch_ID,Batch from {0} where BillDate >='{1}' and BillDate < '{2}'".FormatWith(TableNames., date, date.AddDays(1));
var table = SqlHelperEx.DoQuery(querySql);
if (table.Rows.Count == 0)
return new List<ProductBatch>();
foreach (DataRow row in table.Rows) {
var batch = new ProductBatch();
batch.Batch_ID = DataTypeUtil.GetLongNum(row[0]);
batch.Batch = row[1].ToString();
list.Add(batch);
}
return list;
}
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<ButcherDetailData> GetButcherDetails(DateTime date, long planTeamID)
{
var list = new List<ButcherDetailData>();
var sql = "select a.Goods_ID, a.Goods_Name,a.Goods_Spec,a.Goods_MainUnit,a.PlanNumber,a.DetailID,e.FinishNumber,a.Department_Name,a.Remark from {0} a inner join {1} b on a.ProductPlan_ID = b.ProductPlan_ID inner join {2} c on c.Goods_ID = a.Goods_ID inner join {3} d on c.ProductTeam_ID = d.ProductTeam_ID left outer join {4} e on a.DetailID = e.DetailID where b.PlanDate >='{5}' and b.PlanDate < '{6}' and a.IsFinish = 0 and d.ProductTeam_ID = {7}".FormatWith(TableNames., TableNames., TableNames., TableNames., TableNames., date, date.AddDays(1), planTeamID);
if (!string.IsNullOrEmpty(sql)) {
var table = SqlHelperEx.DoQuery(sql);
foreach (DataRow row in table.Rows) {
var outDetail = new ButcherDetailData();
outDetail.Goods_ID = (long)((int)row[0]);
outDetail.Goods_Name = (string)row[1];
outDetail.Goods_Spec = (string)row[2];
outDetail.Goods_MainUnit = (string)row[3];
outDetail.PlanNumber = DataTypeUtil.GetDecimalNum(row[4]);
outDetail.DetailID = (long)((int)row[5]);
outDetail.FinishNum = DataTypeUtil.GetDecimalNum(row[6]);
outDetail.Department_Name = (string)row[7];
outDetail.Remark = (string)row[8];
list.Add(outDetail);
}
}
return list;
}
}
public class ButcherDetailData : INotifyPropertyChanged
{
public long DetailID { get; set; }
public long Goods_ID { get; set; }
public string Goods_Name { get; set; }
public string Goods_Spec { get; set; }
public string Goods_MainUnit { get; set; }
public decimal? PlanNumber { get; set; }
public string Department_Name { get; set; }
public string Remark { get; set; }
private decimal? mFinishNum;
public decimal? FinishNum
{
get
{
return mFinishNum;
}
set
{
mFinishNum = value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("FinishNum"));
}
}
}
private decimal? mSurplusNum;
public decimal? SurplusNum
{
get
{
return mSurplusNum;
}
set
{
mSurplusNum = value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("SurplusNum"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}