package com.hfkj.schedule;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.channel.lakala.LaKaLaTradeSeparateService;
import com.hfkj.channel.lakala.LaKaLaTradeService;
import com.hfkj.channel.tianque.service.TianQueTradeService;
import com.hfkj.channel.weixin.utils.WxOrderConfig;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.entity.*;
import com.hfkj.service.*;
import com.hfkj.sysenum.*;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 交易订单定时任务
 *
 */
@Configuration
public class TradeOrderSchedule {

    private static final Logger log = LoggerFactory.getLogger(TradeOrderSchedule.class);

    @Resource
    private BsTradeOrderProfitSharingService tradeOrderProfitSharingService;
    @Resource
    private BsTradeOrderProfitSharingReceiverService tradeOrderProfitSharingReceiverService;
    @Resource
    private LaKaLaTradeSeparateService laKaLaTradeSeparateService;
    @Resource
    private BsTradeOrderService tradeOrderService;
    @Resource
    private BsTradeOrderRefundService tradeOrderRefundService;
    @Resource
    private LaKaLaTradeService laKaLaTradeService;
    @Resource
    private TianQueTradeService tianQueTradeService;
    @Resource
    private BsTradeSettleService tradeSettleService;

    /**
     * 订单发起分账
     * 5分钟扫描一次需要分账的订单
     */
    @Scheduled(cron="0 0/1 * * * ?")
    public void profitSharing() {
        List<BsTradeOrderProfitSharing> tradeOrderProfitSharingList = tradeOrderProfitSharingService.getNeedProfitSharingOrder();
        for (BsTradeOrderProfitSharing tradeOrderProfitSharing : tradeOrderProfitSharingList) {
            try {
                if (tradeOrderProfitSharing.getPlatformType().equals(PlatformTypeEnum.type1.getNumber())) {
                    // 拉卡拉分账
                    if (tradeOrderProfitSharing.getCmdType().equals(ProfitSharingCmdTypeEnum.CMD_TYPE_1.getNumber())) {
                        if (tradeOrderProfitSharing.getStatus().equals(ProfitSharingStatusEnum.status0.getNumber())) {
                            laKaLaTradeSeparateService.separate(tradeOrderProfitSharing.getProfitSharingOrderNo());

                        } else if (tradeOrderProfitSharing.getStatus().equals(ProfitSharingStatusEnum.status2.getNumber())
                            || tradeOrderProfitSharing.getStatus().equals(ProfitSharingStatusEnum.status3.getNumber())) {
                            // 查询拉卡拉分账记录
                            JSONObject queryProfitSharing = laKaLaTradeSeparateService.query(tradeOrderProfitSharing.getProfitSharingOrderNo());
                            if (queryProfitSharing != null) {
                                // ACCEPTED:已受理,PROCESSING:处理中,FAIL:失败,SUCCESS:成功,
                                //（如果分账指令后有反向操作指令，则原分账指令会变更成以下的状态之一：）
                                //CANCELING:撤销中,CANCELED:撤销成功,CANCEL_FAIL:撤销失败,FALLBACKING:回退中,FALLBACK_END:回退结束
                                String status = queryProfitSharing.getString("status");
                                if (status.equals("PROCESSING")) {
                                    tradeOrderProfitSharing.setStatus(ProfitSharingStatusEnum.status3.getNumber());
                                } else if (status.equals("ACCEPTED")) {
                                    tradeOrderProfitSharing.setStatus(ProfitSharingStatusEnum.status2.getNumber());
                                } else if (status.equals("SUCCESS")) {
                                    tradeOrderProfitSharing.setStatus(ProfitSharingStatusEnum.status1.getNumber());
                                    tradeOrderProfitSharing.setFinishTime(new Date());
                                    // 修改接收方状态
                                    List<BsTradeOrderProfitSharingReceiver> receiverList = tradeOrderProfitSharingReceiverService.getReceiverListByProfitSharingId(tradeOrderProfitSharing.getId());
                                    for (BsTradeOrderProfitSharingReceiver receiver : receiverList) {
                                        receiver.setStatus(ProfitSharingReceiverStatusEnum.status1.getNumber());
                                        tradeOrderProfitSharingReceiverService.editData(receiver);
                                    }
                                } else if (status.equals("FAIL")) {
                                    tradeOrderProfitSharing.setStatus(ProfitSharingStatusEnum.status4.getNumber());
                                }
                                tradeOrderProfitSharingService.editData(tradeOrderProfitSharing);
                            }
                        }
                    } else if (tradeOrderProfitSharing.getCmdType().equals(ProfitSharingCmdTypeEnum.CMD_TYPE_2.getNumber())) {
                        if (tradeOrderProfitSharing.getStatus().equals(ProfitSharingStatusEnum.status0.getNumber())) {
                            laKaLaTradeSeparateService.cancel(tradeOrderProfitSharing.getProfitSharingOrderNo());

                        } else if (tradeOrderProfitSharing.getStatus().equals(ProfitSharingStatusEnum.status2.getNumber())
                                || tradeOrderProfitSharing.getStatus().equals(ProfitSharingStatusEnum.status3.getNumber())) {
                            // 查询拉卡拉分账记录
                            JSONObject queryProfitSharing = laKaLaTradeSeparateService.query(tradeOrderProfitSharing.getProfitSharingOrderNo());
                            if (queryProfitSharing != null) {
                                // ACCEPTED:已受理,PROCESSING:处理中,FAIL:失败,SUCCESS:成功,
                                //（如果分账指令后有反向操作指令，则原分账指令会变更成以下的状态之一：）
                                //CANCELING:撤销中,CANCELED:撤销成功,CANCEL_FAIL:撤销失败,FALLBACKING:回退中,FALLBACK_END:回退结束
                                String status = queryProfitSharing.getString("status");
                                if (status.equals("PROCESSING")) {
                                    tradeOrderProfitSharing.setStatus(ProfitSharingStatusEnum.status3.getNumber());
                                } else if (status.equals("ACCEPTED")) {
                                    tradeOrderProfitSharing.setStatus(ProfitSharingStatusEnum.status2.getNumber());
                                } else if (status.equals("SUCCESS")) {
                                    tradeOrderProfitSharing.setStatus(ProfitSharingStatusEnum.status1.getNumber());
                                    tradeOrderProfitSharing.setFinishTime(new Date());
                                    // 修改接收方状态
                                    List<BsTradeOrderProfitSharingReceiver> receiverList = tradeOrderProfitSharingReceiverService.getReceiverListByProfitSharingId(tradeOrderProfitSharing.getId());
                                    for (BsTradeOrderProfitSharingReceiver receiver : receiverList) {
                                        receiver.setStatus(ProfitSharingReceiverStatusEnum.status1.getNumber());
                                        tradeOrderProfitSharingReceiverService.editData(receiver);
                                    }
                                } else if (status.equals("FAIL")) {
                                    tradeOrderProfitSharing.setStatus(ProfitSharingStatusEnum.status4.getNumber());
                                }
                                tradeOrderProfitSharingService.editData(tradeOrderProfitSharing);
                            }
                        }
                    } else if (tradeOrderProfitSharing.getCmdType().equals(ProfitSharingCmdTypeEnum.CMD_TYPE_3.getNumber())) {
                        if (tradeOrderProfitSharing.getStatus().equals(ProfitSharingStatusEnum.status0.getNumber())) {
                            laKaLaTradeSeparateService.fallback(tradeOrderProfitSharing.getProfitSharingOrderNo());

                        } else if (tradeOrderProfitSharing.getStatus().equals(ProfitSharingStatusEnum.status2.getNumber())
                                || tradeOrderProfitSharing.getStatus().equals(ProfitSharingStatusEnum.status3.getNumber())) {
                            // 查询拉卡拉分账记录
                            JSONObject queryProfitSharing = laKaLaTradeSeparateService.query(tradeOrderProfitSharing.getProfitSharingOrderNo());
                            if (queryProfitSharing != null) {
                                // ACCEPTED:已受理,PROCESSING:处理中,FAIL:失败,SUCCESS:成功,
                                //（如果分账指令后有反向操作指令，则原分账指令会变更成以下的状态之一：）
                                //CANCELING:撤销中,CANCELED:撤销成功,CANCEL_FAIL:撤销失败,FALLBACKING:回退中,FALLBACK_END:回退结束
                                String status = queryProfitSharing.getString("status");
                                if (status.equals("PROCESSING")) {
                                    tradeOrderProfitSharing.setStatus(ProfitSharingStatusEnum.status3.getNumber());
                                } else if (status.equals("ACCEPTED")) {
                                    tradeOrderProfitSharing.setStatus(ProfitSharingStatusEnum.status2.getNumber());
                                } else if (status.equals("SUCCESS")) {
                                    tradeOrderProfitSharing.setStatus(ProfitSharingStatusEnum.status1.getNumber());
                                    tradeOrderProfitSharing.setFinishTime(new Date());
                                    // 修改接收方状态
                                    List<BsTradeOrderProfitSharingReceiver> receiverList = tradeOrderProfitSharingReceiverService.getReceiverListByProfitSharingId(tradeOrderProfitSharing.getId());
                                    for (BsTradeOrderProfitSharingReceiver receiver : receiverList) {
                                        receiver.setStatus(ProfitSharingReceiverStatusEnum.status1.getNumber());
                                        tradeOrderProfitSharingReceiverService.editData(receiver);
                                    }
                                } else if (status.equals("FAIL")) {
                                    tradeOrderProfitSharing.setStatus(ProfitSharingStatusEnum.status4.getNumber());
                                }
                                tradeOrderProfitSharingService.editData(tradeOrderProfitSharing);
                            }
                        }
                    }

                } else if (tradeOrderProfitSharing.getPlatformType().equals(PlatformTypeEnum.type4.getNumber())) {
                    List<Map<String,Object>> receiversList = new ArrayList<>();
                    Map<String,Object> receiversMap;

                    // 分账接收方
                    Map<String,Object> param = new HashMap<>();
                    param.put("tradeOrderProfitSharingId", tradeOrderProfitSharing.getId());
                    List<BsTradeOrderProfitSharingReceiver> profitSharingReceivers = tradeOrderProfitSharingReceiverService.getProfitSharingList(param);
                    for (BsTradeOrderProfitSharingReceiver receiver : profitSharingReceivers) {
                        if (!receiver.getReceiverAccount().equals(new BigDecimal("0"))) {
                            receiversMap = new HashMap<>();
                            receiversMap.put("type", "MERCHANT_ID");
                            receiversMap.put("account", receiver.getReceiverAccount());
                            receiversMap.put("amount", receiver.getReceiverAmount().multiply(new BigDecimal("100")).intValue());
                            receiversMap.put("description", "分给商户：" + receiver.getReceiverName());
                            receiversList.add(receiversMap);
                        }
                    }

                    if (tradeOrderProfitSharing.getPlatformType().equals(PlatformTypeEnum.type4.getNumber())) {
                        // 微信分账
                        TreeMap<String, String> refundModel = WxOrderConfig.profitsharingCreate(
                                tradeOrderProfitSharing.getPlatformAppid(),
                                tradeOrderProfitSharing.getPlatformMerNo(),
                                tradeOrderProfitSharing.getPlatformTradeNo(),
                                tradeOrderProfitSharing.getTradeOrderNo(),
                                receiversList
                        );
                        if("SUCCESS".equals(MapUtils.getString(refundModel, "return_code"))
                                && "SUCCESS".equals(MapUtils.getString(refundModel, "result_code"))) {
                            tradeOrderProfitSharing.setPlatformProfitSharingOrderNo(MapUtils.getString(refundModel, "order_id"));
                            tradeOrderProfitSharing.setStatus(1);
                            tradeOrderProfitSharing.setProfitSharingTime(new Date());
                            tradeOrderProfitSharingService.editData(tradeOrderProfitSharing);

                            for (BsTradeOrderProfitSharingReceiver receiver : profitSharingReceivers) {
                                receiver.setStatus(1);
                                receiver.setProfitSharingTime(new Date());
                                tradeOrderProfitSharingReceiverService.editData(receiver);
                            }
                        } else {
                            tradeOrderProfitSharing.setExt1(MapUtils.getString(refundModel,"description"));
                            tradeOrderProfitSharingService.editData(tradeOrderProfitSharing);
                        }
                    }
                }

            } catch (Exception e) {
                tradeOrderProfitSharing.setExt1("分账失败");
                tradeOrderProfitSharingService.editData(tradeOrderProfitSharing);
            }
        }
    }

    /**
     * 完结分账
     * 每分钟扫描一次需要完结的订单
     */
    @Scheduled(cron="0 0/1 * * * ?")
    public void profitSharingFinish() {
        List<BsTradeOrderProfitSharing> tradeOrderProfitSharingList = tradeOrderProfitSharingService.getNeedFinishProfitSharingOrder();
        for (BsTradeOrderProfitSharing tradeOrderProfitSharing : tradeOrderProfitSharingList) {
            try {

                TreeMap<String, String> refundModel = WxOrderConfig.profitSharingFinish(
                        tradeOrderProfitSharing.getPlatformAppid(),
                        tradeOrderProfitSharing.getPlatformMerNo(),
                        tradeOrderProfitSharing.getPlatformTradeNo(),
                        tradeOrderProfitSharing.getPlatformProfitSharingOrderNo(),
                        "分账已完成");

                if("SUCCESS".equals(MapUtils.getString(refundModel, "return_code"))
                        && "SUCCESS".equals(MapUtils.getString(refundModel, "result_code"))) {
                    tradeOrderProfitSharing.setPlatformProfitSharingOrderNo(MapUtils.getString(refundModel, "order_id"));
                    tradeOrderProfitSharing.setStatus(2);
                    tradeOrderProfitSharing.setFinishTime(new Date());
                    tradeOrderProfitSharingService.editData(tradeOrderProfitSharing);

                    // 分账接收方
                    Map<String,Object> param = new HashMap<>();
                    param.put("tradeOrderProfitSharingId", tradeOrderProfitSharing.getId());
                    List<BsTradeOrderProfitSharingReceiver> profitSharingReceivers = tradeOrderProfitSharingReceiverService.getProfitSharingList(param);
                    for (BsTradeOrderProfitSharingReceiver receiver : profitSharingReceivers) {
                        receiver.setStatus(2);
                        receiver.setFinishTime(new Date());
                        tradeOrderProfitSharingReceiverService.editData(receiver);
                    }
                } else {
                    tradeOrderProfitSharing.setExt1(MapUtils.getString(refundModel,"description"));
                    tradeOrderProfitSharingService.editData(tradeOrderProfitSharing);
                }

            } catch (Exception e) {
                tradeOrderProfitSharing.setExt1("完结分账失败");
                tradeOrderProfitSharingService.editData(tradeOrderProfitSharing);
            }
        }
    }

    /**
     * 交易结算
     * 每分钟扫描一次
      */
    @Scheduled(cron="0 0/1 * * * ?")
    public void tradeOrderSettle() {
        Map<String, Object> param = new HashMap<>();
        param.put("statusList", "3,6");
        param.put("shdFeeAmtIsNull", true);

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -2);
        param.put("payTimeS", calendar.getTimeInMillis());  

        List<BsTradeOrder> tradeOrderList = tradeOrderService.getTradeOrderList(param);
        for (BsTradeOrder tradeOrder : tradeOrderList) {
            try {
                if (tradeOrder.getPlatformType().equals(PlatformTypeEnum.type1.getNumber())) {
                    // 查询手续费
                    JSONObject queryTrans = laKaLaTradeService.queryTradeOrderTrans(tradeOrder.getOutTradeNo());
                    if (queryTrans != null && queryTrans.getString("trade_state").equals("03")) {
                        tradeOrder.setShdFeeAmt(queryTrans.getBigDecimal("fee_amount"));
                        tradeOrder.setSettleAmount(new BigDecimal(queryTrans.getString("settle_amount").replace(",","")));
                        tradeOrder.setSettleDate(queryTrans.getDate("clear_date"));
                        tradeOrder.setFeeRate(new BigDecimal(queryTrans.getString("fee_rate").split(",")[0]));
                        tradeOrderService.editOrder(tradeOrder);

                        BsTradeSettle tradeSettle = new BsTradeSettle();
                        tradeSettle.setCompanyId(tradeOrder.getCompanyId());
                        tradeSettle.setAgentId(tradeOrder.getAgentId());
                        tradeSettle.setSalesmanId(tradeOrder.getSalesmanId());
                        tradeSettle.setSalesmanName(tradeOrder.getSalesmanName());
                        tradeSettle.setMerId(tradeOrder.getMerId());
                        tradeSettle.setMerNo(tradeOrder.getMerNo());
                        tradeSettle.setMerName(tradeOrder.getMerName());
                        tradeSettle.setMerAbbreviate(tradeOrder.getMerAbbreviate());
                        tradeSettle.setStoreId(tradeOrder.getStoreId());
                        tradeSettle.setStoreName(tradeOrder.getStoreName());
                        tradeSettle.setPlatformType(tradeOrder.getPlatformType());
                        tradeSettle.setPlatformMerNo(tradeOrder.getPlatformMerNo());
                        tradeSettle.setPlatformTradeNo(tradeOrder.getPlatformTradeNo());
                        tradeSettle.setPlatformLogNo(tradeOrder.getPlatformLogNo());
                        tradeSettle.setPayMode(tradeOrder.getPayMode());
                        tradeSettle.setOutTradeNo(tradeOrder.getOutTradeNo());
                        tradeSettle.setAccTradeNo(tradeOrder.getAccTradeNo());
                        tradeSettle.setTradeAmount(tradeOrder.getAccSettleAmount());
                        tradeSettle.setTradeType(TradeSettleTradeType.type1.getNumber());
                        tradeSettle.setSettleAmount(tradeOrder.getAccSettleAmount());
                        tradeSettle.setTradeTime(tradeOrder.getPayTime());
                        tradeSettle.setShdFeeAmt(tradeOrder.getShdFeeAmt());
                        tradeSettle.setSettleAmount(tradeOrder.getSettleAmount());
                        tradeSettle.setSettleDate(tradeOrder.getSettleDate());
                        tradeSettle.setFeeRate(tradeOrder.getFeeRate());
                        tradeSettleService.editData(tradeSettle);
                    }

                } else if (tradeOrder.getPlatformType().equals(PlatformTypeEnum.type5.getNumber())) {
                    // 查询手续费
                    JSONObject queryTrans = tianQueTradeService.poundageQuery(tradeOrder.getPlatformMerNo(), tradeOrder.getOutTradeNo());
                    tradeOrder.setShdFeeAmt(queryTrans.getBigDecimal("recFeeAmt"));
                    tradeOrder.setFeeRate(new BigDecimal(queryTrans.getString("recFeeRate")));
                    tradeOrderService.editOrder(tradeOrder);

                    BsTradeSettle tradeSettle = new BsTradeSettle();
                    tradeSettle.setCompanyId(tradeOrder.getCompanyId());
                    tradeSettle.setAgentId(tradeOrder.getAgentId());
                    tradeSettle.setSalesmanId(tradeOrder.getSalesmanId());
                    tradeSettle.setSalesmanName(tradeOrder.getSalesmanName());
                    tradeSettle.setMerId(tradeOrder.getMerId());
                    tradeSettle.setMerNo(tradeOrder.getMerNo());
                    tradeSettle.setMerName(tradeOrder.getMerName());
                    tradeSettle.setMerAbbreviate(tradeOrder.getMerAbbreviate());
                    tradeSettle.setStoreId(tradeOrder.getStoreId());
                    tradeSettle.setStoreName(tradeOrder.getStoreName());
                    tradeSettle.setPlatformType(tradeOrder.getPlatformType());
                    tradeSettle.setPlatformMerNo(tradeOrder.getPlatformMerNo());
                    tradeSettle.setPlatformTradeNo(tradeOrder.getPlatformTradeNo());
                    tradeSettle.setPlatformLogNo(tradeOrder.getPlatformLogNo());
                    tradeSettle.setPayMode(tradeOrder.getPayMode());
                    tradeSettle.setOutTradeNo(tradeOrder.getOutTradeNo());
                    tradeSettle.setAccTradeNo(tradeOrder.getAccTradeNo());
                    tradeSettle.setTradeAmount(tradeOrder.getAccSettleAmount());
                    tradeSettle.setTradeType(TradeSettleTradeType.type1.getNumber());
                    tradeSettle.setSettleAmount(tradeOrder.getAccSettleAmount());
                    tradeSettle.setTradeTime(tradeOrder.getPayTime());
                    tradeSettle.setShdFeeAmt(tradeOrder.getShdFeeAmt());
                    tradeSettle.setSettleAmount(tradeOrder.getSettleAmount());
                    tradeSettle.setSettleDate(tradeOrder.getSettleDate());
                    tradeSettle.setFeeRate(tradeOrder.getFeeRate());
                    tradeSettleService.editData(tradeSettle);
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }

    /**
     * 交易退款结算
     * 每分钟扫描一次
     */
    @Scheduled(cron="0 0/1 * * * ?")
    public void tradeOrderRefundSettle() {
        Map<String, Object> param = new HashMap<>();
        param.put("status", TradeOrderRefundStatusEnum.status2.getNumber());
        param.put("shdFeeAmtIsNull", true);

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -2);
        param.put("payTimeS", calendar.getTimeInMillis());

        List<BsTradeOrderRefund> tradeOrderRefundList = tradeOrderRefundService.getRefundList(param);
        for (BsTradeOrderRefund tradeOrderRefund : tradeOrderRefundList) {
            try {
                // 查询交易
                BsTradeOrder tradeOrder = tradeOrderService.getOrderByOutTradeNo(tradeOrderRefund.getOutTradeNo());
                if (tradeOrder != null && tradeOrder.getSettleAmount() != null) {
                    if (tradeOrder.getPlatformType().equals(PlatformTypeEnum.type1.getNumber())) {
                        // 查询手续费
                        JSONObject queryTrans = laKaLaTradeService.queryTradeOrderRefundTrans(tradeOrderRefund.getRefundNo());
                        if (queryTrans != null) {
                            tradeOrderRefund.setShdFeeAmt(new BigDecimal(queryTrans.getString("fee_amount").replace("-","")));
                            tradeOrderRefund.setSettleAmount(new BigDecimal(queryTrans.getString("settle_amount").replace(",","").replace("-","")));
                            tradeOrderRefund.setSettleDate(queryTrans.getDate("clear_date"));
                            tradeOrderRefundService.editOrderRefund(tradeOrderRefund);

                            BsTradeSettle tradeSettle = new BsTradeSettle();
                            tradeSettle.setCompanyId(tradeOrder.getCompanyId());
                            tradeSettle.setAgentId(tradeOrder.getAgentId());
                            tradeSettle.setSalesmanId(tradeOrder.getSalesmanId());
                            tradeSettle.setSalesmanName(tradeOrder.getSalesmanName());
                            tradeSettle.setMerId(tradeOrder.getMerId());
                            tradeSettle.setMerNo(tradeOrder.getMerNo());
                            tradeSettle.setMerName(tradeOrder.getMerName());
                            tradeSettle.setMerAbbreviate(tradeOrder.getMerAbbreviate());
                            tradeSettle.setStoreId(tradeOrder.getStoreId());
                            tradeSettle.setStoreName(tradeOrder.getStoreName());
                            tradeSettle.setPlatformType(tradeOrder.getPlatformType());
                            tradeSettle.setPlatformMerNo(tradeOrder.getPlatformMerNo());
                            tradeSettle.setPlatformTradeNo(tradeOrderRefund.getPlatformOrderNo());
                            tradeSettle.setPlatformLogNo(tradeOrderRefund.getPlatformLogNo());
                            tradeSettle.setPayMode(tradeOrder.getPayMode());
                            tradeSettle.setOutTradeNo(tradeOrderRefund.getRefundNo());
                            tradeSettle.setAccTradeNo(tradeOrderRefund.getAccTradeNo());
                            tradeSettle.setTradeType(TradeSettleTradeType.type2.getNumber());
                            tradeSettle.setTradeAmount(tradeOrderRefund.getRefundAmount());
                            tradeSettle.setTradeTime(tradeOrderRefund.getRefundTime());
                            tradeSettle.setShdFeeAmt(tradeOrderRefund.getShdFeeAmt());
                            tradeSettle.setSettleAmount(tradeOrderRefund.getSettleAmount());
                            tradeSettle.setSettleDate(tradeOrderRefund.getSettleDate());
                            tradeSettle.setFeeRate(tradeOrder.getFeeRate());
                            tradeSettle.setOrgOutTradeNo(tradeOrder.getOutTradeNo());
                            tradeSettleService.editData(tradeSettle);

                            tradeOrder.setShdFeeAmt(tradeOrder.getShdFeeAmt().subtract(tradeOrderRefund.getShdFeeAmt()));
                            tradeOrder.setSettleAmount(tradeOrder.getSettleAmount().subtract(tradeOrderRefund.getSettleAmount()));
                            tradeOrderService.editOrder(tradeOrder);
                        }
                    } else if (tradeOrder.getPlatformType().equals(PlatformTypeEnum.type5.getNumber())) {
                        // 查询手续费
                        JSONObject queryTrans = tianQueTradeService.poundageQuery(tradeOrder.getPlatformMerNo(), tradeOrderRefund.getRefundNo());
                        if (queryTrans != null) {
                            tradeOrderRefund.setShdFeeAmt(new BigDecimal(queryTrans.getString("recFeeAmt")));
                            tradeOrderRefundService.editOrderRefund(tradeOrderRefund);

                            BsTradeSettle tradeSettle = new BsTradeSettle();
                            tradeSettle.setCompanyId(tradeOrder.getCompanyId());
                            tradeSettle.setAgentId(tradeOrder.getAgentId());
                            tradeSettle.setSalesmanId(tradeOrder.getSalesmanId());
                            tradeSettle.setSalesmanName(tradeOrder.getSalesmanName());
                            tradeSettle.setMerId(tradeOrder.getMerId());
                            tradeSettle.setMerNo(tradeOrder.getMerNo());
                            tradeSettle.setMerName(tradeOrder.getMerName());
                            tradeSettle.setMerAbbreviate(tradeOrder.getMerAbbreviate());
                            tradeSettle.setStoreId(tradeOrder.getStoreId());
                            tradeSettle.setStoreName(tradeOrder.getStoreName());
                            tradeSettle.setPlatformType(tradeOrder.getPlatformType());
                            tradeSettle.setPlatformMerNo(tradeOrder.getPlatformMerNo());
                            tradeSettle.setPlatformTradeNo(tradeOrderRefund.getPlatformOrderNo());
                            tradeSettle.setPlatformLogNo(tradeOrderRefund.getPlatformLogNo());
                            tradeSettle.setPayMode(tradeOrder.getPayMode());
                            tradeSettle.setOutTradeNo(tradeOrderRefund.getRefundNo());
                            tradeSettle.setAccTradeNo(tradeOrderRefund.getAccTradeNo());
                            tradeSettle.setTradeType(TradeSettleTradeType.type2.getNumber());
                            tradeSettle.setTradeAmount(tradeOrderRefund.getRefundAmount());
                            tradeSettle.setTradeTime(tradeOrderRefund.getRefundTime());
                            tradeSettle.setShdFeeAmt(tradeOrderRefund.getShdFeeAmt());
                            tradeSettle.setSettleAmount(tradeOrderRefund.getSettleAmount());
                            tradeSettle.setSettleDate(tradeOrderRefund.getSettleDate());
                            tradeSettle.setFeeRate(tradeOrder.getFeeRate());
                            tradeSettle.setOrgOutTradeNo(tradeOrder.getOutTradeNo());
                            tradeSettleService.editData(tradeSettle);

                            tradeOrder.setShdFeeAmt(tradeOrder.getShdFeeAmt().subtract(tradeOrderRefund.getShdFeeAmt()));
                            tradeOrder.setSettleAmount(tradeOrder.getSettleAmount().subtract(tradeOrderRefund.getSettleAmount()));
                            tradeOrderService.editOrder(tradeOrder);
                        }
                    }

                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }

}
