/// <summary>
        /// 后台用户库存入库(库存减少用的)
        /// </summary>
        /// <param name="operationId"></param>
        /// <param name="entity"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public static bool CreateStockDeliverAdmin(string operationId, OrderEntity entity, string loginId)
        {
            List<DeliverEntity> deliverList = new List<DeliverEntity>();//需要更新的交割单列表
            List<DeliverRecordEntity> deliverRecordList = new List<DeliverRecordEntity>();
            List<UpdateDataMember> updateList = new List<UpdateDataMember>();//需要更新的订单信息
            List<UpdateDataMember> userList = new List<UpdateDataMember>();//需要更新用户数量

            StockEntity stockEntity = LoadByAccount(entity.UserId);//获取订单所属用户信息
            entity.OrderType = 13;//默认提货单
            Direction d = Direction.库存调整;

            //获取交割单信息
            List<DeliverEntity> list = GetListByAccount(entity.Account, d, 1);
            if (list == null || list.Count <= 0)
                return false;

            #region 计算交割单信息

            Dictionary<GoodsType, decimal> dic = new Dictionary<GoodsType, decimal>();
            dic.Add(GoodsType.Au, entity.Au);
            dic.Add(GoodsType.Ag, entity.Ag);
            dic.Add(GoodsType.Pd, entity.Pd);
            dic.Add(GoodsType.Pt, entity.Pt);
            foreach (var key in dic.Keys)
            {
                var tmp = list.Where(m => m.Goods.Equals((int)key)).ToList();
                var values = dic[key];
                if (values >= 0) continue;
                foreach (var mm in tmp)
                {
                    if (values > mm.AvailableTotal)
                    {
                        values -= mm.AvailableTotal;
                        mm.AvailableTotal = 0;
                        mm.State = 0;
                        deliverList.Add(mm);
                    }
                    else
                    {
                        mm.State = values.Equals(mm.AvailableTotal) ? 0 : 1;
                        mm.AvailableTotal = mm.AvailableTotal + values;
                        if (mm.AvailableTotal == 0)
                        {
                            mm.State = 0;
                        }
                        deliverList.Add(mm);
                        break;
                    }
                }
            }

            #endregion

            List<DeliverAdjustmentEntity> listDeliver = new List<DeliverAdjustmentEntity>();

            DeliverAdjustmentEntity deliver = null;
            if (entity.Au < 0)
            {
                deliver = new DeliverAdjustmentEntity();
                deliver.DeliverAdjustmentID = Guid.NewGuid().ToString("n");
                deliver.DeliverAdjustmentNO = "JS" + DateTime.Now.ToString("yyMMddhhmmssffff");
                deliver.Goods = (int)GoodsType.Au;
                deliver.Direction = (int)OrderType.库存调整;
                deliver.Total = entity.Au;
                deliver.Account = entity.Account;
                deliver.LockPrice = 0;
                deliver.State = 0;
                deliver.AvailableTotal = 0;
                deliver.FromFlag = 1;
                deliver.OperationUserID = operationId;
                deliver.UserID = entity.UserId;
                listDeliver.Add(deliver);
            }
            if (entity.Ag < 0)
            {
                deliver = new DeliverAdjustmentEntity();
                deliver.DeliverAdjustmentID = Guid.NewGuid().ToString("n");
                deliver.DeliverAdjustmentNO = "JS" + DateTime.Now.ToString("yyMMddhhmmssffff");
                deliver.Goods = (int)GoodsType.Ag;
                deliver.Direction = (int)OrderType.库存调整;
                deliver.Total = entity.Ag;
                deliver.Account = entity.Account;
                deliver.LockPrice = 0;
                deliver.State = 0;
                deliver.AvailableTotal = 0;
                deliver.FromFlag = 1;
                deliver.OperationUserID = operationId;
                deliver.UserID = entity.UserId;
                listDeliver.Add(deliver);
            }
            if (entity.Pd < 0)
            {
                deliver = new DeliverAdjustmentEntity();
                deliver.DeliverAdjustmentID = Guid.NewGuid().ToString("n");
                deliver.DeliverAdjustmentNO = "JS" + DateTime.Now.ToString("yyMMddhhmmssffff");
                deliver.Goods = (int)GoodsType.Pd;
                deliver.Direction = (int)OrderType.库存调整;
                deliver.Total = entity.Pd;
                deliver.Account = entity.Account;
                deliver.LockPrice = 0;
                deliver.State = 0;
                deliver.AvailableTotal = 0;
                deliver.FromFlag = 1;
                deliver.OperationUserID = operationId;
                deliver.UserID = entity.UserId;
                listDeliver.Add(deliver);
            }
            if (entity.Pt < 0)
            {
                deliver = new DeliverAdjustmentEntity();
                deliver.DeliverAdjustmentID = Guid.NewGuid().ToString("n");
                deliver.DeliverAdjustmentNO = "JS" + DateTime.Now.ToString("yyMMddhhmmssffff");
                deliver.Goods = (int)GoodsType.Pt;
                deliver.Direction = (int)OrderType.库存调整;
                deliver.Total = entity.Pt;
                deliver.Account = entity.Account;
                deliver.LockPrice = 0;
                deliver.State = 0;
                deliver.AvailableTotal = 0;
                deliver.FromFlag = 1;
                deliver.OperationUserID = operationId;
                deliver.UserID = entity.UserId;
                listDeliver.Add(deliver);
            }

            UserLogEntity userLogEntity = new UserLogEntity();

            userLogEntity.Account = operationId;
            userLogEntity.DESC = string.Format(@"后台用户库存调整(减少):{0}库存,减少.Ag:{1},Au:{2},Pt:{3},Pd:{4};", entity.Account, entity.Au, entity.Ag, entity.Pt, entity.Pd);
            userLogEntity.UserType = (int)LogType.用户库存调整减少;
            YicelTransaction tran = new YicelTransaction();
            try
            {
                tran.BeginTransaction();
                //  CreateOrder(order, tran);//创建订单信息
                deliverList.ForEach(m => UpdateDeliver(m, entity, operationId, tran));//更新交割单信息
                listDeliver.ForEach(m => CreateDeliverAdjustment(m, tran));
                UpdateUserTotal(entity, stockEntity, tran);//更新用户库存数量
                CreateLog(userLogEntity, tran);
                tran.Commit();
                return true;
            }
            catch (Exception ex)
            {
                ManagerLog.WriteErr(ex + ":" + userLogEntity.DESC);
                tran.Rollback();
                return false;
            }
        }
        /// <summary>
        /// 金商提货受理明细
        /// </summary>
        /// <param name="agentID"></param>
        /// <param name="order"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static DataTable GetAgentOrderList(string agentID, OrderEntity order, int pageindex, int pagesize, ref int page)
        {
            SqlParameter[] paras = null;
            StringBuilder sb = new StringBuilder();
            StringBuilder sbSearch = new StringBuilder();

            sbSearch.AppendFormat(@" State=2 and AgentInfoId='{0}'", agentID);

            if (!string.IsNullOrEmpty(order.OrderCode))
            {
                sbSearch.AppendFormat(@" and OrderCode='{0}'", order.OrderCode);
            }
            if (!string.IsNullOrEmpty(order.Account))
            {
                sbSearch.AppendFormat(@" and Account like '{0}%'", order.Account);
            }
            if (!string.IsNullOrEmpty(order.Name))
            {
                sbSearch.AppendFormat(@" and userName like '{0}%'", order.Name);
            }

            if (!string.IsNullOrEmpty(order.OrderNo))
            {
                sbSearch.AppendFormat(@" and OrderNo = '{0}'", order.OrderNo);
            }

            if (!string.IsNullOrEmpty(order.CreateDate))
            {
                sbSearch.AppendFormat(@" and CreateDate BETWEEN '{0}' AND '{1}'", order.CreateDate, order.EndDate);
            }

            paras = new SqlParameter[]
            {
                new SqlParameter("@selectlist", "AgentInfoId,OrderId, OrderCode, OrderNo, OrderType, UserId, Account, CarryWay, Au, Ag, Pt, Pd, CreateDate, AvailableAu, AvailableAg, AvailablePt,AvailablePd, userName,AgQuantity,AuQuantity,PtQuantity,PdQuantity,AuP,AgP,PdP,PtP,OperationDate,tradeAccount,AgentId,ClerkId"),
                new SqlParameter("@SubSelectList", "AgentInfoId,OrderId, OrderCode, OrderNo, OrderType, UserId, Account, CarryWay, Au, Ag, Pt, Pd, CreateDate, AvailableAu, AvailableAg, AvailablePt,AvailablePd, userName,AgQuantity,AuQuantity,PtQuantity,PdQuantity,AuP,AgP,PdP,PtP,OperationDate,tradeAccount,AgentId,ClerkId"),
                new SqlParameter("@TableSource", "V_Agent"),
                new SqlParameter("@TableOrder", "a"),
                new SqlParameter("@SearchCondition",sbSearch.ToString()),
                new SqlParameter("@OrderExpression", "order by CreateDate desc"),
                new SqlParameter("@PageIndex", pageindex),
                new SqlParameter("@PageSize", pagesize),
                new SqlParameter("@PageCount", page)
            };

            paras[8].Direction = ParameterDirection.Output;
            DataTable dt = DbHelper.RunProcedure("GetRecordFromPage", paras, "V_Agent").Tables[0];
            page = Convert.ToInt32(paras[8].Value);
            return dt;
        }
 /// <summary>
 /// 订单增加
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="tran"></param> 
 /// <returns></returns>
 public static int CreateOrder(OrderEntity entity, YicelTransaction tran)
 {
     if (string.IsNullOrEmpty(entity.JUserId))
     {
         entity.JUserId = "";
     }
     SqlParameter[] parms = new SqlParameter[] {
         new SqlParameter("@OrderId",entity.OrderId),
         new SqlParameter("@OrderNo",entity.OrderNo),
         new SqlParameter("@OrderCode",entity.OrderCode),
         new SqlParameter("@OrderType",entity.OrderType),
         new SqlParameter("@UserId",entity.UserId),
         new SqlParameter("@Account",entity.Account),
         new SqlParameter("@JUserId",entity.JUserId),
         new SqlParameter("@CarryWay",entity.CarryWay),
         new SqlParameter("@Au",entity.Au),
         new SqlParameter("@Ag",entity.Ag),
         new SqlParameter("@Pt",entity.Pt),
         new SqlParameter("@Pd",entity.Pd),
         new SqlParameter("@CreateDate",entity.CreateDate),
         new SqlParameter("@EndDate",entity.EndDate),
         new SqlParameter("@State",entity.State),
         new SqlParameter("@Version",entity.Version),
         new SqlParameter("@AuQuantity",entity.AuQuantity),
         new SqlParameter("@AgQuantity",entity.AgQuantity),
         new SqlParameter("@PtQuantity",entity.PtQuantity),
         new SqlParameter("@PdQuantity",entity.PdQuantity),
         new SqlParameter("@AgP",entity.AgP),
         new SqlParameter("@AuP",entity.AuP),
         new SqlParameter("@PdP",entity.PdP),
         new SqlParameter("@PtP",entity.PtP)
     };
     StringBuilder strSql = new StringBuilder();
     strSql.AppendFormat("insert into Order_BZJ({0})", Fields.Order_FIELD_LIST);
     strSql.AppendFormat(" values ({0})", "@" + Fields.Order_FIELD_LIST.Replace(",", ",@"));
     object obj = DbHelper.ExecuteNonQuery(strSql.ToString(), parms, tran.Transaction);
     return obj == null || obj == DBNull.Value ? 0 : Convert.ToInt32(obj);
 }
 /// <summary>
 /// 创建提货,卖订单 返回提货,卖码
 /// </summary>
 public static int CreateOrder(string loginId, OrderEntity entity, OrderAttachEntity attachEntity)
 {
     SqlParameter[] paras = new SqlParameter[]
     {
     new SqlParameter("@loginId", loginId),
     new SqlParameter("@UserID", entity.UserId),
     new SqlParameter("@OrderType", entity.OrderType),
     new SqlParameter("@CarryWay", entity.CarryWay),
     new SqlParameter("@UserType", attachEntity.UserType),
     new SqlParameter("@OrderNo", entity.OrderNo),
     new SqlParameter("@OrderCode", entity.OrderCode),
     new SqlParameter("@Account", entity.Account),
     new SqlParameter("@State", entity.State),
     new SqlParameter("@Version", entity.Version),
     new SqlParameter("@Name", attachEntity.Name),
     new SqlParameter("@Phone", attachEntity.Phone),
     new SqlParameter("@IDType", attachEntity.IDType),
     new SqlParameter("@IDNo", attachEntity.IDNo),
     new SqlParameter("@Remark", attachEntity.Remark)
     };
     int rows;
     return DbHelper.RunProcedure("P_CreateOrder", paras, out rows);
 }
 /// <summary>
 /// 更新员工库存数量
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="userEntity"></param>
 /// <param name="tran"></param>
 private static void UpdateUserTotal(OrderEntity entity, StockEntity userEntity, YicelTransaction tran)
 {
     if (entity.OrderType == (int)OrderType.提货单 || entity.OrderType == (int)OrderType.回购单)
     {
         if (entity.Au != 0)
             UpdateTotal(entity.UserId, Direction.提货单, GoodsType.Au, entity.Au * -1, userEntity, tran);
         if (entity.Ag != 0)
             UpdateTotal(entity.UserId, Direction.提货单, GoodsType.Ag, entity.Ag * -1, userEntity, tran);
         if (entity.Pt != 0)
             UpdateTotal(entity.UserId, Direction.提货单, GoodsType.Pt, entity.Pt * -1, userEntity, tran);
         if (entity.Pd != 0)
             UpdateTotal(entity.UserId, Direction.提货单, GoodsType.Pd, entity.Pd * -1, userEntity, tran);
     }
     if (entity.OrderType == (int)OrderType.卖单)
     {
         if (entity.Au != 0)
             UpdateTotal(entity.UserId, Direction.卖单, GoodsType.Au, entity.Au * -1, userEntity, tran);
         if (entity.Ag != 0)
             UpdateTotal(entity.UserId, Direction.卖单, GoodsType.Ag, entity.Ag * -1, userEntity, tran);
         if (entity.Pt != 0)
             UpdateTotal(entity.UserId, Direction.卖单, GoodsType.Pt, entity.Pt * -1, userEntity, tran);
     }
     if (entity.OrderType == (int)OrderType.金生金)
     {
         if (entity.Au != 0)
             UpdateTotal(entity.UserId, Direction.金生金, GoodsType.Au, entity.Au * -1, userEntity, tran);
         if (entity.Ag != 0)
             UpdateTotal(entity.UserId, Direction.金生金, GoodsType.Ag, entity.Ag * -1, userEntity, tran);
         if (entity.Pt != 0)
             UpdateTotal(entity.UserId, Direction.金生金, GoodsType.Pt, entity.Pt * -1, userEntity, tran);
         if (entity.Pd != 0)
             UpdateTotal(entity.UserId, Direction.金生金, GoodsType.Pd, entity.Pd * -1, userEntity, tran);
     }
     if (entity.OrderType == (int)OrderType.库存调整)
     {
         if (entity.Au != 0)
             UpdateTotal(entity.UserId, Direction.库存调整, GoodsType.Au, entity.Au, userEntity, tran);
         if (entity.Ag != 0)
             UpdateTotal(entity.UserId, Direction.库存调整, GoodsType.Ag, entity.Ag, userEntity, tran);
         if (entity.Pt != 0)
             UpdateTotal(entity.UserId, Direction.库存调整, GoodsType.Pt, entity.Pt, userEntity, tran);
         if (entity.Pd != 0)
             UpdateTotal(entity.UserId, Direction.库存调整, GoodsType.Pd, entity.Pd, userEntity, tran);
     }
 }
        /// <summary>
        /// 金生金办理
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="operationEntity"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public static int CreateJSJOrder(OrderEntity entity, OrderOperationEntity operationEntity, string loginId)
        {
            List<DeliverEntity> deliverList = new List<DeliverEntity>();//需要更新的交割单列表
            List<DeliverRecordEntity> deliverRecordList = new List<DeliverRecordEntity>();//交割单记录列表
            #region 赋值

            entity.OrderId = Guid.NewGuid().ToString("n");
            entity.OrderNo = "JSJ" + DateTime.Now.ToString("yyMMddhhmmssffff");
            entity.OrderCode = "";
            entity.OrderType = (int)OrderType.金生金;
            entity.State = (int)OrderState.新订单;
            entity.CreateDate = DateTime.Now.ToString();
            entity.EndDate = DateTime.Now.ToString();

            operationEntity.OperationId = Guid.NewGuid().ToString("n");
            operationEntity.OperationDate = DateTime.Now.ToString();
            operationEntity.Account = entity.Account;
            operationEntity.OperationId = Guid.NewGuid().ToString("n");
            operationEntity.OrderId = entity.OrderId;
            operationEntity.OrderNo = entity.OrderNo;
            operationEntity.Type = 5;
            operationEntity.Remark = string.Format("用户:{0} 创建金生金订单", entity.Account);

            #endregion

            List<DeliverEntity> list = GetListByAccount(entity.Account, Direction.提货单, 1);

            #region 计算交割单信息

            Dictionary<GoodsType, decimal> dic = new Dictionary<GoodsType, decimal>();
            dic.Add(GoodsType.Au, entity.Au);
            dic.Add(GoodsType.Ag, entity.Ag);
            dic.Add(GoodsType.Pd, entity.Pd);
            dic.Add(GoodsType.Pt, entity.Pt);
            foreach (var key in dic.Keys)
            {
                var tmp = list.Where(m => m.Goods.Equals((int)key)).ToList();
                var values = dic[key];
                if (values <= 0) continue;
                foreach (var mm in tmp)
                {
                    DeliverRecordEntity recordEntity = new DeliverRecordEntity
                    {
                        DeliverId = mm.DeliverId,
                        DeliverNo = mm.DeliverNo,
                        Goods = mm.Goods,
                        Direction = mm.Direction,
                        LockPrice = mm.LockPrice,
                        OrderType = (int)OrderType.金生金,
                        OrderId = entity.OrderId,
                        OrderNo = entity.OrderNo
                    };
                    if (values > mm.AvailableTotal)
                    {
                        recordEntity.UseTotal = mm.AvailableTotal;
                        values -= mm.AvailableTotal;
                        mm.AvailableTotal = 0;
                        mm.State = 0;
                        deliverList.Add(mm);
                        deliverRecordList.Add(recordEntity);
                        break;
                    }
                    else
                    {
                        mm.State = values.Equals(mm.AvailableTotal) ? 0 : 1;
                        recordEntity.UseTotal = values;
                        mm.AvailableTotal -= values;
                        deliverList.Add(mm);
                        deliverRecordList.Add(recordEntity);
                        break;
                    }
                }
            }

            #endregion
            StockEntity userEntity = new StockEntity();
            userEntity = LoadByAccount(entity.UserId);

            entity.AuP = userEntity.AuPrice;
            entity.AgP = userEntity.AgPrice;
            UserLogEntity userLogEntity = new UserLogEntity();

            userLogEntity.Account = entity.UserId;
            userLogEntity.DESC = string.Format(@"用户{0}金生金办理:Ag:{1},Au:{2};订单号:{3}", entity.Account, entity.Au, entity.Ag, entity.OrderNo);
            userLogEntity.UserType = (int)LogType.用户库存调整减少;
            YicelTransaction tran = new YicelTransaction();
            try
            {
                tran.BeginTransaction();
                CreateOrder(entity, tran);//创建订单信息
                CreateOrderOperation(operationEntity, tran);//创建金生金订单操作记录
                deliverRecordList.ForEach(m => CreateDeliverRecord(m, tran));//插入交割单记录信息
                deliverList.ForEach(m => UpdateDeliver(m, entity, operationEntity.OperationId, tran));//更新交割单信息
                UpdateUserTotal(entity, userEntity, tran);//更新用户库存数量
                CreateLog(userLogEntity, tran);
                tran.Commit();
                return 1;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                ManagerLog.WriteErr(ex);
                return 0;
            }
        }
 /// <summary>
 /// 检查库存中心是否与交割信息相等
 /// </summary>
 private static bool CheckTotal(OrderEntity entity, StockEntity userEntity)
 {
     //获取用户交割单各物品总重量
     Dictionary<GoodsType, GoodsEntity> goodsDic = null;
     //判断用户物品数量是否与交割物品数量相等 2013/4/6
     if (entity.OrderType == (int)OrderType.提货单 || entity.OrderType == (int)OrderType.金生金 || entity.OrderType == (int)OrderType.回购单)
     {
         goodsDic = ComFunction.GetGoodsDic(entity.Account, Direction.提货单);
         if (goodsDic[GoodsType.Au].Total != userEntity.Au)
             return false;
         if (goodsDic[GoodsType.Ag].Total != userEntity.Ag)
             return false;
         if (goodsDic[GoodsType.Pt].Total != userEntity.Pt)
             return false;
         if (goodsDic[GoodsType.Pd].Total != userEntity.Pd)
             return false;
     }
     else if (entity.OrderType == (int)OrderType.卖单)
     {
         goodsDic = ComFunction.GetGoodsDic(entity.Account, Direction.卖单);
         if (goodsDic[GoodsType.Au].Total != userEntity.Au_b)
             return false;
         if (goodsDic[GoodsType.Ag].Total != userEntity.Ag_b)
             return false;
         if (goodsDic[GoodsType.Pt].Total != userEntity.Pt_b)
             return false;
         if (goodsDic[GoodsType.Pd].Total != userEntity.Pd_b)
             return false;
     }
     else
     {
         return false;
     }
     return true;
 }
 /// <summary>
 /// 创建订单实体
 /// </summary>
 /// <param name="reader"></param>
 /// <returns></returns>
 private static OrderEntity CreateOrderEntity(DbDataReader reader)
 {
     OrderEntity entity = new OrderEntity();
     entity.OrderId = reader["OrderId"].ToString();
     entity.OrderNo = reader["OrderNo"].ToString();
     entity.OrderCode = reader["OrderCode"].ToString();
     entity.OrderType = Convert.ToInt32(reader["OrderType"]);
     entity.UserId = reader["UserId"].ToString();
     entity.Account = reader["Account"].ToString();
     entity.JUserId = reader["JUserId"].ToString();
     entity.CarryWay = Convert.ToInt32(reader["CarryWay"]);
     entity.Au = Convert.ToDecimal(reader["Au"]);
     entity.Ag = Convert.ToDecimal(reader["Ag"]);
     entity.Pt = Convert.ToDecimal(reader["Pt"]);
     entity.Pd = Convert.ToDecimal(reader["Pd"]);
     entity.CreateDate = reader["CreateDate"].ToString();
     entity.EndDate = reader["EndDate"].ToString();
     entity.State = Convert.ToInt32(reader["State"]);
     entity.Version = Convert.ToInt32(reader["Version"]);
     entity.AuQuantity = Math.Abs(Convert.ToDecimal(reader["AuQuantity"]));
     entity.AgQuantity = Math.Abs(Convert.ToDecimal(reader["AgQuantity"]));
     entity.PtQuantity = Math.Abs(Convert.ToDecimal(reader["PtQuantity"]));
     entity.PdQuantity = Math.Abs(Convert.ToDecimal(reader["PdQuantity"]));
     entity.AgP = Convert.ToDecimal(reader["AgP"]);
     entity.AuP = Convert.ToDecimal(reader["AuP"]);
     entity.PdP = Convert.ToDecimal(reader["PdP"]);
     entity.PtP = Convert.ToDecimal(reader["PtP"]);
     return entity;
 }
        /// <summary>
        /// 交割单记录
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="orderEntity"></param>
        /// <param name="operationId"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        public static int UpdateDeliver(DeliverEntity entity, OrderEntity orderEntity, string operationId, YicelTransaction tran)
        {
            SqlParameter[] parms = new SqlParameter[] {
                new SqlParameter("@DeliverId",entity.DeliverId),
                new SqlParameter("@DeliverNo",entity.DeliverNo),
                new SqlParameter("@Account",entity.Account),
                new SqlParameter("@Direction",entity.Direction),
                new SqlParameter("@Total",entity.Total),
                new SqlParameter("@Goods",entity.Goods),
                new SqlParameter("@DeliverDate",entity.DeliverDate),
                new SqlParameter("@LockPrice",entity.LockPrice),
                new SqlParameter("@AvailableTotal",entity.AvailableTotal),
                new SqlParameter("@FromFlag",entity.FromFlag),
                new SqlParameter("@State",entity.State),
                new SqlParameter("@CreateDate",entity.CreateDate)
            };
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Deliver_BZJ set ");
            strSql.Append("DeliverNo=@DeliverNo,");
            strSql.Append("Account=@Account,");
            strSql.Append("Goods=@Goods,");
            strSql.Append("Direction=@Direction,");
            strSql.Append("Total=@Total,");
            strSql.Append("DeliverDate=@DeliverDate,");
            strSql.Append("LockPrice=@LockPrice,");
            strSql.Append("AvailableTotal=@AvailableTotal,");
            strSql.Append("FromFlag=@FromFlag,");
            strSql.Append("State=@State,");
            strSql.Append("CreateDate=@CreateDate");
            strSql.AppendFormat(" where DeliverId='{0}'", entity.DeliverId);
            return DbHelper.ExecuteNonQuery(strSql.ToString(), parms, tran.Transaction);
            #region 记录出库记录
            //DeliverAdjustmentEntity deliver = new DeliverAdjustmentEntity();
            //deliver.DeliverAdjustmentID = Guid.NewGuid().ToString("n");
            //deliver.DeliverAdjustmentNO = "JS" + DateTime.Now.ToString("yyMMddhhmmssffff");
            //deliver.Goods = entity.Goods;
            //deliver.Direction = orderEntity.OrderType;
            //if (entity.Goods == 1)
            //{
            //    deliver.Total =- Math.Abs( orderEntity.Au);
            //}

            //if (entity.Goods == 2)
            //{
            //    deliver.Total = - Math.Abs(orderEntity.Ag);
            //}
            //if (entity.Goods == 3)
            //{
            //    deliver.Total = - Math.Abs(orderEntity.Pt);
            //}
            //if (entity.Goods == 4)
            //{
            //    deliver.Total = - Math.Abs(orderEntity.Pd);
            //}
            //deliver.Account = entity.Account;
            //deliver.LockPrice = 0;
            //deliver.State = 0;
            //deliver.AvailableTotal = 0;
            //deliver.FromFlag = 1;
            //deliver.OperationUserID = OperationId;
            //deliver.UserID = orderEntity.UserId;
            #endregion
            // return CreateDeliverAdjustment(deliver, tran);
        }
        /// <summary>
        /// 更新订单状态为已提货或卖
        /// </summary>
        /// <param name="agentId"></param>
        /// <param name="order"></param>
        /// <param name="dataMember"></param>
        /// <param name="operationEntity"></param>
        /// <returns></returns>
        public static bool UpdateOrder(string agentId, OrderEntity order, List<UpdateDataMember> dataMember, OrderOperationEntity operationEntity)
        {
            List<DeliverEntity> deliverList = new List<DeliverEntity>();//需要更新的交割单列表
            List<DeliverRecordEntity> deliverRecordList = new List<DeliverRecordEntity>();
            List<UpdateDataMember> updateList = new List<UpdateDataMember>();//需要更新的订单信息
            List<UpdateDataMember> userList = new List<UpdateDataMember>();//需要更新用户数量

            OrderEntity entity = LoadByOrderCode(order.OrderCode);//获取订单信息
            StockEntity stockEntity = LoadByAccount(entity.UserId);//获取订单所属用户信息
            Direction d = entity.OrderType == (int)OrderType.提货单 ? Direction.提货单 : Direction.卖单;

            #region 合法性验证
            //获取用户交割单各物品总重量
            Dictionary<GoodsType, GoodsEntity> goodsDic = GetGoodsDic(entity.Account, d);
            //判断用户交割单种类是否小于0 或者小于提交的种类数量
            if (goodsDic.Count <= 0 || goodsDic.Count < dataMember.Count)
                return false;
            //判断用户物品数量是否与交割物品数量相等 2013/4/6
            if (!CheckTotal(order, stockEntity))
                return false;

            //循环判断提交重量是否大于用户交割单总重量
            foreach (var m in dataMember)
            {
                foreach (var g in goodsDic.Values)
                {
                    if (m.field.ToLower() == g.Symbol.ToString().ToLower() && Convert.ToDecimal(m.value) > g.Total)
                    {
                        return false;
                    }
                    else
                    {
                        if (m.field.ToLower() == "au")
                        {
                            entity.Au = Convert.ToDecimal(m.value);
                            updateList.Add(new UpdateDataMember { field = "Au", value = m.value });

                            updateList.Add(new UpdateDataMember { field = "AuP", value = stockEntity.AuPrice });
                            if (Convert.ToDecimal(m.value) > stockEntity.AuAmount)
                            {
                                updateList.Add(new UpdateDataMember { field = "AuQuantity", value = stockEntity.AuAmount });
                            }
                            else
                            {
                                updateList.Add(new UpdateDataMember { field = "AuQuantity", value = m.value });
                            }
                            break;
                        }
                        if (m.field.ToLower() == "ag")
                        {
                            entity.Ag = Convert.ToDecimal(m.value);
                            updateList.Add(new UpdateDataMember { field = "Ag", value = m.value });

                            updateList.Add(new UpdateDataMember { field = "AgP", value = stockEntity.AgPrice });
                            if (Convert.ToDecimal(m.value) > stockEntity.AuAmount)
                            {
                                updateList.Add(new UpdateDataMember { field = "AgQuantity", value = stockEntity.AgAmount });
                            }
                            else
                            {
                                updateList.Add(new UpdateDataMember { field = "AgQuantity", value = m.value });
                            }
                            break;
                        }
                        if (m.field.ToLower() == "pt")
                        {
                            entity.Pt = Convert.ToDecimal(m.value);
                            updateList.Add(new UpdateDataMember { field = "Pt", value = m.value });

                            updateList.Add(new UpdateDataMember { field = "PtP", value = stockEntity.PtPrice });
                            if (Convert.ToDecimal(m.value) > stockEntity.AuAmount)
                            {
                                updateList.Add(new UpdateDataMember { field = "PtQuantity", value = stockEntity.PtAmount });
                            }
                            else
                            {
                                updateList.Add(new UpdateDataMember { field = "PtQuantity", value = m.value });
                            }
                            break;
                        }
                        if (m.field.ToLower() == "pd")
                        {
                            entity.Pd = Convert.ToDecimal(m.value);
                            updateList.Add(new UpdateDataMember { field = "Pd", value = m.value });

                            updateList.Add(new UpdateDataMember { field = "PdP", value = stockEntity.AuPrice });
                            if (Convert.ToDecimal(m.value) > stockEntity.AuAmount)
                            {
                                updateList.Add(new UpdateDataMember { field = "PdQuantity", value = stockEntity.AuAmount });
                            }
                            else
                            {
                                updateList.Add(new UpdateDataMember { field = "PdQuantity", value = m.value });
                            }
                            break;
                        }
                    }
                }
            }

            #endregion

            //获取交割单信息
            List<DeliverEntity> list = GetListByAccount(entity.Account, d, 1);
            if (list == null || list.Count <= 0)
                return false;

            #region 计算交割单信息

            Dictionary<GoodsType, decimal> dic = new Dictionary<GoodsType, decimal>();
            dic.Add(GoodsType.Au, entity.Au);
            dic.Add(GoodsType.Ag, entity.Ag);
            dic.Add(GoodsType.Pd, entity.Pd);
            dic.Add(GoodsType.Pt, entity.Pt);
            foreach (var key in dic.Keys)
            {
                var tmp = list.Where(m => m.Goods.Equals(key)).ToList(); var values = dic[key];
                if (values <= 0) continue;
                foreach (var mm in tmp)
                {
                    DeliverRecordEntity recordEntity = new DeliverRecordEntity
                    {
                        DeliverId = mm.DeliverId,
                        DeliverNo = mm.DeliverNo,
                        Goods = mm.Goods,
                        Direction = mm.Direction,
                        LockPrice = mm.LockPrice,
                        OrderType = order.OrderType,
                        OrderId = entity.OrderId,
                        OrderNo = entity.OrderNo
                    };
                    if (values > mm.AvailableTotal)
                    {
                        recordEntity.UseTotal = mm.AvailableTotal;
                        values -= mm.AvailableTotal;
                        mm.AvailableTotal = 0;
                        mm.State = 0;
                        deliverList.Add(mm);
                        deliverRecordList.Add(recordEntity);
                    }
                    else
                    {
                        mm.State = values.Equals(mm.AvailableTotal) ? 0 : 1;
                        recordEntity.UseTotal = values;
                        mm.AvailableTotal -= values;
                        deliverList.Add(mm);
                        deliverRecordList.Add(recordEntity);
                        break;
                    }
                }
            }

            #endregion

            #region 部分实体赋值
            AgentDeliverEntity agentDeliverEntity = new AgentDeliverEntity
            {
                AgentInfoId = agentId,
                OrderId = entity.OrderId,
                FromTo = 1,
                Direction = d,
                Ag = entity.Ag,
                Au = entity.Au,
                Pd = entity.Pd,
                Pt = entity.Pt
            };
            #endregion
            updateList.Add(new UpdateDataMember { field = "State", value = "2" });
            updateList.Add(new UpdateDataMember { field = "Version", value = (entity.Version + 1).ToString() });
            updateList.Add(new UpdateDataMember { field = "EndDate", value = DateTime.Now.ToString() });

            UserLogEntity userLogEntity = new UserLogEntity();

            userLogEntity.Account = agentId;
            userLogEntity.DESC = string.Format(@"金商{5},用户{0}提货:Ag:{1},Au:{2},Pt:{3},Pd:{4};", entity.Account, entity.Au, entity.Ag, entity.Pt, entity.Pd, entity.AgentName);
            userLogEntity.UserType = (int)LogType.用户库存调整减少;

            operationEntity.OrderId = entity.OrderId;
            operationEntity.OrderNo = entity.OrderNo;
            YicelTransaction tran = new YicelTransaction();
            try
            {
                tran.BeginTransaction();
                UpdateColumns(entity.OrderId, updateList, tran);
                CreateOrderOperation(operationEntity, tran);//创建订单操作记录
                deliverList.ForEach(m => UpdateDeliver(m, entity, operationEntity.OperationId, tran));//更新交割单信息
                deliverRecordList.ForEach(m => CreateDeliverRecord(m, tran));//创建订单对应交割单信息
                UpdateUserTotal(entity, stockEntity, tran);//更新用户库存数量
                CreateAgent(agentDeliverEntity, tran);//创建金商库存
                CreateLog(userLogEntity, tran);
                tran.Commit();
                return true;
            }
            catch
            {
                tran.Rollback();
                return false;
            }
        }
 /// <summary>
 /// 更新回购订单状态并记录操作日志 
 /// </summary>
 /// <param name="agentId"></param>
 /// <param name="order"></param>
 /// <returns></returns>
 public static bool UpdateBuyBackOrder(string agentId, OrderEntity order)
 {
     UserLogEntity userLogEntity = new UserLogEntity();
     userLogEntity.OperTime = DateTime.Now.ToString();
     userLogEntity.Account = agentId;
     userLogEntity.DESC = string.Format(@"更新回购订单状态:金商{0},订单号:{1}订单ID:{2};", agentId, order.IDNo, order.OrderId);
     userLogEntity.UserType = (int)LogType.金商回购单状态修改;
     YicelTransaction tran = new YicelTransaction();
     SqlParameter[] parms = new SqlParameter[] {
     };
     string strSql = string.Format(@"update Order_BZJ set version={0},state={1} ,OperationDate=GETDATE(),ClerkID='{4}',AgentID='{4}' where OrderNo='{2}' and userid='{3}'", order.Version, order.State, order.OrderNo, order.UserId, agentId);
     try
     {
         tran.BeginTransaction();
         DbHelper.ExecuteNonQuery(strSql, parms, tran.Transaction);
         CreateLog(userLogEntity, tran);
         tran.Commit();
         return true;
     }
     catch
     {
         tran.Rollback();
         return false;
     }
 }
        /// <summary>
        /// 根据订单号查询订单实体
        /// </summary>
        /// <param name="orderNO"></param>
        /// <returns></returns>
        public static OrderEntity LoadByOrder(string orderNO)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append("from vOrder_BZJ ");
            strSql.AppendFormat("where OrderNo='{0}' ", orderNO);

            DataTable dt = DbHelper.ExecQuery(strSql.ToString());
            if (dt.Rows.Count > 0)
            {
                OrderEntity entity = new OrderEntity();
                entity.OrderId = dt.Rows[0]["OrderId"].ToString();
                entity.OrderNo = dt.Rows[0]["OrderNo"].ToString();
                entity.OrderCode = dt.Rows[0]["OrderCode"].ToString();
                entity.OrderType = Convert.ToInt32(dt.Rows[0]["OrderType"]);
                entity.UserId = dt.Rows[0]["UserId"].ToString();
                entity.Account = dt.Rows[0]["Account"].ToString();
                entity.JUserId = dt.Rows[0]["JUserId"].ToString();
                entity.CarryWay = Convert.ToInt32(dt.Rows[0]["CarryWay"]);
                entity.Au = Convert.ToDecimal(dt.Rows[0]["Au"]);
                entity.Ag = Convert.ToDecimal(dt.Rows[0]["Ag"]);
                entity.Pt = Convert.ToDecimal(dt.Rows[0]["Pt"]);
                entity.Pd = Convert.ToDecimal(dt.Rows[0]["Pd"]);
                entity.CreateDate = dt.Rows[0]["CreateDate"].ToString();
                entity.EndDate = dt.Rows[0]["EndDate"].ToString();
                entity.State = Convert.ToInt32(dt.Rows[0]["State"]);
                entity.Version = Convert.ToInt32(dt.Rows[0]["Version"]);
                entity.AuQuantity = Math.Abs(Convert.ToDecimal(dt.Rows[0]["AuQuantity"]));
                entity.AgQuantity = Math.Abs(Convert.ToDecimal(dt.Rows[0]["AgQuantity"]));
                entity.PtQuantity = Math.Abs(Convert.ToDecimal(dt.Rows[0]["PtQuantity"]));
                entity.PdQuantity = Math.Abs(Convert.ToDecimal(dt.Rows[0]["PdQuantity"]));
                entity.AgP = Convert.ToDecimal(dt.Rows[0]["AgP"]);
                entity.AuP = Convert.ToDecimal(dt.Rows[0]["AuP"]);
                entity.PdP = Convert.ToDecimal(dt.Rows[0]["PdP"]);
                entity.PtP = Convert.ToDecimal(dt.Rows[0]["PtP"]);
                return entity;
            }
            return null;
        }
        /// <summary>
        /// 创建回购订单
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="operationEntity"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public static bool CreateBackOrder(OrderEntity entity, OrderOperationEntity operationEntity, string loginId)
        {
            List<DeliverEntity> deliverList = new List<DeliverEntity>();//需要更新的交割单列表
            List<DeliverRecordEntity> deliverRecordList = new List<DeliverRecordEntity>();//交割单记录列表

            StockEntity stockEntity = new StockEntity();
            stockEntity = LoadByAccount(entity.UserId); //用户信息
            #region 赋值

            entity.OrderId = Guid.NewGuid().ToString("n");
            entity.OrderNo = "HG" + DateTime.Now.ToString("yyMMddhhmmssffff");
            entity.OrderCode = "";
            entity.OrderType = (int)OrderType.回购单;
            entity.State = (int)OrderState.新订单;
            entity.CreateDate = DateTime.Now.ToString();
            entity.EndDate = DateTime.Now.AddDays(7).ToString();

            decimal temp = 0;
            if (stockEntity.Au < entity.Au)
            {
                temp = entity.Au - stockEntity.Au;
                entity.AuP = temp * entity.AuP + entity.Au * (entity.AuP - 3);
            }
            if (stockEntity.Ag < entity.Ag)
            {
                temp = entity.Ag - stockEntity.Ag;
                entity.AgP = temp * entity.AgP + entity.Ag * (entity.AgP - 1);
            }

            if (stockEntity.Pt < entity.Pt)
            {
                temp = entity.Pt - stockEntity.Pt;
                entity.PtP = temp * entity.PtP + entity.Pt * (entity.PtP - 3);
            }

            if (stockEntity.Pd < entity.Pd)
            {
                temp = entity.Pd - stockEntity.Pd;
                entity.PdP = temp * entity.PdP + entity.Pd * (entity.PdP - 3);
            }

            entity.AuQuantity = entity.Au * entity.AuP;
            entity.AgQuantity = entity.Ag * entity.AgP;
            entity.PtQuantity = entity.Pt * entity.PtP;
            entity.PdQuantity = entity.Pd * entity.PdP;

            operationEntity.OperationId = Guid.NewGuid().ToString("n");
            operationEntity.OperationDate = DateTime.Now.ToString();
            operationEntity.OrderNo = entity.OrderNo;
            operationEntity.Remark = string.Format("用户:{0} 创建回购订单", entity.Account);
            operationEntity.OrderId = entity.OrderId;
            operationEntity.Account = entity.Account;
            operationEntity.Type = entity.OrderType;

            OrderPriceEntity orderPriceEntity = new OrderPriceEntity();
            orderPriceEntity.OrderId = entity.OrderId;
            orderPriceEntity.OrderNo = entity.OrderNo;
            orderPriceEntity.PriceId = "";
            orderPriceEntity.AuPrice = stockEntity.AuPrice;
            orderPriceEntity.AgPrice = stockEntity.AgPrice;
            orderPriceEntity.PtPrice = stockEntity.PtPrice;
            orderPriceEntity.PdPrice = stockEntity.PdPrice;

            #endregion

            //判断用户是否存在交割单
            List<DeliverEntity> list = GetListByAccount(entity.Account, Direction.提货单, 1);
            if (list == null || list.Count <= 0)
            {
                return false;
            }
            #region 计算交割单信息

            Dictionary<GoodsType, decimal> dic = new Dictionary<GoodsType, decimal>();
            dic.Add(GoodsType.Au, entity.Au);
            dic.Add(GoodsType.Ag, entity.Ag);
            dic.Add(GoodsType.Pd, entity.Pd);
            dic.Add(GoodsType.Pt, entity.Pt);
            foreach (var key in dic.Keys)
            {
                var tmp = list.Where(m => m.Goods.Equals((int)key)).ToList();
                var values = dic[key];
                if (values <= 0) continue;
                foreach (var mm in tmp)
                {
                    DeliverRecordEntity recordEntity = new DeliverRecordEntity
                    {
                        DeliverId = mm.DeliverId,
                        DeliverNo = mm.DeliverNo,
                        Goods = mm.Goods,
                        Direction = mm.Direction,
                        LockPrice = mm.LockPrice,
                        OrderType = (int)OrderType.回购单,
                        OrderId = entity.OrderId,
                        OrderNo = entity.OrderNo
                    };
                    if (values > mm.AvailableTotal)
                    {
                        recordEntity.UseTotal = mm.AvailableTotal;
                        values -= mm.AvailableTotal;
                        mm.AvailableTotal = 0;
                        mm.State = 0;
                        deliverList.Add(mm);
                        deliverRecordList.Add(recordEntity);
                    }
                    else
                    {
                        mm.State = values.Equals(mm.AvailableTotal) ? 0 : 1;
                        recordEntity.UseTotal = values;
                        mm.AvailableTotal -= values;
                        deliverList.Add(mm);
                        deliverRecordList.Add(recordEntity);
                        break;
                    }
                }
            }

            #endregion

            UserLogEntity userLogEntity = new UserLogEntity();

            userLogEntity.Account = entity.UserId;
            userLogEntity.DESC = string.Format(@"用户{0}创建回购订单,订单号:{1}", entity.Account, entity.OrderNo);
            userLogEntity.UserType = (int)LogType.创建回购订单;

            YicelTransaction tran = new YicelTransaction();
            try
            {
                tran.BeginTransaction();
                CreateOrder(entity, tran);//创建订单信息
                CreateOrderOperation(operationEntity, tran);//创建回购订单操作记录
                CreatePrice(orderPriceEntity, tran);//创建回购单价格信息
                deliverRecordList.ForEach(m => CreateDeliverRecord(m, tran));//插入交割单记录信息
                deliverList.ForEach(m => UpdateDeliver(m, entity, operationEntity.OperationId, tran));//更新交割单信息
                UpdateUserTotal(entity, stockEntity, tran);//更新用户库存数量
                CreateLog(userLogEntity, tran);
                tran.Commit();
                return true;
            }
            catch
            {
                tran.Rollback();
                return false;
            }
        }
        /// <summary>
        /// 根据订单号加载单据,个人,库存信息
        /// </summary>
        /// <param name="orderCode"></param>
        /// <returns></returns>
        public static OrderEntity GetOrderNOInfo(string orderCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append("from V_OrderInfo ");
            strSql.AppendFormat("where orderCode='{0}'  ", orderCode);

            DataTable dt = DbHelper.ExecQuery(strSql.ToString());
            OrderEntity entity = new OrderEntity();
            if (dt.Rows.Count > 0)
            {
                entity.OrderId = dt.Rows[0]["OrderId"].ToString();
                entity.UserId = dt.Rows[0]["UserId"].ToString();
                entity.Au = Convert.ToDecimal(dt.Rows[0]["Au"]);
                entity.Ag = Convert.ToDecimal(dt.Rows[0]["Ag"]);
                entity.AuT = Convert.ToDecimal(dt.Rows[0]["AuTotal"]);
                entity.AgT = Convert.ToDecimal(dt.Rows[0]["AgTotal"]);

                entity.Pt = Convert.ToDecimal(dt.Rows[0]["Pt"]);
                entity.Pd = Convert.ToDecimal(dt.Rows[0]["Pd"]);
                entity.PtT = Convert.ToDecimal(dt.Rows[0]["PtTotal"]);
                entity.PdT = Convert.ToDecimal(dt.Rows[0]["PdTotal"]);

                entity.AgP = Convert.ToDecimal(dt.Rows[0]["AgPrice"]);
                entity.AuP = Convert.ToDecimal(dt.Rows[0]["AuPrice"]);
                entity.PtP = Convert.ToDecimal(dt.Rows[0]["PtPrice"]);
                entity.PdP = Convert.ToDecimal(dt.Rows[0]["PdPrice"]);

                entity.AgAmount = Convert.ToDecimal(dt.Rows[0]["AgAmount"]);
                entity.AuAmount = Convert.ToDecimal(dt.Rows[0]["AuAmount"]);
                entity.PtAmount = Convert.ToDecimal(dt.Rows[0]["PtAmount"]);
                entity.PdAmount = Convert.ToDecimal(dt.Rows[0]["PdAmount"]);

                entity.Name = dt.Rows[0]["userName"].ToString();
                entity.CardType = Convert.ToInt32(dt.Rows[0]["CardType"]);
                entity.CardNum = dt.Rows[0]["CardNum"].ToString();
                entity.PhoneNum = dt.Rows[0]["PhoneNum"].ToString();
                entity.OrderNo = dt.Rows[0]["OrderNo"].ToString();
                entity.State = Convert.ToInt32(dt.Rows[0]["State"]);
                entity.OrderType = Convert.ToInt32(dt.Rows[0]["OrderType"]);
                entity.IDType = Convert.ToInt32(dt.Rows[0]["IDType"]);
                entity.IDNo = dt.Rows[0]["IDNo"].ToString();
                entity.Phone = dt.Rows[0]["Phone"].ToString();
                entity.Account = dt.Rows[0]["tradeAccount"].ToString();
                return entity;
            }
            return entity;
        }
        /// <summary>
        /// 提货单,回购单、金生金单查询
        /// </summary>
        /// <param name="order"></param>
        /// <param name="agentId"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static DataTable GetOrderList(OrderEntity order, string agentId, int pageindex, int pagesize, ref int page)
        {
            SqlParameter[] paras = null;
            StringBuilder sb = new StringBuilder();
            StringBuilder sbSearch = new StringBuilder();

            if (!string.IsNullOrEmpty(order.OrderCode))
            {
                sbSearch.AppendFormat(@" and OrderCode='{0}'", order.OrderCode);
            }

            if (!string.IsNullOrEmpty(order.OrderCode))
            {
                sbSearch.AppendFormat(@" and OrderNo='{0}'", order.OrderNo);
            }

            if (!string.IsNullOrEmpty(order.Account))
            {
                sbSearch.AppendFormat(@" and Account='{0}'", order.Account);
            }
            if (!string.IsNullOrEmpty(order.Name))
            {
                sbSearch.AppendFormat(@" and userName like '{0}%'", order.Name);
            }
            if (!string.IsNullOrEmpty(order.CreateDate))
            {
                sbSearch.AppendFormat(@" and CreateDate BETWEEN '{0}' AND '{1}'", order.CreateDate, order.EndDate);
            }

            if (!string.IsNullOrEmpty(agentId))
            {
                sbSearch.AppendFormat(@" and agentid in ({0}) ", ComFunction.GetOrgIds(agentId));
            }

            if (order.OrderType == 0) // 交割单
            {
                paras = new SqlParameter[]
            {
                new SqlParameter("@selectlist", "  Account,userName,DeliverId,DeliverDate,DeliverNo,Goods,Direction,Total,LockPrice,State,AvailableTotal,FromFlag,CreateDate"),
                new SqlParameter("@SubSelectList", " Account,userName,DeliverId,DeliverDate,DeliverNo,Goods,Direction,Total,LockPrice,State,AvailableTotal,FromFlag,CreateDate"),
                new SqlParameter("@TableSource", "V_OrderDelivery"),
                new SqlParameter("@TableOrder", "a"),
                new SqlParameter("@SearchCondition", string.Format(@" OrderType=1 {0}",sbSearch.ToString())),
                new SqlParameter("@OrderExpression", "order by CreateDate desc"),
                new SqlParameter("@PageIndex", pageindex),
                new SqlParameter("@PageSize", pagesize),
                new SqlParameter("@PageCount", page)
            };
            }
            if (order.OrderType == 1) // 提货单
            {
                paras = new SqlParameter[]
            {
                new SqlParameter("@selectlist", "  Account, userName, OrderNo, OrderCode, CarryWay, Au, Ag, Pt, Pd, AgQuantity, AuQuantity, PtQuantity, PdQuantity, AuP, AgP, PtP, PdP, CreateDate, State, OrderType,OperationDate,AgentID,ClerkID,tradeAccount"),
                new SqlParameter("@SubSelectList", " Account, userName, OrderNo, OrderCode, CarryWay, Au, Ag, Pt, Pd, AgQuantity, AuQuantity, PtQuantity, PdQuantity, AuP, AgP, PtP, PdP, CreateDate, State, OrderType,OperationDate,AgentID,ClerkID,tradeAccount"),
                new SqlParameter("@TableSource", "V_OrderTake"),
                new SqlParameter("@TableOrder", "a"),
                new SqlParameter("@SearchCondition", string.Format(@" OrderType=1 {0}",sbSearch.ToString())),
                new SqlParameter("@OrderExpression", "order by CreateDate desc"),
                new SqlParameter("@PageIndex", pageindex),
                new SqlParameter("@PageSize", pagesize),
                new SqlParameter("@PageCount", page)
            };
            }
            if (order.OrderType == 3) //回购
            {
                paras = new SqlParameter[]
            {
                new SqlParameter("@selectlist", "  OperationDate ,Account, userName, OrderNo, OrderCode, CarryWay, Au, Ag, Pt, Pd,AuQuantity, AgQuantity, PtQuantity, PdQuantity, AuPrice, AgPrice,PtPrice, PdPrice, CreateDate, State,OrderType,EndDate,Aup,agp,ptp,pdp,ClerkID"),
                new SqlParameter("@SubSelectList", " OperationDate ,Account, userName, OrderNo, OrderCode, CarryWay, Au, Ag, Pt, Pd,AuQuantity, AgQuantity, PtQuantity, PdQuantity, AuPrice, AgPrice,PtPrice, PdPrice, CreateDate, State,OrderType,EndDate,Aup,agp,ptp,pdp,ClerkID"),
                new SqlParameter("@TableSource", "V_BzckOrder"),
                new SqlParameter("@TableOrder", "a"),
                new SqlParameter("@SearchCondition", string.Format(@" OrderType=3 {0}",sbSearch.ToString())),
                new SqlParameter("@OrderExpression", "order by CreateDate desc"),
                new SqlParameter("@PageIndex", pageindex),
                new SqlParameter("@PageSize", pagesize),
                new SqlParameter("@PageCount", page)
            };
            }
            if (order.OrderType == 4) //金生金
            {
                paras = new SqlParameter[]
            {
                new SqlParameter("@selectlist", "  tradeAccount,userName,OrderNo,AuP,AgP,Au,Ag,CreateDate,State,OrderType"),
                new SqlParameter("@SubSelectList", " tradeAccount,userName,OrderNo,AuP,AgP,Au,Ag,CreateDate,State,OrderType"),
                new SqlParameter("@TableSource", "V_JsjOrder"),
                new SqlParameter("@TableOrder", "a"),
                new SqlParameter("@SearchCondition", string.Format(@" OrderType=4 {0}",sbSearch.ToString())),
                new SqlParameter("@OrderExpression", "order by CreateDate desc"),
                new SqlParameter("@PageIndex", pageindex),
                new SqlParameter("@PageSize", pagesize),
                new SqlParameter("@PageCount", page)
            };
            }
            paras[8].Direction = ParameterDirection.Output;
            DataTable dt = DbHelper.RunProcedure("GetRecordFromPage", paras, "Deliver_BZJ").Tables[0];
            page = Convert.ToInt32(paras[8].Value);
            return dt;
        }