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 WeightInfoBL { public static bool Insert(WeightInfo weight,SqlHelperEx she2 = null) { string insertSql = InsertUtil.GetInsertSql(TableNames.称重记录, new string[] { "Goods_Name", "Goods_Spec", "Goods_ID", "Weight", "SubWeight", "NetWeight", "CreateTime", "ProductBatch", "IsSend", "SecondNum", "ButcDetailID" }, new string[] { weight.Goods_Name, weight.Goods_Spec, weight.Goods_ID.ToString(), weight.Weight.ToString(), weight.SubWeight == null ? "null" : weight.SubWeight.ToString(), weight.NetWeight == null ? "null" : weight.NetWeight.ToString(), weight.CreateTime == null ? "null" : weight.CreateTime.ToString(), weight.ProductBatch, weight.IsSelected ? "1" : "0", weight.SecondNum == null ? "null" : weight.SecondNum.ToString(), weight.ButcDetailID == null ? "null" : weight.ButcDetailID.ToString() }); bool success; string errorMsg; if (she2 == null) { using (var she = new SqlHelperEx()) { she.CreateTransaction(); she.ExecuteNonQuery(insertSql, out success, out errorMsg); if (!success) { she.Rollback(); throw new ApplicationException(errorMsg); } she.Commit(); } } else { she2.ExecuteNonQuery(insertSql, out success, out errorMsg); if (!success) { she2.Rollback(); throw new ApplicationException(errorMsg); } } return success; } 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 int GetWeightInfoPageCount(int pageSize, bool hasInputId = false) { var sql = "select count(ID) from {0} where 1=1 {1}".FormatWith(TableNames.称重记录, hasInputId ? "and Input_ID is not null" : string.Empty); var table = SqlHelperEx.DoQuery(sql); if (table.Rows.Count == 0) return 0; var counts = (int)table.Rows[0][0]; var num1 = counts / pageSize; var num2 = counts % pageSize; return num2 > 0 ? num1 + 1 : num1; } public static List GetWeightInfos(bool hasOutputId = false) { var sql = "select SecondNum,Weight,SubWeight,NetWeight,CreateTime,Goods_ID,ID,Goods_Name,Goods_Spec,ButcDetailID from {0} where 1=1 {1} order by ID desc".FormatWith(TableNames.称重记录, hasOutputId ? "and Output_ID is not null" : "and Output_ID is null"); var table = SqlHelperEx.DoQuery(sql); if (table.Rows.Count == 0) return new List(); var list = new List(); var i = table.Rows.Count; foreach (DataRow row in table.Rows) { var detail = new WeightInfo(); detail.SecondNum = DataTypeUtil.GetDecimalNum(row[0]); detail.Weight = DataTypeUtil.GetDecimalNum(row[1]); detail.SubWeight = DataTypeUtil.GetDecimalNullNum(row[2]); detail.NetWeight = DataTypeUtil.GetDecimalNullNum(row[3]); detail.CreateTime = DateTime.Parse(row[4].ToString()); detail.Goods_ID = DataTypeUtil.GetLongNum(row[5]); detail.ID = DataTypeUtil.GetLongNum(row[6]); detail.Goods_Name = DataTypeUtil.ToStringEmptyIfNull(row[7]); detail.Goods_Spec = DataTypeUtil.ToStringEmptyIfNull(row[8]); detail.ButcDetailID = DataTypeUtil.GetLongNullNum(row[9]); detail.Number = i; i--; list.Add(detail); } return list; } public static decimal? GetButcDetailIDWeight(long? butcDetailID,SqlHelperEx she2 = null) { if (butcDetailID != null) { var sql = "select sum(NetWeight) from {0} where ButcDetailID = {1}".FormatWith(TableNames.称重记录, butcDetailID); DataTable table; if (she2 == null) { using (var she = new SqlHelperEx()) { table = she.Query(sql); } } else { table = she2.Query(sql); } if (table.Rows.Count == 0) { return null; } return (decimal?)table.Rows[0][0]; } return null; } public static void UpdateOutputId(long[] ids,long outputId) { string updateSql = string.Format("update " + TableNames.称重记录 + " set Output_ID = {0} where ID in ({1})",outputId, string.Join(",",ids)); 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 UpdateOutputIdByButcDetailID(long butcDetailID, long outputId) { string updateSql = string.Format("update " + TableNames.称重记录 + " set Output_ID = {0} where ButcDetailID = {1}", outputId, butcDetailID); bool success = true; using (var she = new SqlHelperEx()) { she.CreateTransaction(); she.ExecuteNonQuery(updateSql, out success); if (!success) { she.Rollback(); } else { she.Commit(); } } } public static int GetUnfinishedBatchPageCount(int pageSize) { var sql = "select count(ID) from {0} where State=0".FormatWith(TableNames.未完成生产批次); var table = SqlHelperEx.DoQuery(sql); if (table.Rows.Count == 0) return 0; var counts = (int)table.Rows[0][0]; var num1 = counts / pageSize; var num2 = counts % pageSize; return num2 > 0 ? num1 + 1 : num1; } public static List GetUnfinishedBatchPage(int pageIndex, int pageSize) { var sql = "select * from(select ProductBatch,LegType from {0} where State=0) rows {1} to {2}".FormatWith(TableNames.未完成生产批次, pageIndex == 1 ? 1 : (pageIndex - 1) * pageSize + 1, pageIndex * pageSize); var table = SqlHelperEx.DoQuery(sql); if (table.Rows.Count == 0) return new List(); var list = new List(); foreach (DataRow row in table.Rows) { var detail = new UnfinishedBatch(); detail.ProductBatch = DataTypeUtil.ToStringEmptyIfNull(row[0]); detail.LegType = row[1].ToString() == "前腿" ? LegType.前腿 : LegType.后腿; list.Add(detail); } return list; } public static List GetWeightInfoByIds(params long[] weightIds) { var sql = "select LegType,Weight,SubWeight,NetWeight,CreateTime,Goods_ID,ProductBatch,ID from {0} where ID in ({1})".FormatWith(TableNames.称重记录,string.Join(",",weightIds)); var table = SqlHelperEx.DoQuery(sql); if (table.Rows.Count == 0) return new List(); var list = new List(); foreach (DataRow row in table.Rows) { var detail = new WeightInfo(); //detail.LegType = row[0].ToString() == "前腿" ? LegType.前腿 : LegType.后腿; detail.Weight = DataTypeUtil.GetDecimalNum(row[1]); detail.SubWeight = DataTypeUtil.GetDecimalNullNum(row[2]); detail.NetWeight = DataTypeUtil.GetDecimalNullNum(row[3]); detail.CreateTime = DateTime.Parse(row[4].ToString()); detail.Goods_ID = DataTypeUtil.GetLongNum(row[5]); detail.ProductBatch = DataTypeUtil.ToStringEmptyIfNull(row[6]); detail.ID = DataTypeUtil.GetLongNum(row[7]); list.Add(detail); } return list; } public static void DeleteByID(long id, SqlHelperEx she2 = null) { var sql = SqlUtil.GetDeleteSql(TableNames.称重记录, "where ID = '{0}'".FormatWith(id)); bool success; if (she2 == null) { using (var she = new SqlHelperEx()) { she.CreateTransaction(); she.ExecuteNonQuery(sql, out success); if (!success) she.Rollback(); else she.Commit(); } } else { she2.ExecuteNonQuery(sql, out success); if (!success) she2.Rollback(); } } //public static void Delete(List> tuples) //{ // var sql = "select ProductBatch,LegType from {0} where State=0) rows {1} to {2}".FormatWith(TableNames.未完成生产批次, pageIndex == 1 ? 1 : (pageIndex - 1) * pageSize + 1, pageIndex * pageSize); // var table = SqlHelperEx.DoQuery(sql); // var sql = SqlUtil.GetDeleteSql(TableNames.称重记录, "where ID in ({0})".FormatWith(string.Join(",", ids))); // bool success; // using (var she = new SqlHelperEx()) { // she.CreateTransaction(); // she.ExecuteNonQuery(sql, out success); // if (!success) { // she.Rollback(); // } else { // she.Commit(); // } // } //} // public static Tuple GetTotalWeightByDetailID(long? BatchNumber_ID,long Detail_ID,WeightType type,bool isBatch = false) // { // return LocalQueryUtil.GetTotalWeightByDetailID(BatchNumber_ID,Detail_ID,type,isBatch); // } // public static List GetWeightInfoByDetailIDs(long? BatchNumber_ID, List detailIDs, WeightType type, bool isBatch = false) // { // var list = new List(); // var querySql = "select ID,Goods_ID,NetWeight,Detail_ID,ProductDate,BatchNumber_ID,ProductLink,Routing_ID,ProductShift_ID,StandardNum,Department_ID,Employee_ID,GoodsBatchId,OutDetailId from {0} where Detail_ID in ({1}) and IsBatch = {2} and WeightType='{3}' and BatchNumber_ID = {4}".FormatWith(TableNames.称重记录, string.Join(",", detailIDs), !isBatch ? "0" : "1", type.ToString(), BatchNumber_ID); // var table = SqlHelperEx.DoQuery(querySql); // if (table.Rows.Count == 0) // return new List(); // foreach (DataRow row in table.Rows) { // var weightInfo = new WeightInfo(); // weightInfo.ID = DataTypeUtil.GetLongNum(row[0]); // weightInfo.Goods_ID = DataTypeUtil.GetLongNum(row[1]); // weightInfo.NetWeight = DataTypeUtil.GetDecimalNum(row[2]); // weightInfo.Detail_ID = DataTypeUtil.GetLongNum(row[3]); // weightInfo.ProductDate = DataTypeUtil.GetDateTimeNullData(row[4]); // weightInfo.BatchNumber_ID = DataTypeUtil.GetLongNullNum(row[5]); // weightInfo.ProductLink = DataTypeUtil.ToStringEmptyIfNull(row[6]); // weightInfo.Routing_ID = DataTypeUtil.GetLongNullNum(row[7]); // weightInfo.ProductShift_ID = DataTypeUtil.GetLongNullNum(row[8]); // weightInfo.StandardNum = DataTypeUtil.GetDecimalNullNum(row[9]); // weightInfo.Department_ID = DataTypeUtil.GetLongNullNum(row[10]); // weightInfo.Employee_ID = DataTypeUtil.GetLongNullNum(row[11]); // weightInfo.GoodsBatchId = DataTypeUtil.GetLongNullNum(row[12]); // weightInfo.OutDetailID = DataTypeUtil.GetLongNullNum(row[13]); // list.Add(weightInfo); // } // return list; // } // public static List GetInStoreWeightInfos(WeightType type,bool isBatch,long? detailId = null, long?batchNumberId= null) // { // var list = new List(); // var querySql = "select Goods_ID,Goods_Name,Weight,SubWeight,CreateTime,ID,NetWeight,GoodsBatch_Name,GoodsBatchId from {0} where IsBatch = '{1}' and WeightType = '{2}'".FormatWith(TableNames.称重记录, isBatch ? "1" : "0", type.ToString()); // if (detailId != null) // { // querySql = querySql + " and Detail_ID = "+detailId; // } // if (batchNumberId != null) // { // querySql = querySql + " and BatchNumber_ID = " + batchNumberId; // } // var table = SqlHelperEx.DoQuery(querySql); // if (table.Rows.Count == 0) // return new List(); // foreach (DataRow row in table.Rows) { // var weightInfo = new WeightInfo(); // weightInfo.Goods_ID = DataTypeUtil.GetLongNum(row[0]); // weightInfo.Goods_Name = DataTypeUtil.ToStringEmptyIfNull(row[1]); // weightInfo.Weight = DataTypeUtil.GetDecimalNum(row[2]); // weightInfo.SubWeight = DataTypeUtil.GetDecimalNum(row[3]); // weightInfo.CreateTime = DataTypeUtil.GetDateTimeNullData(row[4]).Value; // weightInfo.ID = DataTypeUtil.GetLongNum(row[5]); // weightInfo.NetWeight = DataTypeUtil.GetDecimalNum(row[6]); // weightInfo.GoodsBatch_Name = DataTypeUtil.ToStringEmptyIfNull(row[7]); // weightInfo.GoodsBatchId = DataTypeUtil.GetLongNullNum(row[8]); // list.Add(weightInfo); // } // return list; // } // public static decimal? GetInStoreWeightByGoodsID(long? GoodsId) // { // var querySql = "select sum(NetWeight) from {0} where Goods_ID = {1} and WeightType = '{2}'".FormatWith(TableNames.称重记录, GoodsId,WeightType.InStore.ToString()); // var table = SqlHelperEx.DoQuery(querySql); // if (table.Rows.Count == 0) // return null; // return DataTypeUtil.GetDecimalNum(table.Rows[0][0]); // } // public static void DeleteInStoreWeightInfos(WeightType type) // { // var sql = SqlUtil.GetDeleteSql(TableNames.称重记录, "where WeightType = '{0}'".FormatWith(type.ToString())); // using (var she = new SqlHelperEx()) { // bool success; // she.CreateTransaction(); // she.ExecuteNonQuery(sql, out success); // if (!success) // she.Rollback(); // else // she.Commit(); // } // } //public static bool GetSameWeightByGoodsId(long? GoodsId,decimal? weight) //{ // var querySql = "select ID from {0} where Goods_ID = {1} and NetWeight = {2} and WeightType = '{3}'".FormatWith(TableNames.称重记录, GoodsId,weight, WeightType.InStore.ToString()); // var table = SqlHelperEx.DoQuery(querySql); // if (table.Rows.Count == 0) // return false; // var id = DataTypeUtil.GetLongNullNum(table.Rows[0][0]); // return id != null; //} //public static void DeleteImport(List ids) //{ // var sql = SqlUtil.GetDeleteSql(TableNames.称重记录, "where Detail_ID in ({0}) and WeightType = '{1}' and IsImport = '1' and IsBatch = '1'".FormatWith(string.Join(",", ids), WeightType.Input.ToString())); // using (var she = new SqlHelperEx()) { // bool success; // she.CreateTransaction(); // she.ExecuteNonQuery(sql, out success); // if (!success) // she.Rollback(); // else // she.Commit(); // } //} } }