using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Utils.Datas;
|
|
using WeighBusiness.BO;
|
|
using WeighBusiness.Utils;
|
|
using WeighBusiness.Utils.SqlUtils;
|
|
|
|
namespace WeighBusiness.BL
|
|
{
|
|
public class WeighBL
|
|
{
|
|
public static Weigh Load(long id)
|
|
{
|
|
Weigh result = LocalQueryUtil.GetLocalWeighHead(id);
|
|
if (result == null)
|
|
return result;
|
|
var details = LocalQueryUtil.GetLocalWeighDetails(id);
|
|
if (details == null || details.Count == 0)
|
|
return result;
|
|
foreach (var d in details) {
|
|
result.Details.Add(d);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取最后插入的称重单
|
|
/// </summary>
|
|
public static Weigh LoadLast()
|
|
{
|
|
var idValue = LocalQueryUtil.GetLocalTableLastRowField(TableNames.称重表, "ID");
|
|
if (idValue == null)
|
|
return new Weigh();
|
|
return Load(DataTypeUtil.GetLongNum(idValue));
|
|
}
|
|
|
|
public static bool Save(Weigh weigh)
|
|
{
|
|
if (weigh.IsChecked)
|
|
throw new ApplicationException("已审核,不允许修改");
|
|
if (weigh.ID == 0)
|
|
return Insert(weigh);
|
|
|
|
return Update(weigh);
|
|
}
|
|
|
|
public static bool Check(Weigh weigh)
|
|
{
|
|
var isSame = EntityBL.EnsureStateSame<Weigh>(weigh);
|
|
if (!isSame)
|
|
throw new ApplicationException("请先保存修改!");
|
|
if (weigh.IsChecked)
|
|
return true;
|
|
weigh.IsChecked = true;
|
|
bool success = Update(weigh);
|
|
if (success)
|
|
SynchronousUtil.InsertWeighSynchronous(weigh.ID);//增加需要同步的数据
|
|
return success;
|
|
}
|
|
|
|
public static bool UnCheck(Weigh weigh, Func<long, int> DeleteRemoteWeigh)
|
|
{
|
|
var isSame = EntityBL.EnsureStateSame<Weigh>(weigh);
|
|
if (!isSame)
|
|
throw new ApplicationException("界面显示的数据与实际值不符。请重新打开单据!");
|
|
if (!weigh.IsChecked)
|
|
return true;
|
|
weigh.IsChecked = false;
|
|
|
|
if (weigh.Remote_Weigh_ID.HasValue) {
|
|
var status = DeleteRemoteWeigh(weigh.Remote_Weigh_ID.Value);//0:单据不存在;1:已作废;2:删除成功;3:已审核;4:已完毕;5:删除报错
|
|
ThrowDeleteErrorIfNeed(status);
|
|
weigh.Remote_Weigh_ID = null;
|
|
}
|
|
var success = Update(weigh);
|
|
if (success)
|
|
SynchronousUtil.DeleteWeighSynchronous(weigh.ID);
|
|
return success;
|
|
}
|
|
|
|
private static void ThrowDeleteErrorIfNeed(int status)//0:单据不存在;1:已作废;2:删除成功;3:已审核;4:已完毕;5:删除报错
|
|
{
|
|
if (status < 3)
|
|
return;
|
|
if (status == 3)
|
|
throw new ApplicationException("对应的系统称重单已审核");
|
|
if (status == 4)
|
|
throw new ApplicationException("对应的系统称重单已完毕");
|
|
if (status == 5)
|
|
throw new ApplicationException("删除对应的系统称重单失败!请重试!如果多次删除失败,请登录系统查看对应的单据");
|
|
}
|
|
|
|
public static bool Update(Weigh weigh)
|
|
{
|
|
var oldWeigh = Load(weigh.ID);
|
|
if (oldWeigh.RowVersion != weigh.RowVersion)
|
|
throw new ApplicationException("对象已经被更改,请重新打开");
|
|
weigh.RowVersion++;
|
|
var needUpdateItems = GetNeedUpdateItems(weigh, oldWeigh);
|
|
needUpdateItems.Add("RowVersion");
|
|
needUpdateItems.Add(weigh.RowVersion.ToString());
|
|
string updateHeadSql = UpdateUtil.GetUpdateString(TableNames.称重表, "ID", weigh.ID.ToString(), needUpdateItems.ToArray());
|
|
var updateDetailSqls = GetUpdateDetailsSql(weigh, oldWeigh);
|
|
var insertDetailSqls = GetInsertDetailSqls(weigh);
|
|
string deleteDetailSql = GetDeleteDetailSql(weigh, oldWeigh);
|
|
|
|
bool success = true;
|
|
using (SqlHelperEx she = new SqlHelperEx()) {
|
|
she.CreateTransaction();
|
|
if (!string.IsNullOrEmpty(updateHeadSql)) {
|
|
she.ExecuteNonQuery(updateHeadSql, out success);
|
|
if (!success) {
|
|
she.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
if (updateDetailSqls.Count > 0) {
|
|
foreach (var updateSql in updateDetailSqls) {
|
|
if (string.IsNullOrEmpty(updateSql))
|
|
continue;
|
|
she.ExecuteNonQuery(updateSql, out success);
|
|
if (!success) {
|
|
she.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
if (insertDetailSqls.Count > 0) {
|
|
foreach (var insertSql in insertDetailSqls) {
|
|
if (string.IsNullOrEmpty(insertSql))
|
|
continue;
|
|
she.ExecuteNonQuery(insertSql, out success);
|
|
if (!success) {
|
|
she.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
if (!string.IsNullOrEmpty(deleteDetailSql)) {
|
|
she.ExecuteNonQuery(deleteDetailSql, out success);
|
|
if (!success) {
|
|
she.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
if (success)
|
|
she.Commit();
|
|
else
|
|
she.Rollback();
|
|
}
|
|
return success;
|
|
}
|
|
|
|
private static bool Insert(Weigh weigh)
|
|
{
|
|
if (weigh.ID > 0)
|
|
return Update(weigh);
|
|
string insertHeadSql = InsertUtil.GetInsertSql(TableNames.称重表,
|
|
new string[] { "IsChecked", "CreateDateTime", "Car_ID", "CarNumber", "Number", "HeadWeight", "Remote_Weigh_ID", "RowVersion" },
|
|
new string[] { weigh.IsChecked.ToString(), weigh.CreateDateTime.Value.ToShortDateString(), weigh.Car_ID.ToString(), DataTypeUtil.ToStringNullIfNull(weigh.CarNumber), DataTypeUtil.ToStringNullIfNull(weigh.Number), DataTypeUtil.ToStringNullIfNull(weigh.HeadWeight), "null", "1" });
|
|
if (string.IsNullOrEmpty(insertHeadSql))//TODO:处理是否正确?
|
|
throw new ApplicationException("表头数据不允许为空");
|
|
bool success;
|
|
using (SqlHelperEx she = new SqlHelperEx()) {
|
|
she.CreateTransaction();
|
|
she.ExecuteNonQuery(insertHeadSql, out success);
|
|
if (!success) {
|
|
she.Rollback();
|
|
return false;
|
|
}
|
|
var weighID = she.GetCurrentID(TableNames.称重表);
|
|
weigh.ID = weighID;
|
|
List<string> insertDetailSqls = InsertDetailsSqls(weigh.Details, weighID);
|
|
if (insertDetailSqls.Count == 0) {//没有数据要插入时。TODO:是否处理妥当?
|
|
she.Commit();
|
|
return true;
|
|
}
|
|
foreach (var detailSql in insertDetailSqls) {
|
|
she.ExecuteNonQuery(detailSql, out success);
|
|
if (!success)
|
|
break;
|
|
}
|
|
if (!success)
|
|
she.Rollback();
|
|
else
|
|
she.Commit();
|
|
}
|
|
return success;
|
|
}
|
|
|
|
private static List<string> InsertDetailsSqls(List<Weigh_Detail> details, long weighID)
|
|
{
|
|
List<string> insertDetailSqls = new List<string>();
|
|
if (details == null || details.Count == 0)
|
|
return insertDetailSqls;
|
|
string[] columns = new string[] { "Local_Weigh_ID", "Weight", "WeighTime" };
|
|
foreach (var d in details) {
|
|
insertDetailSqls.Add(InsertUtil.GetInsertSql(TableNames.称重清单表, columns, GetValue(d, weighID)));
|
|
}
|
|
return insertDetailSqls;
|
|
}
|
|
|
|
private static string[] GetValue(Weigh_Detail detail, long weighID)
|
|
{
|
|
return new string[]{
|
|
weighID.ToString(),
|
|
DataTypeUtil.ToStringNullIfNull(detail.Weight),
|
|
DataTypeUtil.ToStringNullIfNull(detail.WeighTime),
|
|
};
|
|
}
|
|
|
|
private static List<string> GetUpdateDetailsSql(Weigh weigh, Weigh oldWeigh)
|
|
{
|
|
List<string> updateDetailSqls = new List<string>();
|
|
foreach (var detail in weigh.Details) {
|
|
if (detail.ID == 0)//新Detail,需要插入
|
|
continue;
|
|
var oldDetail = GetDetail(oldWeigh.Details, detail.ID);
|
|
if (oldDetail == null)//这种情况不应该发生
|
|
throw new ApplicationException("更新的清单不存在。是否已经删除?请重新打开该单据");
|
|
IList<string> needUpdateItems = GetNeedUpdateItems(detail, oldDetail);
|
|
updateDetailSqls.Add(UpdateUtil.GetUpdateString(TableNames.称重清单表, "ID", detail.ID.ToString(), needUpdateItems.ToArray()));
|
|
}
|
|
return updateDetailSqls;
|
|
}
|
|
|
|
private static List<string> GetInsertDetailSqls(Weigh weigh)
|
|
{
|
|
List<string> insertDetailSqls = new List<string>();
|
|
foreach (var detail in weigh.Details) {
|
|
if (detail.ID > 0)
|
|
continue;
|
|
insertDetailSqls.Add(InsertUtil.GetInsertSql(TableNames.称重清单表,
|
|
new string[] { "Local_Weigh_ID", "Weight", "WeighTime" },
|
|
new string[] { weigh.ID.ToString(), DataTypeUtil.ToStringNullIfNull(detail.Weight), DataTypeUtil.ToStringNullIfNull(detail.WeighTime) }));//TODO:为空时如何处理?此处需要修改
|
|
}
|
|
return insertDetailSqls;
|
|
}
|
|
|
|
private static string GetDeleteDetailSql(Weigh weigh, Weigh oldWeigh)
|
|
{
|
|
var willDeleteDetailID = new List<string>();
|
|
foreach (var detail in oldWeigh.Details) {
|
|
var newDetail = GetDetail(weigh.Details, detail.ID);
|
|
if (newDetail == null)
|
|
willDeleteDetailID.Add(detail.ID.ToString());
|
|
}
|
|
return DeleteUtil.GetDeleteString(TableNames.称重清单表, "ID", willDeleteDetailID.ToArray());
|
|
}
|
|
|
|
private static IList<string> GetNeedUpdateItems(Weigh weigh, Weigh oldWeigh)
|
|
{
|
|
IList<string> updateItems = new List<string>();
|
|
if (weigh.IsChecked != oldWeigh.IsChecked) {
|
|
updateItems.Add("IsChecked");
|
|
updateItems.Add(weigh.IsChecked.ToString());
|
|
}
|
|
if (weigh.CreateDateTime != oldWeigh.CreateDateTime) {
|
|
updateItems.Add("CreateDateTime");
|
|
updateItems.Add(weigh.CreateDateTime.ToString());
|
|
}
|
|
if (weigh.Car_ID != oldWeigh.Car_ID) {
|
|
updateItems.Add("Car_ID");
|
|
updateItems.Add(weigh.Car_ID.ToString());
|
|
}
|
|
if (weigh.CarNumber != oldWeigh.CarNumber) {
|
|
updateItems.Add("CarNumber");
|
|
updateItems.Add(DataTypeUtil.ToStringNullIfNull(weigh.CarNumber));
|
|
}
|
|
if (weigh.Number != oldWeigh.Number) {
|
|
updateItems.Add("Number");
|
|
updateItems.Add(DataTypeUtil.ToStringNullIfNull(weigh.Number));
|
|
}
|
|
if (weigh.HeadWeight != oldWeigh.HeadWeight) {
|
|
updateItems.Add("HeadWeight");
|
|
updateItems.Add(DataTypeUtil.ToStringNullIfNull(weigh.HeadWeight));
|
|
}
|
|
if (weigh.Remote_Weigh_ID != oldWeigh.Remote_Weigh_ID) {
|
|
updateItems.Add("Remote_Weigh_ID");
|
|
updateItems.Add(weigh.Remote_Weigh_ID == null ? "null" : (weigh.Remote_Weigh_ID ?? 0).ToString());//TODO:为空时处理是否正确?此处可不修改
|
|
}
|
|
return updateItems;
|
|
}
|
|
|
|
private static IList<string> GetNeedUpdateItems(Weigh_Detail detail, Weigh_Detail oldDetail)
|
|
{
|
|
IList<string> updateItems = new List<string>();
|
|
//if (detail.Local_Weigh_ID != oldDetail.Local_Weigh_ID) {
|
|
// updateItems.Add("Local_Weigh_ID");
|
|
// updateItems.Add(detail.Local_Weigh_ID.ToString());
|
|
//}
|
|
if (detail.Weight != oldDetail.Weight) {
|
|
updateItems.Add("Weight");
|
|
updateItems.Add(DataTypeUtil.ToStringNullIfNull(detail.Weight));
|
|
}
|
|
if (detail.WeighTime != oldDetail.WeighTime) {
|
|
updateItems.Add("WeighTime");
|
|
updateItems.Add(DataTypeUtil.ToStringNullIfNull(detail.WeighTime));
|
|
}
|
|
|
|
return updateItems;
|
|
}
|
|
|
|
private static Weigh_Detail GetDetail(List<Weigh_Detail> details, long detailID)
|
|
{
|
|
foreach (var detail in details) {
|
|
if (detail.ID == detailID)
|
|
return detail;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|