// -----------------------------------------------------
  // HELP METHOD
  // -----------------------------------------------------
  private void cashoutLogic(
      Context respCtx,
      CashoutPass cashoutpass,
      String cashoutOperatorid,
      BigDecimal cashoutAmount,
      int transType)
      throws ApplicationException {
    GsonCashOutOperator gcoo = new GsonCashOutOperator();
    // check account whether or not sufficient
    Operator operator = this.operatorDao.findById(Operator.class, cashoutOperatorid);
    if (operator == null) {
      throw new ApplicationException(
          SystemException.CODE_NO_OPERATOR,
          "[Cashout] operator(id=" + cashoutOperatorid + ") doesn't exist.");
    }

    // check cash out operator id whether equals respCtx operator id , should not be the same
    if (cashoutOperatorid.equals(respCtx.getOperatorId())) {
      throw new ApplicationException(
          SystemException.CODE_CASHOUT_OPERATOR_SHOULD_NOT_SAME,
          "[Cashout] operator(id=" + cashoutOperatorid + ") should not the same with Operator.");
    }

    if (operator.getCreditType() == Merchant.CREDIT_TYPE_DEFINITIVEVALUE) {
      // check operator balance
      BigDecimal totalBalance = new BigDecimal("0");
      totalBalance = totalBalance.add(operator.getCommisionBalance());
      totalBalance = totalBalance.add(operator.getPayoutCreditLevel());
      totalBalance = totalBalance.add(operator.getCashoutBalance());

      if (logger.isDebugEnabled()) {
        logger.debug(
            "[Cashout] current operator :operator_id:"
                + operator.getId()
                + ",totalbalance="
                + totalBalance.toPlainString());
      }

      // judge the balance whether or not sufficient
      if (totalBalance.compareTo(cashoutAmount) < 0) {
        throw new ApplicationException(
            SystemException.CODE_INSUFFICIENT_BALANCE,
            "[Cashout] OPERATOR(id="
                + operator.getId()
                + ") insufficient balance[commission balance,cashout balance,payout balance].");
      }

      // ==============
      // deduct the money from balance order by [1)Commission balance
      // 2)Payout balance 3)Cash-out balance]
      BigDecimal commissionDeducted = new BigDecimal("0");
      BigDecimal payoutDeducted = new BigDecimal("0");
      BigDecimal cashoutDeducted = new BigDecimal("0");

      BigDecimal cashoutAmountTemp = operator.getCommisionBalance().subtract(cashoutAmount);
      if (cashoutAmountTemp.doubleValue() >= 0) {
        commissionDeducted = cashoutAmount;
      } else {
        // negative value
        if (operator.getCommisionBalance().doubleValue() < 0
            && operator.getPayoutCreditLevel().doubleValue() < 0) {
          cashoutDeducted = cashoutAmount;
        } else if (operator.getCommisionBalance().doubleValue() < 0) {
          // deducted account only (payout balance)
          if (operator.getPayoutCreditLevel().subtract(cashoutAmount).doubleValue() > 0) {
            payoutDeducted = cashoutAmount;
          } else {
            payoutDeducted = operator.getPayoutCreditLevel();
            cashoutDeducted = (cashoutAmount.subtract(payoutDeducted));
          }
        } else {
          // deducted account (commission balance + payout balance)
          if ((operator.getCommisionBalance().add(operator.getPayoutCreditLevel()))
                  .subtract(cashoutAmount)
                  .doubleValue()
              >= 0) {
            commissionDeducted = operator.getCommisionBalance();
            payoutDeducted = cashoutAmount.subtract(commissionDeducted);
          } else {
            if ((operator
                        .getCommisionBalance()
                        .add(operator.getPayoutCreditLevel())
                        .add(operator.getCashoutBalance()))
                    .subtract(cashoutAmount)
                    .doubleValue()
                >= 0) {
              commissionDeducted = operator.getCommisionBalance();
              payoutDeducted = operator.getPayoutCreditLevel();
              cashoutDeducted =
                  (cashoutAmount.subtract(commissionDeducted)).subtract(payoutDeducted);
            }
          }
        }
      }

      if (logger.isDebugEnabled()) {
        logger.debug(
            "[Cashout] current operator :operator_id:"
                + cashoutOperatorid
                + ",deducted money balance:commissionDeducted="
                + commissionDeducted.toPlainString()
                + ";payoutdeducted:"
                + payoutDeducted
                + ";cashoutdeducted:"
                + cashoutDeducted);
      }

      // set 2 decimals place
      commissionDeducted.setScale(
          MLotteryContext.getInstance().getInt(MLotteryContext.COMMISSION_BALANCE_PRECISION),
          BigDecimal.ROUND_HALF_UP);
      payoutDeducted.setScale(2, BigDecimal.ROUND_HALF_UP);
      cashoutDeducted.setScale(2, BigDecimal.ROUND_HALF_UP);
      this.operatorDao.deductBalanceByOperator(
          commissionDeducted, payoutDeducted, cashoutDeducted, cashoutOperatorid);
      // ADD BALANCE_TRANSACTION RECORD
      addBalanceTransactionRecord(
          respCtx,
          new BigDecimal(String.valueOf(cashoutAmount.doubleValue())),
          transType,
          cashoutOperatorid,
          BalanceTransactions.OWNER_TYPE_OPERATOR,
          BalanceTransactions.PAYMENT_TYPE_DEDUCTING_MONEY,
          new BigDecimal("0"),
          new BigDecimal("0"));

      // assemble the Json obj
      gcoo.setOperatorMerchantType(BalanceTransactions.OWNER_TYPE_OPERATOR);
      gcoo.setOperatorMerchantid(cashoutOperatorid);
      gcoo.setOperatorid(cashoutOperatorid);
      gcoo.setTotalAmount(cashoutAmount);
      gcoo.setCommission(commissionDeducted);
      gcoo.setPayout(payoutDeducted);
      gcoo.setCashout(cashoutDeducted);

    } else if (operator.getCreditType() == Merchant.CREDIT_TYPE_USE_PARENT) {
      // lookup the merchant
      Merchant originmerchant = getMerchantByOperator(cashoutOperatorid, false);
      Merchant finalMerchant =
          this.merchantDao.findDistributeMerchantByMerchantId(originmerchant.getId());
      if (finalMerchant == null) {
        throw new ApplicationException(
            SystemException.CODE_NO_MERCHANT,
            "operator(id=" + cashoutOperatorid + ") doesn't exist parent merchant.");
      }
      // check merchant balance
      BigDecimal totalBalance = new BigDecimal("0");
      totalBalance = totalBalance.add(finalMerchant.getCommisionBalance());
      totalBalance = totalBalance.add(finalMerchant.getCashoutBalance());
      totalBalance = totalBalance.add(finalMerchant.getPayoutCreditLevel());

      if (logger.isDebugEnabled()) {
        logger.debug(
            "current merchant :final_merchant_id:"
                + finalMerchant.getId()
                + ",totalbalance="
                + totalBalance.toPlainString());
      }

      // judge the balance whether or not sufficient
      if (totalBalance.compareTo(cashoutAmount) < 0) {
        throw new ApplicationException(
            SystemException.CODE_INSUFFICIENT_BALANCE,
            "MERCHANT(id="
                + finalMerchant.getId()
                + ") insufficient balance[commission balance,cashout balance,payout balance].");
      }

      // ==============
      // deduct the money from balance order by [1)Commission balance
      // 2)Payout balance 3)Cash-out balance]
      BigDecimal commissionDeducted = new BigDecimal("0");
      BigDecimal payoutDeducted = new BigDecimal("0");
      BigDecimal cashoutDeducted = new BigDecimal("0");

      BigDecimal cashoutAmountTemp = finalMerchant.getCommisionBalance().subtract(cashoutAmount);
      if (cashoutAmountTemp.doubleValue() >= 0) {
        commissionDeducted = cashoutAmount;
      } else {
        // negative value
        if (finalMerchant.getCommisionBalance().doubleValue() < 0
            && finalMerchant.getPayoutCreditLevel().doubleValue() < 0) {
          cashoutDeducted = cashoutAmount;
        } else if (finalMerchant.getCommisionBalance().doubleValue() < 0) {
          // deducted account only (payout balance)
          if (finalMerchant.getPayoutCreditLevel().subtract(cashoutAmount).doubleValue() > 0) {
            payoutDeducted = cashoutAmount;
          } else {
            payoutDeducted = finalMerchant.getPayoutCreditLevel();
            cashoutDeducted = (cashoutAmount.subtract(payoutDeducted));
          }
        } else {
          // deducted account (commission balance + payout balance)
          if ((finalMerchant.getCommisionBalance().add(finalMerchant.getPayoutCreditLevel()))
                  .subtract(cashoutAmount)
                  .doubleValue()
              >= 0) {
            commissionDeducted = finalMerchant.getCommisionBalance();
            payoutDeducted = cashoutAmount.subtract(commissionDeducted);
          } else {
            if ((finalMerchant
                        .getCommisionBalance()
                        .add(finalMerchant.getPayoutCreditLevel())
                        .add(finalMerchant.getCashoutBalance()))
                    .subtract(cashoutAmount)
                    .doubleValue()
                >= 0) {
              commissionDeducted = finalMerchant.getCommisionBalance();
              payoutDeducted = finalMerchant.getPayoutCreditLevel();
              cashoutDeducted =
                  (cashoutAmount.subtract(commissionDeducted)).subtract(payoutDeducted);
            }
          }
        }
      }

      if (logger.isDebugEnabled()) {
        logger.debug(
            "[Cashout] current merchant :merchant_id:"
                + finalMerchant.getId()
                + ",deducted money balance:commissionDeducted="
                + commissionDeducted.toPlainString()
                + ";payoutdeducted:"
                + payoutDeducted
                + ";cashoutdeducted:"
                + cashoutDeducted);
      }

      commissionDeducted.setScale(
          MLotteryContext.getInstance().getInt(MLotteryContext.COMMISSION_BALANCE_PRECISION),
          BigDecimal.ROUND_HALF_UP);
      payoutDeducted.setScale(2, BigDecimal.ROUND_HALF_UP);
      cashoutDeducted.setScale(2, BigDecimal.ROUND_HALF_UP);
      this.merchantDao.deductBalanceByMerchant(
          commissionDeducted, payoutDeducted, cashoutDeducted, finalMerchant.getId());

      // ADD BALANCE_TRANSACTION RECORD
      addBalanceTransactionRecord(
          respCtx,
          new BigDecimal(String.valueOf(cashoutAmount.doubleValue())),
          transType,
          String.valueOf(finalMerchant.getId()),
          BalanceTransactions.OWNER_TYPE_MERCHANT,
          BalanceTransactions.PAYMENT_TYPE_DEDUCTING_MONEY,
          new BigDecimal("0"),
          new BigDecimal("0"));

      // assemble the Json obj
      gcoo.setOperatorMerchantType(BalanceTransactions.OWNER_TYPE_MERCHANT);
      gcoo.setOperatorMerchantid(String.valueOf(finalMerchant.getId()));
      gcoo.setOperatorid(cashoutOperatorid);
      gcoo.setTotalAmount(cashoutAmount);
      gcoo.setCommission(commissionDeducted);
      gcoo.setPayout(payoutDeducted);
      gcoo.setCashout(cashoutDeducted);
    }

    // update 'CASHOUT_PASS' the field 'cashout_te_transaction_id'
    // increase the tried times add 1
    if (cashoutpass != null) {
      cashoutpass.setId(cashoutpass.getId());
      cashoutpass.setTriedTimes(cashoutpass.getTriedTimes());
      cashoutpass.setCashoutTeTransactionId(respCtx.getTransaction().getId());
      cashoutpass.setUpdateBy(respCtx.getOperatorId());
      cashoutpass.setUpdateTime(net.mpos.fk.util.DateUtils.getCurrentDate());
      this.cashoutPassDao.update(cashoutpass);
    }
    // ==========================
    // //add cash out balance & commission to self operator
    String commissionOperatorId = respCtx.getOperatorId();
    Operator commisionOperator = this.operatorDao.findById(Operator.class, commissionOperatorId);
    if (commisionOperator == null) {
      throw new ApplicationException(
          SystemException.CODE_NO_OPERATOR,
          "[CashoutByPassword] commisionOperator(id=" + commissionOperatorId + ") doesn't exist.");
    }

    // credit
    if (commisionOperator.getCreditType() == Merchant.CREDIT_TYPE_DEFINITIVEVALUE) {
      BigDecimal cashoutrate = commisionOperator.getCashoutRate();
      BigDecimal cashoutamount = cashoutAmount;
      // BigDecimal commissionAmount = cashoutamount.multiply(cashoutrate);
      BigDecimal commissionAmount =
          SimpleToolkit.mathMultiple(
              cashoutamount,
              cashoutrate,
              MLotteryContext.getInstance().getInt(MLotteryContext.COMMISSION_BALANCE_PRECISION));

      cashoutamount.setScale(2, BigDecimal.ROUND_HALF_UP);
      this.operatorDao.addCashoutAndCommissionToOperator(
          cashoutamount, commissionAmount, commissionOperatorId);

      // insert record into 'BALANCE_TRANSACTIONS'
      // ADD BALANCE_TRANSACTION RECORD
      addBalanceTransactionRecord(
          respCtx,
          cashoutAmount,
          transType,
          commissionOperatorId,
          BalanceTransactions.OWNER_TYPE_OPERATOR,
          BalanceTransactions.PAYMENT_TYPE_PLUSING_MONEY,
          commissionAmount,
          cashoutrate);
      // assemble the Json obj
      gcoo.setPlusOperatorMerchantType(BalanceTransactions.OWNER_TYPE_OPERATOR);
      gcoo.setPlusOperatorid(commissionOperatorId);
      gcoo.setPlusOperatorCashoutBalance(cashoutamount);
      gcoo.setPlusOperatorCommissionBalance(commissionAmount);
      gcoo.setPlusOperatorCommissionRate(cashoutrate);
    } else if (commisionOperator.getCreditType() == Merchant.CREDIT_TYPE_USE_PARENT) {
      // lookup the merchant
      Merchant originmerchant = getMerchantByOperator(commissionOperatorId, false);
      Merchant finalMerchant =
          this.merchantDao.findDistributeMerchantByMerchantId(originmerchant.getId());
      if (finalMerchant == null) {
        throw new ApplicationException(
            SystemException.CODE_NO_MERCHANT,
            "commission operator(id=" + commissionOperatorId + ") doesn't exist parent merchant.");
      }

      // use operator cash out rate
      BigDecimal cashoutrate = commisionOperator.getCashoutRate();
      BigDecimal cashoutamount = cashoutAmount;
      // BigDecimal commissionOperatorAmount = cashoutamount.multiply(cashoutrate);
      BigDecimal commissionOperatorAmount =
          SimpleToolkit.mathMultiple(
              cashoutamount,
              cashoutrate,
              MLotteryContext.getInstance().getInt(MLotteryContext.COMMISSION_BALANCE_PRECISION));

      // merchant cash out rate
      // BigDecimal finalcashoutrate = finalMerchant.getCashoutRate();
      // BigDecimal finalcashoutamount = cashoutAmount;
      // BigDecimal commissionMerchantAmount = finalcashoutamount.multiply(finalcashoutrate);

      cashoutamount.setScale(2, BigDecimal.ROUND_HALF_UP);
      this.merchantDao.addCashoutAndCommissionToMerchant(
          cashoutamount, null, finalMerchant.getId());
      // ADD BALANCE_TRANSACTION RECORD
      addBalanceTransactionRecord(
          respCtx,
          cashoutAmount,
          transType,
          commissionOperatorId,
          BalanceTransactions.OWNER_TYPE_OPERATOR,
          BalanceTransactions.PAYMENT_TYPE_PLUSING_MONEY,
          commissionOperatorAmount,
          cashoutrate);

      addBalanceTransactionRecord(
          respCtx,
          cashoutAmount,
          transType,
          String.valueOf(finalMerchant.getId()),
          BalanceTransactions.OWNER_TYPE_MERCHANT,
          BalanceTransactions.PAYMENT_TYPE_PLUSING_MONEY,
          new BigDecimal("0"),
          new BigDecimal("0"));

      // assemble the Json obj
      gcoo.setPlusOperatorMerchantType(BalanceTransactions.OWNER_TYPE_OPERATOR);
      gcoo.setPlusOperatorid(commissionOperatorId);
      gcoo.setPlusOperatorCashoutBalance(cashoutamount);
      gcoo.setPlusOperatorCommissionBalance(commissionOperatorAmount);
      gcoo.setPlusOperatorCommissionRate(cashoutrate);

      gcoo.setPlusOperatorMerchantType(BalanceTransactions.OWNER_TYPE_MERCHANT);
      gcoo.setPlusMerchantid(String.valueOf(finalMerchant.getId()));
      gcoo.setPlusMerchantCashoutBalance(cashoutamount);
      gcoo.setPlusMerchantCommissionBalance(new BigDecimal("0"));
      gcoo.setPlusMerchantCommissionRate(new BigDecimal("0"));
    }

    // DESTINATION_OPEATOR
    respCtx.getTransaction().setDestinationOpeator(cashoutOperatorid);
    respCtx.getTransaction().setTotalAmount(cashoutAmount);

    // write transaction message for reversal.
    // add record to te_transaction_msg
    // assemble request message use JSON
    String reqmsg = new Gson().toJson(gcoo);
    TransactionMessage msg = new TransactionMessage();
    msg.setTransactionId(respCtx.getTransaction().getId());
    msg.setRequestMsg(reqmsg);
    respCtx.getTransaction().setTransMessage(msg);
  }
  @Override
  public CashOutByOperatorPassDto cashoutOperatorByPass(
      Context respCtx, Context responseCtx, CashOutByOperatorPassDto cashoutDto)
      throws ApplicationException {
    // tried times add 1
    this.cashoutPassDao.increaseTriedTimes(cashoutDto.getBarcode());

    // lookup the 'CASHOUT_PASS' according to field 'BARCODE' & 'password'
    CashoutPass cashoutpass = this.cashoutPassDao.findByBarcode(cashoutDto.getBarcode());
    if (cashoutpass == null || cashoutpass.getId() == null || "".equals(cashoutpass.getId())) {
      // throw new ApplicationException(SystemException.CODE_CASHOUTPASS_NO_EXIST_BARCODE,
      // "[CashoutByPassword](barcode="
      // + cashoutDto.getBarcode() + ") not exist the barcode].");
      responseCtx.setResponseCode(SystemException.CODE_CASHOUTPASS_NO_EXIST_BARCODE);
      logger.error("======================================");
      logger.error(
          "[CashoutByPassword](barcode=" + cashoutDto.getBarcode() + ") not exist the barcode].");
      logger.error("======================================");
      return null;
    }

    // check whether or not used
    if (cashoutpass.getCashoutTeTransactionId() != null
        && !"".equals(cashoutpass.getCashoutTeTransactionId())) {
      // throw new ApplicationException(SystemException.CODE_CASHOUTPASS_ALREADY_USED,
      // "[CashoutByPassword](barcode="
      // + cashoutDto.getBarcode() + ") is already used].");
      responseCtx.setResponseCode(SystemException.CODE_CASHOUTPASS_ALREADY_USED);
      logger.error("======================================");
      logger.error(
          "[CashoutByPassword](barcode=" + cashoutDto.getBarcode() + ") is already used].");
      logger.error("======================================");
      return null;
    }

    // check expired time
    if (new Date().compareTo(cashoutpass.getExpireTime()) > 0) {
      // throw new ApplicationException(SystemException.CODE_CASHOUTPASS_EXPIRETIME,
      // "[CashoutByPassword] The cashout password is expiry !");
      responseCtx.setResponseCode(SystemException.CODE_CASHOUTPASS_EXPIRETIME);
      logger.error("======================================");
      logger.error("[CashoutByPassword] The cashout password is expiry !");
      logger.error("======================================");
      return null;
    }

    // check max tried times
    SysConfiguration sysConf = this.getSysConfigurationDao().getSysConfiguration();
    int maxiumtimes = sysConf.getMaxiumTimesOfCashoutPass();
    if ((cashoutpass.getTriedTimes()) > maxiumtimes) {
      // throw new ApplicationException(SystemException.CODE_CASHOUTPASS_EXCEED_MAXTIMES,
      // "[CashoutByPassword] The cashout by password is exceed max tried times !");
      responseCtx.setResponseCode(SystemException.CODE_CASHOUTPASS_EXCEED_MAXTIMES);
      logger.error("======================================");
      logger.error("[CashoutByPassword] The cashout by password is exceed max tried times !");
      logger.error("======================================");
      return null;
    }

    // check password whether or not correct
    if (!cashoutpass.getCashoutPassword().equals(cashoutDto.getPassword())) {
      // throw new ApplicationException(SystemException.CODE_CASHOUTPASS_INCORRECT,
      // "[CashoutByPassword](barcode="
      // + cashoutDto.getBarcode() + ") password is incorrect].");
      responseCtx.setResponseCode(SystemException.CODE_CASHOUTPASS_INCORRECT);
      logger.error("======================================");
      logger.error(
          "[CashoutByPassword](barcode=" + cashoutDto.getBarcode() + ") password is incorrect].");
      logger.error("======================================");
      return null;
    }

    // main logic for cash out
    cashoutLogic(
        respCtx,
        cashoutpass,
        cashoutpass.getOperatorId(),
        cashoutpass.getCashoutAmount(),
        TransactionType.CASH_OUT_OPERATOR_PASS.getRequestType());

    // ==========================

    // assemble response bean
    CashOutByOperatorPassDto respCashoutPassDto = new CashOutByOperatorPassDto();
    respCashoutPassDto.setAmount(cashoutpass.getCashoutAmount());
    respCashoutPassDto.setOperatorId(cashoutpass.getOperatorId());

    return respCashoutPassDto;
  }