|
|
using BWP.B3ClientService.BO;
|
|
|
using BWP.B3Frameworks.Utils;
|
|
|
using Forks.EnterpriseServices.DomainObjects2;
|
|
|
using Forks.EnterpriseServices.DomainObjects2.DQuery;
|
|
|
using Forks.EnterpriseServices.JsonRpc;
|
|
|
using Forks.EnterpriseServices.SqlDoms;
|
|
|
using Forks.JsonRpc.Client.Data;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Web.Script.Serialization;
|
|
|
using TSingSoft.WebPluginFramework;
|
|
|
|
|
|
namespace BWP.B3ClientService.Rpcs.BillRpc
|
|
|
{
|
|
|
[Rpc]
|
|
|
public static class HouseAndSanctionRpc
|
|
|
{
|
|
|
static JavaScriptSerializer serializer = new JavaScriptSerializer();
|
|
|
|
|
|
/*
|
|
|
* //原型
|
|
|
SELECT w2.id, w1.* FROM B3ClientService_WeightBill w1, (
|
|
|
|
|
|
SELECT TOP 100 row_number() OVER (order by (case when Remark is null or Remark='' then 0 else 1 end),ID) n, ID FROM B3ClientService_WeightBill ) w2 WHERE w1.ID = w2.ID AND w2.n > 90 ORDER BY w2.n ASC
|
|
|
|
|
|
*/
|
|
|
[Rpc]
|
|
|
public static string GetHouseAndSanctionList(DateTime date, int pageSize, int pageNumber)
|
|
|
{
|
|
|
var temp = new JoinAlias(typeof(OrderTemp));
|
|
|
var query = new DQueryDom(temp);
|
|
|
OrderTemp.Register(query, date, pageSize * pageNumber);
|
|
|
|
|
|
var main = new JoinAlias(typeof(WeightBill));
|
|
|
var detail = new JoinAlias(typeof(WeightBill_Detail));
|
|
|
query.From.AddJoin(JoinType.Left, new DQDmoSource(main), DQCondition.EQ(temp, "ID", main, "ID"));
|
|
|
query.From.AddJoin(JoinType.Left, new DQDmoSource(detail), DQCondition.And(DQCondition.EQ(detail, "DeleteState", false), DQCondition.EQ(main, "ID", detail, "WeightBill_ID")));
|
|
|
query.Columns.Add(DQSelectColumn.Field("ID", main));
|
|
|
query.Columns.Add(DQSelectColumn.Field("B3ID", main));
|
|
|
query.Columns.Add(DQSelectColumn.Field("Employee_Name", main));
|
|
|
query.Columns.Add(DQSelectColumn.Field("Supplier_Name", main));
|
|
|
query.Columns.Add(DQSelectColumn.Field("HouseNames", main));
|
|
|
query.Columns.Add(DQSelectColumn.Field("Number", detail));
|
|
|
query.Where.Conditions.Add(DQCondition.GreaterThan(temp, "RowNumber", pageSize * (pageNumber - 1)));
|
|
|
var list = new List<HouseAndSanctionList>();
|
|
|
using (var session = Dmo.NewSession())
|
|
|
{
|
|
|
using (var reader = session.ExecuteReader(query))
|
|
|
{
|
|
|
while (reader.Read())
|
|
|
{
|
|
|
var entity = new HouseAndSanctionList();
|
|
|
entity.ID = (long)reader[0];
|
|
|
entity.B3ID = (long?)reader[1];
|
|
|
entity.Employee_Name = (string)reader[2];
|
|
|
entity.Supplier_Name = (string)reader[3];
|
|
|
entity.HouseNames = (string)reader[4];
|
|
|
entity.Number = (int?)reader[5];
|
|
|
list.Add(entity);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return serializer.Serialize(list);
|
|
|
}
|
|
|
|
|
|
class OrderTemp
|
|
|
{
|
|
|
public int RowNumber { get; set; }
|
|
|
|
|
|
public long ID { get; set; }
|
|
|
|
|
|
public static void Register(DQueryDom root, DateTime date, int total)
|
|
|
{
|
|
|
var query = new DQueryDom(new JoinAlias("_pageTemp", typeof(WeightBill)));
|
|
|
query.Range = SelectRange.Top(total);
|
|
|
query.Columns.Add(DQSelectColumn.Create(DQExpression.Snippet("row_number() OVER (order by (case when [_pageTemp].[HouseNames] is null or [_pageTemp].[HouseNames]='' then 0 else 1 end),[_pageTemp].[ID])"), "RowNumber"));
|
|
|
query.Columns.Add(DQSelectColumn.Field("ID"));
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.Between("WeighTime", date, date + new TimeSpan(23, 59, 29)), DQCondition.EQ("DeleteState", false)));
|
|
|
root.RegisterQueryTable(typeof(OrderTemp), new string[] { "RowNumber", "ID" }, query);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static int GetMaxPageNumber(DateTime date, int pageSize)
|
|
|
{
|
|
|
var query = new DQueryDom(new JoinAlias(typeof(WeightBill)));
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.Between("WeighTime", date, date + new TimeSpan(23, 59, 29)), DQCondition.EQ("DeleteState", false)));
|
|
|
query.Columns.Add(DQSelectColumn.Count());
|
|
|
var total = Convert.ToInt32(query.EExecuteScalar());
|
|
|
var maxPageSize = total / pageSize;
|
|
|
if (total % pageSize != 0)
|
|
|
maxPageSize += 1;
|
|
|
return maxPageSize;
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static int GetDetailTotalNumber(DateTime date)
|
|
|
{
|
|
|
var main = new JoinAlias(typeof(WeightBill));
|
|
|
var detail = new JoinAlias(typeof(WeightBill_Detail));
|
|
|
var query = new DQueryDom(main);
|
|
|
query.From.AddJoin(JoinType.Left, new DQDmoSource(detail), DQCondition.EQ(main, "ID", detail, "WeightBill_ID"));
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.And(DQCondition.EQ(detail, "DeleteState", false), DQCondition.EQ("DeleteState", false), DQCondition.Between("WeighTime", date.Date, date.Date + new TimeSpan(23, 59, 29)))));
|
|
|
query.Columns.Add(DQSelectColumn.Sum(detail, "Number"));
|
|
|
var result = query.EExecuteScalar();
|
|
|
|
|
|
if (result == null)
|
|
|
return 0;
|
|
|
return Convert.ToInt32(result);
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static string GetHouseAndSanctionDetail(long id)
|
|
|
{
|
|
|
var result = new HouseAndSanctionEdit();
|
|
|
using (var session = Dmo.NewSession())
|
|
|
{
|
|
|
var exist = ClientServiceUtils.Exist<WeightBill>(id);
|
|
|
if (!exist)
|
|
|
throw new Exception("过磅单已被删除");
|
|
|
result.ID = id;
|
|
|
result.HogGrade_ID = InnerBLUtil.GetDmoPropertyByID<long?>(session, typeof(WeightBill), "HogGrade_ID", id);
|
|
|
result.Number = InnerBLUtil.GetDmoProperty<int?>(session, typeof(WeightBill_Detail), "Number", new Tuple<string, object>("WeightBill_ID", id), new Tuple<string, object>("DeleteState", false));
|
|
|
result.HouseDetails = InnerBLUtil.GetPartialFieldsDmoList<WeightBill_HouseDetail>(session, (query) =>
|
|
|
{
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("WeightBill_ID", id), DQCondition.EQ("DeleteState", false)));
|
|
|
}, "ID", "LiveColonyHouse_ID").ToList();
|
|
|
|
|
|
result.SanctionDetails = InnerBLUtil.GetPartialFieldsDmoList<WeightBill_SanctionDetail>(session, (query) =>
|
|
|
{
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("WeightBill_ID", id), DQCondition.EQ("DeleteState", false)));
|
|
|
}, "ID", "AbnormalItem_ID", "Number").ToList();
|
|
|
}
|
|
|
return serializer.Serialize(result);
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static int UpdateInsertHouseAndSanction(string json)
|
|
|
{
|
|
|
var entity = serializer.Deserialize<HouseAndSanctionEdit>(json);
|
|
|
var exist = ClientServiceUtils.Exist<WeightBill>(entity.ID);
|
|
|
if (!exist)
|
|
|
throw new Exception("过磅单已被删除,提交失败!");
|
|
|
var detailExist = ClientServiceUtils.Exist<WeightBill_Detail>("WeightBill_ID", entity.ID);
|
|
|
if (!detailExist)
|
|
|
throw new Exception("过磅记录已被删除,提交失败!");
|
|
|
using (var session = Dmo.NewSession())
|
|
|
{
|
|
|
//为了节省ID,把传过来的通过栋舍匹配,并到原有明细里。然后从新的集合里把上传的集合里不存在的剔除掉
|
|
|
var houseDetail = GetList<WeightBill_HouseDetail>(entity.ID);
|
|
|
foreach (var item in entity.HouseDetails)
|
|
|
{
|
|
|
var first = houseDetail.FirstOrDefault(x => x.LiveColonyHouse_ID == item.LiveColonyHouse_ID);
|
|
|
if (first == null)
|
|
|
houseDetail.Add(item);
|
|
|
else
|
|
|
first.Index = item.Index;
|
|
|
}
|
|
|
var delete = new List<long>();
|
|
|
var houseNames = new List<string>();
|
|
|
foreach (var item in houseDetail)
|
|
|
{
|
|
|
if (entity.HouseDetails.Any(x => x.LiveColonyHouse_ID == item.LiveColonyHouse_ID))
|
|
|
{
|
|
|
UpdateOrInsertHouseDetail(session, item);
|
|
|
houseNames.Add(item.LiveColonyHouse_Name);
|
|
|
}
|
|
|
else
|
|
|
delete.Add(item.LiveColonyHouse_ID ?? 0);
|
|
|
}
|
|
|
|
|
|
if (delete.Any())
|
|
|
{
|
|
|
Delete<WeightBill_HouseDetail>(session, entity.ID, (dom) =>
|
|
|
{
|
|
|
dom.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("LiveColonyHouse_ID"), delete.Select(x => DQExpression.Value(x)).ToArray()));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
delete.Clear();
|
|
|
var sanctionDetail = GetList<WeightBill_SanctionDetail>(entity.ID);
|
|
|
foreach (var item in entity.SanctionDetails)
|
|
|
{
|
|
|
var first = sanctionDetail.FirstOrDefault(x => x.AbnormalItem_ID == item.AbnormalItem_ID);
|
|
|
if (first == null)
|
|
|
sanctionDetail.Add(item);
|
|
|
else
|
|
|
{
|
|
|
first.Index = item.Index;
|
|
|
first.Number = item.Number;
|
|
|
}
|
|
|
}
|
|
|
foreach (var item in sanctionDetail)
|
|
|
{
|
|
|
if (entity.SanctionDetails.Any(x => x.AbnormalItem_ID == item.AbnormalItem_ID))
|
|
|
UpdateOrInsertSanctionDetail(session, item);
|
|
|
else
|
|
|
delete.Add(item.AbnormalItem_ID ?? 0);
|
|
|
}
|
|
|
|
|
|
if (delete.Any())
|
|
|
{
|
|
|
Delete<WeightBill_SanctionDetail>(session, entity.ID, (dom) =>
|
|
|
{
|
|
|
dom.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("AbnormalItem_ID"), delete.Select(x => DQExpression.Value(x)).ToArray()));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
var dUpdate = new DQUpdateDom(typeof(WeightBill_Detail));
|
|
|
dUpdate.Columns.Add(new DQUpdateColumn("Number", entity.Number));
|
|
|
dUpdate.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("WeightBill_ID", entity.ID), DQCondition.EQ("DeleteState", false)));
|
|
|
session.ExecuteNonQuery(dUpdate);
|
|
|
|
|
|
var update = new DQUpdateDom(typeof(WeightBill));
|
|
|
update.Columns.Add(new DQUpdateColumn("HogGrade_ID", entity.HogGrade_ID));
|
|
|
update.Columns.Add(new DQUpdateColumn("HogGrade_Name", entity.HogGrade_Name));
|
|
|
update.Columns.Add(new DQUpdateColumn("Inspector_ID", entity.Inspector_ID));
|
|
|
update.Columns.Add(new DQUpdateColumn("Inspector_Name", entity.Inspector_Name));
|
|
|
update.Columns.Add(new DQUpdateColumn("HouseNames", string.Join(",", houseNames)));
|
|
|
update.Columns.Add(new DQUpdateColumn("SanctionNumber", entity.SanctionDetails.Sum(x => x.Number ?? 0)));
|
|
|
update.Columns.Add(new DQUpdateColumn("Sync", false));
|
|
|
update.Columns.Add(new DQUpdateColumn("RowVersion", DQExpression.Add(DQExpression.Field("RowVersion"), DQExpression.Value(1))));
|
|
|
update.Columns.Add(new DQUpdateColumn("ModifyTime", DateTime.Now));
|
|
|
update.Where.Conditions.Add(DQCondition.EQ("ID", entity.ID));
|
|
|
session.ExecuteNonQuery(update);
|
|
|
session.Commit();
|
|
|
}
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
static List<T> GetList<T>(long billID)
|
|
|
{
|
|
|
var query = new DmoQuery(typeof(T));
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("DeleteState", false), DQCondition.EQ("WeightBill_ID", billID)));
|
|
|
return query.EExecuteList().Cast<T>().ToList();
|
|
|
}
|
|
|
|
|
|
static void Delete<T>(IDmoSession session, long billID, Action<DQUpdateDom> conditions)
|
|
|
where T : SyncBase
|
|
|
{
|
|
|
var delete = new DQUpdateDom(typeof(T));
|
|
|
delete.Columns.Add(new DQUpdateColumn("DeleteState", true));
|
|
|
delete.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("DeleteState", false), DQCondition.EQ("WeightBill_ID", billID)));
|
|
|
conditions(delete);
|
|
|
session.ExecuteNonQuery(delete);
|
|
|
}
|
|
|
|
|
|
static void UpdateOrInsertHouseDetail(IDmoSession session, WeightBill_HouseDetail detail)
|
|
|
{
|
|
|
if (detail.ID == 0)
|
|
|
{
|
|
|
session.Insert(detail);
|
|
|
return;
|
|
|
}
|
|
|
var update = new DQUpdateDom(typeof(WeightBill_HouseDetail));
|
|
|
update.Columns.Add(new DQUpdateColumn("Index", detail.Index));
|
|
|
update.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("DeleteState", false), DQCondition.EQ("LiveColonyHouse_ID", detail.LiveColonyHouse_ID)));
|
|
|
session.ExecuteNonQuery(update);
|
|
|
}
|
|
|
|
|
|
static void UpdateOrInsertSanctionDetail(IDmoSession session, WeightBill_SanctionDetail detail)
|
|
|
{
|
|
|
if (detail.ID == 0)
|
|
|
{
|
|
|
session.Insert(detail);
|
|
|
return;
|
|
|
}
|
|
|
var update = new DQUpdateDom(typeof(WeightBill_SanctionDetail));
|
|
|
update.Columns.Add(new DQUpdateColumn("Index", detail.Index));
|
|
|
update.Columns.Add(new DQUpdateColumn("Number", detail.Number));
|
|
|
update.Columns.Add(new DQUpdateColumn("Sanction_ID", detail.Sanction_ID));
|
|
|
update.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("DeleteState", false), DQCondition.EQ("AbnormalItem_ID", detail.AbnormalItem_ID)));
|
|
|
session.ExecuteNonQuery(update);
|
|
|
}
|
|
|
}
|
|
|
}
|