package com.gingersoft.gsa.cloud.base.common.bean.mealManage;

import android.app.Activity;

import com.gingersoft.gsa.cloud.base.common.bean.BillOrderMoney;
import com.gingersoft.gsa.cloud.base.common.bean.OrderBean;
import com.gingersoft.gsa.cloud.base.common.bean.OrderDetail;
import com.gingersoft.gsa.cloud.base.common.bean.PayMethod;
import com.gingersoft.gsa.cloud.print.bean.OrderDetails;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 作者：ELEGANT_BIN
 * 版本：1.6.0
 * 创建日期：2019/9/17
 * 修订历史：2019/9/17
 * 描述：
 */
public class MyOrderManage {

    private static MyOrderManage myOrderManage;
    //外賣接單
    private static OrderDetails.DataBean dataBean;

    public static MyOrderManage getInstance() {
        if (myOrderManage == null) {
            myOrderManage = new MyOrderManage();
        }
        return myOrderManage;
    }

    private Activity context;
    //    private MealStandPresenter presenter;

    private boolean overMaxQty = false;
    private int Languageindex;
    private boolean Edit_Send = false;
    private int selpostion = 0;

    private int selstartpostion = 0;
    private int selendpostion = 0;
    private int selmainstartpositon = 0;
    private int selmainendpostion = 0;
    private int select_Full_start_position = 0;
    private int select_Full_end_position = 0;

    private int seqno = 1; //一级序
    private int sseq = 0;//二级序
    private int mseq = 0;//三级序

    private OrderBean orderBean;
    /**食品列表*/
    private List<OrderDetail> orderFoodList = new ArrayList<>();
    /**本次送單食品，用於打印*/
    private List<OrderDetail> newFoodList = new ArrayList<>();
    /**整單折扣，服務費*/
    protected List<BillOrderMoney> orderMoneyList = new ArrayList<>();
    /**本次支付方式，用於打印*/
    private List<PayMethod> billMoney = new ArrayList<>();

    /**記錄修改訂單操作*/
    private boolean modifyOrder = false;

    public long getOrderId() {
        if (orderBean != null) {
            return orderBean.getId();
        }
        return -1;
    }

    public String getOrderNo() {
        if (orderBean != null) {
            return orderBean.getOrderNo();
        }
        return "";
    }

    public int getOrderStatus() {
        if (orderBean != null) {
            return orderBean.getStatus();
        }
        return 0;
    }

    public Date getOpenTableTime() {
        if (orderBean != null && orderBean.getCreateTime() != null) {
            return new Date(orderBean.getCreateTime());
        }
        return new Date();
    }

    public void setOrderId(double orderId) {
        if (orderBean == null) {
            orderBean = new OrderBean();
        }
        if (orderBean != null) {
            orderBean.setId((long) orderId);
        }
    }

    public void setOrderBean(OrderBean orderBean) {
        this.orderBean = orderBean;
    }

    public OrderBean getOrderBean() {
        return orderBean;
    }

    public void changeNumber(int number) {

        if (orderFoodList.size() <= 0) {
            return;
        }
        if (this.selmainstartpositon > orderFoodList.size()) {
            return;
        }

        OrderDetail orderFood = orderFoodList.get(this.selmainstartpositon);

//        CurrentSoldoutFood.DatasBean scClass;
//        Db_SoldoutCtrl db = new Db_SoldoutCtrl(GSAApplication.getAppContext());
//        if (orderFood.isIsfood()) {
//            scClass = db.getSoldoutCtrl("F", orderFood.getItemno());
//        } else {
//            scClass = db.getSoldoutCtrl("M", orderFood.getItemno());
//        }
//        db.closeDb();
//
//        int OR_Qty = scClass.getOR_Qty();
//        int Used_Qty = scClass.getUsed_Qty();
//        if (OR_Qty == -999999999 && Used_Qty == -999999999) {
//
//        } else {
//            int countNumber = getCountNumber(orderFood.getItemno());
//            if (OR_Qty - Used_Qty - countNumber - number < 0) {
//                return;
//            }
//        }

        int qty = orderFood.getNumber();

        boolean intQty = number == 1 || qty == 1 || number < qty;

        change_food_number(orderFood, number);
        change_child_number(orderFood, number);

        if (intQty) {
//            if (number != 1 && number < qty) {
//                //從大到小直接抹掉細項
//                removeFoodModifify(orderFood);
//            }
//            change_child_number(orderFood, number);
        }
    }

    private void change_food_number(OrderDetail orderFood, int number) {

        orderFood.setNumber(number);

        setCalculatePrice(orderFood);
    }

    private void change_child_number(OrderDetail orderFood, int number) {
        int parentIndex = orderFood.getMyIndex();
        for (int i = 0; i < orderFoodList.size(); i++) {
            OrderDetail datasBean = orderFoodList.get(i);
            if (datasBean.getParentIndex() == parentIndex) {
                datasBean.setNumber(number);
                setCalculatePrice(datasBean);
            }
        }
    }

//    private void change_child_number(OrderDetail orderFood, int number) {
//
//        //每个组子项最大选择数量
//        Map<String, String> maxNumberArray = new HashMap<>();
//
//        for (int i = 0; i < orderFoodList.size(); i++) {
//            OrderDetail datasBean = orderFoodList.get(i);
//            if (datasBean.getParent_index() == orderFood.getMy_index() && datasBean.getItemtype() == 2) {
//
//                String comId = datasBean.getComid();
//
//                if (!datasBean.getComid().equals("discount")) {
//
//                    //当前主食品组子项最大选择数
//                    int comboMaxQty = selectMealAdapter.queryComboMaxQty(datasBean.getItemNo(), datasBean.getComid());
//                    int comboGroupMaxQty = selectMealAdapter.queryComboMaxQty(datasBean.getItemNo(), datasBean.getComid()) * number;
//
//                    maxNumberArray.put(comId, comboMaxQty + "_" + comboGroupMaxQty);
//                } else {
//                    maxNumberArray.put(comId, "-1_-1");
//                }
////                int comIdIndex = maxNumberArray.indexOfKey(comId);
////                if (comIdIndex != -1) {
////                    int totalQty;
////                    if (!TextUtils.isEmpty(maxNumberArray.get(comId))) {
////                        totalQty = Integer.parseInt(maxNumberArray.get(comId).split("_")[0]) + datasBean.getQty();
////                    } else {
////                        totalQty = datasBean.getQty();
////                    }
////                } else {
////                    maxNumberArray.put(comId, datasBean.getQty() + "");
////                }
//            }
//        }
//
//        Iterator<String> iterator = maxNumberArray.keySet().iterator();
//        while (iterator.hasNext()) {
//            String key = iterator.next();
//            String totalQty = maxNumberArray.get(key);
//            int combNumber = 0;
//            for (int j = 0; j < orderFoodList.size(); j++) {
//                OrderDetail datasBean = orderFoodList.get(j);
//                if (datasBean.getParent_index() == orderFood.getMy_index() && datasBean.getItemtype() == 2) {
//                    String comId = datasBean.getComid();
//                    if (key.equals(comId)) {
//                        combNumber += datasBean.getQty();
//                    }
//                }
//            }
//            String value = totalQty + "_" + combNumber;
//            maxNumberArray.put(key, value);
//        }
//
////        for (int i = 0; i < maxNumberArray.size(); i++) {
////            String key = maxNumberArray.get(i);
////            String totalQty = maxNumberArray.get(key);
////            int combNumber = 0;
////            for (int j = 0; j < orderFoodList.size(); j++) {
////                OrderDetail datasBean = orderFoodList.get(j);
////                if (datasBean.getParent_index() == orderFood.getMy_index() && datasBean.getItemtype() == 2) {
////                    String comId = datasBean.getComid();
////                    if (key.equals(comId)) {
////                        combNumber += datasBean.getQty();
////                    }
////                }
////            }
////            String value = totalQty + "_" + combNumber;
////            maxNumberArray.put(key, value);
////        }
//
//        for (int i = orderFoodList.size() - 1; i >= 0; i--) {
//            OrderDetail datasBean = orderFoodList.get(i);
//            if (datasBean.getParent_index() == orderFood.getMy_index()) {
//                if (datasBean.getItemtype() == 3) {
//                    datasBean.setQty(number);
//                    datasBean.newnumber = number;
//                    setCalculatePrice(datasBean, datasBean.isFuncmoney(), datasBean.getUnitprice());
//                } else {
//
//                    if (datasBean.getComid().equals("discount")) {
//                        datasBean.setQty(number);
//                        datasBean.newnumber = number;
//                        continue;
//                    }
//
//                    int comboQty = datasBean.getQty();
//
//                    //当前主食品子项最大选择数
////                    int comboMaxQty = selectMealAdapter.queryComboMaxQty(datasBean.getItemNo(), datasBean.getComid()) * orderFood.getQty();
//
//                    int currentNumber = datasBean.getChangeBeforeQty();
////
//                    String key = datasBean.getComid();
//                    String vaule = maxNumberArray.get(key);
//
//                    String[] totalQty_combNumber = vaule.split("_");
//                    int comboMaxQty = Integer.parseInt(totalQty_combNumber[0]);
//                    int comboGroupMaxQty = Integer.parseInt(totalQty_combNumber[1]);
////                    int combNumber = Integer.parseInt(totalQty_combNumber[2]);
//
//                    if (number == 1) {
//                        currentNumber = datasBean.getChangeBeforeQty();
//                    } else {
//                        if (number < datasBean.getQty()) {
//                            //从大到小
//                            if (comboMaxQty == 1) {
//                                //这一组最大选择数为1
//                                currentNumber = number;
//                            } else {
//                                if (comboGroupMaxQty > 0) {
//                                    currentNumber = number;
//                                    //最大选择数减去当前子项的数量
//                                    comboGroupMaxQty -= currentNumber;
//                                    maxNumberArray.put(key, comboMaxQty + "_" + comboGroupMaxQty);
//                                } else {
//                                    //已达到最大选择数，后面还有食品直接删除
//                                    currentNumber = 0;
//                                }
//                            }
//                        } else {
//                            currentNumber = number * datasBean.getChangeBeforeQty();
//                        }
//                    }
//
//                    if (currentNumber == 0) {
//                        orderFoodList.remove(i);
//                    } else {
//                        datasBean.setQty(currentNumber);
//                        datasBean.newnumber = currentNumber;
//                        setCalculatePrice(datasBean, datasBean.isFuncmoney(), datasBean.getUnitprice());
//                    }
//                }
//            }
//        }
//
//        lastTimeChangeNumber = number;
//
//        maxNumberArray.clear();
//    }

//    private void removeFoodModifify(OrderDetail myorder) {
//        int endIndex = myorder.getMy_index() - 1;
//        for (int i = orderFoodList.size() - 1; i > endIndex; i--) {
//            OrderDetail foodBean = orderFoodList.get(i);
//            if (foodBean.getProp().equalsIgnoreCase("modifify_modalkeyboard") && foodBean.getParent_index() == myorder.getMy_index()) {
//                //删除当前食品下的所有子项
//                orderFoodList.remove(i);
//            }
//        }
//    }

//    public void Re_Discount() {
//        if (orderFoodList.size() == 0) {
//            return;
//        }
//        try {
//            //计算数呈
//            if (this.selstartpostion > orderFoodList.size()) {
//                return;
//            }
//
//            OrderDetail mainorder = orderFoodList.get(this.selstartpostion);
//            for (int i = selstartpostion; i < orderFoodList.size(); i++) {
//                OrderDetail childOrder = orderFoodList.get(i);
//                if (childOrder.getItemtype() == 0) {
//                    break;
//                }
//                if (childOrder.comid.equals("discount")) {
//                    childOrder.setAmount_price(childOrder.getUnitprice());
//                    childOrder.Famount_price_sign = childOrder.getUnitprice() / childOrder.newnumber;
//                }
//            }
//            //从selstartpostion开始向下查找到全部的归属于本茶品的最后位置
//            DiscountTypeAndOrderPay.DatasBean dt = new DiscountTypeAndOrderPay.DatasBean();
//            dt.setSelStartPostion(this.selstartpostion);
//            dt.setSelEndPostion(this.selendpostion);
//            dt.recalc_doubleandfixed6(orderFoodList);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 检查当前食品有没有超过数量或者已经沽清
     *
     * @param fid
     * @param isfood
     * @param number
     * @return
     */
//    public boolean checkOverSoldoutNumber(String fid, boolean isfood, int number) {
//        int newNumber = 0;
//        //检查是否需要改变估清
//        CurrentSoldoutFood.DatasBean scClass;
//        Db_SoldoutCtrl db = new Db_SoldoutCtrl(context);
//        if (isfood) {
//            scClass = db.getSoldoutCtrl("F", fid);
//        } else {
//            scClass = db.getSoldoutCtrl("M", fid);
//        }
//        db.closeDb();
//        db = null;
//
//        if (scClass.OR_Qty == -999999999 && scClass.Used_Qty == -999999999) {
//            //不需要判读数量
//            return true;
//        } else {
//            newNumber = getCountNumber(fid);
//            //需要判读数量，如果当前数量超过，直接返回估清
//            if (scClass.OR_Qty - scClass.Used_Qty - newNumber - number < 0) {
//                return false;
//            } else {
//                return true;
//            }
//        }
//    }

    /*查找并计算碰餐，计算出的结果：如果是已经存在的，则需要记录此单新金额*/

    /*得到当前的碰餐折扣表，从高到低的折扣金额排列*/

    /*获取食品细项，把该食品与其它任意食品B依金额碰，如果B尚未碰过，则两者相碰成功*/

    /*检查到B食品时，向下碰C食品，如果C食品碰的金额大于*/
//    public boolean calc_Mix(int operatType) {
//        int max_mixIndex = 0;
//        MixOldOrders.clear();
//
//        //获得碰餐有效数据(已经数据库中对status=false和日期不在起始日期内 或者不在假期内的数据进行了屏蔽并且已经是取到了最优的碰餐结果了)
//        Db_MixComboGroup_Discount db_md = new Db_MixComboGroup_Discount(context);// this.activity);
//        mixComboGroup_Discounts = db_md.get_valid_discount();
//        db_md.closeDb();
//
//        if (mixComboGroup_Discounts.size() == 0) {
//            return false;
//        }
//
//        int n = 0;
//        for (OrderDetail order : orderFoodList) {
//            if (order.getItemtype() == 1) {
////                n = n + order.newnumber;
//                n = n + 1;
//                order.setMix_DETAIL_ID(0);
//                order.setMixGroupDiscPrice(0);
//                order.setMixDesc1("");
//                order.setMixDesc2("");
//                order.setMixDesc3("");
//                order.setMixQty(0);
//                order.setMixGroupID(0);
//            }
//        }
//        /*只有一个食品，则退出*/
////        if (operatType == ADD_OPERAT && n <= 1) {
////            return false;
////        }
//
//        /*删除后食品必须大于一，否则则退出*/
//        boolean allowDelete = operatType == DELETE_OPERAT && n > 1;
//
//        if (operatType == ADD_OPERAT || allowDelete) {
//            Db_MixComboGroup db_maxgroup = new Db_MixComboGroup(GSAApplication.getAppContext());
//            for (MixComboGroupDiscount.DatasBean mcgd : mixComboGroup_Discounts) {
//
//                boolean findgroup1;
//                boolean findgroup2;
//
//                OrderDetail findorder1;
//                OrderDetail findorder2;
//
//                List<MixComboGroup.DatasBean> mcgroups1 = db_maxgroup.query_byFood(mcgd.getGROUPID1());
//                List<MixComboGroup.DatasBean> mcgroups2 = db_maxgroup.query_byFood(mcgd.getGROUPID2());// + " and status=1 order by ISNULL(SEQ,-1),f.Price desc,g.ID,id ");
//
//                for (int i = 0; i < mcgroups1.size(); i++) {
//                    MixComboGroup.DatasBean mcgroup1 = mcgroups1.get(i);
//                    //加个循环，保证找完所有的order
//                    for (OrderDetail morder : orderFoodList) {
//                        findgroup1 = false;
//                        findgroup2 = false;
//                        findorder1 = null;
//                        findorder2 = null;
//                        int recordIndex = -1;
//                        for (int k = 0; k < orderFoodList.size(); k++) {
//                            OrderDetail order = orderFoodList.get(k);
//                            String desc1 = order.getDesc1();
//                            if (order.getItemtype() != 1 || !order.isIsfood()) {
//                                continue;
//                            }
//                            //非食品则不操作
//                            int qty = order.getQty();
//                            int mixQty = order.getMixQty();
//                            if (qty <= 0 || qty <= mixQty) {
//                                continue;
//                            }
//                            String fid1 = order.getItemno();
//                            String fid2 = mcgroup1.getFID();
//                            if (fid1.equals(fid2)) {
//                                findgroup1 = true;
//                                findorder1 = order;
//                                recordIndex = k;
//                                break;
//                            }
//                        }
//                        if (findgroup1 == false) {
//                            continue;
//                        } //没有找到就跳下一个
//
//                        for (int k = 0; k < mcgroups2.size(); k++) {
//                            MixComboGroup.DatasBean mcgroup2 = mcgroups2.get(k);
//                            for (int o = 0; o < orderFoodList.size(); o++) {
//                                OrderDetail order = orderFoodList.get(o);
//                                String desc1 = order.getDesc1();
//                                if (order.getItemtype() != 1 || !order.isIsfood()) {
//                                    continue;
//                                } //非食品则不操作
//                                int qty = order.getQty();
//                                int mixQty = order.getMixQty();
//                                if (qty <= 0 || qty <= mixQty) {
//                                    continue;
//                                }
//                                if (recordIndex == o) {
//                                    //该食品已经碰餐了
//                                    continue;
//                                }
//                                String fid1 = order.getItemno();
//                                String fid2 = mcgroup2.getFID();
//                                if (fid1.equals(fid2)) {
//                                    findgroup2 = true;
//                                    findorder2 = order;
//                                    break;
//                                }
//                            }
//                            if (findgroup2 == true) {
//                                break;
//                            }
//                        }
//                        if (findgroup2 == false) {
//                            continue;
//                        }
//
//                        max_mixIndex = max_mixIndex + 1;
//
//                        int Mix_DETAIL_ID1 = findorder1.getMix_DETAIL_ID() + max_mixIndex;
//                        int Mix_DETAIL_ID2 = findorder2.getMix_DETAIL_ID() + max_mixIndex;
//                        findorder1.setMix_DETAIL_ID(Mix_DETAIL_ID1);
//                        findorder2.setMix_DETAIL_ID(Mix_DETAIL_ID2);
//                        /*开始设置这两个食品的碰餐属性*/
//                        /*数量算法,如果限制了数量，则最高为限制数量，如果没有限制，则使用最小的可以折扣的数量*/
//                        int findorder1MixNumber = db_maxgroup.get_mixqty(findorder1.getItemno(), mcgd.getGROUPID1());
//                        int findorder2MixNumber = db_maxgroup.get_mixqty(findorder2.getItemno(), mcgd.getGROUPID2());
//                        //最后需要更新的数量
//                        int nowMixQty = 0;
//                        //得到最小的剩余数量
//                        int mixqty = findorder1.getQty() - findorder1.getMixQty() > findorder2.getQty() - findorder2.getMixQty() ?
//                                findorder2.getQty() - findorder2.getMixQty() : findorder1.getQty() - findorder1.getMixQty();
//
//                        if (findorder1MixNumber < 1 || findorder2MixNumber < 1) {
//                            //有一个有无限数量或者两个都是无限数量的情况
//                            if (findorder1MixNumber + findorder2MixNumber <= 0) {
//                                //两个都是无限数量，此时取最小的数量
//                                nowMixQty = mixqty;
//                            } else if (findorder1MixNumber > 1) { //表明2号食品无限制，1号食品有限制
//                                //食品1有限制数量:
//                                // 食品1总数量-碰餐1数量即剩下的数量大于MixNumber，取最小的剩余数量。
//                                //                          小于
//                                nowMixQty = mixqty > findorder1MixNumber ? findorder1MixNumber : mixqty;
//                            } else { //2号食品有限制 1号食品无限制
//                                nowMixQty = mixqty > findorder2MixNumber ? findorder2MixNumber : mixqty;
//                            }
//
//                        } else {
//                            //两个都有限制,取最小值
//                            if (findorder1MixNumber > findorder2MixNumber) {
//                                nowMixQty = mixqty > findorder2MixNumber ? findorder2MixNumber : mixqty;
//                            } else {
//                                nowMixQty = mixqty > findorder1MixNumber ? findorder1MixNumber : mixqty;
//                            }
//                        }
//
//                        int mixQty1 = findorder1.getMixQty();
//                        findorder1.setMixQty(mixQty1 + nowMixQty);
//
//                        double GROUP1Price = mcgd.getGROUP1Price();
//                        if (GROUP1Price > findorder1.getFamount_price_sign()) {
//                            //碰餐金額大於 食品總金額，直接減去原因金額
//                            GROUP1Price = findorder1.getFamount_price_sign();
//                        }
//                        findorder1.setMixGroupDiscPrice(findorder1.getMixGroupDiscPrice() + GROUP1Price * nowMixQty);
//                        findorder1.setMixGroupID(mcgd.getGROUPID1());
//                        findorder1.setMixDesc1(mcgd.getDesc1());
//                        findorder1.setMixDesc2(mcgd.getDesc2());
//                        findorder1.setMixDesc3(mcgd.getDesc3());
//
//                        double GROUP2Price = mcgd.getGROUP2Price();
//                        if (GROUP2Price > findorder2.getFamount_price_sign()) {
//                            //碰餐金額大於 食品總金額，直接減去原因金額
//                            GROUP2Price = findorder2.getFamount_price_sign();
//                        }
//
//                        int mixQty2 = findorder2.getMixQty();
//                        findorder2.setMixQty(mixQty2 + nowMixQty);
////                        findorder2.setMixGroupDiscPrice(findorder2.getMixGroupDiscPrice() + mcgd.getGROUP2Price() * nowMixQty);
//                        findorder2.setMixGroupDiscPrice(findorder2.getMixGroupDiscPrice() + GROUP2Price * nowMixQty);
//                        findorder2.setMixGroupID(mcgd.getGROUPID2());
//                        findorder2.setMixDesc1(mcgd.getDesc1());
//                        findorder2.setMixDesc2(mcgd.getDesc2());
//                        findorder2.setMixDesc3(mcgd.getDesc3());
//                    }
//                }
//            }
//            db_maxgroup.closeDb();
//        }
//
//        //2:更新各项数据
//        for (OrderDetail order : orderFoodList) {
//            if (order.getItemtype() != 1) {
//                continue;
//            }
//            if (!order.isIsfood()) {
//                continue;
//            }
//            if (order.getQty() <= 0) {
//                continue;
//            }
//            if (order.getOrder_detail_id() > -1) {//是已经保存过的
//                MixOldOrder moo = new MixOldOrder();
//                moo.Mix_DETAIL_ID = order.getMix_DETAIL_ID();
//                moo.Mixqty = order.getMixQty();
//                moo.ORDER_DETAIL_ID = order.getOrder_detail_id();
//                moo.MixDesc1 = order.getMixDesc1();
//                moo.MixDesc2 = order.getMixDesc2();
//                moo.MixDesc3 = order.getMixDesc3();
//                moo.MixGroupDiscPrice = order.getMixGroupDiscPrice();
//                moo.MixGroupID = order.getMixGroupID();
//                MixOldOrders.add(moo);
//            }
//        }
//        for (OrderDetail order : orderFoodList) {
//            int fnumber = order.getQty();
//            double famount_price_sign = 0.0;
//            if (!order.isnew) {
//                famount_price_sign = order.Famount_price_sign / fnumber;
//            } else {
//                famount_price_sign = order.Famount_price_sign;
//            }
//            double mixGroupDiscPrice = order.getMixGroupDiscPrice();
//            double amount_price = famount_price_sign * fnumber - Math.abs(mixGroupDiscPrice);
//            if (amount_price < 0 && order.getDiscount_id() == 0) {
//                //排除掉折扣，食品金额不能为负数
//                order.setAmount_price(0);
//            } else {
//                if (order.getDiscount_id() != 0) {
//                    updateDiscoutMoney(order);
//                } else {
//                    order.setAmount_price(amount_price);
//                }
//            }
//
//            LogUtil.d("mix_detail_id:  " + order.getMix_DETAIL_ID() + "  amount_price:  " + order.getAmount_price());
//        }
//        return true;
//    }

//    /**
//     * 更新折扣金額
//     *
//     * @param order
//     */
//    public void updateDiscoutMoney(OrderDetail order) {
//        DiscountTypeAndOrderPay.DatasBean discountBean = order.getDiscountTypeAndOrderPayBean();
//        if (discountBean != null) {
//            discountBean.setSelPostion(selectMealAdapter.getSelect_position());
//            discountBean.setSelEndPostion(selectMealAdapter.getSelect_end_position());
//            discountBean.setSelStartPostion(selectMealAdapter.getSelect_start_position());
//            discountBean.setSelMainStartPositon(selectMealAdapter.getMainStartPos());
//            discountBean.setSelMainEnnPostion(selectMealAdapter.getMainEndPos());
//            discountBean.setSelect_Full_start_position(selectMealAdapter.getSelect_full_start_position());
//            discountBean.setSelect_Full_end_position(selectMealAdapter.getSelect_full_end_position());
//            discountBean.setUpdateDiscoutMoney(true);
//            double discountMoney = discountBean.Add_menu_postion(orderFoodList, context);
//            order.setAmount_price(discountMoney);
//        }
//    }

//    public boolean hasMixFood() {
//        for (int i = 0; i < orderFoodList.size(); i++) {
//            OrderDetail datasBean = orderFoodList.get(i);
//            if (datasBean.getMixGroupID() > 0) {
//                return true;
//            }
//        }
//        return false;
//    }

//    public void get_max_seqno() {
//        int i = 1;
//        for (OrderDetail myorder : orderFoodList) {
//            if (myorder.getItemtype() == 1) {
//                i = i + 1;
//            }
//        }
//        seqno = i;
//    }
//
//    private int get_sseq() {
//        int i = 0;
//        if (orderFoodList == null) {
//            return i;
//        }
//        for (OrderDetail myorder : orderFoodList) {
//            if (myorder.getSeqno() == seqno && myorder.getItemtype() == 2) {
//                i = i + 1;
//            }
//        }
//        return i;
//    }
//
//    private int get_mseq(int isseq) {
//        int i = 0;
//        if (orderFoodList == null) {
//            return i;
//        }
//        for (OrderDetail myorder : orderFoodList) {
//            if (myorder.getSeqno() == seqno && myorder.getSseq() == isseq && myorder.getItemtype() == 3) {
//                i = i + 1;
//            }
//        }
//        return i;
//    }
//
//    public void no_select() {
//        for (int i = 0; i < orderFoodList.size(); i++) {
//            orderFoodList.get(i).selected = 0;
//        }
//    }
//
//    public DatasBean getCurrentOrderFood() {
//        if (getSize() == 0) {
//            return null;
//        }
//        if (selpostion < orderFoodList.size()) {
//            return orderFoodList.get(selpostion);
//        } else {
//            return orderFoodList.get(orderFoodList.size() - 1);
//        }
//    }
//
//    private int getCountNumber(String fid) {
//        int number = 0;
//        for (OrderDetail or : orderFoodList) {
//            if (or.getItemno().equals(fid)) {
//                number = number + or.newnumber;
//            }
//        }
//        return number;
//    }
//

    /**
     * 设置所有价格相关字段数据
     * Qty、Itemtype、Itemno、Advprice、MixGroupDiscPrice字段需提前设置值
     * <p>
     * //     * @param datasBean
     * //     * @param isFuncmoney
     * //     * @param Money
     */
    public void setCalculatePrice(OrderDetail datasBean) {
        double money = datasBean.getUnit_price();
//        if (datasBean.getItemtype() == 1 && isFuncmoney == false && datasBean.getAdvprice() != 2) {
////            money = getMoney(datasBean.getItemno(), datasBean.getQty(), 0, new Date());
//        } else {
//            money = Money;
//            datasBean.manualMoney = money;
//            datasBean.isManualMoney = true;
//        }
        datasBean.setUnit_price(money);
        datasBean.setPrice(money * datasBean.getNumber());
    }

    //    /**
//     * 主食品转换
//     *
//     * @param datasBean
//     * @return
//     */
//    public OrderDetail orderConvertOrderDetail(Food datasBean, int Qty, boolean isFuncmoney) {
//        OrderDetail orderDetail = new OrderDetail();
//        orderDetail.setDesc1(datasBean.getFoodName());
//        orderDetail.setDesc2(datasBean.getFoodName());
//        orderDetail.setDesc3(datasBean.getFoodName());
//        orderDetail.setName(datasBean.getFoodName());
//        orderDetail.setItemno(datasBean.getId()+"");
//        orderDetail.setFTID(datasBean.getTopID());
//        orderDetail.setIsfood(true);
//        orderDetail.setIsnew(true);
//        orderDetail.setQty(Qty);
//        orderDetail.setNewnumber(Qty);
//        orderDetail.setItemtype(1);
//        orderDetail.setProp("food");
//        orderDetail.setModid("");
//        orderDetail.setUser_Qty(datasBean.getOR_Qty());
//        orderDetail.setPointsadd(datasBean.getPointsAdd());
//        orderDetail.setPointsRedeem(datasBean.getPointsRedeem());
//        orderDetail.setPointsrote(datasBean.getPointsRatio());
//        orderDetail.setD_able(datasBean.isAblediscount());
//        orderDetail.setSc_able(datasBean.isServercharge());
//        orderDetail.setAutomod(datasBean.isAutoMod() ? 1 : 0);
//        orderDetail.setAdvprice(datasBean.getAdvPrice());
//        orderDetail.setUnit_id(datasBean.getUnit_id());
//        orderDetail.setFuncmoney(isFuncmoney);
//
//        setCalculatePrice(orderDetail, isFuncmoney, datasBean.getPrice());
//
//        //设置seq
//        sseq = get_sseq();
//        mseq = get_mseq(sseq);
//        orderDetail.setSeqno(seqno);
//        orderDetail.setSseq(sseq);
//        orderDetail.setMseq(mseq);
//
//        int comboLevel = datasBean.isComboFood() ? 1 : 0;
//        orderDetail.setComboLevel(comboLevel);
//        orderDetail.setSelected(2);
//        return orderDetail;
//    }
//
//    /**
//     * 子食品转换
//     *
//     * @param datasBean
//     * @param prop
//     * @return
//     */
//    public OrderDetail comboConvertOrderDetail(ComboItem.DatasBean datasBean, String prop) {
//        OrderDetail orderDetail = new OrderDetail();
//        orderDetail.setDesc1(datasBean.getDesc1());
//        orderDetail.setDesc2(datasBean.getDesc2());
//        orderDetail.setDesc3(datasBean.getDesc3());
//        orderDetail.setItemno(datasBean.getFID());
//        orderDetail.setFTID(datasBean.getFID());
//        orderDetail.setComid(datasBean.getComID());
//        orderDetail.setIsfood(true);
//        orderDetail.setIsnew(true);
//        orderDetail.setQty(datasBean.getQty());
//        orderDetail.setNewnumber(datasBean.getQty());
//        orderDetail.setItemtype(2);
//        orderDetail.setD_able(datasBean.isAblediscount());
//        orderDetail.setComboAuto(datasBean.isAutomode());
//        if (prop.equals("comboitem_auto")) {
//            orderDetail.isComboAuto = true;
//        }
//        orderDetail.ComboLevel = 2;
//        orderDetail.limitQty = datasBean.getQty();
//        orderDetail.setProp(prop);
//
//        int order_detail_id = -1;
//        if (MyOrderManage.getInstance().getCurrentOrderFood() != null) {
//            order_detail_id = MyOrderManage.getInstance().getCurrentOrderFood().getOrder_detail_id();
//        }
//        orderDetail.setOrder_detail_id(order_detail_id);
//
//        setCalculatePrice(orderDetail, false, datasBean.getDiffAmt());
//
//        //设置seq
//        sseq = get_sseq();
//        mseq = get_mseq(sseq);
//        orderDetail.setSeqno(seqno);
//        orderDetail.setSseq(sseq);
//        orderDetail.setMseq(mseq);
//
//        if (presenter.isRvMealClicked()) {
//            //手动点击细项 才将子食品作为主体被选中
//            orderDetail.setSelected(2);
//        } else {
//            orderDetail.setSelected(1);
//        }
//        return orderDetail;
//    }
//
//    public OrderDetail modifierConvertOrderDetail(Modifier.DatasBean datasBean) {
//        OrderDetail orderDetail = new OrderDetail();
//        orderDetail.setDesc1(datasBean.getDesc1());
//        orderDetail.setDesc2(datasBean.getDesc2());
//        orderDetail.setDesc3(datasBean.getDesc3());
//        orderDetail.setName(datasBean.getName());
////        orderDetail.setItemno(datasBean.getFid());
//        orderDetail.setItemno(datasBean.getMID());
//        orderDetail.setFTID(datasBean.getTopID());
//        orderDetail.setIsfood(false);
//        orderDetail.setIsnew(true);
//        orderDetail.setQty(1);
//        orderDetail.setNewnumber(1);
//        orderDetail.setItemtype(3);
//        orderDetail.setProp("modifify");
//        orderDetail.setModid(datasBean.getMID());
//        orderDetail.setUser_Qty(datasBean.getOR_Qty());
//        orderDetail.setD_able(datasBean.isAblediscount());
//
//        int order_detail_id = -1;
//        if (MyOrderManage.getInstance().getCurrentOrderFood() != null) {
//            order_detail_id = MyOrderManage.getInstance().getCurrentOrderFood().getOrder_detail_id();
//        }
//        orderDetail.setOrder_detail_id(order_detail_id);
//
//        setCalculatePrice(orderDetail, false, datasBean.getPrice());
//
//        //手动点击细项 才将子食品作为主体被选中
//        orderDetail.setSelected(2);
//
//        return orderDetail;
//    }
//
//    public OrderDetail MyButtonMealModifDetailConvertOrderDetail(MyButtonMealModifDetail datasBean) {
//        OrderDetail orderDetail = new OrderDetail();
//        orderDetail.setDesc1(datasBean.name1);
//        orderDetail.setDesc2(datasBean.name2);
//        orderDetail.setDesc3(datasBean.name3);
//        orderDetail.setItemno(datasBean.mid);
//        orderDetail.setItemNo(datasBean.parentid);
//        orderDetail.setFTID(datasBean.topid);
//        orderDetail.setIsfood(false);
//        orderDetail.setIsnew(true);
//        orderDetail.setQty(1);
//        orderDetail.setNewnumber(1);
//        orderDetail.setItemtype(3);
//        orderDetail.setProp("modifify_modalkeyboard");
//        orderDetail.setModid(datasBean.mid);
//        orderDetail.setD_able(datasBean.ablediscount);
//
//        int order_detail_id = -1;
//        if (MyOrderManage.getInstance().getCurrentOrderFood() != null) {
//            order_detail_id = MyOrderManage.getInstance().getCurrentOrderFood().getOrder_detail_id();
//        }
//        orderDetail.setOrder_detail_id(order_detail_id);
//
////        orderDetail.setOrder_detail_id(datasBean.order_detail_id);
//
//        setCalculatePrice(orderDetail, false, datasBean.money);
//
//        //设置seq
//        sseq = get_sseq();
//        mseq = get_mseq(sseq);
//        orderDetail.setSeqno(seqno);
//        orderDetail.setSseq(sseq);
//        orderDetail.setMseq(mseq);
//
////        if (datasBean.name1.length() > 0) {
////            orderDetail.setUnit_id(datasBean.order_detail_id);
////            add_menu_bmd(bmd_advpriceIs2, bmd_advpriceIs2.get_Fname() + "(" + Title.trim() + ")", money, unitid);
////        }
//        return orderDetail;
//    }
//
//    public MyButtonMealModifDetail modifierConvertMyButtonMealModifDetail(Modifier.DatasBean datasBean) {
//        MyButtonMealModifDetail buttonMealModifDetail = new MyButtonMealModifDetail(context, GSAApplication.androidSetting.getOrderNumberChildShowSize(), GSAApplication.androidSetting.getOrderNumberChildFontSize());
//        buttonMealModifDetail.name1 = datasBean.getDesc1();
//        buttonMealModifDetail.name2 = datasBean.getDesc2();
//        buttonMealModifDetail.name3 = datasBean.getDesc3();
//        buttonMealModifDetail.maxQty = datasBean.getMaxqty();
//        buttonMealModifDetail.minQty = datasBean.getMinqty();
//        buttonMealModifDetail.mid = datasBean.getMID();
//        buttonMealModifDetail.money = datasBean.getPrice();
//
//        buttonMealModifDetail.set_Fname(AndroidSettingUtils.getDataLanguage(datasBean.getDesc1(), datasBean.getDesc2(), datasBean.getDesc3()));
//        buttonMealModifDetail.topid = datasBean.getTopID();
//        buttonMealModifDetail.isParent = datasBean.isParent() ? 1 : 0;
//        buttonMealModifDetail.parentid = datasBean.getFid();
//        buttonMealModifDetail.mod_Comm = datasBean.isMod_comm() ? 1 : 0;
//        buttonMealModifDetail.mod_msg = datasBean.isMod_msg() ? 1 : 0;
//        buttonMealModifDetail.multiple = datasBean.isMod_taste() ? 1 : 0;
//        buttonMealModifDetail.visible = datasBean.isVisible() ? 1 : 0;
//        buttonMealModifDetail.ModKeyboard = datasBean.isModKeyboard();
//        buttonMealModifDetail.ablediscount = datasBean.isAblediscount();
//        int order_detail_id = -1;
//        if (MyOrderManage.getInstance().getCurrentOrderFood() != null) {
//            order_detail_id = MyOrderManage.getInstance().getCurrentOrderFood().getOrder_detail_id();
//        }
//        buttonMealModifDetail.order_detail_id = order_detail_id;
//
//        return buttonMealModifDetail;
//    }
//
//    public Modifier.DatasBean MyButtonMealModifDetailConvertModifier(MyButtonMealModifDetail datasBean) {
//
//        Modifier.DatasBean modifier = new Modifier.DatasBean();
//        modifier.setDesc1(datasBean.name1);
//        modifier.setDesc2(datasBean.name2);
//        modifier.setDesc3(datasBean.name3);
//        modifier.setMaxqty(datasBean.maxQty);
//        modifier.setMinqty(datasBean.minQty);
//        modifier.setMID(datasBean.mid);
//        modifier.setPrice(datasBean.money);
//
//        modifier.setTopID(datasBean.topid);
//        modifier.setIsParent(datasBean.isParent == 1 ? true : false);
//        modifier.setFid(datasBean.parentid);
//        modifier.setMod_comm(datasBean.mod_Comm == 1 ? true : false);
//        modifier.setMod_msg(datasBean.mod_msg == 1 ? true : false);
//
//        modifier.setMultiple(datasBean.multiple);
//        modifier.setVisible(datasBean.visible == 1 ? true : false);
//        modifier.setModKeyboard(datasBean.ModKeyboard);
//        modifier.setAblediscount(datasBean.ablediscount);
//
//        return modifier;
//    }
//
//    public double getMoney(String fid, int number, int unit, Date needCalcdate) {
//        double money = 0.00;
//        Db_Food_Price dfp = new Db_Food_Price(context);
//        money = dfp.getMoney(fid, unit, number, needCalcdate);
//        dfp.closeDb();
//        return money;
//    }

    public int getSize() {
        return orderFoodList != null ? orderFoodList.size() : 0;
    }

    public void close() {
        if (orderFoodList != null) {
            orderFoodList.clear();
        }
//        if (comboOrders != null) {
//            comboOrders.clear();
//        }
//        if (MixOldOrders != null) {
//            MixOldOrders.clear();
//        }
//        if (mixComboGroup_Discounts != null) {
//            mixComboGroup_Discounts.clear();
//        }
//
//        if (MixComboGroups != null) {
//            MixComboGroups.clear();
//        }
//        allowAddAutoDiscount = false;
//        MixComboGroups = null;
    }

    public void clear() {
        if (orderFoodList != null) {
            orderFoodList.clear();
        }
        if (newFoodList != null) {
            newFoodList.clear();
        }
        if (orderMoneyList != null) {
            orderMoneyList.clear();
        }
        if (billMoney != null) {
            billMoney.clear();
        }
        orderBean = null;
        modifyOrder = false;
    }

    public boolean isOverMaxQty() {
        return overMaxQty;
    }

    public void setOverMaxQty(boolean overMaxQty) {
        this.overMaxQty = overMaxQty;
    }

    public int getLanguageindex() {
        return Languageindex;
    }

    public void setLanguageindex(int languageindex) {
        Languageindex = languageindex;
    }

    public boolean isEdit_Send() {
        return Edit_Send;
    }

    public void setEdit_Send(boolean edit_Send) {
        Edit_Send = edit_Send;
    }

    public int getSelpostion() {
        return selpostion;
    }

    public void setSelpostion(int selpostion) {
        this.selpostion = selpostion;
    }

    public int getSelstartpostion() {
        return selstartpostion;
    }

    public void setSelstartpostion(int selstartpostion) {
        this.selstartpostion = selstartpostion;
    }

    public int getSelendpostion() {
        return selendpostion;
    }

    public void setSelendpostion(int selendpostion) {
        this.selendpostion = selendpostion;
    }

    public int getSelmainstartpositon() {
        return selmainstartpositon;
    }

    public void setSelmainstartpositon(int selmainstartpositon) {
        this.selmainstartpositon = selmainstartpositon;
    }

    public int getSelmainendpostion() {
        return selmainendpostion;
    }

    public void setSelmainendpostion(int selmainendpostion) {
        this.selmainendpostion = selmainendpostion;
    }

    public int getSelect_Full_start_position() {
        return select_Full_start_position;
    }

    public void setSelect_Full_start_position(int select_Full_start_position) {
        this.select_Full_start_position = select_Full_start_position;
    }

    public int getSelect_Full_end_position() {
        return select_Full_end_position;
    }

    public void setSelect_Full_end_position(int select_Full_end_position) {
        this.select_Full_end_position = select_Full_end_position;
    }

    public static OrderDetails.DataBean getDataBean() {
        return dataBean;
    }

    public static void setDataBean(OrderDetails.DataBean dataBean) {
        MyOrderManage.dataBean = dataBean;
    }

    public int getSeqno() {
        return seqno;
    }

    public void setSeqno(int seqno) {
        this.seqno = seqno;
    }

    public int getSseq() {
        return sseq;
    }

    public void setSseq(int sseq) {
        this.sseq = sseq;
    }

    public int getMseq() {
        return mseq;
    }

    public void setMseq(int mseq) {
        this.mseq = mseq;
    }

    public List<OrderDetail> getOrderFoodList() {
        if (orderFoodList == null) {
            orderFoodList = new ArrayList<>();
        }
        return orderFoodList;
    }

    public void setOrderFoodList(List<OrderDetail> orderFoodList) {
        this.orderFoodList = orderFoodList;
    }

    public List<BillOrderMoney> getOrderMoneyList() {
        if (orderMoneyList == null) {
            orderMoneyList = new ArrayList<>();
        }
        return orderMoneyList;
    }

    public void setOrderMoneyList(List<BillOrderMoney> orderMoneyList) {
        this.orderMoneyList = orderMoneyList;
    }

    //
//    public List<DatasBean> getComboOrders() {
//        return comboOrders;
//    }
//
//    public void setComboOrders(List<DatasBean> comboOrders) {
//        this.comboOrders = comboOrders;
//    }
//
//    public List<MixOldOrder> getMixOldOrders() {
//        return MixOldOrders;
//    }
//
//    public void setMixOldOrders(List<MixOldOrder> mixOldOrders) {
//        MixOldOrders = mixOldOrders;
//    }
//
//    public List<MixComboGroupDiscount.DatasBean> getMixComboGroup_Discounts() {
//        return mixComboGroup_Discounts;
//    }
//
//    public void setMixComboGroup_Discounts(List<MixComboGroupDiscount.DatasBean> mixComboGroup_Discounts) {
//        this.mixComboGroup_Discounts = mixComboGroup_Discounts;
//    }
//
//    public List<MixComboGroup.DatasBean> getMixComboGroups() {
//        return MixComboGroups;
//    }
//
//    public void setMixComboGroups(List<MixComboGroup.DatasBean> mixComboGroups) {
//        MixComboGroups = mixComboGroups;
//    }
//
//    public void setContext(Activity context) {
//        this.context = context;
//    }
//
//    public void setPresenter(MealStandPresenter presenter) {
//        this.presenter = presenter;
//    }
//
//    public void setSelectMealAdapter(SelectMealAdapter selectMealAdapter) {
//        this.selectMealAdapter = selectMealAdapter;
//    }

    public boolean isModifyOrder() {
        return modifyOrder;
    }

    public void setModifyOrder(boolean modifyOrder) {
        this.modifyOrder = modifyOrder;
    }

    public List<PayMethod> getBillMoney() {
        return billMoney;
    }

    public void setBillMoney(List<PayMethod> billMoney) {
        this.billMoney = billMoney;
    }

    public List<OrderDetail> getNewFoodList() {
        return newFoodList;
    }

    public void setNewFoodList(List<OrderDetail> newFoodList) {
        this.newFoodList = newFoodList;
    }
}
