/// <summary>
        /// 市价单下单
        /// </summary>
        /// <param name="marorderln">下单信息</param>
        /// <returns>下单结果</returns>
        public Marketorders GetMarketorders(MarOrdersLn marorderln)
        {
            Marketorders marketorders = new Marketorders();
            marketorders.MoneyInventory = new MoneyInventory();
            marketorders.TradeOrder = new TradeOrder();
            ProductConfig ptc = new ProductConfig();
            TradeUser TdUser = new TradeUser();
            DateTime dt = DateTime.Now;//服务器当前本地时间
            double _occMoney = 0;//占用资金
            double realprice = 0;//服务器端实时价格(即此时此刻的建仓价)
            double _tradefee = 0;//工费
            double fxrate = 0;
            string orderid = string.Empty;//市价单ID
            string userId = string.Empty;
            string allowStore = "1";//是否允许入库 "1"允许入库 "0" 不允许入库
            string operUser = marorderln.TradeAccount;
            string ipmac = string.Empty;
            int operUserType = 0;
            try
            {

                #region 判断用户登陆标识是否过期

                if (!ComFunction.ExistUserLoginID(marorderln.LoginID, ref TdUser))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL003;
                    marketorders.Desc = ResCode.UL003Desc;
                    return marketorders;
                }

                operUser = TdUser.Account;
                ipmac = ComFunction.GetIpMac(TdUser.Ip, TdUser.Mac);
                operUserType = (int)TdUser.UType;

                if (UserType.NormalType == TdUser.UType)
                {
                    userId = TdUser.UserID;
                }
                else
                {
                    userId = ComFunction.GetUserId(marorderln.TradeAccount, ref TdUser);
                }
                #endregion

                #region 交易手数验证
                if (!ComFunction.ValidateQuantity(marorderln.Quantity, TdUser.OrderUnit))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL044;
                    //marketorders.Desc = ResCode.UL044Desc;
                    marketorders.Desc = string.Format("交易手数必须是{0}的倍数", TdUser.OrderUnit);
                    return marketorders;
                }
                #endregion

                #region 判断用户是否允许下买单或卖单
                if ("0" == marorderln.OrderType) //下买单
                {
                    if (!TdUser.PermitDhuo)
                    {
                        marketorders.Result = false;
                        marketorders.ReturnCode = ResCode.UL017;
                        marketorders.Desc = ResCode.UL017Desc;
                        return marketorders;
                    }
                }
                else if ("1" == marorderln.OrderType) //下卖单
                {
                    if (!TdUser.PermitHshou)
                    {
                        marketorders.Result = false;
                        marketorders.ReturnCode = ResCode.UL018;
                        marketorders.Desc = ResCode.UL018Desc;
                        return marketorders;
                    }
                    allowStore = ComFunction.AllowStore;//是否允许卖单入库,通过配置文件读取
                }
                else
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL021;
                    marketorders.Desc = ResCode.UL021Desc;
                    return marketorders;
                }
                #endregion

                #region 获取商品信息
                ptc = ComFunction.GetProductInfo(marorderln.ProductCode);
                //订单类型(0买、1卖)
                //未能获取商品状态
                if (string.IsNullOrEmpty(ptc.State))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL024;
                    marketorders.Desc = ResCode.UL024Desc;
                    return marketorders;
                }
                #endregion

                #region 判断当前时间是否允许交易
                if (!ComFunction.GetDateset(ptc.PriceCode,dt))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL022;
                    marketorders.Desc = ResCode.UL022Desc;
                    return marketorders;
                }
                #endregion

                #region 判断商品是否处于交易时段
                if (!ComFunction.ProductCanTrade(ptc.Starttime, ptc.Endtime))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL025;
                    marketorders.Desc = ResCode.UL025Desc;
                    return marketorders;
                }
                #endregion
                //获取服务器端实时价
                realprice = ComFunction.GetRealPrice(ptc.PriceCode);

                #region 判断商品是否允许交易 并确定下单价格
                //商品的状态(0 正常交易, 1 只报价, 2 只买, 3 只卖, 4 全部禁止)
                //商品允许正常交易
                if ("0" == ptc.State)
                {
                    //买
                    if ("0" == marorderln.OrderType)
                    {
                        marketorders.Desc = "买成功";
                        realprice += ptc.PriceDot * ptc.AdjustBase;
                    }
                    //卖
                    else if ("1" == marorderln.OrderType)
                    {
                        marketorders.Desc = "卖成功";
                    }
                    else
                    {
                        marketorders.Result = false;
                        marketorders.ReturnCode = ResCode.UL021;
                        marketorders.Desc = ResCode.UL021Desc;
                        return marketorders;
                    }
                }
                //商品只允许买
                else if ("2" == ptc.State)
                {
                    //买
                    if ("0" == marorderln.OrderType)
                    {
                        marketorders.Desc = "买成功";
                        realprice += ptc.PriceDot * ptc.AdjustBase;
                    }
                    else
                    {
                        marketorders.Result = false;
                        marketorders.ReturnCode = ResCode.UL026;
                        marketorders.Desc = ResCode.UL026Desc;
                        return marketorders;
                    }
                }
                //商品只允许卖
                else if ("3" == ptc.State)
                {
                    //卖
                    if ("1" == marorderln.OrderType)
                    {
                        marketorders.Desc = "卖成功";
                    }
                    else
                    {
                        marketorders.Result = false;
                        marketorders.ReturnCode = ResCode.UL027;
                        marketorders.Desc = ResCode.UL027Desc;
                        return marketorders;
                    }
                }
                else
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL025;
                    marketorders.Desc = ResCode.UL025Desc;
                    return marketorders;
                }
                #endregion

                #region 下单价判断
                //最小成交价格 < 建仓价 <最大成交价格
                if (!(realprice - ptc.MinPrice >= ComFunction.dzero && realprice - ptc.MaxPrice <= ComFunction.dzero))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL015;
                    marketorders.Desc = ResCode.UL015Desc;
                    return marketorders;
                }
                #endregion

                #region 最大交易时间差判断
                //当前客户端实时报价时间+允许最大交易时间差>=服务器时间
                if (!(marorderln.CurrentTime.AddSeconds(ptc.MaxTime) >= dt))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL014;
                    marketorders.Desc = ResCode.UL014Desc;
                    return marketorders;
                }
                #endregion

                #region 滑点判断
                //当前客户端实时价+滑点*点差基值>=实时价&&当前客户端实时价-滑点*点差基值<=实时价

                if (!(System.Math.Abs(marorderln.RtimePrices - realprice) - marorderln.MaxPrice * ptc.AdjustBase <= ComFunction.dzero))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL016;
                    marketorders.Desc = ResCode.UL016Desc;
                    return marketorders;
                }
                #endregion

                UserGroups ugs = ComFunction.GetUserGroups(userId);

                #region 对同一行情的商品,平仓后xxx秒不能下单的判断
                //实现方法:在历史订单表中查询此用户此行情在最近xxx秒内是否存在历史订单
                //如果存在,则不允许下单;否则允许下单
                if (ComFunction.JudgeExistLTradeOrder(userId, ptc.PriceCode, ugs.AfterSecond))
                {
                    marketorders.Result = false;
                    marketorders.Desc = string.Format("连接服务器失败,请重试!", ugs.AfterSecond);
                    return marketorders;
                }
                #endregion

                #region 根据下单滑点重新计算下单价

                if ("1" == marorderln.OrderType)//卖单 下单价向下滑
                {
                    realprice -= ugs.PlaceOrderSlipPoint * ptc.AdjustBase;
                }
                else//买单 下单价向上滑
                {
                    realprice += ugs.PlaceOrderSlipPoint * ptc.AdjustBase;
                }
                if (realprice <= ComFunction.dzero)
                {
                    marketorders.Result = false;
                    marketorders.Desc = "市价单下单异常";
                    return marketorders;
                }
                #endregion
                realprice = System.Math.Round(realprice, ptc.AdjustCount, MidpointRounding.AwayFromZero);//把多余的小数位去掉
                #region 风险率判断
                marketorders.MoneyInventory = ComFunction.GetMoneyInventoryByUserId(userId);
                if (!marketorders.MoneyInventory.Result)
                {
                    marketorders.Result = false;
                    marketorders.Desc = "未能获取资金库存";
                    return marketorders;
                }
                //计算本次下单的占用资金
                //_occMoney = System.Math.Round(marorderln.RtimePrices / ptc.AdjustBase * ptc.ValueDot * marorderln.Quantity * marorderln.OrderMoney, 2, MidpointRounding.AwayFromZero);
                _occMoney = System.Math.Round(ComFunction.Getfee(ptc.OrderMoneyFee, marorderln.OrderMoney, realprice, marorderln.Quantity), 2, MidpointRounding.AwayFromZero);
                //工费
                _tradefee = System.Math.Round(ComFunction.Getfee(ptc.ExpressionFee, marorderln.OrderMoney, realprice, marorderln.Quantity), 2, MidpointRounding.AwayFromZero);

                /* 老的风险率判断代码
                //风险率<=60%(加上本单的占用保证金和工费)
                fxrate = (_tradefee + _occMoney + marketorders.MoneyInventory.FdInfo.OccMoney + marketorders.MoneyInventory.FdInfo.FrozenMoney) / marketorders.MoneyInventory.FdInfo.Money;
                if (marketorders.MoneyInventory.FdInfo.Money - _tradefee - _occMoney - marketorders.MoneyInventory.FdInfo.OccMoney - marketorders.MoneyInventory.FdInfo.FrozenMoney <= ComFunction.dzero)
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL012;
                    marketorders.Desc = ResCode.UL012Desc;
                    //com.individual.helper.LogNet4.WriteMsg(string.Format("Money={0},_tradefee + _occMoney + OccMoney={1}+{2}+{3}",
                    //    marketorders.MoneyInventory.FdInfo.Money, _tradefee, _occMoney, marketorders.MoneyInventory.FdInfo.OccMoney));
                    return marketorders;
                }
                if (!(fxrate - ComFunction.fenxian_rate <= ComFunction.dzero))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL013;
                    marketorders.Desc = ResCode.UL013Desc;
                    return marketorders;
                }
                */

                #region 新的风险率判断代码
                double DongJieMoney = 0;//冻结资金
                if (marketorders.MoneyInventory.FdInfo.DongJieMoney > ComFunction.dzero)
                {
                    DongJieMoney = marketorders.MoneyInventory.FdInfo.DongJieMoney;
                }
                double UseMoney = _occMoney + marketorders.MoneyInventory.FdInfo.OccMoney + marketorders.MoneyInventory.FdInfo.FrozenMoney;
                if (UseMoney <= ComFunction.dzero)//如果使用金额为0 则说明有问题 使用金额不可能小于0
                {
                    marketorders.Result = false;
                    marketorders.Desc = "市价单下单失败!";
                    return marketorders;
                }
                double yingkui = ComFunction.GetUserYingKui(userId);//用户的盈亏
                //com.individual.helper.LogNet4.WriteMsg(yingkui.ToString());
                fxrate = (marketorders.MoneyInventory.FdInfo.Money - DongJieMoney - _tradefee + yingkui) / UseMoney;//分子=当前帐户余额-冻结资金-本次的工费

                if (marketorders.MoneyInventory.FdInfo.Money - DongJieMoney - _tradefee - _occMoney - marketorders.MoneyInventory.FdInfo.OccMoney - marketorders.MoneyInventory.FdInfo.FrozenMoney <= ComFunction.dzero)
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL012;
                    marketorders.Desc = ResCode.UL012Desc;
                    //com.individual.helper.LogNet4.WriteMsg(string.Format("Money={0},_tradefee + _occMoney + OccMoney={1}+{2}+{3}",
                    //    marketorders.MoneyInventory.FdInfo.Money, _tradefee, _occMoney, marketorders.MoneyInventory.FdInfo.OccMoney));
                    return marketorders;
                }
                if (fxrate - ComFunction.fenxian_rate <= ComFunction.dzero)
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL013;
                    marketorders.Desc = ResCode.UL013Desc;
                    return marketorders;
                }
                #endregion

                #endregion

                //下单的时候不计算仓储费,直接赋值为0
                marketorders.TradeOrder.StorageFee = 0;
                orderid = ComFunction.GetOrderId(ComFunction.Order);
                #region 数据库事务处理
                List<string> sqlList = new List<string>();
                sqlList.Add(string.Format("INSERT INTO Trade_Order([userId],[Orderid],[productCode],[Ordertype],[Orderprice],[usequantity],[quantity],[lossprice],[profitPrice],[OccMoney],[tradefee],[storagefee],[Ordertime],[OperType],[ip],[mac],[AllowStore]) VALUES('{0}','{1}','{2}',{3},{4},{5},{6},{7},{8},{9},{10},{11},'{12}',{13},'{14}','{15}',{16})",
                    userId, orderid, marorderln.ProductCode, marorderln.OrderType, realprice, marorderln.Quantity,
                    marorderln.Quantity, marorderln.LossPrice, marorderln.ProfitPrice, _occMoney, _tradefee, marketorders.TradeOrder.StorageFee,
                    dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), 1, ComFunction.GetClientIp(), marorderln.Mac, allowStore));
                sqlList.Add(string.Format("update Trade_FundInfo set occMoney = occMoney+{0},[money]=[money]-{1} where userid='{2}' and [state]<>'4'", _occMoney,_tradefee, userId));
                //添加操作记录
                sqlList.Add(string.Format("insert into Base_OperrationLog([OperTime],[Account],[UserType],[Remark]) values('{0}','{1}',{2},'{3}')", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), operUser,operUserType, string.Format("{1}在手订单{0}", orderid,ipmac)));

                if (!ComFunction.SqlTransaction(sqlList))
                {
                    marketorders.Result = false;
                    marketorders.Desc = "市价单下单时出错";
                    return marketorders;
                }
                #endregion

                #region 给返回对象赋值
                marketorders.Result = true;
                marketorders.ChengjiaoPrice = realprice;
                marketorders.MoneyInventory.FdInfo.OccMoney += _occMoney;
                marketorders.MoneyInventory.FdInfo.Money -= _tradefee;
                marketorders.TradeOrder.LossPrice = marorderln.LossPrice;
                marketorders.TradeOrder.OccMoney = _occMoney;
                marketorders.TradeOrder.OrderId = orderid;
                marketorders.TradeOrder.OrderPrice = realprice;
                marketorders.TradeOrder.OrderTime = dt;
                marketorders.TradeOrder.OrderType = marorderln.OrderType;
                marketorders.TradeOrder.ProductCode = marorderln.ProductCode;
                marketorders.TradeOrder.ProductName = ptc.ProductName;
                marketorders.TradeOrder.ProfitPrice = marorderln.ProfitPrice;
                marketorders.TradeOrder.Quantity = marorderln.Quantity;
                marketorders.TradeOrder.TradeFee = _tradefee;
                marketorders.TradeOrder.UseQuantity = marorderln.Quantity;
                marketorders.TradeOrder.PriceCode = ptc.PriceCode;
                #endregion

            }
            catch (Exception ex)
            {
                ComFunction.WriteErr(ex);
                marketorders.Result = false;
                marketorders.Desc = "市价单下单失败";

            }
            return marketorders;
        }
        /// <summary>
        ///  根据原料编码 获取商品信息
        /// </summary>
        /// <param name="goodsCode">原料编码</param>
        /// <param name="lowerprice">输出:下浮价格</param>
        /// <returns>商品信息</returns>
        public static ProductConfig GetProductInfoWithGoodscode(string goodsCode, ref double lowerprice)
        {
            SqlConnection sqlconn = null;
            SqlCommand sqlcmd = null;
            SqlDataReader sqldr = null;
            ProductConfig ptc = new ProductConfig();
            try
            {
                sqlconn = new SqlConnection(SqlConnectionString);
                sqlconn.Open();
                sqlcmd = sqlconn.CreateCommand();
                sqlcmd.CommandText = "select top 1 productcode, ProductName,goodscode,adjustbase,adjustcount,pricedot,valuedot,setBase,holdbase,Ordemoney, "
                + " maxprice,minprice,maxtime,state,unit,pricecode,expressionfee,buystoragefee,sellstoragefee,sellfee,lowerprice from Trade_Product where goodscode=@goodscode";
                SqlParameter p_pcode = new SqlParameter();
                p_pcode.ParameterName = "@goodscode";
                p_pcode.DbType = DbType.String;
                p_pcode.Value = goodsCode;
                sqlcmd.Parameters.Add(p_pcode);

                sqldr = sqlcmd.ExecuteReader();
                while (sqldr.Read())
                {
                    ptc.ProductCode = sqldr["ProductCode"].ToString();
                    ptc.ProductName = sqldr["ProductName"].ToString();
                    ptc.GoodsCode = sqldr["goodscode"].ToString();
                    ptc.AdjustBase = Convert.ToDouble(sqldr["adjustbase"]);
                    ptc.AdjustCount = Convert.ToInt32(sqldr["adjustcount"]);
                    ptc.PriceDot = Convert.ToInt32(sqldr["pricedot"]);
                    ptc.ValueDot = Convert.ToDouble(sqldr["valuedot"]);
                    ptc.SetBase = Convert.ToInt32(sqldr["setBase"]);
                    ptc.HoldBase = Convert.ToInt32(sqldr["holdbase"]);
                    ptc.OrderMoney = Convert.ToDouble(sqldr["Ordemoney"]);
                    ptc.MaxPrice = Convert.ToDouble(sqldr["MaxPrice"]);
                    ptc.MinPrice = Convert.ToDouble(sqldr["MinPrice"]);
                    ptc.MaxTime = Convert.ToDouble(sqldr["MaxTime"]);
                    ptc.State = sqldr["State"].ToString();
                    ptc.Unit = Convert.ToDouble(sqldr["Unit"]);
                    ptc.PriceCode = sqldr["PriceCode"].ToString();
                    ptc.ExpressionFee = sqldr["ExpressionFee"].ToString();
                    ptc.BuyStorageFee = sqldr["BuyStorageFee"].ToString();
                    ptc.SellStorageFee = sqldr["SellStorageFee"].ToString();
                    ptc.SellFee = sqldr["SellFee"].ToString();
                    lowerprice = Convert.ToDouble(sqldr["lowerprice"]);
                    break;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
            finally
            {
                if (null != sqlconn)
                {
                    sqlconn.Close();
                }
                if (null != sqldr)
                {
                    sqldr.Close();
                }
            }
            return ptc;
        }
        /// <summary>
        /// 市价单修改
        /// </summary>
        /// <param name="marln">修改信息</param>
        /// <returns>修改结果</returns>
        public Marketorders ModifyMarketorders(MarketLnEnter marln)
        {
            Marketorders marketorders = new Marketorders();
            marketorders.MoneyInventory = new MoneyInventory();
            marketorders.TradeOrder = new TradeOrder();
            ProductConfig ptc = new ProductConfig();
            DateTime dt = DateTime.Now;//服务器当前本地时间
            string userId = string.Empty;
            string order_ip = string.Empty;
            string order_mac = string.Empty;
            string operUser = marln.TradeAccount;
            TradeUser TdUser = new TradeUser();
            string ipmac = string.Empty;
            try
            {
                #region 判断用户登陆标识是否过期

                if (!ComFunction.ExistUserLoginID(marln.LoginID,ref TdUser))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL003;
                    marketorders.Desc = ResCode.UL003Desc;
                    return marketorders;
                }

                operUser = TdUser.Account;
                ipmac = ComFunction.GetIpMac(TdUser.Ip, TdUser.Mac);

                if (UserType.NormalType == TdUser.UType)
                {
                    userId = TdUser.UserID;
                }
                else
                {
                    userId = ComFunction.GetUserId(marln.TradeAccount);
                }
                #endregion

                #region 根据订单ID获取订单信息
                marketorders.TradeOrder = ComFunction.GetTradeOrder(marln.OrderId, ref order_ip, ref order_mac);
                if (string.IsNullOrEmpty(marketorders.TradeOrder.OrderId))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL028;
                    marketorders.Desc = ResCode.UL028Desc;
                    return marketorders;
                }
                #endregion

                #region 获取商品信息
                ptc = ComFunction.GetProductInfo(marketorders.TradeOrder.ProductCode);
                //订单类型(0买、1卖)
                //未能获取商品状态
                if (string.IsNullOrEmpty(ptc.State))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL024;
                    marketorders.Desc = ResCode.UL024Desc;
                    return marketorders;
                }
                #endregion

                #region 判断当前时间是否允许交易
                if (!ComFunction.GetDateset(ptc.PriceCode,dt))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL022;
                    marketorders.Desc = ResCode.UL022Desc;
                    return marketorders;
                }
                #endregion

                #region 判断商品是否处于交易时段
                if (!ComFunction.ProductCanTrade(ptc.Starttime, ptc.Endtime))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL025;
                    marketorders.Desc = ResCode.UL025Desc;
                    return marketorders;
                }
                #endregion

                #region 最大交易时间差判断
                //当前客户端实时报价时间+允许最大交易时间差>=服务器时间
                if (!(marln.CurrentTime.AddSeconds(ptc.MaxTime) >= dt))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL014;
                    marketorders.Desc = ResCode.UL014Desc;
                    return marketorders;
                }
                #endregion

                marketorders.MoneyInventory = ComFunction.GetMoneyInventoryByUserId(userId);
                if (!marketorders.MoneyInventory.Result)
                {
                    marketorders.Result = false;
                    marketorders.Desc = "未能获取资金库存";
                    return marketorders;
                }

                #region 数据库事务处理
                List<string> sqlList = new List<string>();
                sqlList.Add(string.Format("UPDATE Trade_Order set profitPrice={0},lossprice={1} where Orderid='{2}' and userid='{3}'", marln.ProfitPrice, marln.LossPrice, marln.OrderId,userId));
                //添加操作记录
                sqlList.Add(string.Format("insert into Base_OperrationLog([OperTime],[Account],[UserType],[Remark]) values('{0}','{1}',{2},'{3}')", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), operUser, (int)TdUser.UType, string.Format("{1}在手订单{0},设置盈损点", marln.OrderId, ipmac)));

                if (!ComFunction.SqlTransaction(sqlList))
                {
                    marketorders.Result = false;
                    marketorders.Desc = "更新订单错误";
                    return marketorders;
                }
                #endregion

                #region 给返回对象赋值
                marketorders.Result = true;
                marketorders.Desc = "修改成功";
                marketorders.TradeOrder.ProfitPrice = marln.ProfitPrice;
                marketorders.TradeOrder.LossPrice = marln.LossPrice;
                marketorders.TradeOrder.PriceCode = ptc.PriceCode;
                #endregion
            }
            catch (Exception ex)
            {
                ComFunction.WriteErr(ex);
                marketorders.Result = false;
                marketorders.Desc = "市价单修改失败";

            }
            return marketorders;
        }
        /// <summary>
        /// 市价单取消接口 结算盈亏 退还保证金
        /// </summary>
        /// <param name="delen">市价单取消信息</param>
        /// <returns>市价单取消结果</returns>
        public Marketorders DelOrder(DeliveryEnter delen)
        {
            Marketorders marketorders = new Marketorders();
            marketorders.TradeOrder = new TradeOrder();
            marketorders.MoneyInventory = new MoneyInventory();
            DateTime dt = DateTime.Now;//服务器当前本地时间
            ProductConfig ptc = new ProductConfig();
            TradeUser TdUser = new TradeUser();
            string order_ip = string.Empty;
            string order_mac = string.Empty;
            double realprice = 0;
            double _occmoney = 0;
            double _yingkuifee = 0;
            string tmpsql = string.Empty;
            string userId = string.Empty;
            string restrictions = string.Empty;
            string operUser = delen.TradeAccount;
            string ipmac = string.Empty;
            int operUserType = 0;
            try
            {

                #region 判断用户登陆标识是否过期

                if (!ComFunction.ExistUserLoginID(delen.LoginID, ref TdUser))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL003;
                    marketorders.Desc = ResCode.UL003Desc;
                    return marketorders;
                }
                operUser = TdUser.Account;
                ipmac = ComFunction.GetIpMac(TdUser.Ip, TdUser.Mac);
                operUserType = (int)TdUser.UType;
                if (UserType.NormalType == TdUser.UType)
                {
                    userId = TdUser.UserID;
                }
                else
                {
                    userId = ComFunction.GetUserId(delen.TradeAccount, ref TdUser);
                }
                #endregion

                #region 交易手数验证
                if (!ComFunction.ValidateQuantity(delen.Quantity, TdUser.OrderUnit))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL044;
                    //marketorders.Desc = ResCode.UL044Desc;
                    marketorders.Desc = string.Format("交易手数必须是{0}的倍数", TdUser.OrderUnit);
                    return marketorders;
                }
                #endregion

                #region 判断用户是否允许平仓
                if (!TdUser.PermitDelOrder)
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL019;
                    marketorders.Desc = ResCode.UL019Desc;
                    return marketorders;
                }
                #endregion

                #region 根据订单ID获取订单信息
                marketorders.TradeOrder = ComFunction.GetTradeOrder(delen.Orderid, ref order_ip, ref order_mac);
                if (null == marketorders.TradeOrder.OrderId)
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL028;
                    marketorders.Desc = ResCode.UL028Desc;
                    return marketorders;
                }
                #endregion

                #region 获取商品信息
                ptc = ComFunction.GetProductInfo(marketorders.TradeOrder.ProductCode);
                //订单类型(0买、1卖)
                //未能获取商品状态
                if (string.Empty == ptc.State)
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL024;
                    marketorders.Desc = ResCode.UL024Desc;
                    return marketorders;
                }
                #endregion

                #region 判断当前时间是否允许交易
                if (!ComFunction.GetDateset(ptc.PriceCode,dt))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL022;
                    marketorders.Desc = ResCode.UL022Desc;
                    return marketorders;
                }
                #endregion

                #region 提货数判断
                if (!(delen.Quantity - marketorders.TradeOrder.UseQuantity <= ComFunction.dzero && delen.Quantity > ComFunction.dzero))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL011;
                    marketorders.Desc = ResCode.UL011Desc;
                    return marketorders;
                }
                #endregion

                #region 判断商品是否处于交易时段
                if (!ComFunction.ProductCanTrade(ptc.Starttime, ptc.Endtime))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL025;
                    marketorders.Desc = ResCode.UL025Desc;
                    return marketorders;
                }
                #endregion

                #region 最大交易时间差判断
                //当前客户端实时报价时间+允许最大交易时间差>=服务器时间
                if (!(delen.CurrentTime.AddSeconds(ptc.MaxTime) >= dt))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL014;
                    marketorders.Desc = ResCode.UL014Desc;
                    return marketorders;
                }
                #endregion

                realprice = ComFunction.GetRealPrice(ptc.PriceCode);
                //卖类型的订单 按买价平仓,买类型的订单 按卖价平仓
                if ("1" == marketorders.TradeOrder.OrderType)
                {
                    realprice += ptc.PriceDot * ptc.AdjustBase;
                }

                #region 滑点判断
                if (!(System.Math.Abs(delen.RtimePrices - realprice) - delen.MaxPrice * ptc.AdjustBase <= ComFunction.dzero))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL016;
                    marketorders.Desc = ResCode.UL016Desc;
                    return marketorders;
                }
                #endregion

                #region 获取资金库存
                marketorders.MoneyInventory = ComFunction.GetMoneyInventoryByUserId(userId);
                if (!marketorders.MoneyInventory.Result)
                {
                    marketorders.Result = false;
                    marketorders.Desc = "未能获取资金库存";
                    return marketorders;
                }
                #endregion

                int xs = 1;//工费收取倍数

                #region 根据平仓滑点重新计算平仓价
                UserGroups ugs = ComFunction.GetUserGroups(userId);

                if ("1" == marketorders.TradeOrder.OrderType)//卖单 平仓价向上滑
                {
                    realprice += ugs.FlatOrderSlipPoint * ptc.AdjustBase;
                }
                else//买单 平仓价向下滑
                {
                    realprice -= ugs.FlatOrderSlipPoint * ptc.AdjustBase;
                }
                if (realprice <= ComFunction.dzero)
                {
                    marketorders.Result = false;
                    marketorders.Desc = "平仓异常";
                    return marketorders;
                }
                #endregion
                realprice = System.Math.Round(realprice, ptc.AdjustCount, MidpointRounding.AwayFromZero);//把多余的小数位去掉
                #region 计算占用资金 盈亏
                _occmoney = System.Math.Round(marketorders.TradeOrder.OccMoney * delen.Quantity / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                //卖
                if ("1" == marketorders.TradeOrder.OrderType)
                {
                    //_yingkuifee = System.Math.Round(-(realprice - marketorders.TradeOrder.OrderPrice) / ptc.AdjustBase * ptc.ValueDot * delen.Quantity - (marketorders.TradeOrder.TradeFee * xs + marketorders.TradeOrder.StorageFee) * delen.Quantity / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                    _yingkuifee = System.Math.Round(-(realprice - marketorders.TradeOrder.OrderPrice) / ptc.AdjustBase * ptc.ValueDot * delen.Quantity, 2, MidpointRounding.AwayFromZero);

                }
                //买
                else
                {
                    //_yingkuifee = System.Math.Round((realprice - marketorders.TradeOrder.OrderPrice) / ptc.AdjustBase * ptc.ValueDot * delen.Quantity - (marketorders.TradeOrder.TradeFee * xs + marketorders.TradeOrder.StorageFee) * delen.Quantity / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                    _yingkuifee = System.Math.Round((realprice - marketorders.TradeOrder.OrderPrice) / ptc.AdjustBase * ptc.ValueDot * delen.Quantity, 2, MidpointRounding.AwayFromZero);
                }

                #endregion

                #region 数据库事务处理
                if (System.Math.Abs(marketorders.TradeOrder.UseQuantity - delen.Quantity) <= ComFunction.dzero)
                {
                    tmpsql = string.Format("delete from Trade_Order where Orderid='{0}'", delen.Orderid);
                }
                else
                {
                    tmpsql = string.Format("update Trade_Order set usequantity = {1},OccMoney=OccMoney*{1}/usequantity,tradefee=tradefee*{1}/usequantity,storagefee=storagefee*{1}/usequantity where Orderid='{0}'", delen.Orderid, marketorders.TradeOrder.UseQuantity - delen.Quantity);
                }
                List<string> sqlList = new List<string>();
                double ResultOccMoney = marketorders.MoneyInventory.FdInfo.OccMoney - _occmoney;
                if (ResultOccMoney < ComFunction.dzero)
                {
                    ResultOccMoney = 0;
                }
                sqlList.Add(string.Format("update Trade_FundInfo set occMoney={0}, money=money+{1} where userid='{2}' and [state]<>'4'", ResultOccMoney, _yingkuifee, userId));
                sqlList.Add(string.Format("INSERT INTO Fund_Change([userId],[reason],[Oldvalue],[NewValue],[OperUser],[OperTime],[RelaOrder],[ChangeValue],[CashUser]) VALUES('{0}','{1}',{2},{3},'{4}','{5}','{6}',{7},'{8}')", userId, 2, marketorders.MoneyInventory.FdInfo.Money, marketorders.MoneyInventory.FdInfo.Money + _yingkuifee, operUser, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), delen.Orderid, _yingkuifee, marketorders.MoneyInventory.FdInfo.CashUser));
                sqlList.Add(tmpsql);
                sqlList.Add(string.Format("INSERT INTO L_Trade_Order([userId],[historyOrderId],[Orderid],[productCode],[OrderType],[Orderprice],[overprice],[quantity],[lossprice],[profitPrice],[OccMoney],[tradefee],[storagefee],[Ordertime],[OperType],[profitValue],[Overtime],[overtype],[ip],[mac]) VALUES('{0}','{1}','{2}','{3}',{4},{5},{6},{7},{8},{9},{10},{11},{12},'{13}',{14},{15},'{16}',{17},'{18}','{19}')",
                    userId, ComFunction.GetOrderId(ComFunction.Order_His), marketorders.TradeOrder.OrderId, marketorders.TradeOrder.ProductCode, marketorders.TradeOrder.OrderType,
                    marketorders.TradeOrder.OrderPrice, realprice, delen.Quantity, marketorders.TradeOrder.LossPrice, marketorders.TradeOrder.ProfitPrice,
                    _occmoney, marketorders.TradeOrder.TradeFee * delen.Quantity / marketorders.TradeOrder.UseQuantity, marketorders.TradeOrder.StorageFee * delen.Quantity / marketorders.TradeOrder.UseQuantity,
                    marketorders.TradeOrder.OrderTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                    marketorders.TradeOrder.OperType, _yingkuifee, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), 1, order_ip, order_mac));
                //添加操作记录
                sqlList.Add(string.Format("insert into Base_OperrationLog([OperTime],[Account],[UserType],[Remark]) values('{0}','{1}',{2},'{3}')", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), operUser, operUserType, string.Format("{1}平仓{0}", delen.Orderid, ipmac)));

                if (!ComFunction.SqlTransaction(sqlList))
                {
                    marketorders.Result = false;
                    marketorders.Desc = "平仓出错";
                    return marketorders;
                }
                #endregion

                //给返回对象赋值
                marketorders.Result = true;
                marketorders.Desc = "交易成功";
                marketorders.ChengjiaoPrice = realprice;
                marketorders.MoneyInventory.FdInfo.Money += _yingkuifee;
                marketorders.MoneyInventory.FdInfo.OccMoney -= _occmoney;
                marketorders.TradeOrder.OccMoney = System.Math.Round(marketorders.TradeOrder.OccMoney * (marketorders.TradeOrder.UseQuantity - delen.Quantity) / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                marketorders.TradeOrder.TradeFee = System.Math.Round(marketorders.TradeOrder.TradeFee * (marketorders.TradeOrder.UseQuantity - delen.Quantity) / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                marketorders.TradeOrder.StorageFee = System.Math.Round(marketorders.TradeOrder.StorageFee * (marketorders.TradeOrder.UseQuantity - delen.Quantity) / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                marketorders.TradeOrder.UseQuantity -= delen.Quantity;
                marketorders.TradeOrder.PriceCode = ptc.PriceCode;
            }
            catch (Exception ex)
            {
                ComFunction.WriteErr(ex);
                marketorders.Result = false;
                marketorders.Desc = "平仓失败";

            }

            return marketorders;
        }
        /// <summary>
        /// 挂单取消
        /// </summary>
        /// <param name="DhInfo">取消信息</param>
        /// <returns>取消结果</returns>
        public MarDelivery DelHoldOrder(DelHoldInfo DhInfo)
        {
            MarDelivery ctpOrder = new MarDelivery();
            ctpOrder.MoneyInventory = new MoneyInventory();
            DateTime dt = DateTime.Now;//服务器当前本地时间
            TradeHoldOrder tradeholdorder = new TradeHoldOrder();
            ProductConfig ptc = new ProductConfig();
            TradeUser TdUser = new TradeUser();
            //从挂单表中查询出的用户ID IP MAC
            string hold_userid = string.Empty;
            string hold_ip = string.Empty;
            string hold_mac = string.Empty;
            string userId = string.Empty;
            string operUser = DhInfo.TradeAccount;
            string ipmac = string.Empty;
            try
            {
                #region 判断用户登陆标识是否过期

                if (!ComFunction.ExistUserLoginID(DhInfo.LoginID,ref TdUser))
                {
                    ctpOrder.Result = false;
                    ctpOrder.ReturnCode = ResCode.UL003;
                    ctpOrder.Desc = ResCode.UL003Desc;
                    return ctpOrder;
                }
                operUser = TdUser.Account;
                ipmac = ComFunction.GetIpMac(TdUser.Ip, TdUser.Mac);

                if (UserType.NormalType == TdUser.UType)
                {
                    userId = TdUser.UserID;
                }
                else
                {
                    userId = ComFunction.GetUserId(DhInfo.TradeAccount);
                }
                #endregion

                #region 根据挂单ID获取挂单信息
                tradeholdorder = ComFunction.GetTradeHoldOrder(DhInfo.HoldOrderID, ref hold_userid, ref hold_ip, ref hold_mac);
                if (string.IsNullOrEmpty(tradeholdorder.ProductCode))
                {
                    ctpOrder.Result = false;
                    ctpOrder.ReturnCode = ResCode.UL028;
                    ctpOrder.Desc = ResCode.UL028Desc;
                    return ctpOrder;
                }
                #endregion

                #region 获取商品信息
                ptc = ComFunction.GetProductInfo(tradeholdorder.ProductCode);
                //挂单类型(0买、1卖)
                //未能获取商品状态
                if (string.IsNullOrEmpty(ptc.State))
                {
                    ctpOrder.Result = false;
                    ctpOrder.ReturnCode = ResCode.UL024;
                    ctpOrder.Desc = ResCode.UL024Desc;
                    return ctpOrder;
                }
                #endregion

                #region 判断当前时间是否允许交易
                if (!ComFunction.GetDateset(ptc.PriceCode, dt))
                {
                    ctpOrder.Result = false;
                    ctpOrder.ReturnCode = ResCode.UL022;
                    ctpOrder.Desc = ResCode.UL022Desc;
                    return ctpOrder;
                }
                #endregion

                #region 判断商品是否处于交易时段
                if (!ComFunction.ProductCanTrade(ptc.Starttime, ptc.Endtime))
                {
                    ctpOrder.Result = false;
                    ctpOrder.ReturnCode = ResCode.UL025;
                    ctpOrder.Desc = ResCode.UL025Desc;
                    return ctpOrder;
                }
                #endregion

                #region 最大交易时间差判断
                //当前客户端实时报价时间+允许最大交易时间差>=服务器时间
                if (!(DhInfo.CurrentTime.AddSeconds(ptc.MaxTime) >= dt))
                {
                    ctpOrder.Result = false;
                    ctpOrder.ReturnCode = ResCode.UL014;
                    ctpOrder.Desc = ResCode.UL014Desc;
                    return ctpOrder;
                }
                #endregion

                ctpOrder.MoneyInventory = ComFunction.GetMoneyInventoryByUserId(userId);
                if (!ctpOrder.MoneyInventory.Result)
                {
                    ctpOrder.Result = false;
                    ctpOrder.Desc = "未能获取资金库存";
                    return ctpOrder;
                }
                #region 数据库事务处理

                /*
                List<string> sqlList = new List<string>();
                sqlList.Add(string.Format("delete from Trade_HoldOrder where holdorderid='{0}'", DhInfo.HoldOrderID));

                sqlList.Add(string.Format("INSERT INTO L_Trade_HoldOrder([userId],[HoldOrderID],[productCode],[quantity],[OrderType],[HoldPrice],[lossPrice],[profitPrice],[frozenMoney],[validtime],[ordertime],[IsTrade],[tradetime],[OrderID],[failreason],[ip],[mac]) VALUES('{0}','{1}','{2}',{3},{4},{5},{6},{7},{8},'{9}','{10}',{11},'{12}','{13}','{14}','{15}','{16}')",
                    hold_userid, tradeholdorder.HoldOrderID, tradeholdorder.ProductCode, tradeholdorder.Quantity,
                    tradeholdorder.OrderType, tradeholdorder.HoldPrice, tradeholdorder.LossPrice, tradeholdorder.ProfitPrice,
                    tradeholdorder.FrozenMoney, tradeholdorder.ValidTime.ToString("yyyy-MM-dd HH:mm:ss.fff"), tradeholdorder.OrderTime.ToString("yyyy-MM-dd HH:mm:ss.fff"), 1, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                   ComFunction.GetOrderId(ComFunction.Hold_His), DhInfo.ReasonType, hold_ip, hold_mac));
                double ResultfrozenMoney = ctpOrder.MoneyInventory.FdInfo.FrozenMoney - tradeholdorder.FrozenMoney;
                if (ResultfrozenMoney < ComFunction.dzero)
                {
                    ResultfrozenMoney = 0;
                }
                sqlList.Add(string.Format("update Trade_FundInfo set frozenMoney={0} where userid='{1}' and [state]<>'4'", ResultfrozenMoney, userId));

                //添加操作记录
                sqlList.Add(string.Format("insert into Base_OperrationLog([OperTime],[Account],[UserType],[Remark]) values('{0}','{1}',{2},'{3}')", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), operUser, (int)TdUser.UType, string.Format("{1}撤销委托订单{0}", DhInfo.HoldOrderID,ipmac)));

                if (!ComFunction.SqlTransaction(sqlList))
                {
                    ctpOrder.Result = false;
                    ctpOrder.Desc = "挂单取消时数据库事务处理出错";
                    return ctpOrder;
                }
                */
                //改用存储过程实现
                int result = ComFunction.DoHoldTradeOrder(DhInfo.HoldOrderID, 1, 0);
                #endregion

                if (99 == result)
                {
                    ctpOrder.Result = false;
                    ctpOrder.Desc = "挂单取消失败!";
                    return ctpOrder;
                }

                #region 给返回对象赋值

                ctpOrder.MoneyInventory.FdInfo.FrozenMoney -= tradeholdorder.FrozenMoney;
                ctpOrder.Result = true;
                switch (result)//99失败,100成功,0挂单已被自动撤销 1挂单已被手动撤销 2挂单已经自动转换为订单
                {
                    case 0: ctpOrder.Desc = "挂单已被自动撤销";
                        break;
                    case 1: ctpOrder.Desc = "挂单已被手动撤销";
                        break;
                    case 2: ctpOrder.Desc = "挂单已经自动转换为订单";
                        break;
                    default: ctpOrder.Desc = "挂单取消成功";
                        break;
                }
                #endregion
            }
            catch (Exception ex)
            {
                ComFunction.WriteErr(ex);
                ctpOrder.Result = false;
                ctpOrder.Desc = "挂单取消失败";

            }

            return ctpOrder;
        }
        /// <summary>
        /// 挂单下单
        /// </summary>
        /// <param name="orderln">下单信息</param>
        /// <returns>下单结果</returns>
        public Orders GetOrders(OrdersLncoming orderln)
        {
            Orders orders = new Orders();
            orders.MoneyInventory = new MoneyInventory();
            orders.TradeHoldOrder = new TradeHoldOrder();
            ProductConfig ptc = new ProductConfig();
            DateTime dt = DateTime.Now;//服务器当前本地时间\
            TradeUser TdUser = new TradeUser();
            double _frozenMoney = 0;//冻结资金
            double fxrate = 0; //风险率
            string holdid = string.Empty;//挂单ID
            string userId = string.Empty; //被操作的用户ID
            string operUser = string.Empty;//操作人
            string ipmac = string.Empty;
            int operUserType = 0;
            try
            {

                #region 判断用户登陆标识是否过期

                if (!ComFunction.ExistUserLoginID(orderln.LoginID, ref TdUser))
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL003;
                    orders.Desc = ResCode.UL003Desc;
                    return orders;
                }
                operUser = TdUser.Account;
                ipmac = ComFunction.GetIpMac(TdUser.Ip, TdUser.Mac);
                operUserType = (int)TdUser.UType;
                if (UserType.NormalType == TdUser.UType)
                {
                    userId = TdUser.UserID;
                }
                else
                {
                    userId = ComFunction.GetUserId(orderln.TradeAccount, ref TdUser);
                }

                #endregion

                #region 交易手数验证
                if (!ComFunction.ValidateQuantity(orderln.Quantity,TdUser.OrderUnit))
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL044;
                    //orders.Desc = ResCode.UL044Desc;
                    orders.Desc = string.Format("交易手数必须是{0}的倍数", TdUser.OrderUnit);
                    return orders;
                }
                #endregion

                #region 判断用户是否允许挂买单或挂卖单
                if ("0" == orderln.OrderType) //挂买单
                {
                    if (!TdUser.PermitDhuo)
                    {
                        orders.Result = false;
                        orders.ReturnCode = ResCode.UL017;
                        orders.Desc = ResCode.UL017Desc;
                        return orders;
                    }
                }
                else if ("1" == orderln.OrderType) //挂卖单
                {
                    if (!TdUser.PermitHshou)
                    {
                        orders.Result = false;
                        orders.ReturnCode = ResCode.UL018;
                        orders.Desc = ResCode.UL018Desc;
                        return orders;
                    }
                }
                else
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL021;
                    orders.Desc = ResCode.UL021Desc;
                    return orders;
                }
                #endregion

                #region 获取商品信息

                ptc = ComFunction.GetProductInfo(orderln.ProductCode);
                //挂单类型(0买、1卖)
                if (string.IsNullOrEmpty(ptc.State)) //未能获取商品状态
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL024;
                    orders.Desc = ResCode.UL024Desc;
                    return orders;
                }

                #endregion

                #region 判断当前时间服务器是否允许交易

                if (!ComFunction.GetDateset(ptc.PriceCode,dt))
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL022;
                    orders.Desc = ResCode.UL022Desc;
                    return orders;
                }

                #endregion

                #region 挂单有效期判断

                //判断有效期是否大于挂单时间
                DateTime vlidt =
                    Convert.ToDateTime(string.Format("{0}-{1}-{2} 23:59:59", orderln.ValidTime.Year,
                        orderln.ValidTime.Month, orderln.ValidTime.Day));
                if (!(vlidt > dt))
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL023;
                    orders.Desc = ResCode.UL023Desc;
                    return orders;
                }

                #endregion

                #region 判断商品是否处于交易时段
                if (!ComFunction.ProductCanTrade(ptc.Starttime, ptc.Endtime))
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL025;
                    orders.Desc = ResCode.UL025Desc;
                    return orders;
                }
                #endregion

                #region 判断商品是否允许交易

                //商品的状态(0 正常交易, 1 只报价, 2 只买, 3 只卖, 4 全部禁止)
                if ("0" == ptc.State) //商品允许正常交易
                {
                    if ("0" == orderln.OrderType) //买
                    {
                        orders.Desc = "买成功";
                    }
                    else if ("1" == orderln.OrderType) //卖
                    {
                        orders.Desc = "卖成功";
                    }
                    else
                    {
                        orders.Result = false;
                        orders.ReturnCode = ResCode.UL021;
                        orders.Desc = ResCode.UL021Desc;
                        return orders;
                    }
                }
                else if ("2" == ptc.State) //商品只允许买
                {
                    if ("0" == orderln.OrderType) //买
                    {
                        orders.Desc = "买成功";
                    }
                    else
                    {
                        orders.Result = false;
                        orders.ReturnCode = ResCode.UL026;
                        orders.Desc = ResCode.UL026Desc;
                        return orders;
                    }
                }
                else if ("3" == ptc.State) //商品只允许卖
                {
                    if ("1" == orderln.OrderType) //卖
                    {
                        orders.Desc = "卖成功";
                    }
                    else
                    {
                        orders.Result = false;
                        orders.ReturnCode = ResCode.UL027;
                        orders.Desc = ResCode.UL027Desc;
                        return orders;
                    }
                }
                else
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL025;
                    orders.Desc = ResCode.UL025Desc;
                    return orders;
                }

                #endregion

                #region 挂单价判断 (浮点数比较时,把两个数相减,然后和0.000001比较)

                //最小成交价格 < 挂单价 <最大成交价格
                if (!(orderln.HoldPrice - ptc.MinPrice >= ComFunction.dzero && orderln.HoldPrice - ptc.MaxPrice <= ComFunction.dzero))
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL015;
                    orders.Desc = ResCode.UL015Desc;
                    return orders;
                }

                #endregion

                #region 最大交易时间差判断

                //当前客户端实时报价时间+允许最大交易时间差>=服务器时间
                if (!(orderln.CurrentTime.AddSeconds(ptc.MaxTime) >= dt))
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL014;
                    orders.Desc = ResCode.UL014Desc;
                    return orders;
                }

                #endregion

                #region 风险率判断

                //获取帐户资金信息
                orders.MoneyInventory = ComFunction.GetMoneyInventoryByUserId(userId);
                if (!orders.MoneyInventory.Result)
                {
                    orders.Result = false;
                    orders.Desc = "未能获取资金库存";
                    return orders;
                }
                //冻结保证金=挂单价/[点差基值]*[点值]*下单数量*保证金百分比
                //风险率=([占用]+[冻结])/[账户结余]
                //风险率<=60%(加上本单的冻结保证金)
                //_frozenMoney =
                //    System.Math.Round(
                //        orderln.HoldPrice / ptc.AdjustBase * ptc.ValueDot * orderln.Quantity * orderln.OrderMoney, 2,
                //        MidpointRounding.AwayFromZero);
                _frozenMoney = System.Math.Round(ComFunction.Getfee(ptc.OrderMoneyFee, orderln.OrderMoney, orderln.HoldPrice, orderln.Quantity), 2, MidpointRounding.AwayFromZero);

                /* 老的风险率判断代码
                fxrate = (_frozenMoney + orders.MoneyInventory.FdInfo.OccMoney + orders.MoneyInventory.FdInfo.FrozenMoney) /
                         orders.MoneyInventory.FdInfo.Money;
                if (orders.MoneyInventory.FdInfo.Money - _frozenMoney - orders.MoneyInventory.FdInfo.OccMoney - orders.MoneyInventory.FdInfo.FrozenMoney<=ComFunction.dzero)
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL012;
                    orders.Desc = ResCode.UL012Desc;
                    return orders;
                }
                if (!(fxrate - ComFunction.fenxian_rate <= ComFunction.dzero))
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL013;
                    orders.Desc = ResCode.UL013Desc;
                    return orders;
                }
                */

                #region 新的风险率判断代码
                double DongJieMoney = 0;//冻结资金
                if (orders.MoneyInventory.FdInfo.DongJieMoney > ComFunction.dzero)
                {
                    DongJieMoney = orders.MoneyInventory.FdInfo.DongJieMoney;
                }
                double UseMoney = _frozenMoney + orders.MoneyInventory.FdInfo.OccMoney + orders.MoneyInventory.FdInfo.FrozenMoney;
                if (UseMoney <= ComFunction.dzero)//如果使用金额为0 则说明有问题 使用金额不可能小于0
                {
                    orders.Result = false;
                    orders.Desc = "挂单下单失败!";
                    return orders;
                }
                double yingkui = ComFunction.GetUserYingKui(userId);//用户的盈亏
                fxrate = (orders.MoneyInventory.FdInfo.Money - DongJieMoney + yingkui) / UseMoney;

                if (orders.MoneyInventory.FdInfo.Money - DongJieMoney - _frozenMoney - orders.MoneyInventory.FdInfo.OccMoney - orders.MoneyInventory.FdInfo.FrozenMoney <= ComFunction.dzero)
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL012;
                    orders.Desc = ResCode.UL012Desc;
                    return orders;
                }
                if (fxrate - ComFunction.fenxian_rate <= ComFunction.dzero)
                {
                    orders.Result = false;
                    orders.ReturnCode = ResCode.UL013;
                    orders.Desc = ResCode.UL013Desc;
                    return orders;
                }
                //
                #endregion

                #endregion

                holdid = ComFunction.GetOrderId(ComFunction.Hold);

                #region 数据库事务处理
                List<string> sqlList = new List<string>();
                sqlList.Add(string.Format("INSERT INTO Trade_HoldOrder([userId],[HoldOrderID],[productCode],[quantity],[OrderType],[HoldPrice],[lossPrice],[profitPrice],[frozenMoney],[validtime],[ordertime],[ip],[mac]) VALUES('{0}','{1}','{2}',{3},{4},{5},{6},{7},{8},'{9}','{10}','{11}','{12}')",
                            userId, holdid, orderln.ProductCode,
                            orderln.Quantity, orderln.OrderType, orderln.HoldPrice, orderln.LossPrice,
                            orderln.ProfitPrice, _frozenMoney, orderln.ValidTime.ToString("yyyy-MM-dd"),
                            dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), ComFunction.GetClientIp(), orderln.Mac));

                sqlList.Add(string.Format("update Trade_FundInfo set frozenMoney = frozenMoney+{0} where userid='{1}' and [state]<>'4'", _frozenMoney, userId));
                //添加操作记录

                sqlList.Add(string.Format("insert into Base_OperrationLog([OperTime],[Account],[UserType],[Remark]) values('{0}','{1}',{2},'{3}')",
                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), operUser, operUserType, string.Format("{1}委托订单{0}", holdid, ipmac)));

                if (!ComFunction.SqlTransaction(sqlList))
                {
                    orders.Result = false;
                    orders.Desc = "挂单时数据库事务处理出错";
                    return orders;
                }

                #endregion

                #region 给返回对象赋值

                orders.Result = true;
                orders.TradeHoldOrder.FrozenMoney = _frozenMoney;
                orders.TradeHoldOrder.HoldOrderID = holdid;
                orders.TradeHoldOrder.HoldPrice = orderln.HoldPrice;
                orders.TradeHoldOrder.LossPrice = orderln.LossPrice;
                orders.TradeHoldOrder.OrderTime = dt;
                orders.TradeHoldOrder.OrderType = orderln.OrderType;
                orders.TradeHoldOrder.ProductCode = orderln.ProductCode;
                orders.TradeHoldOrder.ProductName = ptc.ProductName;
                orders.TradeHoldOrder.ProfitPrice = orderln.ProfitPrice;
                orders.TradeHoldOrder.Quantity = orderln.Quantity;
                orders.TradeHoldOrder.ValidTime = orderln.ValidTime;

                orders.MoneyInventory.FdInfo.FrozenMoney += _frozenMoney;

                #endregion

            }
            catch (Exception ex)
            {
                ComFunction.WriteErr(ex);
                orders.Result = false;
                orders.Desc = "挂单下单失败";

            }
            return orders;
        }
        /// <summary>
        /// 商品配置数据
        /// </summary>
        /// <param name="loginID">用户登陆标识</param>
        /// <returns>商品列表</returns>
        public List<ProductConfig> GetWXProductConfig(string loginID)
        {
            var list = new List<ProductConfig>();

            DbDataReader dbreader = null;
            try
            {
                if (!ComFunction.ExistUserLoginID(loginID))
                {
                    return list;
                }

                Dictionary<string, ProPrice> prodic = ComFunction.GetProPrice();

                dbreader = DbHelper.ExecuteReader(ComFunction.GetProductSql);
                while (dbreader.Read())
                {
                    var ptc = new ProductConfig
                    {
                        ProductCode =
                            DBNull.Value != dbreader["productcode"] ? dbreader["productcode"].ToString() : string.Empty,
                        ProductName =
                            DBNull.Value != dbreader["ProductName"] ? dbreader["ProductName"].ToString() : string.Empty,
                        GoodsCode =
                            DBNull.Value != dbreader["goodscode"] ? dbreader["goodscode"].ToString() : string.Empty,
                        AdjustBase =
                            DBNull.Value != dbreader["adjustbase"] ? Convert.ToDouble(dbreader["adjustbase"]) : 0,
                        AdjustCount =
                            DBNull.Value != dbreader["adjustcount"] ? Convert.ToInt32(dbreader["adjustcount"]) : 0,
                        PriceDot = DBNull.Value != dbreader["pricedot"] ? Convert.ToInt32(dbreader["pricedot"]) : 0,
                        ValueDot = DBNull.Value != dbreader["valuedot"] ? Convert.ToDouble(dbreader["valuedot"]) : 0,
                        SetBase = DBNull.Value != dbreader["setBase"] ? Convert.ToInt32(dbreader["setBase"]) : 0,
                        HoldBase = DBNull.Value != dbreader["holdbase"] ? Convert.ToInt32(dbreader["holdbase"]) : 0,
                        OrderMoney = DBNull.Value != dbreader["Ordemoney"] ? Convert.ToDouble(dbreader["Ordemoney"]) : 1,
                        MaxPrice = DBNull.Value != dbreader["maxprice"] ? Convert.ToDouble(dbreader["maxprice"]) : 8000,
                        MinPrice = DBNull.Value != dbreader["minprice"] ? Convert.ToDouble(dbreader["minprice"]) : 1,
                        MaxTime = DBNull.Value != dbreader["maxtime"] ? Convert.ToDouble(dbreader["maxtime"]) : 60,
                        State = DBNull.Value != dbreader["state"] ? dbreader["state"].ToString() : string.Empty,
                        Unit = DBNull.Value != dbreader["unit"] ? Convert.ToDouble(dbreader["unit"]) : 1,
                        PriceCode =
                            DBNull.Value != dbreader["pricecode"] ? dbreader["pricecode"].ToString() : string.Empty
                    };

                    if (prodic != null && prodic.ContainsKey(ptc.PriceCode))
                    {
                        ptc.weektime = prodic[ptc.PriceCode].weektime;
                        ptc.realprice = prodic[ptc.PriceCode].realprice;
                    }
                    list.Add(ptc);
                }
            }
            catch (Exception ex)
            {
                ComFunction.WriteErr(ex);
                if (null != list && list.Count > 0)
                {
                    list.Clear();
                }
            }
            finally
            {
                if (null != dbreader)
                {
                    dbreader.Close();
                    dbreader.Dispose();
                }
            }
            return list;
        }
        /// <summary>
        /// 下单
        /// </summary>
        /// <param name="marorderln">下单信息</param>
        /// <returns>下单结果</returns>
        public Marketorders GetWXMarketorders(MarOrdersLn marorderln, List<UserExperience>  experiences)
        {
            ComFunction.WriteErr(new Exception("1"));
            Marketorders marketorders = new Marketorders();
            marketorders.MoneyInventory = new MoneyInventory();
            marketorders.TradeOrder = new TradeOrder();
            ProductConfig ptc = new ProductConfig();
            TradeUser TdUser = new TradeUser();
            DateTime dt = DateTime.Now;//服务器当前本地时间
            double _occMoney = 0;//占用资金
            double realprice = 0;//服务器端实时价格(即此时此刻的建仓价)
            double _tradefee = 0;//工费
            double fxrate = 0;//风险率
            string orderid = string.Empty;//市价单ID
            string userId = string.Empty;
            string allowStore = "1";//是否允许入库 "1"允许入库 "0" 不允许入库
            string operUser = marorderln.TradeAccount;
            string ipmac = string.Empty;
            int operUserType = 0;
            bool isExperience=false;//是否以体验券下单
            try
            {
                if (experiences != null && experiences.Count > 0)
                    isExperience = true;

                #region 判断用户登陆标识是否过期
                if (!ComFunction.ExistUserLoginID(marorderln.LoginID, ref TdUser))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL003;
                    marketorders.Desc = ResCode.UL003Desc;
                    return marketorders;
                }

                operUser = TdUser.Account;
                ipmac = ComFunction.GetIpMac(TdUser.Ip, TdUser.Mac);
                operUserType = (int)TdUser.UType;

                if (UserType.NormalType == TdUser.UType)
                {
                    userId = TdUser.UserID;
                }
                else
                {
                    userId = ComFunction.GetUserId(marorderln.TradeAccount, ref TdUser);
                }
                #endregion

                #region 交易手数验证
                if (!ComFunction.ValidateQuantity(marorderln.Quantity, TdUser.OrderUnit))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL044;
                    marketorders.Desc = string.Format("交易手数必须是{0}的倍数", TdUser.OrderUnit);
                    return marketorders;
                }
                #endregion

                #region 判断用户是否允许下买单或卖单
                if ("0" == marorderln.OrderType) //下买单
                {
                    if (!TdUser.PermitDhuo)
                    {
                        marketorders.Result = false;
                        marketorders.ReturnCode = ResCode.UL017;
                        marketorders.Desc = ResCode.UL017Desc;
                        return marketorders;
                    }
                }
                else if ("1" == marorderln.OrderType) //下卖单
                {
                    if (!TdUser.PermitHshou)
                    {
                        marketorders.Result = false;
                        marketorders.ReturnCode = ResCode.UL018;
                        marketorders.Desc = ResCode.UL018Desc;
                        return marketorders;
                    }
                    allowStore = ComFunction.AllowStore;//是否允许卖单入库,通过配置文件读取
                }
                else
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL021;
                    marketorders.Desc = ResCode.UL021Desc;
                    return marketorders;
                }
                #endregion

                #region 获取商品信息
                ptc = ComFunction.GetProductInfo(marorderln.ProductCode);
                //订单类型(0买、1卖)
                //未能获取商品状态
                if (string.IsNullOrEmpty(ptc.State))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL024;
                    marketorders.Desc = ResCode.UL024Desc;
                    return marketorders;
                }
                #endregion

                #region 判断当前时间是否允许交易
                if (!ComFunction.GetDateset(ptc.PriceCode, dt))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL022;
                    marketorders.Desc = ResCode.UL022Desc;
                    return marketorders;
                }
                #endregion

                #region 判断商品是否处于交易时段
                if (!ComFunction.ProductCanTrade(ptc.Starttime, ptc.Endtime))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL025;
                    marketorders.Desc = ResCode.UL025Desc;
                    return marketorders;
                }
                #endregion
                ComFunction.WriteErr(new Exception("2"));
                //获取服务器端实时价
                realprice = ComFunction.GetRealPrice(ptc.PriceCode);

                #region 判断商品是否允许交易 并确定下单价格
                //商品的状态(0 正常交易, 1 只报价, 2 只买, 3 只卖, 4 全部禁止)
                //商品允许正常交易
                if ("0" == ptc.State)
                {
                    //买
                    if ("0" == marorderln.OrderType)
                    {
                        marketorders.Desc = "买成功";
                        realprice += ptc.PriceDot * ptc.AdjustBase;
                    }
                    //卖
                    else if ("1" == marorderln.OrderType)
                    {
                        marketorders.Desc = "卖成功";
                    }
                    else
                    {
                        marketorders.Result = false;
                        marketorders.ReturnCode = ResCode.UL021;
                        marketorders.Desc = ResCode.UL021Desc;
                        return marketorders;
                    }
                }
                //商品只允许买
                else if ("2" == ptc.State)
                {
                    //买
                    if ("0" == marorderln.OrderType)
                    {
                        marketorders.Desc = "买成功";
                        realprice += ptc.PriceDot * ptc.AdjustBase;
                    }
                    else
                    {
                        marketorders.Result = false;
                        marketorders.ReturnCode = ResCode.UL026;
                        marketorders.Desc = ResCode.UL026Desc;
                        return marketorders;
                    }
                }
                //商品只允许卖
                else if ("3" == ptc.State)
                {
                    //卖
                    if ("1" == marorderln.OrderType)
                    {
                        marketorders.Desc = "卖成功";
                    }
                    else
                    {
                        marketorders.Result = false;
                        marketorders.ReturnCode = ResCode.UL027;
                        marketorders.Desc = ResCode.UL027Desc;
                        return marketorders;
                    }
                }
                else
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL025;
                    marketorders.Desc = ResCode.UL025Desc;
                    return marketorders;
                }
                #endregion

                #region 下单价判断
                //最小成交价格 < 建仓价 <最大成交价格
                if (!(realprice - ptc.MinPrice >= ComFunction.dzero && realprice - ptc.MaxPrice <= ComFunction.dzero))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL015;
                    marketorders.Desc = ResCode.UL015Desc;
                    return marketorders;
                }
                #endregion

                #region 最大交易时间差判断
                //当前客户端实时报价时间+允许最大交易时间差>=服务器时间
                if (!(marorderln.CurrentTime.AddSeconds(ptc.MaxTime) >= dt))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL014;
                    marketorders.Desc = ResCode.UL014Desc;
                    return marketorders;
                }
                #endregion

                #region 滑点判断
                //当前客户端实时价+滑点*点差基值>=实时价&&当前客户端实时价-滑点*点差基值<=实时价
                if (!(System.Math.Abs(marorderln.RtimePrices - realprice) - marorderln.MaxPrice * ptc.AdjustBase <= ComFunction.dzero))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL016;
                    marketorders.Desc = ResCode.UL016Desc;
                    return marketorders;
                }
                #endregion

                UserGroups ugs = ComFunction.GetUserGroups(userId);

                #region 对同一行情的商品,平仓后xxx秒不能下单的判断
                //实现方法:在历史订单表中查询此用户此行情在最近xxx秒内是否存在历史订单
                //如果存在,则不允许下单;否则允许下单
                if (ComFunction.JudgeExistLTradeOrder(userId, ptc.PriceCode, ugs.AfterSecond))
                {
                    marketorders.Result = false;
                    marketorders.Desc = string.Format("连接服务器失败,请重试!", ugs.AfterSecond);
                    return marketorders;
                }
                #endregion

                #region 根据下单滑点重新计算下单价

                if ("1" == marorderln.OrderType)//卖单 下单价向下滑
                {
                    realprice -= ugs.PlaceOrderSlipPoint * ptc.AdjustBase;
                }
                else//买单 下单价向上滑
                {
                    realprice += ugs.PlaceOrderSlipPoint * ptc.AdjustBase;
                }
                if (realprice <= ComFunction.dzero)
                {
                    marketorders.Result = false;
                    marketorders.Desc = "市价单下单异常";
                    return marketorders;
                }
                #endregion
                realprice = Math.Round(realprice, ptc.AdjustCount, MidpointRounding.AwayFromZero);//把多余的小数位去掉
                marketorders.MoneyInventory = ComFunction.GetMoneyInventoryByUserId(userId);

                #region 计算占用资金和工费
                if (!marketorders.MoneyInventory.Result)
                {
                    marketorders.Result = false;
                    marketorders.Desc = "未能获取资金库存";
                    return marketorders;
                }

                //计算本次下单的占用资金,根据产品表中[保证金计算公式]进行计算
                _occMoney = Math.Round(ComFunction.Getfee(ptc.OrderMoneyFee, marorderln.OrderMoney, realprice, marorderln.Quantity), 2, MidpointRounding.AwayFromZero);
                if (!isExperience)//体验券下单不扣工费
                {
                    //计算本次下单工费,根据产品表中[交易工费公式]进行计算
                    _tradefee = Math.Round(ComFunction.Getfee(ptc.ExpressionFee, marorderln.OrderMoney, realprice, marorderln.Quantity), 2, MidpointRounding.AwayFromZero);
                }
                #endregion

                #region 新的风险率判断代码
                if (!isExperience)//体验券下单不用计算风险率
                {
                    double DongJieMoney = 0;//冻结资金
                    if (marketorders.MoneyInventory.FdInfo.DongJieMoney > ComFunction.dzero)
                    {
                        DongJieMoney = marketorders.MoneyInventory.FdInfo.DongJieMoney;
                    }
                    //客户总共的使用金额=本次下单的保证金+客户账户中的保证金+客户账户中的预付款
                    double UseMoney = _occMoney + marketorders.MoneyInventory.FdInfo.OccMoney + marketorders.MoneyInventory.FdInfo.FrozenMoney;
                    if (UseMoney <= ComFunction.dzero)//如果使用金额为0 则说明有问题 使用金额不可能小于0
                    {
                        marketorders.Result = false;
                        marketorders.Desc = "市价单下单失败!";
                        return marketorders;
                    }

                    //如果客户账户余额-客户账户冻结资金-本次下单工费-本次下单的占用资金-客户账户的保证金-客户账户的预付款<=0
                    if (marketorders.MoneyInventory.FdInfo.Money - DongJieMoney - _tradefee - _occMoney - marketorders.MoneyInventory.FdInfo.OccMoney
                        - marketorders.MoneyInventory.FdInfo.FrozenMoney <= ComFunction.dzero)
                    {
                        marketorders.Result = false;
                        marketorders.ReturnCode = ResCode.UL012;
                        marketorders.Desc = ResCode.UL012Desc;
                        return marketorders;
                    }

                    //用户的盈亏
                    double yingkui = ComFunction.GetUserYingKui(userId);
                    //风险率=(当前帐户余额-冻结资金-本次的工费+盈亏)/客户账户总共的使用金额
                    fxrate = (marketorders.MoneyInventory.FdInfo.Money - DongJieMoney - _tradefee + yingkui) / UseMoney;//分子=当前帐户余额-冻结资金-本次的工费
                    if (fxrate - ComFunction.fenxian_rate <= ComFunction.dzero)
                    {
                        marketorders.Result = false;
                        marketorders.ReturnCode = ResCode.UL013;
                        marketorders.Desc = ResCode.UL013Desc;
                        return marketorders;
                    }
                }
                #endregion

                #region 计算止盈止损价
                double lose = marorderln.LossPrice;//微交易下单时传送的是一个百分比
                if (lose > 0)
                {
                    //止损价
                    //买涨:止损价=实时价-(保证金*止损百分比/点值)*点差基值
                    //买跌:止损价=实时价+(保证金*止损百分比/点值)*点差基值
                    var temp = (marorderln.OrderMoney * lose / ptc.ValueDot) * ptc.AdjustBase;//保证金没有乘数量所以公式也没有数量

                    if ("0" == marorderln.OrderType)//买涨
                    {
                        marorderln.LossPrice = realprice - temp;
                    }
                    else//买跌
                    {
                        marorderln.LossPrice = realprice + temp;
                    }
                    marorderln.LossPrice = Math.Round(marorderln.LossPrice, ptc.AdjustCount);
                }
                double gain = marorderln.ProfitPrice;

                if (gain > 0)//微交易下单时传送的是一个百分比
                {
                    //止盈价
                    //买涨:止盈价=实时价+((保证金*止盈百分比+手续费)/点值)*点差基值
                    //买跌:止盈价=实时价-((保证金*止盈百分比+手续费)/点值)*点差基值
                    var temp = (marorderln.OrderMoney * gain / ptc.ValueDot) * ptc.AdjustBase;//保证金没有乘数量所以公式也没有数量

                    if ("0" == marorderln.OrderType)//买涨
                    {
                        marorderln.ProfitPrice = realprice + temp;
                    }
                    else//买跌
                    {
                        marorderln.ProfitPrice = realprice - temp;
                    }
                    marorderln.ProfitPrice = Math.Round(marorderln.ProfitPrice, ptc.AdjustCount);
                }
                #endregion
                ComFunction.WriteErr(new Exception("3"));
                //下单的时候不计算仓储费,直接赋值为0
                marketorders.TradeOrder.StorageFee = 0;

                orderid = ComFunction.GetOrderId(ComFunction.Order);
                #region 数据库事务处理
                List<string> sqlList = new List<string>();
                sqlList.Add(string.Format("INSERT INTO Trade_Order([userId],[Orderid],[productCode],[Ordertype],[Orderprice],[usequantity],[quantity],[lossprice],[profitPrice]," +
                                          "[OccMoney],[tradefee],[storagefee],[Ordertime],[OperType],[ip],[mac],[AllowStore],[IsExperience]) " +
                                          "VALUES('{0}','{1}','{2}',{3},{4},{5},{6},{7},{8},{9},{10},{11},'{12}',{13},'{14}','{15}',{16},'{17}')",
                    userId, orderid, marorderln.ProductCode, marorderln.OrderType, realprice, marorderln.Quantity,
                    marorderln.Quantity, marorderln.LossPrice, marorderln.ProfitPrice, _occMoney, _tradefee, marketorders.TradeOrder.StorageFee,
                    dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), 1, ComFunction.GetClientIp(), marorderln.Mac, allowStore,isExperience));
                if (!isExperience)//体验券下单不扣用户资金
                {
                    sqlList.Add(string.Format("update Trade_FundInfo set occMoney = occMoney+{0},[money]=[money]-{1} where userid='{2}' and [state]<>'4'", _occMoney, _tradefee, userId));

                    sqlList.Add(string.Format("INSERT INTO Fund_Change([userId],[reason],[Oldvalue],[NewValue],[OperUser],[OperTime],[RelaOrder],[ChangeValue],[CashUser]) VALUES('{0}','{1}',{2},{3},'{4}','{5}','{6}',{7},'{8}')",
                       userId, 10, marketorders.MoneyInventory.FdInfo.Money, marketorders.MoneyInventory.FdInfo.Money - _tradefee, operUser, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), orderid, -_tradefee, marketorders.MoneyInventory.FdInfo.CashUser));
                }

                //添加操作记录
                sqlList.Add(string.Format("insert into Base_OperrationLog([OperTime],[Account],[UserType],[Remark]) values('{0}','{1}',{2},'{3}')", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), operUser, operUserType, string.Format("{1}在手订单{0}", orderid, ipmac)));

                if (isExperience)
                {
                    foreach (UserExperience item in experiences)
                    {
                        sqlList.Add(string.Format("UPDATE [USER_EXPERIENCE] SET [USE_NUM] ={0}  WHERE ID={1}", item.Num, item.ID));
                    }
                }

                if (!ComFunction.SqlTransaction(sqlList))
                {
                    marketorders.Result = false;
                    marketorders.Desc = "市价单下单时出错";
                    return marketorders;
                }
                #endregion

                #region 给返回对象赋值
                marketorders.Result = true;
                marketorders.ChengjiaoPrice = realprice;
                marketorders.MoneyInventory.FdInfo.OccMoney += _occMoney;
                marketorders.MoneyInventory.FdInfo.Money -= _tradefee;
                marketorders.TradeOrder.LossPrice = marorderln.LossPrice;
                marketorders.TradeOrder.OccMoney = _occMoney;
                marketorders.TradeOrder.OrderId = orderid;
                marketorders.TradeOrder.OrderPrice = realprice;
                marketorders.TradeOrder.OrderTime = dt;
                marketorders.TradeOrder.OrderType = marorderln.OrderType;
                marketorders.TradeOrder.ProductCode = marorderln.ProductCode;
                marketorders.TradeOrder.ProductName = ptc.ProductName;
                marketorders.TradeOrder.ProfitPrice = marorderln.ProfitPrice;
                marketorders.TradeOrder.Quantity = marorderln.Quantity;
                marketorders.TradeOrder.TradeFee = _tradefee;
                marketorders.TradeOrder.UseQuantity = marorderln.Quantity;
                marketorders.TradeOrder.PriceCode = ptc.PriceCode;
                #endregion

            }
            catch (Exception ex)
            {
                ComFunction.WriteErr(ex);
                marketorders.Result = false;
                marketorders.Desc = "市价单下单失败" + ex.Message + ex.InnerException;

            }
            return marketorders;
        }
        /// <summary>
        /// 商品配置
        /// </summary>
        /// <param name="LoginID">用户登陆标识</param>
        /// <returns>商品列表</returns>
        public List<ProductConfig> GetProductConfigEx(string LoginID)
        {
            List<ProductConfig> list = new List<ProductConfig>();

            System.Data.Common.DbDataReader dbreader = null;
            try
            {

                //Dictionary<string, ProPrice> prodic = ComFunction.GetProPrice();
                Dictionary<string, string> dic = ComFunction.GetAllLastPillar("D1");
                Dictionary<string, double> opendic = ComFunction.GetYesterdayPrice();

                dbreader = DbHelper.ExecuteReader(ComFunction.GetProductSqlEx);
                string[] splits = null;
                while (dbreader.Read())
                {
                    ProductConfig ptc = new ProductConfig();
                    ptc.ProductCode = System.DBNull.Value != dbreader["productcode"] ? dbreader["productcode"].ToString() : string.Empty;
                    ptc.ProductName = System.DBNull.Value != dbreader["ProductName"] ? dbreader["ProductName"].ToString() : string.Empty;
                    ptc.GoodsCode = System.DBNull.Value != dbreader["goodscode"] ? dbreader["goodscode"].ToString() : string.Empty;
                    ptc.AdjustBase = System.DBNull.Value != dbreader["adjustbase"] ? Convert.ToDouble(dbreader["adjustbase"]) : 0;
                    ptc.AdjustCount = System.DBNull.Value != dbreader["adjustcount"] ? Convert.ToInt32(dbreader["adjustcount"]) : 0;
                    ptc.PriceDot = System.DBNull.Value != dbreader["pricedot"] ? Convert.ToInt32(dbreader["pricedot"]) : 0;
                    ptc.ValueDot = System.DBNull.Value != dbreader["valuedot"] ? Convert.ToDouble(dbreader["valuedot"]) : 0;
                    ptc.SetBase = System.DBNull.Value != dbreader["setBase"] ? Convert.ToInt32(dbreader["setBase"]) : 0;
                    ptc.HoldBase = System.DBNull.Value != dbreader["holdbase"] ? Convert.ToInt32(dbreader["holdbase"]) : 0;
                    ptc.OrderMoney = System.DBNull.Value != dbreader["Ordemoney"] ? Convert.ToDouble(dbreader["Ordemoney"]) : 1;
                    ptc.MaxPrice = System.DBNull.Value != dbreader["maxprice"] ? Convert.ToDouble(dbreader["maxprice"]) : 8000;
                    ptc.MinPrice = System.DBNull.Value != dbreader["minprice"] ? Convert.ToDouble(dbreader["minprice"]) : 1;
                    ptc.MaxTime = System.DBNull.Value != dbreader["maxtime"] ? Convert.ToDouble(dbreader["maxtime"]) : 60;
                    ptc.State = System.DBNull.Value != dbreader["state"] ? dbreader["state"].ToString() : string.Empty;
                    ptc.Unit = System.DBNull.Value != dbreader["unit"] ? Convert.ToDouble(dbreader["unit"]) : 1;
                    ptc.PriceCode = System.DBNull.Value != dbreader["pricecode"] ? dbreader["pricecode"].ToString() : string.Empty;

                    //if (prodic != null && prodic.ContainsKey(ptc.PriceCode))
                    //{
                    //    ptc.weektime = prodic[ptc.PriceCode].weektime;
                    //    ptc.realprice = prodic[ptc.PriceCode].realprice;
                    //}
                    if (dic != null && dic.ContainsKey(ptc.PriceCode))
                    {
                        splits = dic[ptc.PriceCode].Split('\t');//周期 编码 时间 开盘 最高 最低 收盘 成交量
                        ptc.weektime = splits[2] + "00";
                        ptc.OpenPrice = Convert.ToDouble(splits[3]);
                        ptc.HighPrice = Convert.ToDouble(splits[4]);
                        ptc.LowPrice = Convert.ToDouble(splits[5]);
                        ptc.realprice = Convert.ToDouble(splits[6]);
                    }
                    if (opendic != null && opendic.ContainsKey(ptc.PriceCode))
                    {
                        ptc.YesterdayPrice = opendic[ptc.PriceCode];
                    }
                    list.Add(ptc);
                }
            }
            catch (Exception ex)
            {
                ComFunction.WriteErr(ex);
                if (null != list && list.Count > 0)
                {
                    list.Clear();
                }
            }
            finally
            {
                if (null != dbreader)
                {
                    dbreader.Close();
                    dbreader.Dispose();
                }
            }
            return list;
        }