public static JArray Get采购询价商品(this Pinhua2Context context, string customerId, string orderId)
        {
            if (orderId == null)
            {
                return(new JArray());
            }

            var set = from m in context.tb_报价表.AsNoTracking()
                      join d in context.tb_报价表D.AsNoTracking() on m.RecordId equals d.RecordId
                      join x in context.tb_商品表.AsNoTracking() on d.品号 equals x.品号
                      where /*m.往来号 == customerId &&*/ m.业务类型 == "采购询价" && m.单号 == orderId
                      select new
            {
                询 = d,
                品 = x
            };
            JArray jsonArray = new JArray();

            foreach (var item in set)
            {
                jsonArray.Add(Pinhua2Helper.JObjectFromMerge(item.询, item.品));
            }

            return(jsonArray);
        }
        public static JArray Get销售订单商品(this Pinhua2Context context, string customerId)
        {
            if (string.IsNullOrEmpty(customerId))
            {
                return(new JArray());
            }

            var set = from m in context.tb_订单表.AsNoTracking()
                      join d in context.tb_订单表D.AsNoTracking() on m.RecordId equals d.RecordId
                      join x in context.tb_商品表.AsNoTracking() on d.品号 equals x.品号
                      where m.往来号 == customerId && m.业务类型 == "销售订单"
                      select new
            {
                订 = d,
                品 = x
            };
            JArray jsonArray = new JArray();

            foreach (var item in set)
            {
                jsonArray.Add(Pinhua2Helper.JObjectFromMerge(item.订, item.品));
            }

            return(jsonArray);
        }
        public static JArray Get销售出库商品(this Pinhua2Context context, string customerId)
        {
            if (string.IsNullOrEmpty(customerId))
            {
                return(new JArray());
            }

            var set = from m in context.tb_IO.AsNoTracking()
                      join d in context.tb_IOD.AsNoTracking() on m.RecordId equals d.RecordId
                      join x in context.tb_商品表.AsNoTracking() on d.品号 equals x.品号
                      where m.往来号 == customerId && m.类型 == "销售出库"
                      select new
            {
                出 = d,
                品 = x
            };
            JArray jsonArray = new JArray();

            foreach (var item in set)
            {
                var jsonObj = Pinhua2Helper.JObjectFromMerge(item.出, item.品);
                jsonObj["个数"] = jsonObj["发"];
                jsonArray.Add(jsonObj);
            }

            return(jsonArray);
        }
        public TRemote AddRecord <TLocal, TRemote>(TLocal local,
                                                   Action <TLocal> BeforeNew = null /*, Action<TRemote> AfterNew = null*/)
            where TLocal : _BaseTableMain
            where TRemote : _BaseTableMain
        {
            BeforeNew?.Invoke(local);

            Pinhua2Helper.CompleteMainOnCreate(local);
            var remote = _mapper.Map <TLocal, TRemote>(local);

            _pinhua2Context.Entry <TRemote>(remote).State = EntityState.Added;

            //AfterNew?.Invoke(remote);
            //var i = _pinhua2Context.SaveChanges();
            return(remote);
        }
        public static TOriginal RecordAdd <TDto, TOriginal>(this Pinhua2Context context, TDto local,
                                                            Action <TDto> Adding = null /*, Action<TRemote> AfterNew = null*/)
            where TDto : _BaseTableMain
            where TOriginal : _BaseTableMain
        {
            Adding?.Invoke(local);

            Pinhua2Helper.CompleteMainOnCreate(local);
            var remote = StaticAutoMapper.Current.Map <TDto, TOriginal>(local);

            context.Entry <TOriginal>(remote).State = EntityState.Added;

            //AfterNew?.Invoke(remote);
            //context.SaveChanges();
            return(remote);
        }
        public static JArray Get销售报价商品(this Pinhua2Context context)
        {
            var set = from m in context.tb_报价表.AsNoTracking()
                      join d in context.tb_报价表D.AsNoTracking() on m.RecordId equals d.RecordId
                      join x in context.tb_商品表.AsNoTracking() on d.品号 equals x.品号
                      where m.业务类型 == "销售报价"
                      select new
            {
                报 = d,
                品 = x
            };
            JArray jsonArray = new JArray();

            foreach (var item in set)
            {
                jsonArray.Add(Pinhua2Helper.JObjectFromMerge(item.报, item.品));
            }

            return(jsonArray);
        }
        public static JArray Get销售报价商品(this Pinhua2Context context, string customerId, string orderId)
        {
            var set = from m in context.tb_报价表.AsNoTracking()
                      join d in context.tb_报价表D.AsNoTracking() on m.RecordId equals d.RecordId
                      join x in context.tb_商品表.AsNoTracking() on d.品号 equals x.品号
                      where /*m.往来号 == customerId &&*/ m.业务类型 == "销售报价" && m.单号 == orderId
                      orderby d.RN
                      select new
            {
                报 = d,
                品 = x
            };
            JArray jsonArray = new JArray();

            foreach (var item in set)
            {
                jsonArray.Add(Pinhua2Helper.JObjectFromMerge(item.报, item.品));
            }

            return(new JArray(jsonArray.OrderBy(m => m["RN"])));
        }
        public static JArray Get销售收款商品(this Pinhua2Context context, string customerId, string orderId)
        {
            var set = from m in context.tb_收付表.AsNoTracking()
                      join d in context.tb_收付表D.AsNoTracking() on m.RecordId equals d.RecordId
                      //join s in context.tb_IOD.AsNoTracking() on d.子单号 equals s.子单号
                      join x in context.tb_商品表.AsNoTracking() on d.品号 equals x.品号
                      where /*m.往来号 == customerId &&*/ m.单号 == orderId && m.类型 == "收款"
                      select new
            {
                收 = d,
                品 = x
            };
            JArray jsonArray = new JArray();

            foreach (var item in set)
            {
                jsonArray.Add(Pinhua2Helper.JObjectFromMerge(item.收, item.品));
            }

            return(jsonArray);
        }
        public static IQueryable <TRemoteD> funcEditDetails2 <TLocal, TLocalD, TRemote, TRemoteD>(this Pinhua2Context context, TLocal _local, IList <TLocalD> _localDs,/*TRemote remote, IList<TRemoteD> _remoteDs,*/
                                                                                                  Action <TLocalD> CreatingD = null, Action <TLocalD> UpdatingD = null, Action <TRemoteD> DeletingD = null)
            where TLocal : _BaseTableMain
            where TLocalD : _BaseTableDetail
            where TRemote : _BaseTableMain
            where TRemoteD : _BaseTableDetail
        {
            var remote = context.Set <TRemote>().AsNoTracking().FirstOrDefault(r => r.RecordId == _local.RecordId);

            if (remote == null)
            {
                return(null);
            }

            var remoteDs = context.Set <TRemoteD>();

            foreach (var remoteD in remoteDs)
            {
                if (!_localDs.Any(p => p.子单号 == remoteD.子单号)) // 新列表没有数据库中的子单号,则删除
                {
                    #region 应该在外部执行
                    //var tb_报价D = context.Set<tb_报价表D>().FirstOrDefault(d => d.子单号 == remoteD.子单号);
                    //if (tb_报价D != null)
                    //    tb_报价D.状态 = "";
                    #endregion

                    DeletingD?.Invoke(remoteD);
                    context.Remove <TRemoteD>(remoteD);
                }
            }

            foreach (var localD in _localDs)
            {
                Pinhua2Helper.CompleteDetailOnUpdate(remote, localD);

                if (remoteDs.Any(d => d.子单号 == localD.子单号)) // 子单号有相同的,则修改
                {
                    UpdatingD?.Invoke(localD);

                    // 将删除的重新标记为修改
                    var remoteD = remoteDs.FirstOrDefault(m => m.子单号 == localD.子单号);
                    context.Attach <TRemoteD>(remoteD).State = EntityState.Modified;
                    Mapper.Map <TLocalD, TRemoteD>(localD, remoteD);
                }
                else if (!remoteDs.Any(d => d.子单号 == localD.子单号)) // 子单号没有相同的,则添加
                {
                    CreatingD?.Invoke(localD);
                    #region 应该在外部执行
                    // 将新的标记为添加
                    //if (string.IsNullOrEmpty(localD.子单号)) // 子单号为空的,表示新插入
                    //{
                    //    //localD.子单号 = context.funcAutoCode("子单号");
                    //    var remoteD = Mapper.Map<TRemoteD>(localD);
                    //    context.Entry<TRemoteD>(remoteD).State = EntityState.Added;
                    //}
                    //else if (!string.IsNullOrEmpty(localD.子单号)) // 子单号不为空,表示从报价单引入,插入
                    //{
                    //    var remoteD = Mapper.Map<TRemoteD>(localD);
                    //    context.Entry<TRemoteD>(remoteD).State = EntityState.Added;
                    //}
                    #endregion
                    var remoteD = Mapper.Map <TRemoteD>(localD);
                    context.Attach <TRemoteD>(remoteD).State = EntityState.Added;
                }
            }

            return(remoteDs);
        }