示例#1
0
        public ActionResult EditPositionPassword()
        {
            var password = Request["password"];

            var orderid = Converter.ToInt(Request["orderid"], 0);
            var types = Request["types"];//0保存 1删除 2平仓
            var OrderNum = Request["OrderNum"];
            var ProductID = Request["ProductID"];
            var TRealAccountsID = Converter.ToInt(Request["TRealAccountID"], 0);
            var Deal = Request["Deal"];
            var OrderCount = Request["OrderCount"];
            var OpenPrice = Request["OpenPrice"];
            var Loss = Request["Loss"];
            var OpenTime = Request["OpenTime"];
            var Profit = Request["Profit"];
            var NowPrice = Request["NowPrice"];
            var Deposit = Request["Deposit"];
            var UpdateTime = Request["UpdateTime"];

            APIDataDataContext db = APIDataContextProxy.APIDB;
            TOperationLog OLog = new TOperationLog();

            var user2 = (from c in db.TRealAccounts where c.ID == TRealAccountsID && c.PassWord == password select c).FirstOrDefault();

            if (user2 == null)
            {
                return Json(new { code = "0", msg = "客户密码不正确!" });
            }
            try
            {
                if (orderid > 0)
                {
                    var order = (from c in db.TOrders where c.ID == orderid select c).FirstOrDefault();
                    OLog.BeforeValue = ObjectToJson.ModelToJson(order);

                    OLog.UserID = Convert.ToInt32(Session["ID"]);
                    OLog.UserType = 2;//0 用户 1 会员 2 员工
                    OLog.CreateDate = DateTime.Now;

                    if (order != null)
                    {
                        if (types == "1")
                        {
                            #region 删除订单
                            db.TOrders.DeleteOnSubmit(order);
                            db.SubmitChanges();
                            OLog.UpdateValue = "";
                            //写入操作日志
                            OLog.PageName = "删除在手订单";
                            Converter.InsertOperationLog(OLog);
                            return Json(new { code = "1", msg = "提交成功!" });
                            #endregion

                        }
                        else if (types == "0")
                        {
                            #region 修改保存
                            order.ProductID = Converter.ToInt(ProductID, 0);
                            order.TRealAccountsID = Converter.ToInt(TRealAccountsID, 0);
                            order.Deal = Deal == "1" ? true : false;
                            order.OrderCount = Converter.ToInt(OrderCount, 0);
                            order.OpenPrice = Converter.ToFloat(OpenPrice, 0);
                            order.Loss = Converter.ToFloat(Loss, 0);
                            order.OpenTime = Convert.ToDateTime(OpenTime);
                            order.Profit = Converter.ToFloat(Profit, 0);
                            order.NowPrice = Converter.ToFloat(NowPrice, 0);
                            order.Deposit = Converter.ToFloat(Deposit, 0);
                            order.UpdateTime = Convert.ToDateTime(UpdateTime);
                            db.SubmitChanges();
                            OLog.UpdateValue = ObjectToJson.ModelToJson(order);
                            //写入操作日志
                            OLog.PageName = "修改在手订单";
                            Converter.InsertOperationLog(OLog);
                            return Json(new { code = "1", msg = "提交成功!" });
                            #endregion
                        }
                        else
                        {
                            #region 平仓
                            int OrderId = orderid;
                            var item = (from c in db.TOrders
                                        where c.ID == OrderId
                                        && c.OrderState == 1
                                        && c.OrderType == 1
                                        select c).FirstOrDefault();
                            if (item != null)
                            {
                                var product = (from c in db.TProducts
                                               join b in db.TLastClose on c.ProductCode equals b.ProductCode
                                               where c.ID == item.ProductID
                                               orderby c.ID
                                               select new
                                               {
                                                   c,
                                                   b.LastClose,
                                                   b.LastUpdateTime
                                               }).FirstOrDefault();
                                if (product != null)
                                {
                                    var Scale = Converter.ToFloat(product.c.Scale, 2);
                                    var nowdate = product.LastUpdateTime;
                                    #region 加工费
                                    double closedeposit = 0.0;   //加工费
                                    if (product.c.DepositType == 1)//固定
                                    {
                                        closedeposit = Converter.ToFloat(product.c.CloseDeposit, 0) * Converter.ToFloat(product.c.ContractNumber, 0) * Converter.ToFloat(item.OrderCount, 0);
                                    }
                                    else if (product.c.DepositType == 2)//浮动
                                    {
                                        if (item.Deal == false)
                                            closedeposit = 0.01 * Converter.ToFloat(product.c.CloseDeposit, 0) * (Converter.ToFloat(product.LastClose, 0)) * Converter.ToFloat(product.c.ContractNumber, 0) * Converter.ToFloat(item.OrderCount, 0);
                                        else
                                            closedeposit = 0.01 * Converter.ToFloat(product.c.CloseDeposit, 0) * (Converter.ToFloat(product.LastClose, 0) + Converter.ToFloat(product.c.Fixedly, 0)) * Converter.ToFloat(product.c.ContractNumber, 0) * Converter.ToFloat(item.OrderCount, 0);
                                    }
                                    closedeposit = Converter.ToFloat(closedeposit.ToString("F" + Scale), 0);
                                    #endregion
                                    item.Deposit = Converter.ToFloat((Converter.ToFloat(item.Deposit, 0) + closedeposit).ToString("F" + Scale), 0);
                                    double LastClose = 0.0;       //市场价 收盘价
                                    double Spread = 0.0;        //点数
                                    //计算盈亏
                                    if (item.Deal == false)//卖出
                                    {
                                        LastClose = Converter.ToFloat(product.LastClose, 0) + Converter.ToFloat(product.c.Fixedly, 0);
                                        Spread = Converter.ToFloat(item.OpenPrice, 0) - LastClose;
                                    }
                                    else if (item.Deal == true)//买入
                                    {
                                        LastClose = Converter.ToFloat(product.LastClose, 0);
                                        Spread = LastClose - Converter.ToFloat(item.OpenPrice, 0);
                                    }
                                    item.Spread = Converter.ToFloat(Spread.ToString("F" + Scale), 0);
                                    item.ProfitAndLoss = Converter.ToFloat((Converter.ToFloat(Spread * item.OrderCount, 0) * Converter.ToFloat(product.c.ContractNumber, 0)).ToString("F" + Scale), 0);
                                    item.NowPrice = Converter.ToFloat(LastClose.ToString("F" + Scale), 0);
                                    item.NetProfit = Converter.ToFloat((Converter.ToFloat(item.ProfitAndLoss, 0) - Converter.ToFloat(item.Deposit, 0) - Converter.ToFloat(item.Interest, 0)).ToString("F" + Scale), 0);
                                    item.UpdateTime = nowdate;
                                    item.OrderState = 2;
                                    item.OrderType = 2;
                                    item.CloseType = "授权平仓";
                                    var user = (from c in db.TRealAccounts where c.ID == item.TRealAccountsID select c).FirstOrDefault();

                                    try
                                    {
                                        var agent = (from c in db.TAgents
                                                     join b in db.TAgentGroup on c.GroupID equals b.ID
                                                     where c.ID == user.AgendID
                                                     select new
                                                     {
                                                         c.ID,
                                                         c.DirectAgentID,
                                                         c.Depth,
                                                         c.HeadRate1,
                                                         c.ProcessRate1,
                                                         c.InterestRate1,
                                                         c.HeadRate2,
                                                         c.ProcessRate2,
                                                         c.InterestRate2,
                                                         c.HeadRate3,
                                                         c.ProcessRate3,
                                                         c.InterestRate3,
                                                         c.HeadRate4,
                                                         c.ProcessRate4,
                                                         c.InterestRate4,
                                                         c.HeadRate5,
                                                         c.ProcessRate5,
                                                         c.InterestRate5,
                                                         GID = b.ID,
                                                         b.GroupName,
                                                         b.ProcessRate,
                                                         b.HeadRate,
                                                         b.InterestRate,
                                                         b.NextRate
                                                     }).FirstOrDefault();

                                        #region 分成
                                        if (agent != null)
                                        {
                                            //查询客户所属会员的分成
                                            double HeadRate = Convert.ToDouble(Converter.ToFloat(agent.HeadRate, 0).ToString("F4")) * 0.01;
                                            double ProcessRate = Convert.ToDouble(Converter.ToFloat(agent.ProcessRate, 0).ToString("F4")) * 0.01;
                                            double InterestRate = Convert.ToDouble(Converter.ToFloat(agent.InterestRate, 0).ToString("F4")) * 0.01;
                                            //计算分成
                                            double HeadInfo = Convert.ToDouble((Converter.ToFloat(item.ProfitAndLoss, 0) * HeadRate * -1).ToString("F4"));
                                            double ProcessInfo = Convert.ToDouble((Converter.ToFloat(item.Deposit, 0) * ProcessRate).ToString("F4"));
                                            double InterestInfo = Convert.ToDouble((Converter.ToFloat(item.Interest, 0) * InterestRate).ToString("F4"));

                                            TAgentRate tar = new TAgentRate();
                                            tar.HeadRate = HeadInfo.ToString();
                                            tar.ProcessRate = ProcessInfo.ToString();
                                            tar.InterestRate = InterestInfo.ToString();
                                            tar.AllRate = (HeadInfo + ProcessInfo + InterestInfo).ToString();
                                            tar.NextHeadRate = "0";
                                            tar.NextProcessRate = "0";
                                            tar.NextInterestRate = "0";
                                            tar.NextAllRate = "0";
                                            tar.Date = DateTime.Now;
                                            tar.TAgentId = agent.ID;
                                            tar.TOrderId = item.ID;

                                            IList<TAgentRate> tarlist = new List<TAgentRate>();
                                            int depth = agent.Depth;//深度
                                            if (depth > 6)
                                                depth = 6;
                                            var ag = (from c in db.TAgents where c.ID == agent.ID select c).FirstOrDefault();
                                            var age = ag;
                                            for (int i = 1; i < depth; i++)
                                            {
                                                if (i == 1)
                                                {
                                                    //会员的分成
                                                    HeadRate = Convert.ToDouble(Converter.ToFloat(age.HeadRate1, 0).ToString("F4")) * 0.01;
                                                    ProcessRate = Convert.ToDouble(Converter.ToFloat(age.ProcessRate1, 0).ToString("F4")) * 0.01;
                                                    InterestRate = Convert.ToDouble(Converter.ToFloat(age.ProcessRate1, 0).ToString("F4")) * 0.01;
                                                    //计算分成
                                                    var HeadInfo_ = Convert.ToDouble((Converter.ToFloat(item.ProfitAndLoss, 0) * HeadRate * -1).ToString("F4"));
                                                    var ProcessInfo_ = Convert.ToDouble((Converter.ToFloat(item.Deposit, 0) * ProcessRate).ToString("F4"));
                                                    var InterestInfo_ = Convert.ToDouble((Converter.ToFloat(item.Interest, 0) * InterestRate).ToString("F4"));

                                                    TAgentRate tars = new TAgentRate();
                                                    tars.HeadRate = "0";
                                                    tars.ProcessRate = "0";
                                                    tars.InterestRate = "0";
                                                    tars.AllRate = "0";
                                                    tars.NextHeadRate = HeadInfo_.ToString();
                                                    tars.NextProcessRate = ProcessInfo_.ToString();
                                                    tars.NextInterestRate = InterestInfo_.ToString();
                                                    tars.NextAllRate = (HeadInfo_ + ProcessInfo_ + InterestInfo_).ToString();
                                                    tars.Date = DateTime.Now;
                                                    tars.TAgentId = ag.DirectAgentID;
                                                    tars.TOrderId = item.ID;
                                                    tarlist.Add(tars);
                                                    tar.HeadRate = (Convert.ToDouble(tar.HeadRate) - HeadInfo_).ToString("F4");
                                                    tar.ProcessRate = (Convert.ToDouble(tar.ProcessRate) - ProcessInfo_).ToString("F4");
                                                    tar.InterestRate = (Convert.ToDouble(tar.InterestRate) - InterestInfo_).ToString("F4");
                                                    tar.AllRate = (Convert.ToDouble(tar.AllRate) - (HeadInfo_ + ProcessInfo_ + InterestInfo_)).ToString("F4");
                                                }
                                                else if (i == 2)
                                                {
                                                    //会员的分成
                                                    HeadRate = Convert.ToDouble(Converter.ToFloat(age.HeadRate2, 0).ToString("F4")) * 0.01;
                                                    ProcessRate = Convert.ToDouble(Converter.ToFloat(age.ProcessRate2, 0).ToString("F4")) * 0.01;
                                                    InterestRate = Convert.ToDouble(Converter.ToFloat(age.ProcessRate2, 0).ToString("F4")) * 0.01;
                                                    //计算分成
                                                    var HeadInfo_ = Convert.ToDouble((Converter.ToFloat(item.ProfitAndLoss, 0) * HeadRate * -1).ToString("F4"));
                                                    var ProcessInfo_ = Convert.ToDouble((Converter.ToFloat(item.Deposit, 0) * ProcessRate).ToString("F4"));
                                                    var InterestInfo_ = Convert.ToDouble((Converter.ToFloat(item.Interest, 0) * InterestRate).ToString("F4"));

                                                    TAgentRate tars = new TAgentRate();
                                                    tars.HeadRate = "0";
                                                    tars.ProcessRate = "0";
                                                    tars.InterestRate = "0";
                                                    tars.AllRate = "0";
                                                    tars.NextHeadRate = HeadInfo_.ToString();
                                                    tars.NextProcessRate = ProcessInfo_.ToString();
                                                    tars.NextInterestRate = InterestInfo_.ToString();
                                                    tars.NextAllRate = (HeadInfo_ + ProcessInfo_ + InterestInfo_).ToString();
                                                    tars.Date = DateTime.Now;
                                                    tars.TAgentId = ag.DirectAgentID;
                                                    tars.TOrderId = item.ID;
                                                    tarlist.Add(tars);
                                                    tar.HeadRate = (Convert.ToDouble(tar.HeadRate) - HeadInfo_).ToString("F4");
                                                    tar.ProcessRate = (Convert.ToDouble(tar.ProcessRate) - ProcessInfo_).ToString("F4");
                                                    tar.InterestRate = (Convert.ToDouble(tar.InterestRate) - InterestInfo_).ToString("F4");
                                                    tar.AllRate = (Convert.ToDouble(tar.AllRate) - (HeadInfo_ + ProcessInfo_ + InterestInfo_)).ToString("F4");
                                                }
                                                else if (i == 3)
                                                {
                                                    //会员的分成
                                                    HeadRate = Convert.ToDouble(Converter.ToFloat(age.HeadRate3, 0).ToString("F4")) * 0.01;
                                                    ProcessRate = Convert.ToDouble(Converter.ToFloat(age.ProcessRate3, 0).ToString("F4")) * 0.01;
                                                    InterestRate = Convert.ToDouble(Converter.ToFloat(age.ProcessRate3, 0).ToString("F4")) * 0.01;
                                                    //计算分成
                                                    var HeadInfo_ = Convert.ToDouble((Converter.ToFloat(item.ProfitAndLoss, 0) * HeadRate * -1).ToString("F4"));
                                                    var ProcessInfo_ = Convert.ToDouble((Converter.ToFloat(item.Deposit, 0) * ProcessRate).ToString("F4"));
                                                    var InterestInfo_ = Convert.ToDouble((Converter.ToFloat(item.Interest, 0) * InterestRate).ToString("F4"));

                                                    TAgentRate tars = new TAgentRate();
                                                    tars.HeadRate = "0";
                                                    tars.ProcessRate = "0";
                                                    tars.InterestRate = "0";
                                                    tars.AllRate = "0";
                                                    tars.NextHeadRate = HeadInfo_.ToString();
                                                    tars.NextProcessRate = ProcessInfo_.ToString();
                                                    tars.NextInterestRate = InterestInfo_.ToString();
                                                    tars.NextAllRate = (HeadInfo_ + ProcessInfo_ + InterestInfo_).ToString();
                                                    tars.Date = DateTime.Now;
                                                    tars.TAgentId = ag.DirectAgentID;
                                                    tars.TOrderId = item.ID;
                                                    tarlist.Add(tars);
                                                    tar.HeadRate = (Convert.ToDouble(tar.HeadRate) - HeadInfo_).ToString("F4");
                                                    tar.ProcessRate = (Convert.ToDouble(tar.ProcessRate) - ProcessInfo_).ToString("F4");
                                                    tar.InterestRate = (Convert.ToDouble(tar.InterestRate) - InterestInfo_).ToString("F4");
                                                    tar.AllRate = (Convert.ToDouble(tar.AllRate) - (HeadInfo_ + ProcessInfo_ + InterestInfo_)).ToString("F4");
                                                }
                                                else if (i == 4)
                                                {
                                                    //会员的分成
                                                    HeadRate = Convert.ToDouble(Converter.ToFloat(age.HeadRate4, 0).ToString("F4")) * 0.01;
                                                    ProcessRate = Convert.ToDouble(Converter.ToFloat(age.ProcessRate4, 0).ToString("F4")) * 0.01;
                                                    InterestRate = Convert.ToDouble(Converter.ToFloat(age.ProcessRate4, 0).ToString("F4")) * 0.01;
                                                    //计算分成
                                                    var HeadInfo_ = Convert.ToDouble((Converter.ToFloat(item.ProfitAndLoss, 0) * HeadRate * -1).ToString("F4"));
                                                    var ProcessInfo_ = Convert.ToDouble((Converter.ToFloat(item.Deposit, 0) * ProcessRate).ToString("F4"));
                                                    var InterestInfo_ = Convert.ToDouble((Converter.ToFloat(item.Interest, 0) * InterestRate).ToString("F4"));

                                                    TAgentRate tars = new TAgentRate();
                                                    tars.HeadRate = "0";
                                                    tars.ProcessRate = "0";
                                                    tars.InterestRate = "0";
                                                    tars.AllRate = "0";
                                                    tars.NextHeadRate = HeadInfo_.ToString();
                                                    tars.NextProcessRate = ProcessInfo_.ToString();
                                                    tars.NextInterestRate = InterestInfo_.ToString();
                                                    tars.NextAllRate = (HeadInfo_ + ProcessInfo_ + InterestInfo_).ToString();
                                                    tars.Date = DateTime.Now;
                                                    tars.TAgentId = ag.DirectAgentID;
                                                    tars.TOrderId = item.ID;
                                                    tarlist.Add(tars);
                                                    tar.HeadRate = (Convert.ToDouble(tar.HeadRate) - HeadInfo_).ToString("F4");
                                                    tar.ProcessRate = (Convert.ToDouble(tar.ProcessRate) - ProcessInfo_).ToString("F4");
                                                    tar.InterestRate = (Convert.ToDouble(tar.InterestRate) - InterestInfo_).ToString("F4");
                                                    tar.AllRate = (Convert.ToDouble(tar.AllRate) - (HeadInfo_ + ProcessInfo_ + InterestInfo_)).ToString("F4");
                                                }
                                                else if (i == 5)
                                                {
                                                    //会员的分成
                                                    HeadRate = Convert.ToDouble(Converter.ToFloat(age.HeadRate5, 0).ToString("F4")) * 0.01;
                                                    ProcessRate = Convert.ToDouble(Converter.ToFloat(age.ProcessRate5, 0).ToString("F4")) * 0.01;
                                                    InterestRate = Convert.ToDouble(Converter.ToFloat(age.ProcessRate5, 0).ToString("F4")) * 0.01;
                                                    //计算分成
                                                    var HeadInfo_ = Convert.ToDouble((Converter.ToFloat(item.ProfitAndLoss, 0) * HeadRate * -1).ToString("F4"));
                                                    var ProcessInfo_ = Convert.ToDouble((Converter.ToFloat(item.Deposit, 0) * ProcessRate).ToString("F4"));
                                                    var InterestInfo_ = Convert.ToDouble((Converter.ToFloat(item.Interest, 0) * InterestRate).ToString("F4"));

                                                    TAgentRate tars = new TAgentRate();
                                                    tars.HeadRate = "0";
                                                    tars.ProcessRate = "0";
                                                    tars.InterestRate = "0";
                                                    tars.AllRate = "0";
                                                    tars.NextHeadRate = HeadInfo_.ToString();
                                                    tars.NextProcessRate = ProcessInfo_.ToString();
                                                    tars.NextInterestRate = InterestInfo_.ToString();
                                                    tars.NextAllRate = (HeadInfo_ + ProcessInfo_ + InterestInfo_).ToString();
                                                    tars.Date = DateTime.Now;
                                                    tars.TAgentId = ag.DirectAgentID;
                                                    tars.TOrderId = item.ID;
                                                    tarlist.Add(tars);
                                                    tar.HeadRate = (Convert.ToDouble(tar.HeadRate) - HeadInfo_).ToString("F4");
                                                    tar.ProcessRate = (Convert.ToDouble(tar.ProcessRate) - ProcessInfo_).ToString("F4");
                                                    tar.InterestRate = (Convert.ToDouble(tar.InterestRate) - InterestInfo_).ToString("F4");
                                                    tar.AllRate = (Convert.ToDouble(tar.AllRate) - (HeadInfo_ + ProcessInfo_ + InterestInfo_)).ToString("F4");
                                                }
                                                ag = (from c in db.TAgents where c.ID == ag.DirectAgentID select c).FirstOrDefault();
                                            }
                                            tarlist.Add(tar);
                                            db.TAgentRate.InsertAllOnSubmit(tarlist);
                                            db.SubmitChanges();

                                            //AgentRate ar = new AgentRate();
                                            //ar.Id = agent.ID;
                                            //ar.DirectAgentID = Converter.ToInt(agent.DirectAgentID, 0);
                                            //ar.Groupid = agent.GID;
                                            //ar.Num = 0;
                                            //ar.HeadInfo = HeadInfo;
                                            //ar.ProcessInfo = ProcessInfo;
                                            //ar.InterestInfo = InterestInfo;
                                            //ar.NextheadInfo = HeadInfo;
                                            //ar.NextprocessInfo = ProcessInfo;
                                            //ar.NextinterestInfo = InterestInfo;
                                            //if (ar.DirectAgentID > 0)
                                            //{
                                            //    Converter.GetAgent(ar, ar, item.ID);
                                            //}
                                            //ar.HeadInfo = ar.NextheadInfo;
                                            //ar.ProcessInfo = ar.NextprocessInfo;
                                            //ar.InterestInfo = ar.NextinterestInfo;
                                            //ar.NextheadInfo = 0;
                                            //ar.NextprocessInfo = 0;
                                            //ar.NextinterestInfo = 0;

                                            //APIDataDataContext db2 = ConnectionHelper.Db;
                                            //{
                                            //    TAgentRate tar = new TAgentRate();
                                            //    tar.HeadRate = ar.HeadInfo.ToString();
                                            //    tar.ProcessRate = ar.ProcessInfo.ToString();
                                            //    tar.InterestRate = ar.InterestInfo.ToString();
                                            //    tar.AllRate = (ar.HeadInfo + ar.ProcessInfo + ar.InterestInfo).ToString();
                                            //    tar.NextHeadRate = ar.NextheadInfo.ToString();
                                            //    tar.NextProcessRate = ar.NextprocessInfo.ToString();
                                            //    tar.NextInterestRate = ar.NextinterestInfo.ToString();
                                            //    tar.NextAllRate = (ar.NextheadInfo + ar.NextprocessInfo + ar.NextinterestInfo).ToString();
                                            //    tar.Date = DateTime.Now;
                                            //    tar.TAgentId = agent.ID;
                                            //    tar.TOrderId = item.ID;
                                            //    db2.TAgentRate.InsertOnSubmit(tar);
                                            //    db2.SubmitChanges();
                                            //}
                                        }
                                        #endregion
                                    }
                                    catch
                                    {
                                    }

                                    #region 资金记录
                                    TAccountMoney tam = new TAccountMoney();
                                    tam.MoneyOrderNum = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                                    tam.RealAccountId = Converter.ToInt(item.TRealAccountsID, 0);
                                    tam.TypeId = 4;
                                    tam.OriginalMoney = user.AccountMoney;
                                    tam.CreateMoney = item.ProfitAndLoss;
                                    tam.NowMoney = user.AccountMoney + item.ProfitAndLoss;
                                    if (tam.NowMoney < 0)
                                    {
                                        tam.NowMoney = 0;
                                    }
                                    tam.CreateTime = nowdate;
                                    tam.UpDateTime = nowdate;
                                    tam.Status = 2;
                                    tam.TOrderId = item.ID;
                                    db.TAccountMoney.InsertOnSubmit(tam);

                                    TAccountMoney tams = new TAccountMoney();
                                    tams.MoneyOrderNum = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                                    tams.RealAccountId = Converter.ToInt(item.TRealAccountsID, 0);
                                    tams.TypeId = 5;
                                    tams.OriginalMoney = tam.NowMoney;
                                    tams.CreateMoney = item.Deposit;
                                    tams.NowMoney = tam.NowMoney - item.Deposit;
                                    if (tams.NowMoney < 0)
                                    {
                                        tams.NowMoney = 0;
                                    }
                                    tams.CreateTime = nowdate;
                                    tams.UpDateTime = nowdate;
                                    tams.Status = 2;
                                    tams.TOrderId = item.ID;
                                    db.TAccountMoney.InsertOnSubmit(tams);

                                    #endregion
                                    user.AccountMoney = user.AccountMoney + item.ProfitAndLoss - item.Deposit;
                                    if (user.AccountMoney < 0)
                                    {
                                        user.AccountMoney = 0;
                                    }
                                    db.SubmitChanges();
                                    OLog.UpdateValue = ObjectToJson.ModelToJson(order);
                                    //写入操作日志
                                    OLog.PageName = "平仓";
                                    Converter.InsertOperationLog(OLog);
                                    return Json(new { code = "1", msg = "提交成功!" });
                                }
                                else
                                {
                                    return Json(new { code = "0", msg = "订单id不正确!" });
                                }
                            }
                            else
                            {
                                return Json(new { code = "0", msg = "订单id不正确!" });
                            }
                            #endregion
                        }
                    }
                    else
                    {
                        return Json(new { code = "0", msg = "提交失败请稍后在试!" });
                    }
                }
                else
                {
                    //添加
                    #region 添加
                    TOrders to = new TOrders();
                    to.OrderNum = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    to.ProductID = Converter.ToInt(ProductID, 0);
                    to.TRealAccountsID = TRealAccountsID;
                    to.Deal = Deal == "1" ? true : false;
                    to.OrderCount = Converter.ToInt(OrderCount, 0);
                    to.OpenPrice = Converter.ToFloat(OpenPrice, 0);
                    to.Profit = Converter.ToFloat(Profit, 0);
                    to.Loss = Converter.ToFloat(Loss, 0);
                    to.NowPrice = Converter.ToFloat(NowPrice, 0);
                    to.Deposit = Converter.ToFloat(Deposit, 0);
                    to.Interest = 0;
                    double nowbaozhengjin = 0;
                    var product = (from c in db.TProducts where c.ID == Converter.ToInt(ProductID, 0) select c).FirstOrDefault();
                    int OccupyFundsType = product.OccupyFundsType.Value;    //保证金方式(每单必须有的保证金) 1固定(每笔统一价) 2浮动(按照交易金额的百分比来算)
                    double OccupyFunds = Converter.ToFloat(product.OccupyFunds, 0);         //保证金   固定(固定值*手数)   浮动(开仓价*固定值*手数)
                    if (OccupyFundsType == 1)
                        nowbaozhengjin = OccupyFunds * product.ContractNumber.Value * Converter.ToInt(OrderCount, 0);
                    else
                        nowbaozhengjin = 0.01 * OccupyFunds * product.ContractNumber.Value * Converter.ToFloat(NowPrice, 0) * Converter.ToInt(OrderCount, 0);
                    to.OccupyFunds = nowbaozhengjin;
                    to.OpenTime = Convert.ToDateTime(OpenTime);
                    to.UpdateTime = DateTime.Now;
                    try
                    {
                        to.OperatorIP = GetUserIp;
                    }
                    catch
                    {
                        return Json(new { code = "0", msg = "服务器内部错误!" });
                    }
                    to.OrderState = 1;
                    to.OrderType = 1;
                    to.EntrustType = 0;
                    db.TOrders.InsertOnSubmit(to);
                    db.SubmitChanges();
                    return Json(new { code = "1", msg = "提交成功!" });
                    #endregion
                }
            }
            catch
            {
                return Json(new { code = "0", msg = "服务器内部错误!" });
            }
        }
示例#2
0
        public ActionResult PostOrderInfo()
        {
            int id = 0;  //Id
            string name = ""; //登陆名
            string Type = "";  //type
            string msg = "";  //提示信息
            bool fruit = false;  //结果

            try
            {
                var OrderNum = Request["OrderNum"];                                     //合约号
                var ProductID = Converter.ToInt(Request["ProductID"], 0);               //交易品种
                var TRealAccountsID = Converter.ToInt(Request["TRealAccountsID"], 0);   //用户
                var Deal = Converter.ToInt(Request["Deal"], 0);                         //类型 1买0卖
                var OrderCount = Converter.ToFloat(Request["OrderCount"], 0);           //手数
                var OpenPrice = Converter.ToFloat(Request["OpenPrice"], 0);             //开仓价
                var Profit = Converter.ToFloat(Request["Profit"], 0);                   //止盈
                var Loss = Converter.ToFloat(Request["Loss"], 0);                       //止损
                var NowPrice = Converter.ToFloat(Request["NowPrice"], 0);               //市场价,现价
                var ProfitAndLoss = Converter.ToFloat(Request["ProfitAndLoss"], 0);     //浮动盈亏
                var Spread = Converter.ToFloat(Request["Spread"], 0);                   //点数
                var Deposit = Converter.ToFloat(Request["Deposit"], 0);                 //加工费
                var Interest = Converter.ToFloat(Request["Interest"], 0);               //仓息
                var OccupyFunds = Converter.ToFloat(Request["OccupyFunds"], 0);         //占用资金  保证金
                var NetProfit = Converter.ToFloat(Request["NetProfit"], 0);             //盈亏净利
                var OpenTime = Convert.ToDateTime(Request["OpenTime"]);                 //开仓时间
                //var UpdateTime = Convert.ToDateTime(Request["UpdateTime"]);             //最后修改时间
                var OperatorIP = GetUserIp;                                             //操作Ip
                var OrderState = Converter.ToInt(Request["OrderState"], 1);             //订单状态 1开仓 2 平仓
                var OrderType = Converter.ToInt(Request["OrderType"], 1);               //订单类型  0委托 1持仓 2历史单
                var EntrustType = Converter.ToInt(Request["EntrustType"], 0);           //委托类型 0限价交易 1止损交易
                APIDataDataContext db = APIDataContextProxy.APIDB;
                try
                {
                    TOrders order = new TOrders();
                    if (string.IsNullOrEmpty(OrderNum))
                        OrderNum = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    order.OrderNum = OrderNum;               //合约号
                    order.ProductID = ProductID;             //交易品种
                    order.TRealAccountsID = TRealAccountsID; //用户
                    order.Deal = (Deal == 1);                  //类型 1买0卖
                    order.OrderCount = OrderCount;           //手数
                    order.OpenPrice = OpenPrice;             //开仓价
                    order.Profit = Profit;                   //止损
                    order.Loss = Loss;                       //止损
                    order.NowPrice = NowPrice;               //市场价,现价
                    order.ProfitAndLoss = ProfitAndLoss;     //浮动盈亏
                    order.Spread = Spread;                   //点数
                    order.Deposit = Deposit;                 //加工费
                    order.Interest = Interest;               //仓息
                    order.OccupyFunds = OccupyFunds;         //占用资金  保证金
                    order.NetProfit = NetProfit;             //盈亏净利
                    order.OpenTime = OpenTime;               //开仓时间
                    order.UpdateTime = DateTime.Now;           //最后修改时间
                    order.OperatorIP = OperatorIP;           //操作Ip
                    order.OrderState = OrderState;           //订单状态 1开仓 2 平仓
                    order.OrderType = OrderType;             //订单类型  0委托 1持仓 2历史单
                    order.EntrustType = EntrustType;         //委托类型 0限价交易 1止损交易
                    db.TOrders.InsertOnSubmit(order);
                    db.SubmitChanges();

                    //存入日志
                    id = Convert.ToInt32(Session["ID"]); //id
                    Type = Session["Type"].ToString();  //类型
                    msg = "添加订单成功";
                    fruit = true;
                    Converter.InsertLog(id, name, Type, msg, fruit);

                    return Content("{result:'success',msg:'添加订单成功!'}");
                }
                catch
                {
                    id = Convert.ToInt32(Session["ID"]); //id
                    Type = Session["Type"].ToString();  //类型
                    msg = "添加订单失败";
                    fruit = false;
                    Converter.InsertLog(id, name, Type, msg, fruit);

                    return Content("{result:'fail',msg:'添加订单失败!'}");
                }

            }
            catch
            {
                id = Convert.ToInt32(Session["ID"]); //id
                Type = Session["Type"].ToString();  //类型
                msg = "添加订单失败";
                fruit = false;
                Converter.InsertLog(id, name, Type, msg, fruit);

                return Content("{result:'fail',msg:'参数不正确!'}");
            }
        }
示例#3
0
        /// <summary>
        /// 正反向订单
        /// </summary>
        /// <returns></returns>
        public string RepeatOrder()
        {
            APIDataDataContext db = APIDataContextProxy.APIDB;
            int OrderId = Converter.ToInt(Request["OrderId"], 0);               //订单id
            int RepeatType = Converter.ToInt(Request["RepeatType"], 0);         //0方向订单  1正向订单
            var item = (from c in db.TOrders
                        where c.ID == OrderId
                        && c.OrderState == 1
                        && c.OrderType == 1
                        select c).FirstOrDefault();
            if (item != null)
            {
                try
                {
                    var Scale = Converter.ToInt((from c in db.TProducts where c.ID == item.ProductID select c.Scale).FirstOrDefault(), 2);
                    bool AllowOccupyPrePay = userGroup.AllowOccupyPrePay.Value;         //允许委托订单占用预付款(委托订单占用保证金)
                    var product = (from c in db.TProducts
                                   join b in db.TLastClose on c.ProductCode equals b.ProductCode
                                   where c.ID == item.ProductID
                                   select new { c, b }).FirstOrDefault();
                    var OrderCount = item.OrderCount;
                    var NowPrice = Converter.ToFloat(product.b.LastClose.Value.ToString("F" + Scale));

                    #region 允许交易
                    var AllowTrade = userGroup.AllowTrade;                       //允许交易
                    if (AllowTrade == false)
                    {
                        return ErrMsg(10039, "对不起您所在的用户组现在不允许交易!");
                    }
                    TRealAccounts tra = (from c in db.TRealAccounts where c.ID == userid select c).FirstOrDefault();
                    var traAllowTrade = tra.AllowTrade;
                    if (traAllowTrade == false)
                    {
                        return ErrMsg(10039, "对不起您现在不允许交易!");
                    }
                    #endregion
                    if (product == null)
                    {
                        return ErrMsg(10050, "价格已经改变请重新开仓!");
                    }
                    else
                    {
                        bool deal = item.Deal.Value;
                        if (RepeatType == 0)
                        {
                            deal = !deal;
                        }
                        double ContractNumber = product.c.ContractNumber.Value;   //合约数(乘)
                        //查询所有在手订单
                        var order = (from c in db.TOrders
                                     join a in db.TProducts on c.ProductID equals a.ID
                                     join b in db.TLastClose on a.ProductCode equals b.ProductCode
                                     where c.TRealAccountsID == userid && c.OrderType == 1 && c.OrderState == 1
                                     select new
                                     {
                                         c,
                                         b,
                                         a
                                     }).ToList();
                        if (AllowOccupyPrePay == true)
                        {
                            var order2 = (from c in db.TOrders
                                          join a in db.TProducts on c.ProductID equals a.ID
                                          join b in db.TLastClose on a.ProductCode equals b.ProductCode
                                          where c.TRealAccountsID == userid && c.OrderType == 0 && c.OrderState == 1
                                          select new
                                          {
                                              c,
                                              b,
                                              a
                                          }).ToList();
                            order = order.Union(order2).ToList();
                        }
                        var allprice = 0.0;
                        foreach (var items in order)
                        {
                            var fudongyingkui = 0.0;//浮动盈亏
                            //if (items.c.Deal == true)//买
                            //    fudongyingkui = (items.b.LastClose.Value - items.c.OpenPrice.Value) * items.a.ContractNumber.Value * items.c.OrderCount.Value;
                            //else//卖
                            //    fudongyingkui = (items.c.OpenPrice.Value - items.b.LastClose.Value - Converter.ToFloat(items.a.Fixedly)) * items.a.ContractNumber.Value * items.c.OrderCount.Value;
                            fudongyingkui = Converter.ToFloat(items.c.ProfitAndLoss);
                            var jiagongfei = Converter.ToFloat(items.c.Deposit);      //加工费
                            var cangxi = Converter.ToFloat(items.c.Interest);         //仓息
                            var baozhengjin = Converter.ToFloat(items.c.OccupyFunds); //占用资金 保证金OccupyFundsType
                            allprice = allprice + fudongyingkui - jiagongfei - cangxi - baozhengjin;
                        }
                        var userinfo = (from c in db.TRealAccounts where c.ID == userid select c).FirstOrDefault();
                        var AvailablePrice = userinfo.AccountMoney + allprice;  //可用余额
                        int DepositType = product.c.DepositType.Value;            //加工费方式(每单收取的加工费) 1固定(每笔统一价) 2浮动(按照交易金额的百分比来算)
                        double OpenDeposit = Converter.ToFloat(product.c.OpenDeposit, 0);         //开仓加工费 固定(固定值)   浮动(开仓*固定值)
                        int OccupyFundsType = product.c.OccupyFundsType.Value;    //保证金方式(每单必须有的保证金) 1固定(每笔统一价) 2浮动(按照交易金额的百分比来算)
                        double OccupyFunds = Converter.ToFloat(product.c.OccupyFunds, 0);         //保证金   固定(固定值*手数)   浮动(开仓价*固定值*手数)
                        //计算当前订单加工费
                        var nowjiagongfei = 0.0;
                        if (DepositType == 1)
                            nowjiagongfei = OpenDeposit * product.c.ContractNumber.Value * Converter.ToFloat(OrderCount, 1);
                        else
                            nowjiagongfei = 0.01 * OpenDeposit * product.c.ContractNumber.Value * NowPrice * Converter.ToFloat(OrderCount, 1);
                        //计算当前订单保证金
                        var nowbaozhengjin = 0.0;
                        if (OccupyFundsType == 1)
                            nowbaozhengjin = OccupyFunds * product.c.ContractNumber.Value * Converter.ToFloat(OrderCount, 1);
                        else
                            nowbaozhengjin = 0.01 * OccupyFunds * product.c.ContractNumber.Value * NowPrice * Converter.ToFloat(OrderCount, 1);
                        //仓息刚刚下的订单是没有的
                        //现在的钱可以开多少手
                        var ordernumMax = AvailablePrice / (nowjiagongfei + nowbaozhengjin);
                        if (OrderCount > ordernumMax || ordernumMax == null)
                            return ErrMsg(10033, "当前可用余额不足,请修改手数或更换产品!");
                        string OperatorIP = "";  //操作Ip
                        try
                        {
                            OperatorIP = GetUserIp;
                        }
                        catch { }
                        TOrders addorder = new TOrders();
                        addorder.OrderNum = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                        addorder.ProductID = item.ProductID;
                        addorder.TRealAccountsID = userid;
                        addorder.Deal = deal;
                        addorder.OrderCount = OrderCount;
                        addorder.OpenPrice = NowPrice;
                        addorder.Profit = item.Profit;
                        addorder.Loss = item.Loss;
                        addorder.OpenTime = DateTime.Now;
                        addorder.UpdateTime = DateTime.Now;
                        addorder.Deposit = nowjiagongfei;
                        addorder.OccupyFunds = nowbaozhengjin;
                        addorder.OperatorIP = OperatorIP;
                        addorder.OrderState = 1;
                        addorder.OrderType = 1;
                        addorder.EntrustType = 1;
                        db.TOrders.InsertOnSubmit(addorder);
                        db.SubmitChanges();

                        JObject jb = new JObject();
                        jb["success"] = "TRUE";
                        jb["content"] = addorder.ID;
                        return jb.ToString();
                    }
                }
                catch
                {
                    return ErrMsg(10051, "订单id不正确!");
                }
            }
            else
            {
                return ErrMsg(10051, "订单id不正确!");
            }
        }
示例#4
0
 public Registration(TInfo info, TOrders orders)
 {
     Info   = info;
     Orders = orders;
 }
示例#5
0
        /// <summary>
        /// 创建订单
        /// 10022 开仓时间不能为空
        /// 10023 对不起您所在的用户组现在不允许交易
        /// 10024 对不起您所在的用户组现在不允许开委托订单
        /// 10025 手数范围不正确
        /// 10026 超过当日最大持仓量
        /// 10027 超过当日最大操作次数限制
        /// 10028 pid错误
        /// 10029 当前产品不支持交易
        /// 10030 超过止盈距离
        /// 10031 超过止损距离
        /// 10032 超过挂单距离
        /// 10033 当前可用余额不足
        /// 10034 创建订单失败
        /// </summary>
        /// <returns></returns>
        public string CreateOrder()
        {
            double NowPrice = Converter.ToFloat(Request["nowprice"], 0);       //现价
            int id = Converter.ToInt(Request["pid"], 0);                       //产品id
            string Deal = Converter.ToString(Request["deal"], "true");         //true (true买 false卖)默认买
            double OrderCount = Converter.ToFloat(Request["ordercount"], 1);    //手数
            double OpenPrice = Converter.ToFloat(Request["openprice"], 0);      //开仓价(委托的开仓价)
            string OpenTimes = Converter.ToString(Request["opentime"], "");    //开时间(如果是委托订单就是委托单的截止时间)
            string OperatorIP = "";  //操作Ip
            try
            {
                OperatorIP = GetUserIp;
            }
            catch { }
            double Profit = Converter.ToFloat(Request["profit"], 0);            //止盈   平仓用的
            double Loss = Converter.ToFloat(Request["loss"], 0);                //止损   平仓用的
            int OrderType = Converter.ToInt(Request["ordertype"], 1);          //订单类型  0委托 1持仓 2历史单
            int EntrustType = Converter.ToInt(Request["entrusttype"], 0);      //委托类型  0限价交易 1止损交易

            APIDataDataContext db = APIDataContextProxy.APIDB;

            //查询用户组,如果用户所在组为空,则使用默认的用户组,如果默认用户组不存在,则给第一条用户组(IsDefault)
            TRealAccounts userinfo = (from c in db.TRealAccounts where c.ID == userid select c).FirstOrDefault();
            TUserGroup userGroup = (from c in db.TUserGroup where c.ID == userinfo.GroupID select c).FirstOrDefault();

            if (userGroup == null)
            {
                userGroup = (from c in db.TUserGroup where c.IsDefault == true select c).FirstOrDefault();
            }
            if (userGroup == null)
            {
                userGroup = (from c in db.TUserGroup select c).FirstOrDefault();
            }

            DateTime OpenTime = DateTime.Now;
            if (string.IsNullOrEmpty(OpenTimes))
            {
                OpenTime = DateTime.Now.AddDays(1);
                //return ErrMsg(10022, "开仓时间不能为空!");
            }
            else
            {
                DateTime.TryParse(OpenTimes, out OpenTime);
            }
            try
            {
                if (OrderType == 0)//委托单
                {
                    if (userGroup.EntrustDeadLine.Value == 1)//当日有效
                    {
                        if (OpenTime.Date != DateTime.Now.Date)
                        {
                            return ErrMsg(10022, "委托订单必须当天有效!");
                        }
                    }
                    else if (userGroup.EntrustDeadLine.Value == 2)//长期有效
                    {
                    }
                    else//本周有效
                    {
                        int weekday2 = (int)(DateTime.Now.DayOfWeek);
                        if (weekday2 == 0)
                            weekday2 = 7;
                        var todayint2 = 7 - weekday2;
                        var aa = (OpenTime.Date - DateTime.Now.Date).Days;
                        if (aa > todayint2)
                        {
                            return ErrMsg(10022, "委托订单必须本周有效!");
                        }
                    }
                }
                //Deposit       加工费
                //OccupyFunds   占用资金

                //用户组逻辑
                #region 允许交易
                var AllowTrade = userGroup.AllowTrade;                       //允许交易
                if (AllowTrade == false)
                {
                    return ErrMsg(10023, "对不起您所在的用户组现在不允许交易!");
                }

                TRealAccounts tra = (from c in db.TRealAccounts where c.ID == userid select c).FirstOrDefault();
                var traAllowTrade = tra.AllowTrade;
                if (traAllowTrade == false)
                {
                    return ErrMsg(10023, "对不起您现在不允许交易!");
                }
                #endregion
                #region 委托交易
                var AllowEntrustOrder = userGroup.AllowEntrustOrder;         //允许委托订单
                if (AllowEntrustOrder == false && OrderType == 0)
                {
                    return ErrMsg(10024, "对不起您所在的用户组现在不允许开委托订单!");
                }
                #endregion
                #region 手数
                var MinVolume = userGroup.MinVolume;                       //最小手数
                var MaxVolume = userGroup.MaxVolume;                       //最大手数
                if (OrderCount < MinVolume || OrderCount > MaxVolume)
                {
                    return ErrMsg(10025, "手数范围在" + MinVolume + "-" + MaxVolume + "!");
                }
                #endregion
                #region 最大持仓量
                var MaxWare = userGroup.MaxWare;                           //当日最大持仓量(在手订单)
                //查询产品持仓量
                var orderWare = (from c in db.TOrders
                                 where c.TRealAccountsID == userid
                                 && c.OrderState == 1
                                 && c.OrderType == 1
                                 && c.ProductID == id
                                 && c.OpenTime.Value.Date == DateTime.Now.Date
                                 select c.OrderCount).Sum();
                if (orderWare + OrderCount > MaxWare)
                {
                    return ErrMsg(10026, "超过当日最大持仓量,当日最大持仓量为" + MaxWare + "!");
                }
                #endregion
                #region 当日操作最大次数
                var MaxOperate = userGroup.MaxOperate;                     //当日操作最大次数限制  开仓(成功的开仓平仓委托动作)
                //查询当日操作次数
                var orderOperate = (from c in db.TOrders //开仓
                                    where c.TRealAccountsID == userid
                                    && c.OrderState == 1
                                    && c.OrderType == 1
                                    && c.OpenTime.Value.Date == DateTime.Now.Date
                                    select c).Count();

                orderOperate = orderOperate + (from c in db.TOrders//委托
                                               where c.TRealAccountsID == userid
                                               && c.OrderState == 1
                                               && c.OrderType == 0
                                               && c.UpdateTime.Value.Date == DateTime.Now.Date
                                               select c).Count();

                orderOperate = orderOperate + (from c in db.TOrders//平仓
                                               where c.TRealAccountsID == userid
                                               && c.OrderState == 2
                                               && c.OrderType == 2
                                               && c.OpenTime.Value.Date == DateTime.Now.Date
                                               select c).Count();
                if (orderOperate >= MaxOperate)
                {
                    return ErrMsg(10027, "超过当日最大操作次数限制,当日最大操作次数为" + MaxOperate + "!");
                }
                #endregion
                //LiquidationRate 强制平仓比率
                var AllowOccupyPrePay = userGroup.AllowOccupyPrePay;         //允许委托订单占用预付款(委托订单占用保证金)

                //产品逻辑
                var product = (from c in db.TProducts where c.ID == id select c).FirstOrDefault();
                if (product == null)
                {
                    return ErrMsg(10028, "pid错误!");
                }
                var Scale = Converter.ToFloat(product.Scale);
                #region 否可以开仓交易 是否显示
                var IsBuy = product.IsBuy;                       //是否可以开仓交易
                var IsLook = product.IsLook;                     //是否显示
                if (IsBuy == false || IsLook == false)
                {
                    return ErrMsg(10029, "当前产品不支持交易!");
                }

                #endregion
                #region 止盈·止损 距离
                var ProfitSize = product.ProfitSize;           //止盈距离
                //if (Profit > ProfitSize)
                //{
                //    return ErrMsg(10030, "超过止盈距离,止盈距离为" + ProfitSize + "!");
                //}
                var LossSize = product.LossSize;               //止损距离
                //if (Loss > LossSize)
                //{
                //    return ErrMsg(10031, "超过止损距离,止损距离为" + LossSize + "!");
                //}
                #endregion
                #region 挂单距离
                var lastclose = (from c in db.TLastClose where c.ProductCode == product.ProductCode select c).FirstOrDefault();
                var lastcloseprice = 0.0;//现价(如果是买,则是当前最新价+点差)
                if(OrderType==1)
                {
                    if (lastclose == null)
                    {
                        return ErrMsg(10050, "价格已经改变请重新开仓!");
                    }
                    var OffsetLow = userGroup.OffsetLow;//允许的价格浮动
                    var OffsetHigh = userGroup.OffsetHigh;
                    //以当前数据库的最新价格为准,而不是使用用户传入的价格
                    if (Deal.ToLower() == "true")
                    {
                        lastcloseprice = product.Fixedly.Value + lastclose.LastClose.Value;
                    }
                    else
                    {
                        lastcloseprice = lastclose.LastClose.Value;
                    }
                    if (lastcloseprice - OpenPrice > 0)
                    {
                        if (((lastcloseprice - OpenPrice) >= OffsetLow))//&& (lastcloseprice - OpenPrice) <= OffsetHigh))
                            return ErrMsg(10050, "价格已经改变请重新开仓!");
                    }
                    else
                    {
                        if (((OpenPrice - lastcloseprice) >= OffsetLow))// && (OpenPrice - lastcloseprice) <= OffsetHigh))
                            return ErrMsg(10050, "价格已经改变请重新开仓!");
                    }
                }
                else
                {
                    if (Deal.ToLower() == "true")
                    {
                        lastcloseprice = product.Fixedly.Value + lastclose.LastClose.Value;
                    }
                    else
                    {
                        lastcloseprice = lastclose.LastClose.Value;
                    }
                }
                NowPrice = lastcloseprice;
                var EntrustSize = product.EntrustSize;         //挂单距离 委托开盘价为当前开盘价格+ - 挂单距离

                //if (OrderType != 1 && !(NowPrice - EntrustSize <= OpenPrice && NowPrice + EntrustSize >= OpenPrice))
                //{
                //    return ErrMsg(10032, "超过挂单距离,挂单距离离为" + EntrustSize + "!");
                //}

                if (OrderType == 0)
                    if (EntrustType == 0)//0限价交易
                    {
                        if (!(NowPrice + EntrustSize <= OpenPrice) && Deal == "false")//卖
                        {
                            return ErrMsg(10032, "超过挂单距离,挂单距离离为" + EntrustSize + "!");
                        }
                        else if (!(NowPrice - EntrustSize >= OpenPrice) && Deal == "true")//买
                        {
                            return ErrMsg(10032, "超过挂单距离,挂单距离离为" + EntrustSize + "!");
                        }
                    }
                    else//止损交易
                    {
                        if (!(NowPrice - EntrustSize >= OpenPrice) && Deal == "false")//卖
                        {
                            return ErrMsg(10032, "超过挂单距离,挂单距离离为" + EntrustSize + "!");
                        }
                        else if (!(NowPrice + EntrustSize <= OpenPrice) && Deal == "true")//买
                        {
                            return ErrMsg(10032, "超过挂单距离,挂单距离离为" + EntrustSize + "!");
                        }

                    }

                #endregion
                var ContractNumber = product.ContractNumber;   //合约数(乘)
                //查询所有在手订单
                var order = (from c in db.TOrders
                             join a in db.TProducts on c.ProductID equals a.ID
                             join b in db.TLastClose on a.ProductCode equals b.ProductCode
                             where c.TRealAccountsID == userid && c.OrderType == 1 && c.OrderState == 1
                             select new
                             {
                                 c,
                                 b,
                                 a
                             }).ToList();
                //如果委托订单占用保证金,则把委托订单也计算在内(计算风险比率 70%)
                if (AllowOccupyPrePay == true)
                {
                    var order2 = (from c in db.TOrders
                                  join a in db.TProducts on c.ProductID equals a.ID
                                  join b in db.TLastClose on a.ProductCode equals b.ProductCode
                                  where c.TRealAccountsID == userid && c.OrderType == 0 && c.OrderState == 1
                                  select new
                                  {
                                      c,
                                      b,
                                      a
                                  }).ToList();
                    order = order.Union(order2).ToList();
                }
                var allprice = 0.0;//所有占用保证金的订单的盈亏净利之和(浮动盈亏-仓息-加工费)-所用订单的保证金
                foreach (var item in order)
                {
                    var Scales=Converter.ToInt(item.a.Scale,2);
                    var fudongyingkui = 0.0;//浮动盈亏
                    if (item.c.OrderType != 0)
                    {
                        if (item.c.Deal == true)//买
                            fudongyingkui = (Converter.ToFloat(item.b.LastClose.Value.ToString("F"+Scales)) - item.c.OpenPrice.Value) * item.a.ContractNumber.Value * item.c.OrderCount.Value;
                        else//卖
                            fudongyingkui = (item.c.OpenPrice.Value - Converter.ToFloat(item.b.LastClose.Value.ToString("F"+Scales)) - Converter.ToFloat(item.a.Fixedly)) * item.a.ContractNumber.Value * item.c.OrderCount.Value;

                    }
                    fudongyingkui = Converter.ToFloat(fudongyingkui.ToString("F" + Scales));
                    //fudongyingkui = Converter.ToFloat(item.c.ProfitAndLoss);
                    var jiagongfei = Converter.ToFloat(item.c.Deposit);      //加工费
                    var cangxi = Converter.ToFloat(item.c.Interest);         //仓息
                    var baozhengjin = Converter.ToFloat(item.c.OccupyFunds); //占用资金 保证金OccupyFundsType
                    allprice = allprice + fudongyingkui - jiagongfei - cangxi - baozhengjin;//
                }
                //var userinfo = (from c in db.TRealAccounts where c.ID == userid select c).FirstOrDefault();
                var AvailablePrice = userinfo.AccountMoney + allprice;  //可用余额(账户余额-仓息-加工费+浮动盈亏-保证金)

                #region 计算当前(即将创建的单子)的相关费用

                int DepositType = product.DepositType.Value;            //加工费方式(每单收取的加工费) 1固定(每笔统一价) 2浮动(按照交易金额的百分比来算)

                double OpenDeposit = Converter.ToFloat(product.OpenDeposit, 0);         //开仓加工费 固定(固定值)   浮动(开仓*固定值)
                int OccupyFundsType = product.OccupyFundsType.Value;    //保证金方式(每单必须有的保证金) 1固定(每笔统一价) 2浮动(按照交易金额的百分比来算)
                double OccupyFunds = Converter.ToFloat(product.OccupyFunds, 0);         //保证金   固定(固定值*手数)   浮动(开仓价*固定值*手数)
                //计算当前订单加工费
                var nowjiagongfei = 0.0;
                if (DepositType == 1)
                    nowjiagongfei = OpenDeposit * product.ContractNumber.Value * Converter.ToFloat(OrderCount, 1);
                else
                    nowjiagongfei = 0.01 * OpenDeposit * product.ContractNumber.Value * NowPrice * Converter.ToFloat(OrderCount, 1);
                //计算当前订单保证金
                var nowbaozhengjin = 0.0;
                if (OccupyFundsType == 1)
                    nowbaozhengjin = OccupyFunds * product.ContractNumber.Value * Converter.ToFloat(OrderCount, 1);
                else
                    nowbaozhengjin = 0.01 * OccupyFunds * product.ContractNumber.Value * NowPrice * Converter.ToFloat(OrderCount, 1);
                //仓息刚刚下的订单是没有的
                //现在的钱可以开多少手
                var ordernumMax = AvailablePrice / (nowjiagongfei + nowbaozhengjin);//只用加工费和保证金需要占用资金
                if (Converter.ToFloat(ordernumMax, 0) < 1)
                    return ErrMsg(10033, "当前可用余额不足,请修改手数或更换产品!");
                TOrders addOrder = new TOrders();

                #endregion
                if (OrderType == 1)//开仓
                {
                    addOrder.ProductID = id;
                    addOrder.OrderNum = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    addOrder.TRealAccountsID = userid;
                    addOrder.OpenPrice = Converter.ToFloat(lastcloseprice.ToString("F" + Scale));
                    addOrder.Deal = Deal.ToLower() == "true";
                    addOrder.OrderCount = OrderCount;
                    addOrder.OpenTime = OpenTime;
                    addOrder.Profit = Convert.ToDouble(Profit.ToString("F" + Scale));
                    addOrder.Loss = Convert.ToDouble(Loss.ToString("F" + Scale));
                    addOrder.OrderState = 1;
                    addOrder.OrderType = OrderType;
                    addOrder.EntrustType = EntrustType;
                    addOrder.UpdateTime = DateTime.Now;
                    addOrder.Deposit = nowjiagongfei;
                    addOrder.OccupyFunds = nowbaozhengjin;
                    addOrder.OperatorIP = OperatorIP;
                }
                else//委托
                {
                    addOrder.ProductID = id;
                    addOrder.OrderNum = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    addOrder.TRealAccountsID = userid;
                    addOrder.OpenPrice = Converter.ToFloat(OpenPrice.ToString("F" + Scale));
                    addOrder.Deal = Deal.ToLower() == "true";
                    addOrder.OrderCount = OrderCount;
                    addOrder.OpenTime = OpenTime;
                    addOrder.Profit = Convert.ToDouble(Profit.ToString("F" + Scale));
                    addOrder.Loss = Convert.ToDouble(Loss.ToString("F" + Scale));
                    addOrder.OrderState = 1;
                    addOrder.OrderType = OrderType;
                    addOrder.EntrustType = EntrustType;
                    addOrder.UpdateTime = DateTime.Now;
                    addOrder.Deposit = nowjiagongfei;
                    addOrder.OccupyFunds = nowbaozhengjin;
                    addOrder.OperatorIP = OperatorIP;
                }
                db.TOrders.InsertOnSubmit(addOrder);
                db.SubmitChanges();

                JObject jb = new JObject();
                jb["success"] = "TRUE";
                jb["content"] = addOrder.ID;
                return jb.ToString();
            }
            catch
            {
                return ErrMsg(10034, "创建订单失败!");
            }
        }