/// <summary>
        /// 订单回购价
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        public static int CreatePrice(OrderPriceEntity entity, YicelTransaction tran)
        {
            entity.OrderPriceId = Guid.NewGuid().ToString("n");

            SqlParameter[] parms = new SqlParameter[] {
            new SqlParameter("@OrderPriceId", entity.OrderPriceId),
            new SqlParameter("@OrderId", entity.OrderId),
            new SqlParameter("@OrderNo", entity.OrderNo),
            new SqlParameter("@PriceId", entity.PriceId),
            new SqlParameter("@AuPrice", entity.AuPrice),
            new SqlParameter("@AgPrice", entity.AgPrice),
            new SqlParameter("@PtPrice", entity.PtPrice),
            new SqlParameter("@PdPrice", entity.PdPrice)
            };

            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("insert into OrderPrice_BZJ({0})", Fields.OrderPrice_FIELD_LIST);
            strSql.AppendFormat(" values ({0})", "@" + Fields.OrderPrice_FIELD_LIST.Replace(",", ",@"));
            object obj = DbHelper.ExecuteNonQuery(strSql.ToString(), parms, tran.Transaction);
            return obj == null || obj == DBNull.Value ? 0 : Convert.ToInt32(obj);
        }
        /// <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;
            }
        }