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

namespace ButcherFactory.BO.LocalBL
{
  public static class FormClientGoodsSetBL
  {
    public static IEnumerable<ClientGoodsSet_Detail> GetGoodsList()
    {
      var main = new JoinAlias(typeof(ClientGoodsSet));
      var detail = new JoinAlias(typeof(ClientGoodsSet_Detail));
      var query = new DQueryDom(main);
      query.From.AddJoin(JoinType.Inner, new DQDmoSource(detail), DQCondition.EQ(main, "ID", detail, "ClientGoodsSet_ID"));
      query.Columns.Add(DQSelectColumn.Field("Goods_ID", detail));
      query.Columns.Add(DQSelectColumn.Field("Goods_Name", detail));
      query.OrderBy.Expressions.Add(DQOrderByExpression.Create(detail, "Goods_ID"));
      var list = query.EExecuteList<long, string>();
      return list.Select(x => new ClientGoodsSet_Detail { Goods_ID = x.Item1, Goods_Name = x.Item2 });
    }

    public static Dictionary<string, Dictionary<string, IEnumerable<ClientGoodsSet_Detail>>> GetGoodsSetDic()
    {
      var main = new JoinAlias(typeof(ClientGoodsSet));
      var detail = new JoinAlias(typeof(ClientGoodsSet_Detail));
      var set = new JoinAlias(typeof(GoodsSetTemp));
      var query = new DQueryDom(main);
      query.RegisterQueryTable(typeof(GoodsSetTemp), new string[] { "DetailID" }, GoodsSetTemp.GetQueryDom());
      query.From.AddJoin(JoinType.Inner, new DQDmoSource(detail), DQCondition.EQ(main, "ID", detail, "ClientGoodsSet_ID"));
      query.From.AddJoin(JoinType.Left, new DQDmoSource(set), DQCondition.EQ(detail, "ID", set, "DetailID"));
      query.Columns.Add(DQSelectColumn.Field("Name", main));
      query.Columns.Add(DQSelectColumn.Field("Goods_Name", detail));
      query.Columns.Add(DQSelectColumn.Field("ID", detail));
      query.Columns.Add(DQSelectColumn.Field("DetailID", set));
      query.Columns.Add(DQSelectColumn.Field("ClientGoodsClass_Name", main));
      query.OrderBy.Expressions.Add(DQOrderByExpression.Create(main, "ID"));
      query.OrderBy.Expressions.Add(DQOrderByExpression.Create(detail, "ID"));
      var list = query.EExecuteList<string, string, long, long?, string>();
      var result = new Dictionary<string, Dictionary<string, IEnumerable<ClientGoodsSet_Detail>>>();

      foreach (var items in list.GroupBy(x => x.Item5))
      {
        var r = new Dictionary<string, IEnumerable<ClientGoodsSet_Detail>>();
        foreach (var item in items.GroupBy(x => x.Item1))
        {
          var arr = item.Select(x => new ClientGoodsSet_Detail { Goods_Name = x.Item2, ID = x.Item3, Selected = x.Item4.HasValue }); 
          r.Add(item.Key, arr); 
        }
        var k = items.Key;
        if (string.IsNullOrEmpty(k))
          k = "空";
        result.Add(k, r);
      }
      return result;
    }

    public static IEnumerable<ClientGoodsSet_Detail> GetGoodsSetDic2(string spell)
    {
      var main = new JoinAlias(typeof(ClientGoodsSet));
      var detail = new JoinAlias(typeof(ClientGoodsSet_Detail));
      var set = new JoinAlias(typeof(GoodsSetTemp));
      var query = new DQueryDom(main);
      query.RegisterQueryTable(typeof(GoodsSetTemp), new string[] { "DetailID" }, GoodsSetTemp.GetQueryDom());
      query.From.AddJoin(JoinType.Inner, new DQDmoSource(detail), DQCondition.EQ(main, "ID", detail, "ClientGoodsSet_ID"));
      query.From.AddJoin(JoinType.Left, new DQDmoSource(set), DQCondition.EQ(detail, "ID", set, "DetailID"));
      
      query.Columns.Add(DQSelectColumn.Field("Goods_Name", detail));
      query.Columns.Add(DQSelectColumn.Field("ID", detail));
      query.Columns.Add(DQSelectColumn.Field("DetailID", set)); 

      if (!string.IsNullOrEmpty(spell))
      {
        query.Where.Conditions.Add(DQCondition.Or(
          DQCondition.Like(detail, "Goods_Name", spell), 
          DQCondition.Like(detail, "Goods_Spell", spell)));
      }
       
      var list = query.EExecuteList<  string, long, long? >();
      var result = new List<ClientGoodsSet_Detail>();

      foreach (var item  in list )
      { 
        result.Add(new ClientGoodsSet_Detail { 
          Goods_Name = item.Item1, 
          ID = item.Item2, 
          Selected = item.Item3.HasValue });
      }
      return result;
    }
    public static Dictionary<string, List<ClientGoodsSet_Detail>> GetSelectedDetail()
    {
      var main = new JoinAlias(typeof(ClientGoodsSet));
      var detail = new JoinAlias(typeof(ClientGoodsSet_Detail));
      var set = new JoinAlias(typeof(GoodsSetTemp));
      var query = new DQueryDom(main);
      query.RegisterQueryTable(typeof(GoodsSetTemp), new string[] { "DetailID" }, GoodsSetTemp.GetQueryDom());
      query.From.AddJoin(JoinType.Inner, new DQDmoSource(detail), DQCondition.EQ(main, "ID", detail, "ClientGoodsSet_ID"));
      query.From.AddJoin(JoinType.Inner, new DQDmoSource(set), DQCondition.EQ(detail, "ID", set, "DetailID"));
      query.Columns.Add(DQSelectColumn.Field("Name", main));
      query.Columns.Add(DQSelectColumn.Field("Goods_ID", detail));
      query.Columns.Add(DQSelectColumn.Field("Goods_Name", detail));
      query.Columns.Add(DQSelectColumn.Field("StandardWeight", detail));
      query.Columns.Add(DQSelectColumn.Field("StandardWeightUp", detail));
      query.Columns.Add(DQSelectColumn.Field("StandardWeightLow", detail));//5
      query.Columns.Add(DQSelectColumn.Field("Goods_Spec", detail));
      query.Columns.Add(DQSelectColumn.Field("Goods_Code", detail));
      query.Columns.Add(DQSelectColumn.Field("GoodsType", detail));
      query.Columns.Add(DQSelectColumn.Field("StandardPic", detail));
      query.Columns.Add(DQSelectColumn.Field("MainUnit", detail));//10
      query.Columns.Add(DQSelectColumn.Field("ShotPrintName", detail));
      query.Columns.Add(DQSelectColumn.Field("EachNumber", detail));
      query.Columns.Add(DQSelectColumn.Field("NoTotalCode", detail));
      query.Columns.Add(DQSelectColumn.Field("Goods_CustomerDefine", detail));
      query.OrderBy.Expressions.Add(DQOrderByExpression.Create(main, "ID"));
      query.OrderBy.Expressions.Add(DQOrderByExpression.Create(detail, "ID"));

      var result = new Dictionary<string, List<ClientGoodsSet_Detail>>();
      using (var session = DmoSession.New())
      {
        using (var reader = session.ExecuteReader(query))
        {
          var key = string.Empty;
          while (reader.Read())
          {
            key = (string)reader[0];
            var entity = new ClientGoodsSet_Detail();
            entity.Goods_ID = (long)reader[1];
            entity.Goods_Name = (string)reader[2];
            entity.StandardWeight = (decimal?)reader[3];
            entity.StandardWeightUp = (decimal?)reader[4];
            entity.StandardWeightLow = (decimal?)reader[5];
            entity.Goods_Spec = (string)reader[6];
            entity.Goods_Code = (string)reader[7];
            entity.GoodsType = (short?)reader[8];
            entity.StandardPic = (bool)reader[9];
            entity.MainUnit = (string)reader[10];
            entity.ShotPrintName = (string)reader[11];
            entity.EachNumber = (int?)reader[12];
            entity.NoTotalCode = (bool)reader[13];
            entity.Goods_CustomerDefine = (string)reader[14];
            if (result.ContainsKey(key))
              result[key].Add(entity);
            else
              result.Add(key, new List<ClientGoodsSet_Detail> { entity });
          }
        }
      }
      return result;
    }

    public static void InsertWorkerGoodsSet(long detailID)
    {
      using (var session = DmoSession.New())
      {
        if (!ExistSet(detailID, session))
        {
          var entity = new WorkerGoodsSetProfile { Worker_ID = BO.Utils.AppContext.Worker.ID, ClientGoodsSet_Detail_ID = detailID };
          session.Insert(entity);
          session.Commit();
        }
      }
    }

    static bool ExistSet(long detailID, IDmoSession session)
    {
      var query = new DQueryDom(new JoinAlias(typeof(WorkerGoodsSetProfile)));
      query.Columns.Add(DQSelectColumn.Create(DQExpression.Value(1), "c"));
      query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Worker_ID", BO.Utils.AppContext.Worker.ID), DQCondition.EQ("ClientGoodsSet_Detail_ID", detailID)));
      return query.EExecuteScalar(session) != null;
    }

    public static void DeleteWorkGoodsSet(long detailID)
    {
      var delete = new DQDeleteDom(typeof(WorkerGoodsSetProfile));
      delete.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Worker_ID", BO.Utils.AppContext.Worker.ID), DQCondition.EQ("ClientGoodsSet_Detail_ID", detailID)));
      delete.EExecute();
    }
  }

  class GoodsSetTemp
  {
    public long DetailID { get; set; }

    public static DQueryDom GetQueryDom()
    {
      var query = new DQueryDom(new JoinAlias(typeof(WorkerGoodsSetProfile)));
      query.Columns.Add(DQSelectColumn.Field("ClientGoodsSet_Detail_ID"));
      query.Where.Conditions.Add(DQCondition.EQ("Worker_ID", BO.Utils.AppContext.Worker.ID));
      return query;
    }
  }
}
