using BO.BO.BaseInfo;
using BO.BO.Bill;
using Forks.EnterpriseServices.DomainObjects2;
using Forks.EnterpriseServices.DomainObjects2.DQuery;
using Forks.EnterpriseServices.SqlDoms;
using Forks.JsonRpc.Client;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using TSingSoft.WebPluginFramework;

namespace BO.Utils.BillRpc
{
  public static class GradeAndWeightRpc
  {
    static JavaScriptSerializer serializer = new JavaScriptSerializer();

    public static List<GradeAndWeight> GetGradeAndWeightList(DateTime date, bool isTang)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/GradeAndWeightRpc/GetGradeAndWeightList";
      var result = RpcFacade.Call<string>(method, date, isTang);
      var list = serializer.Deserialize<List<GradeAndWeight>>(result);
      short type = 1;
      if (isTang)
        type = 0;
      var sum = GetAlreadyNumber(date, type);
      var finish = GetFinishRelate(date, type);
      foreach (var item in list)
      {
        var first = sum.FirstOrDefault(x => x.Item1 == item.Order);
        if (first != null)
          item.Already = first.Item2;
        item.Finish = finish.Contains(item.Order);
      }
      return list;
    }

    static List<int> GetFinishRelate(DateTime date, short type)
    {
      var query = new DQueryDom(new JoinAlias(typeof(OrderGradeFinishRelate)));
      query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Date", date), DQCondition.EQ("Technics", type)));
      query.Columns.Add(DQSelectColumn.Field("Order"));
      var list = new List<int>();
      using (var session = LocalDmoSession.New())
      {
        using (var reader = session.ExecuteReader(query))
        {
          while (reader.Read())
            list.Add((int)reader[0]);
        }
      }
      return list;
    }

    static List<Tuple<int, int>> GetAlreadyNumber(DateTime date, short type)
    {
      var query = new DQueryDom(new JoinAlias(typeof(GradeAndWeight_Detail)));
      query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("IsDeleted", false), DQCondition.EQ("Date", date), DQCondition.EQ("Technics", type), DQCondition.IsNotNull(DQExpression.Field("Order"))));
      query.Columns.Add(DQSelectColumn.Field("Order"));
      query.Columns.Add(DQSelectColumn.Count());
      query.GroupBy.Expressions.Add(DQExpression.Field("Order"));
      var list = new List<Tuple<int, int>>();
      using (var session = LocalDmoSession.New())
      {
        using (var reader = session.ExecuteReader(query))
        {
          while (reader.Read())
            list.Add(new Tuple<int, int>((int)reader[0], Convert.ToInt32(reader[1])));
        }
        return list;
      }
    }

    public static List<BodyDiscontItem> GetBodyDiscontItem()
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/GradeAndWeightRpc/GetBodyDiscontItemSetting";
      var json = RpcFacade.Call<string>(method);
      return serializer.Deserialize<List<BodyDiscontItem>>(json);
    }

    public static void SaveBodyDiscontItem(List<CTuple<long, decimal?>> list)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/GradeAndWeightRpc/SaveBodyDiscontItemSetting";
      RpcFacade.Call<int>(method, serializer.Serialize(list));
    }

    public static List<DataConfirmList> GetDataConfirmList(DateTime date, int? order)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/GradeAndWeightRpc/GetDataConfirmList";
      var result = RpcFacade.Call<string>(method, date, order);
      return serializer.Deserialize<List<DataConfirmList>>(result);
    }
  }

  public static class LocalGradeAndWeightBL
  {
    static JavaScriptSerializer serializer = new JavaScriptSerializer();

    public static void SetGradeFinish(int order, DateTime date, short technics)
    {
      using (var session = LocalDmoSession.New())
      {
        if (Finished(session, date, order, technics))
          return;
        session.Insert(new OrderGradeFinishRelate(order, date, technics));
        session.Commit();
      }
    }

    static bool Finished(IDmoSession session, DateTime date, int order, short technics)
    {
      var query = new DQueryDom(new JoinAlias(typeof(OrderGradeFinishRelate)));
      query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Order", order), DQCondition.EQ("Date", date), DQCondition.EQ("Technics", technics)));
      query.Columns.Add(DQSelectColumn.Create(DQExpression.Field("1"), "c"));
      return session.ExecuteScalar(query) != null;
    }

    public static void DeleteBySID(long sid)
    {
      var updateDom = new DQUpdateDom(typeof(GradeAndWeight_Detail));
      updateDom.Where.Conditions.Add(DQCondition.EQ("SID", sid));
      updateDom.Columns.Add(new DQUpdateColumn("IsDeleted", true));
      updateDom.Columns.Add(new DQUpdateColumn("Sync", false));
      using (var session = LocalDmoSession.New())
      {
        session.ExecuteNonQuery(updateDom);
        session.Commit();
      }
    }

    public static BindingList<GradeAndWeight_Detail> GetDetails(DateTime date, int top = 15)
    {
      var bindList = new BindingList<GradeAndWeight_Detail>();
      var list = new List<GradeAndWeight_Detail>();

      var query = new DmoQuery(typeof(GradeAndWeight_Detail));
      query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("IsDeleted", false), DQCondition.EQ("Date", date)));
      query.OrderBy.Expressions.Add(DQOrderByExpression.Create("Index", true));
      query.Range = SelectRange.Top(top);
      using (var session = LocalDmoSession.New())
      {
        list = session.ExecuteList(query).Cast<GradeAndWeight_Detail>().ToList();
      }
      foreach (GradeAndWeight_Detail detail in list)
      {
        bindList.Add(detail);
      }
      return bindList;
    }

    public static List<Tuple<short, int, decimal>> GetSumNumber(DateTime date, int? order, short? type)
    {
      var query = new DQueryDom(new JoinAlias(typeof(GradeAndWeight_Detail)));
      query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("IsDeleted", false), DQCondition.EQ("Date", date), DQCondition.IsNotNull(DQExpression.Field("Technics"))));
      if (order.HasValue)
        query.Where.Conditions.Add(DQCondition.EQ("Order", order));
      if (type.HasValue)
        query.Where.Conditions.Add(DQCondition.EQ("Technics", type));
      query.Columns.Add(DQSelectColumn.Field("Technics"));
      query.Columns.Add(DQSelectColumn.Count());
      query.Columns.Add(DQSelectColumn.Sum("Weight"));
      query.GroupBy.Expressions.Add(DQExpression.Field("Technics"));
      var list = new List<Tuple<short, int, decimal>>();
      using (var session = LocalDmoSession.New())
      {
        using (var reader = session.ExecuteReader(query))
        {
          while (reader.Read())
          {
            decimal w = 0;
            if (reader[2] != null)
              w = Convert.ToDecimal(reader[2]);
            list.Add(new Tuple<short, int, decimal>((short)reader[0], Convert.ToInt32(reader[1]), w));
          }
        }
        return list;
      }
    }

    public static void Insert(GradeAndWeight_Detail detail)
    {
      using (var session = LocalDmoSession.New())
      {
        if (detail.SID != 0)
          throw new Exception("Insert时要保证SID不为0");
        session.Insert(detail);
        session.Commit();
      }
    }

    public static void Update(GradeAndWeight_Detail detail, params string[] properties)
    {
      using (var session = LocalDmoSession.New())
      {
        if (detail.SID == 0)
          throw new Exception("Update时要保证SID不能为0");
        if (properties.Contains("ID"))
          throw new Exception("ID不能通过该方法维护");
        if (properties.Length == 0)
          throw new Exception("Update时要给出属性数组");

        var type = typeof(GradeAndWeight_Detail);
        var update = new DQUpdateDom(type);
        update.Where.Conditions.Add(DQCondition.EQ("SID", detail.SID));
        foreach (var p in properties)
        {
          update.Columns.Add(new DQUpdateColumn(p, type.GetProperty(p).GetValue(detail)));
        }
        update.Columns.Add(new DQUpdateColumn("Sync", false));

        session.ExecuteNonQuery(update);
        session.Commit();
      }
    }

    public static void Update(GradeAndWeight_Detail detail, GradeAndWeight toEntity)
    {
      if (toEntity == null)
      {
        throw new Exception(string.Format("请选择左侧的{0}顺序", detail.Technics_Name));
      }
      detail.Order = toEntity.Order;
      detail.OrderDetail_ID = toEntity.OrderDetail_ID;
      Update(detail, "OrderDetail_ID", "Order", "Livestock_ID", "Livestock_Name", "Technics", "Technics_Name");
    }

    public static void Sync()
    {
      ClearUnSyncDelete();
      var syncs = GetAllNeedSyncDetails();
      foreach (var detail in syncs)
      {       
        const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/GradeAndWeightRpc/UpdateOrInsertDetailOrDelete";
        var id = RpcFacade.Call<long>(method, serializer.Serialize(detail));
        if (detail.IsDeleted)
          Delete(detail.SID);
        else
          SetDetailSynced(detail, id);
      }
    }

    static void ClearUnSyncDelete()
    {
      using (var session = LocalDmoSession.New())
      {
        var delete = new DQDeleteDom(typeof(GradeAndWeight_Detail));
        delete.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("IsDeleted", true), DQCondition.EQ("ID", 0)));
        session.ExecuteNonQuery(delete);
        session.Commit();
      }
    }

    static void Delete(long sid)
    {
      using (var session = LocalDmoSession.New())
      {
        var delete = new DQDeleteDom(typeof(GradeAndWeight_Detail));
        delete.Where.Conditions.Add(DQCondition.EQ("SID", sid));
        session.ExecuteNonQuery(delete);
        session.Commit();
      }
    }

    static IEnumerable<GradeAndWeight_Detail> GetAllNeedSyncDetails()
    {
      using (var session = LocalDmoSession.New())
      {
        var query = new DmoQuery(typeof(GradeAndWeight_Detail));
        query.OrderBy.Expressions.Add(DQOrderByExpression.Create("SID"));
        query.Range = SelectRange.Top(50);
        query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Sync", false),
          DQCondition.IsNotNull(DQExpression.Field("Technics"))));
        return session.ExecuteList(query).Cast<GradeAndWeight_Detail>();
      }
    }

    static void SetDetailSynced(GradeAndWeight_Detail detail, long id)
    {
      using (var session = LocalDmoSession.New())
      {
        var update = new DQUpdateDom(typeof(GradeAndWeight_Detail));
        update.Columns.Add(new DQUpdateColumn("Sync", true));
        if (detail.ID == 0)
        {
          detail.ID = id;
          update.Columns.Add(new DQUpdateColumn("ID", id));
        }
        update.Where.Conditions.Add(DQCondition.EQ("SID", detail.SID));
        session.ExecuteNonQuery(update);
        session.Commit();
      }
    }

    public static int GetTodayTotalCount(DateTime date)
    {
      var query = new DQueryDom(new JoinAlias(typeof(GradeAndWeight_Detail)));
      query.Columns.Add(DQSelectColumn.Count());
      query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("IsDeleted", false), DQCondition.EQ("Date", date)));
      using (var session = LocalDmoSession.New())
      {
        return Convert.ToInt32(session.ExecuteScalar(query));
      }
    }

    public static List<GradeAndWeight_Detail> GetGradeAndWeightDetails(DateTime date, int? order, int? index, short? type)
    {
      var query = new DmoQuery(typeof(GradeAndWeight_Detail));
      query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("IsDeleted", false), DQCondition.EQ("Date", date)));
      if (order.HasValue)
        query.Where.Conditions.Add(DQCondition.Or(DQCondition.IsNull(DQExpression.Field("Order")), DQCondition.EQ("Order", order)));
      if (index.HasValue)
        query.Where.Conditions.Add(DQCondition.EQ("Index", index));
      if (type.HasValue)
        query.Where.Conditions.Add(DQCondition.Or(DQCondition.IsNull(DQExpression.Field("Technics")), DQCondition.EQ("Technics", type)));
      using (var session = LocalDmoSession.New())
      {
        return session.ExecuteList(query).Cast<GradeAndWeight_Detail>().ToList();
      }
    }

    public static void InsertOrUpdate(GradeAndWeight_Detail entity)
    {
      if (entity.SID == 0)
      {
        using (var session = LocalDmoSession.New())
        {
          session.Insert(entity);
          session.Commit();
        }
      }
      else
        Update(entity, "Technics", "Technics_Name", "Index", "Order", "Weight", "Livestock_ID", "Livestock_Name");
    }
  }
}
