using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using BO;
using BO.Utils;
using Distribution.LocalBo;
using Forks.EnterpriseServices.DomainObjects2;
using Forks.EnterpriseServices.DomainObjects2.DQuery;
using Forks.JsonRpc.Client;
using Newtonsoft.Json;

namespace Distribution
{
  public class SaleOutStoreRpc
  {
    //已天为单位更新
    public static BindingList<SaleOutStore> SyncList(DateTime date, string customerName,string sendLineName,string billstate,bool updateLocalDb)
    {
      var resList=new BindingList<SaleOutStore>();
      if (LoginRpcUtil.TestConnection(100))
      {
        var json = RpcFacade.Call<string>("/MainSystem/B3ClientService/Rpcs/BillRpc/SaleOutStoreRpc/SyncList", date,customerName,sendLineName,billstate);
        var list = JsonConvert.DeserializeObject<List<SaleOutStore>>(json);
        if (updateLocalDb)
        {
          //销售出库本地只添加，不做更新操作 特别是已配货数量这块 已本地为主
          AddLocalSaleOutStore(list);
        }
        foreach (SaleOutStore store in list)
        {
          resList.Add(store);
        }
      }
      else
      {
        var dmoquery = new DmoQuery(typeof(SaleOutStore));
        dmoquery.Where.Conditions.Add(DQCondition.GreaterThanOrEqual("LoadTime", date));
        dmoquery.Where.Conditions.Add(DQCondition.LessThan("LoadTime", date.AddDays(1)));
        if (!string.IsNullOrWhiteSpace(customerName))
        {
          dmoquery.Where.Conditions.Add(DQCondition.EQ("Customer_Name", customerName));
        }
        if (!string.IsNullOrWhiteSpace(sendLineName))
        {
          dmoquery.Where.Conditions.Add(DQCondition.EQ("DeliverGoodsLine_Name", sendLineName));
        }
        if (!string.IsNullOrWhiteSpace(sendLineName))
        {
          dmoquery.Where.Conditions.Add(DQCondition.EQ("BillState", billstate));
        }
        var list = new List<SaleOutStore>();
        using (var session=LocalDmoSession.New())
        {
           list =session.ExecuteList(dmoquery).Cast<SaleOutStore>().ToList();
        }
        foreach (SaleOutStore store in list)
        {
          resList.Add(store);
        }
      }
      return resList;
    }

    private static void AddLocalSaleOutStore(List<SaleOutStore> list)
    {
      if (list.Count == 0)
      {
        return;
      }
      using (var session=LocalDmoSession.New())
      {
        foreach (SaleOutStore store in list)
        {
          if (!IsExist(session, store.SaleOutStoreDetail_ID))
          {
            session.Insert(store);
          }
        }
        session.Commit();
      }
    }

    static bool IsExist(IDmoSession session, long detailid)
    {
      var query=new DQueryDom(new JoinAlias(typeof(SaleOutStore)));      
      query.Where.Conditions.Add(DQCondition.EQ("SaleOutStoreDetail_ID",detailid));
      query.Columns.Add(DQSelectColumn.Field("SaleOutStoreDetail_ID"));
      var res = session.ExecuteScalar(query);
      if (res is DBNull || res == null)
      {
        return false;
      }
      return true;
    }

  }
}
