//package cn.muchinfo.rma.global.data;
//
//import java.util.ArrayList;
//
//import cn.muchinfo.rma.global.GlobalDataCollection;
//import cn.muchinfo.rma.global.data.futureOrders.FutureHoldData;
//
///**
// * Created by simonzhou on 2018/4/26.
// * 操作资金账户 计算资金
// */
//
//public class OpeTaAccountInfoData extends AccountData {
//
//    private double dynProfitTotal = 0.0;      // 浮动盈亏
//    private double totalNum = 0.0;            // 全额市值
//    private double entrustOrderAmount = 0d;// 委托冻结
//
////    /**
////     * 获取资金信息
////     *
////     * @param accountId
////     * @return
////     */
////    public AccountData getInfoAccount(long accountId) {
////        for (TAAccountAllData ta : GlobalDataCollention.getInstance().getTaAccountAllData()) {
////            if (ta.getTaAccountInfo() != null && ta.getTaAccountInfo().getAccountId() == accountId) {
////                return ta.getTaAccountInfo();
////            }
////        }
////        return null;
////    }
//
//    /**
//     * 计算资金信息
//     *
//     * @return
//     */
//    public CalculateData getCalculate(long accountId, boolean version) {
//        totalNum = 0;
//        dynProfitTotal = 0;
//        CalculateData data = new CalculateData();
//        //计算相关数据
//        // 持仓单计算
////        for (TradeHolderDetailChild holder : GlobalDataCollention.getInstance().getTradeHolderDetailChildren()) {
////            if (holder.getAccountid().equals(String.valueOf(getAccountId()))) {
////                holder.calc();
////                GoodsInfoAndQuotes info = GlobalDataCollention.getInstance().getmGoodsInfoClass().get(Integer.parseInt(holder.getGoodsid()));
////                if (info != null) {
////                    // 收益权的时候才计算浮动盈亏
////                    if (Integer.parseInt(holder.getTradeproperty()) == MTPEnums.TRADEPROPERTY_CANSHORT) {
////                        dynProfitTotal += holder.getDynProfit();
////                    }
////
////                    totalNum += holder.getTotalAmout();
////                } else {
////                    totalNum += Double.parseDouble(holder.getHolderamount());
////                }
////            }
////
////        }
//
//        // 持仓汇总计算
//        try {
//            ArrayList<FutureHoldData> summaryData = GlobalDataCollection.Companion.getInstance().getFutureHoldData();
//            for (FutureHoldData summary : summaryData) {
//                if (summary.getAccountid().equals(String.valueOf(accountId))) {
//                    summary.calc();
//                    GoodsInfoAndQuotes goodsInfoAndQuotes = GlobalDataCollention.getInstance().getmGoodsInfoClass().get(Integer.parseInt(String.valueOf(summary.getGoodsid())));
//                    if (goodsInfoAndQuotes != null) {
//                        String rate = GlobalDataCollention.getRate(String.valueOf(getAccountId()), String.valueOf(goodsInfoAndQuotes.getGoodsInfo().getCurrencyId()));
//                        MarketsInfoData marketInfo = GlobalDataCollention.getInstance().getMarketInfo(goodsInfoAndQuotes.getGoodsInfo().getMarketID());
//                        if (marketInfo == null) {
//                            continue;
//                        }
//                        if (marketInfo.getTradeProperty() == MTPEnums.TRADEPROPERTY_CANSHORT) {
//                            dynProfitTotal += summary.getDynProfit();
//                        }
//                        totalNum += summary.getTotalAmout() * goodsInfoAndQuotes.getGoodsInfo().getAgreeUnit() * Double.parseDouble(rate);
//                    } else {
//                        totalNum += Double.parseDouble(summary.getCurholderamount());
//                    }
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//
//
//        //region 资管用的单据
////        for (InforInnerHolderRspData inforInnerHolderRspData : GlobalDataCollention.getInstance().getInforInnerHolderRspData()) {
////            if (inforInnerHolderRspData.getAccountid().equals(String.valueOf(accountId))) {// 账户
////                inforInnerHolderRspData.calc();
////                GoodsInfoAndQuotes goodsInfoAndQuotes = GlobalDataCollention.getInstance().getmGoodsInfoClass().get(Integer.parseInt(String.valueOf(inforInnerHolderRspData.getGoodsid())));
////                if (goodsInfoAndQuotes != null) {
////                    String rate = GlobalDataCollention.getRate(String.valueOf(getAccountId()), String.valueOf(goodsInfoAndQuotes.getGoodsInfo().getCurrencyId()));
////                    totalNum += inforInnerHolderRspData.getTotalAmout() * goodsInfoAndQuotes.getGoodsInfo().getAgreeUnit() * Double.parseDouble(rate);
////                }
////            }
////        }
//
//
//        // 委托单的冻结
//        for (TradeEntrustData item : GlobalDataCollention.getInstance().getTradeEntrustDataList()) {
//            if (item.getAccountid().equals(String.valueOf(accountId))) {// 账户
//                if (item.getOrderstatus() == MTPEnums.INNERSTATUS_ORDERSUCC) { // 委托成功的才计算
//                    // 冻结 = 委托价格*可用数量*汇率
//                    double orderPrice = Double.parseDouble(item.getOrderprice());
//                    double num = 0;
//                    if (item.getBuyorsell() == 0) {
//                        num = Double.parseDouble(item.getOpenqty()) - Double.parseDouble(item.getOpentradeqty());
//                    } else if (item.getBuyorsell() == 1) {
//                        num = Double.parseDouble(item.getCloseqty()) - Double.parseDouble(item.getClosetradeqty());
//                    }
//                    GoodsInfoAndQuotes goodsInfoAndQuotes = GlobalDataCollention.getInstance().getmGoodsInfoClass().get(Integer.parseInt(String.valueOf(item.getGoodsid())));
//                    String rate = GlobalDataCollention.getRate(String.valueOf(getAccountId()), String.valueOf(goodsInfoAndQuotes.getGoodsInfo().getCurrencyId()));
//                    entrustOrderAmount += orderPrice * num * Double.parseDouble(rate);
////                    obj.orderPrice*Double((obj.buyOrSell == 0 ? (obj.openOrderQty-obj.openTradeQty) : (obj.closeOrderQty-obj.closeTradeQty)))*
//                }
//            }
//        }
//
//        data.setTotalAmount(totalNum);
//
//        //endregion
//
//
//
//        //找到对应的资金账户
//        TaAccountInfoData accountInfoData = getInfoAccount(accountId);
//        if (accountInfoData != null) {
//            // 增加交易端配置项，账户净值是否减冻结资金 - 0:不减 1:减
//            // 期权商品没有市值
//            double AmountWorth = 0;
//            String config = "1";
//            ConfigInfoData configInfoData = GlobalDataCollention.getInstance().getConfigInfoDatas().get("307");
//            if (configInfoData != null && configInfoData.getParamValue().equals("0")) {
//                config = configInfoData.getParamValue();
//            }
//            // FIXME ====== 计算净值这个一定要注意啊，只有香港资管项目才不需要计算浮动盈亏
//            // FIXME ====== 计算净值这个一定要注意啊，只有香港资管项目才不需要计算浮动盈亏
//            // FIXME ====== 计算净值这个一定要注意啊，只有香港资管项目才不需要计算浮动盈亏
//            // FIXME ====== 计算净值这个一定要注意啊，只有香港资管项目才不需要计算浮动盈亏
//            // FIXME ====== 计算净值这个一定要注意啊，只有香港资管项目才不需要计算浮动盈亏
//            // FIXME ====== 计算净值这个一定要注意啊，只有香港资管项目才不需要计算浮动盈亏
//            if (config.equals("0")) {
//                // 0.净值=期末余额+市值+浮动盈亏(收益权)
//                AmountWorth = accountInfoData.getCurrentBalance() + totalNum/* + dynProfitTotal*/;
//            } else {
//                if (version) {
//                    // 1.净值=期末余额+浮动盈亏(收益权)-其他冻结-手续费冻结-出金冻结
//                    AmountWorth = accountInfoData.getCurrentBalance() /*+ dynProfitTotal*/ - accountInfoData.getOtherFreezeMargin() - accountInfoData.getFreezeCharge() - accountInfoData.getOutAmountFreeze();
//                } else {
//                    // 1.净值=期末余额+市值+浮动盈亏(收益权)-其他冻结-手续费冻结-出金冻结
//                    AmountWorth = accountInfoData.getCurrentBalance() + totalNum /*+ dynProfitTotal*/ - accountInfoData.getOtherFreezeMargin() - accountInfoData.getFreezeCharge() - accountInfoData.getOutAmountFreeze();
//                }
//            }
//
//            data.setAmountWorth(AmountWorth);
//            LogUtils.e("totalNum", totalNum + "");
//            LogUtils.e("dynProfitTotal", dynProfitTotal + "");
//            LogUtils.e("balance", accountInfoData.getCurrentBalance() + "");
//
//            // 可用保证金＝期末余额+浮动盈亏(收益权[亏损计算，盈利是否计算根据系统配置])-占用冻结-其它冻结-手续费冻结-出金冻结 - 委托冻结
//            double AvailMargin = accountInfoData.getCurrentBalance() /*+ dynProfitTotal*/ - accountInfoData.getUsedMargin() - accountInfoData.getFreezeMargin() - accountInfoData.getOtherFreezeMargin() - accountInfoData.getFreezeCharge() - accountInfoData.getOutAmountFreeze() - entrustOrderAmount;
//            data.setAvailMargin(AvailMargin);
//
//            // 风险率
//            double risk;
//            // 风险净值 风险净值公式修改-》=期末余额+市值+浮动盈亏(收益权)-其他冻结-出金冻结
//            double riskAmountWorth = accountInfoData.getCurrentBalance() + totalNum + dynProfitTotal - accountInfoData.getOtherFreezeMargin() - accountInfoData.getOutAmountFreeze();
//
//            // 风险率
//            if (accountInfoData.getUsedMargin() <= 0) {
//                // 占用<= 0,风险率为0%
//                risk = 0;
//            } else {
//                if (riskAmountWorth <= 0) {
//                    // 占用>0 && 风险净值<=0时，风险率为10000%
//                    risk = 100;
//                } else {
//                    // 风险率=占用/风险净值
//                    risk = accountInfoData.getUsedMargin() / riskAmountWorth;
//                }
//            }
//
//            double safeRisk = 0;
//            // 安全度
//            if (riskAmountWorth <= 0) {
//                // 风险净值为0或负数，安全度为0%
//                safeRisk = 0;
//            } else {
//                if (accountInfoData.getMortgageCredit() == 0) {
//                    // 授信资金为0，安全度为10000%
//                    safeRisk = 100;
//                } else {
//                    // 安全度=(风险净值-授信资金)/授信资金
//                    safeRisk = (riskAmountWorth - accountInfoData.getMortgageCredit()) / accountInfoData.getMortgageCredit();
//                }
//            }
//            data.setRisk(risk);
//            data.setSafeRisk(safeRisk);
//
//        }
//        return data;
//
//    }
//
//    public double getDynProfitTotal() {
//        return dynProfitTotal;
//    }
//
//    public void setDynProfitTotal(double dynProfitTotal) {
//        this.dynProfitTotal = dynProfitTotal;
//    }
//
//
//    public double getAllFreeze() {
//        return getOutAmountFreeze() + getFreezeCharge() + getOtherFreezeMargin() + getFreezeMargin();
//    }
//
//
//}
