using ButcherFactory.BO.Utils;
using Forks.EnterpriseServices.DomainObjects2;
using Forks.EnterpriseServices.DomainObjects2.DQuery;
using Forks.EnterpriseServices.SqlDoms;
using Forks.JsonRpc.Client;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ButcherFactory.BO.LocalBL
{
  public static class SegmentStockUpBL
  {
    const string RpcPath = @"/MainSystem/B3Sale/Rpcs/";
    const string MESPath = @"/MainSystem/B3ClientService/Rpcs/";

    /// <summary>
    /// </summary>
    /// <param name="code"></param>
    /// <returns>GoodsCode:StringExt1,Weight:DecimalExt1</returns>
    public static Tuple<string, decimal?, bool> StockUpScan(string code)
    {
      var json = ButcherFactoryUtil.SecondUrlCall<string>(MESPath + "SegmentInStoreRpc/StockUpScan", code);
      return JsonConvert.DeserializeObject<Tuple<string, decimal?, bool>>(json);
    }

    public static List<SaleOutStoreInfo> GetSaleOutStoreList(DateTime date)
    {
      try
      {
        var json = RpcFacade.Call<string>(RpcPath + "SaleOutStoreRpc/GetSaleOutStoreByDate", date);
        var list = JsonConvert.DeserializeObject<List<SaleOutStoreInfo>>(json);
        foreach (var item in list)
          item.Date = date;
        return list;
      }
      catch
      {
#if DEBUG
        throw;
#endif
        return new List<SaleOutStoreInfo>();
      }
    }

    public static List<SegmentStockUp> GetLocalList(DateTime date)
    {
      using (var session = DmoSession.New())
      {
        DeleteOld(session);

        var query = new DmoQuery(typeof(SegmentStockUp));
        query.Where.Conditions.Add(DQCondition.EQ("Date", date));
        return session.ExecuteList(query).Cast<SegmentStockUp>().ToList();
      }
    }

    static void DeleteOld(IDmoSession session)
    {
      var delete = new DQDeleteDom(typeof(SegmentStockUp));
      delete.Where.Conditions.Add(DQCondition.LessThan("Date", DateTime.Today.AddDays(-1)));
      session.ExecuteNonQuery(delete);
    }

    //当明细备完后，要返回该明细已备数量。
    public static AlreadyStockUp Insert(SegmentStockUp detail, AlreadyStockUp already, decimal totalNumber, bool stockBySecond)
    {
      try
      {
        var json = JsonConvert.SerializeObject(new { DetailID = detail.DetailID, StandardPic = detail.StandardPic, BarCode = detail.BarCode, SecondNumber = detail.SecondNumber, UnitNumber = detail.UnitNumber });
        var r = RpcFacade.Call<string>(RpcPath + "SaleOutStoreRpc/InsertStockUpDetail", json, JsonConvert.SerializeObject(already), totalNumber, stockBySecond);
        var outSide = JsonConvert.DeserializeObject<AlreadyStockUp>(r);
        if (outSide.State == 1)
        {
          using (var session = DmoSession.New())
          {
            session.Insert(detail);
            session.Commit();
          }
        }
        return outSide;
      }
      catch
      {
        return null;
      }
    }

    public static List<AlreadyStockUp> SyncAlreadyNumber(IEnumerable<long> detailIDs, long? maxID)
    {
      if (detailIDs.Count() == 0 && maxID == null)
        return new List<AlreadyStockUp>();
      var json = JsonConvert.SerializeObject(detailIDs);
      try
      {
        var result = RpcFacade.Call<string>(RpcPath + "SaleOutStoreRpc/GetStockNumber", json, maxID);
        return JsonConvert.DeserializeObject<List<AlreadyStockUp>>(result);
      }
      catch
      {
#if DEBUG
        throw;
#endif
        return new List<AlreadyStockUp>();
      }
    }

    public static List<DeliverLineGroup> GetDeliverLineGroupList()
    {
      var json = RpcFacade.Call<string>(RpcPath + "SaleOutStoreRpc/GetDeliverLineGroup");
      return JsonConvert.DeserializeObject<List<DeliverLineGroup>>(json);
    }
  }
}
