package com.sifang.modules.supply.order.service;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.UserConstants;
import com.sifang.modules.platform.utils.PlantformUtils;
import com.sifang.modules.sunshine.SystemSetting.dao.TraderDao;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTrader;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.supply.order.constant.ApplyOrderConstants;
import com.sifang.modules.supply.order.constant.DeliverOrderConstants;
import com.sifang.modules.supply.order.dao.*;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialApply;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialApplyDetail;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialApplyGather;
import com.sifang.modules.supply.order.request.*;
import com.sifang.modules.supply.order.resp.TraderWarehouse;
import com.sifang.modules.supply.setting.dao.CrmSupplyWarehouseDao;
import com.sifang.modules.supply.setting.entity.CrmSupplyWarehouse;
import com.sifang.modules.supply.stock.dao.MaterialStockDao;
import com.sifang.modules.sys.common.utils.StringUtil;
import com.sifang.modules.sys.dao.UserDao;
import com.sifang.modules.sys.entity.Ascription;
import com.sifang.modules.sys.entity.Operator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author hongzq
 */
@Service
public class TraderOrderService {
    @Autowired
    MaterialApplyService materialApplyService;
    @Autowired
    DeliverOrderService deliverOrderService;
    @Autowired
    UserDao userDao;
    @Autowired
    TraderDao traderDao;
    @Autowired
    MaterialApplyDao materialApplyDao;
    @Autowired
    MaterialStockDao materialStockDao;
    @Autowired
    MaterialApplyDetailDao materialApplyDetailDao;
    @Autowired
    MaterialApplyMsgDao materialApplyMsgDao;
    @Autowired
    MaterialApplyGatherDao materialApplyGatherDao;
    @Autowired
    CrmSupplyWarehouseDao warehouseDao;
    @Autowired
    SupplyExceptionDao supplyExceptionDao;

    public String findRoleType(String userId) {
        return userDao.findPlatUserRoleType(userId);
    }

    //判断是否可以进行审批
    public boolean checkCanAudit(String orderId, Operator operator) {
        if (!UserConstants.TYPE_PLAT_EMP.equals(operator.getUserType())) {
            return true;
        }
        String roleType = findRoleType(operator.getUserId());
        //不是系统默认角色，可以操作
        if ("3".equals(roleType) || "a".equals(roleType)) {
            return true;
        }
        //是系统默认角色
        CrmSupplyMaterialApply materialApply = materialApplyDao.get(orderId);
        if (ApplyOrderConstants.WAIT_YW_AUDIT.equals(materialApply.getStatus()) && !"5".equals(roleType)) {
            //待业务员审批 如果是系统默认角色，那么必须是业务员才可以操作，如果不是系统默认角色，则可以操作
            return false;
        }
        if (ApplyOrderConstants.WAIT_GC_AUDIT.equals(materialApply.getStatus()) && !"2".equals(roleType)) {
            return false;
        }
        return !ApplyOrderConstants.WAIT_YG_AUDIT.equals(materialApply.getStatus()) || "6".equals(roleType);
    }

    @Transactional(rollbackFor = Exception.class)
    public void auditOrder(AuditOrderReq req, Operator operator) {
        if (!checkCanAudit(req.getOrderId(), operator)) {
            throw new BusinessException("当前用户无操作权限");
        }
        CrmSupplyMaterialApply materialApply = materialApplyDao.get(req.getOrderId());
        if (!StringUtils.endsWithAny(materialApply.getStatus(),
                ApplyOrderConstants.WAIT_YW_AUDIT, ApplyOrderConstants.WAIT_GC_AUDIT, ApplyOrderConstants.WAIT_YG_AUDIT, ApplyOrderConstants.WAIT_MATCH)) {
            throw new BusinessException("订单状态已发送变化，请刷新后再试");
        }
        if ("1".equals(req.getType()) && StrKit.isBlank(req.getReason())) {
            throw new BusinessException("驳回原因不能为空");
        }
        if ("1".equals(req.getType())) {
            //例外统计
            CrmTrader trader = traderDao.get(materialApply.getTraderId());
            Ascription ascription = new Ascription(materialApply.getTraderId(), trader.getName());
            supplyExceptionDao.saveException(materialApply.getNumber(), "4", req.getReason(), getOptException(materialApply.getStatus()), materialApply.getCreateTime(), ascription, operator);
            //审核不通过
            materialApply.setStatus(ApplyOrderConstants.AUDIT_FAIL);
            materialApply.setFailReason(req.getReason());
            materialApply.setAuditTime(new Date());
            materialApplyDao.save(materialApply);
            materialApplyMsgDao.saveOptMsg(req.getOrderId(), ApplyOrderConstants.OPT_AUDIT_NOT_PASS, "原因：" + req.getReason(), operator);
        } else {
            if (ApplyOrderConstants.WAIT_YW_AUDIT.equals(materialApply.getStatus())) {
                //待业务员审批
                materialApply.setStatus(ApplyOrderConstants.WAIT_GC_AUDIT);
            } else if (ApplyOrderConstants.WAIT_GC_AUDIT.equals(materialApply.getStatus())) {
                //待工程部审批可以修改申请数量
                materialApply.setStatus(ApplyOrderConstants.WAIT_YG_AUDIT);
                //删除物料明细表
                materialApplyDetailDao.deleteByApplyId(req.getOrderId());
                //修改汇总表
                List<CrmSupplyMaterialApplyGather> gatherList = materialApplyService.modifyGatherListForAudit(req.getDetails());
                //添加明细表
                List<CrmSupplyMaterialApplyDetail> details = materialApplyService.addMaterialDetails(gatherList, req.getOrderId());
                materialApplyService.setTotalData(materialApply, details);
            } else if (ApplyOrderConstants.WAIT_YG_AUDIT.equals(materialApply.getStatus())) {
                materialApply.setStatus(ApplyOrderConstants.WAIT_MATCH);
            }
            materialApply.setAuditTime(new Date());
            materialApplyDao.save(materialApply);
            //审核通过
            materialApplyMsgDao.saveOptMsg(req.getOrderId(), ApplyOrderConstants.OPT_AUDIT_PASS, "", operator);
        }
    }

    private CrmSupplyMaterialApply createNewSplitOrder(CrmSupplyMaterialApply apply, String suffix, Operator operator) {
//        CrmTrader trader = traderDao.get(apply.getTraderId());
        CrmSupplyMaterialApply oldOrder = new CrmSupplyMaterialApply();
        oldOrder.setNumber(apply.getNumber() + suffix);
        oldOrder.setProjectId(apply.getProjectId());
        oldOrder.setProjectCode(apply.getProjectCode());
        oldOrder.setRelateNo(apply.getNumber());
        oldOrder.setInWarehouseId(apply.getInWarehouseId());
        oldOrder.setAuditTime(apply.getAuditTime());
        oldOrder.setRemark(apply.getRemark());
        oldOrder.setCreateTime(apply.getCreateTime());
        oldOrder.setCreateBy(apply.getCreateBy());
        oldOrder.setCreator(apply.getCreator());
        oldOrder.setTraderId(apply.getTraderId());
        oldOrder.setProvince(apply.getProvince());
        oldOrder.setCity(apply.getCity());
        oldOrder.setArea(apply.getArea());
        oldOrder.setAddress(apply.getAddress());
        oldOrder.setPersonInCharge(apply.getPersonInCharge());
        oldOrder.setMobile(apply.getMobile());
        //带匹配
        oldOrder.setStatus(ApplyOrderConstants.WAIT_MATCH);
        materialApplyDao.save(oldOrder);
        //拆单
        materialApplyMsgDao.saveOptMsg(oldOrder.getId(), ApplyOrderConstants.OPT_CREATE, "由拆单生成，关联单号：" + apply.getNumber(), operator);
        return oldOrder;
    }

    private CrmSupplyMaterialApply createNewMergeOrder(List<CrmSupplyMaterialApply> apply, Operator operator) {
        CrmSupplyMaterialApply oldest = apply.get(0);
        CrmTrader trader = traderDao.get(oldest.getTraderId());
        StringBuilder relateNo = new StringBuilder();
        StringBuilder remark = new StringBuilder();
        for (CrmSupplyMaterialApply materialApply : apply) {
            if (materialApply.getCreateTime().getTime() < oldest.getCreateTime().getTime()) {
                oldest = materialApply;
            }
            relateNo.append(",").append(materialApply.getNumber());
            remark.append(",").append(materialApply.getRemark());
        }
        CrmSupplyMaterialApply oldOrder = new CrmSupplyMaterialApply();
        oldOrder.setNumber(PlantformUtils.findOrderNumber("WL"));
        oldOrder.setProjectId(oldest.getProjectId());
        oldOrder.setProjectCode(oldest.getProjectCode());
        oldOrder.setRelateNo(relateNo.substring(1));
        oldOrder.setInWarehouseId(oldest.getInWarehouseId());
        oldOrder.setRemark(remark.substring(1));
        oldOrder.setAuditTime(oldest.getAuditTime());
        oldOrder.setCreateTime(oldest.getCreateTime());
        oldOrder.setCreateBy(trader.getUserId());
        oldOrder.setCreator(trader.getName());
        oldOrder.setTraderId(oldest.getTraderId());
        //待匹配
        oldOrder.setStatus(ApplyOrderConstants.WAIT_MATCH);
        materialApplyDao.save(oldOrder);
        //合并生成
        materialApplyMsgDao.saveOptMsg(oldOrder.getId(), ApplyOrderConstants.OPT_CREATE, "由合并生成，关联单号：" + oldOrder.getRelateNo(), operator);
        return oldOrder;
    }

    @Transactional(rollbackFor = Exception.class)
    public CrmSupplyMaterialApply splitOrder(TraderOrderReq req, Operator operator) {
        String applyOrderId = req.getOrderId();
        CrmSupplyMaterialApply applyOrder = materialApplyDao.get(applyOrderId);
        //只有待匹配可以操作拆单
        if (!ApplyOrderConstants.WAIT_MATCH.equals(applyOrder.getStatus())) {
            throw new BusinessException("订单状态已发生变化，请刷新后再试");
        }
        Map<String, TraderOrderDetailReq> detailReqMap = getTraderOrderDetailReqMap(req.getDetails());
        List<CrmSupplyMaterialApplyDetail> applyDetails = materialApplyDetailDao.findByApplyId(applyOrderId);
            if(StringUtil.isBlank(applyOrder.getProvince())){
                CrmSupplyWarehouse warehouse = warehouseDao.get(applyOrder.getInWarehouseId());
                applyOrder.setProvince(warehouse.getProvince());
                applyOrder.setCity(warehouse.getCity());
                applyOrder.setArea(warehouse.getArea());
                applyOrder.setAddress(warehouse.getAddress());
            }

        CrmSupplyMaterialApply oldOrder = createNewSplitOrder(applyOrder, "-01", operator);
        CrmSupplyMaterialApply newOrder = createNewSplitOrder(applyOrder, "-02", operator);

        //原订单剩余
        List<CrmSupplyMaterialApplyDetail> oldOrderDetails = new ArrayList<>();
        //新订单
        List<CrmSupplyMaterialApplyDetail> newOrderDetails = new ArrayList<>();
        for (CrmSupplyMaterialApplyDetail applyDetail : applyDetails) {
            TraderOrderDetailReq detailReq = detailReqMap.get(applyDetail.getId());
            if (detailReq != null && detailReq.getAmount().compareTo(BigDecimal.ZERO) > 0) {
                if (applyDetail.getApplyAmount().compareTo(detailReq.getAmount()) < 0) {
                    throw new BusinessException("拆单数量不能超过申请数量");
                }
                if (applyDetail.getApplyAmount().subtract(detailReq.getAmount()).compareTo(BigDecimal.ZERO) > 0) {
                    materialApplyService.addDetails(oldOrderDetails, applyDetail.getMaterialId(),
                            applyDetail.getApplyAmount().subtract(detailReq.getAmount()), oldOrder.getId());
                }
                //新订单
                materialApplyService.addDetails(newOrderDetails, applyDetail.getMaterialId(),
                        detailReq.getAmount(), newOrder.getId());
            } else {
                materialApplyService.addDetails(oldOrderDetails, applyDetail.getMaterialId(),
                        applyDetail.getApplyAmount(), oldOrder.getId());
            }
        }
        if (newOrderDetails.isEmpty() || oldOrderDetails.isEmpty()) {
            throw new BusinessException("无可拆订单");
        }
        materialApplyDetailDao.save(oldOrderDetails);
        materialApplyDetailDao.save(newOrderDetails);

        //维护物料类型和数量
        oldOrder.setMaterialTypeAmount(new BigDecimal(oldOrderDetails.size()));
        oldOrder.setTotalApplyAmount(sumApplyAmount(oldOrderDetails));

        newOrder.setMaterialTypeAmount(new BigDecimal(newOrderDetails.size()));
        newOrder.setTotalApplyAmount(sumApplyAmount(newOrderDetails));
        materialApplyDao.save(oldOrder);
        materialApplyDao.save(newOrder);

        applyOrder.setStatus(ApplyOrderConstants.ALREADY_SPLIT);
        applyOrder.setRelateNo(StringUtil.appendMultiString(applyOrder.getRelateNo(), oldOrder.getNumber(), newOrder.getNumber()));
        materialApplyDao.save(applyOrder);

        //拆单
        materialApplyMsgDao.saveOptMsg(req.getOrderId(), ApplyOrderConstants.OPT_SPLIT_ORDER, "", operator);
        return newOrder;
    }

    private BigDecimal sumApplyAmount(List<CrmSupplyMaterialApplyDetail> details) {
        BigDecimal total = BigDecimal.ZERO;
        for (CrmSupplyMaterialApplyDetail detail : details) {
            total = total.add(detail.getApplyAmount());
        }
        return total;
    }

    @Transactional(rollbackFor = Exception.class)
    public void mergeOrder(String orderIds, Operator operator) {
        //只有待匹配状态，而且是同一个代理商，同一个入库仓库的订单可以合并
        List<CrmSupplyMaterialApply> orderList = materialApplyDao.findOrderByIds(Arrays.asList(orderIds.split(",")));
        checkMergeOrders(orderList);

        //创建新订单
        CrmSupplyMaterialApply newOrder = createNewMergeOrder(orderList, operator);
        List<CrmSupplyMaterialApplyDetail> detailList = new ArrayList<>();
        for (CrmSupplyMaterialApply apply : orderList) {
            List<CrmSupplyMaterialApplyDetail> applyDetails = materialApplyDetailDao.findByApplyId(apply.getId());
            for (CrmSupplyMaterialApplyDetail detail : applyDetails) {
                materialApplyService.addDetails(detailList, detail.getMaterialId(), detail.getApplyAmount(), newOrder.getId());
            }
            //已并单
            apply.setStatus(ApplyOrderConstants.ALREADY_MERGE);
            apply.setRelateNo(StringUtil.appendMultiString(apply.getRelateNo(), newOrder.getNumber()));
            materialApplyDao.save(apply);
            //并单
            materialApplyMsgDao.saveOptMsg(apply.getId(), ApplyOrderConstants.OPT_MERGE_ORDER, "", operator);
        }
        materialApplyDetailDao.save(detailList);

        //维护物料类型和数量
        newOrder.setMaterialTypeAmount(new BigDecimal(detailList.size()));
        newOrder.setTotalApplyAmount(sumApplyAmount(detailList));
        materialApplyDao.save(newOrder);
    }

    /**
     * 下单
     * 若是全部下单，订单状态变为已下单
     * 若是部分下单，先拆单，原订单变成已拆单，生成两个新订单，待匹配和已下单
     */
    @Transactional(rollbackFor = Exception.class)
    public void placeOrder(PlaceOrderReq req, Operator operator) {
        String warehouseId = req.getWarehouseId();
        String applyOrderId = req.getOrderId();
        CrmSupplyMaterialApply applyOrder = materialApplyDao.get(applyOrderId);
        //只有待匹配可以操作下单
        if (!ApplyOrderConstants.WAIT_MATCH.equals(applyOrder.getStatus())) {
            throw new BusinessException("订单状态已发生变化，请刷新后再试");
        }
        CrmSupplyMaterialApply optOrder = applyOrder;
        if (needSplitWhenOrder(req)) {
            TraderOrderReq orderReq = new TraderOrderReq();
            orderReq.setOrderId(req.getOrderId());
            orderReq.setDetails(req.getDetails());
            optOrder = splitOrder(orderReq, operator);
        }
        //已下单
        CrmSupplyWarehouse warehouse = warehouseDao.get(warehouseId);
        optOrder.setStatus(ApplyOrderConstants.ALREADY_ORDER);
        optOrder.setOrderTime(new Date());
        if ("1".equals(warehouse.getType())) {
            optOrder.setOrderTo("a");
            optOrder.setOrderToName("供应链");
        } else {
            optOrder.setOrderTo(warehouse.getAscriptionId());
            optOrder.setOrderToName(warehouse.getAscriptionName());
        }
        materialApplyDao.save(optOrder);
        //操作记录 下单
        String remark = String.format("下单备注：%s", req.getOrderRemark());
        materialApplyMsgDao.saveOptMsg(req.getOrderId(), ApplyOrderConstants.OPT_PLACE_ORDER, remark, operator);
        //生成发货单
        AddSupplierOrderReq supplierOrderReq = new AddSupplierOrderReq();
        supplierOrderReq.setApplyOrderId(optOrder.getId());
        supplierOrderReq.setInWarehouseId(optOrder.getInWarehouseId());
        supplierOrderReq.setOutWarehouseId(req.getWarehouseId());
        supplierOrderReq.setRemarks(req.getOrderRemark());
        List<AddSupplierOrderDetailReq> detailReqList = new ArrayList<>();
        for(TraderOrderDetailReq detailReq: req.getDetails()){
            AddSupplierOrderDetailReq r = new AddSupplierOrderDetailReq();
            r.setMaterialId(detailReq.getMaterialId());
            r.setAmount(detailReq.getAmount());
            detailReqList.add(r);
        }
        supplierOrderReq.setDetails(detailReqList);
        deliverOrderService.addDeliverOrder(supplierOrderReq, DeliverOrderConstants.TYPE_FH, optOrder.getNumber(), operator,optOrder.getProjectCode());
    }

    private boolean needSplitWhenOrder(PlaceOrderReq req) {
        String orderId = req.getOrderId();
        //判断是否需要拆单,并校验下单数量是否合法
        Map<String, TraderOrderDetailReq> detailReqMap = getTraderOrderDetailReqMap(req.getDetails());
        List<CrmSupplyMaterialApplyDetail> applyDetails = materialApplyDetailDao.findByApplyId(orderId);
        boolean ret = false;
        for (CrmSupplyMaterialApplyDetail detail : applyDetails) {
            String detailId = detail.getId();
            TraderOrderDetailReq detailReq = detailReqMap.get(detailId);
            if (detailReq != null && detailReq.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
                throw new BusinessException("下单数量需大于0");
            }
            if (detailReq != null && detailReq.getAmount().compareTo(detail.getApplyAmount()) > 0) {
                throw new BusinessException("下单数量不能超过申请数量");
            }
            if (detailReq == null || detailReq.getAmount().compareTo(detail.getApplyAmount()) < 0) {
                ret = true;
            }
        }
        return ret;
    }

    public List<TraderWarehouse> canOrderData(TraderOrderReq req, String excludeWarehouseIds) {
        List<TraderOrderDetailReq> detailReqs = req.getDetails();
        StringBuilder builder = new StringBuilder();
        for (TraderOrderDetailReq detailReq : detailReqs) {
            builder.append(",").append(detailReq.getMaterialId());
        }

        Map<String, String> params = new HashMap<>();
        params.put("materialIds", builder.toString().substring(1));
        params.put("types", "1,2");
        params.put("excludeWarehouseIds", excludeWarehouseIds);
        List<Record> stockInfo = materialStockDao.stockList(null, params);
        //按仓库分组
        Map<String, List<Record>> warehouseMap = groupByWarehouse(stockInfo);

        List<Record> filteredList = filterWarehouses(warehouseMap, detailReqs);

        List<TraderWarehouse> warehouses = new ArrayList<>();
        //按代理商将仓库分组
        for (Record stock : filteredList) {
            String ascriptionId = stock.getStr("ascription_id");
            String ascriptionName = stock.getStr("ascription_name");
            String warehouseId = stock.getStr("warehouse_id");
            String warehouseName = stock.getStr("warehouseName");
            TraderWarehouse trader = new TraderWarehouse();
            trader.setId(ascriptionId);
            trader.setName(ascriptionName);

            TraderWarehouse warehouse = new TraderWarehouse();
            warehouse.setId(warehouseId);
            warehouse.setName(warehouseName);
            trader.addWarehouse(warehouse);
            if (warehouses.contains(trader)) {
                int index = warehouses.indexOf(trader);
                warehouses.get(index).addWarehouse(warehouse);
            } else {
                warehouses.add(trader);
            }
        }
        //将供应链置顶
        return topSupplyChain(warehouses);
    }

    private List<Record> filterWarehouses(Map<String, List<Record>> warehouseMap, List<TraderOrderDetailReq> detailReqs) {
        List<Record> filteredList = new ArrayList<>();
        //筛选出满足条件的仓库
        for (Map.Entry<String, List<Record>> entry : warehouseMap.entrySet()) {
            List<Record> list = entry.getValue();
            boolean flag = true;
            for (TraderOrderDetailReq applyDetail : detailReqs) {
                boolean match = false;
                for (Record info : list) {
                    String material = info.getStr("material_id");
                    BigDecimal warning = info.getBigDecimal("warning");
                    if (material.equals(applyDetail.getMaterialId()) && warning.compareTo(applyDetail.getAmount()) >= 0) {
                        match = true;
                        break;
                    }
                }
                if (!match) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                filteredList.add(list.get(0));
            }
        }
        return filteredList;
    }

    private Map<String, List<Record>> groupByWarehouse(List<Record> stockInfo) {
        Map<String, List<Record>> warehouseMap = new HashMap<>();
        for (Record stock : stockInfo) {
            String warehouseId = stock.getStr("warehouse_id");
            List<Record> list = warehouseMap.get(warehouseId);
            if (list == null) {
                list = new ArrayList<>();
            }
            list.add(stock);
            warehouseMap.put(warehouseId, list);
        }
        return warehouseMap;
    }

    private List<TraderWarehouse> topSupplyChain(List<TraderWarehouse> warehouses) {
        List<TraderWarehouse> top = new ArrayList<>();
        List<TraderWarehouse> other = new ArrayList<>();
        for (TraderWarehouse record : warehouses) {
            if ("a".equals(record.getId())) {
                top.add(record);
            } else {
                other.add(record);
            }
        }
        top.addAll(other);
        return top;
    }

    private void checkMergeOrders(List<CrmSupplyMaterialApply> orderList) {
        if (orderList.isEmpty()) {
            throw new BusinessException("订单不存在");
        }
        if (orderList.size() < 2) {
            throw new BusinessException("合并订单数不少于2个");
        }
        CrmSupplyMaterialApply firstOrder = orderList.get(0);
        for (CrmSupplyMaterialApply order : orderList) {
            if (!ApplyOrderConstants.WAIT_MATCH.equals(order.getStatus())) {
                throw new BusinessException("仅待匹配订单可以合并");
            }
            if (!order.getInWarehouseId().equals(firstOrder.getInWarehouseId())) {
                throw new BusinessException("订单入库仓库不同，不支持合并");
            }
            if (!order.getTraderId().equals(firstOrder.getTraderId())) {
                throw new BusinessException("订单所属代理商不同，不支持合并");
            }
            if (!order.getProjectId().equals(firstOrder.getProjectId())) {
                throw new BusinessException("订单产品编号不同，不支持合并");
            }
        }
    }

    private Map<String, TraderOrderDetailReq> getTraderOrderDetailReqMap(List<TraderOrderDetailReq> detailReq) {
        Map<String, TraderOrderDetailReq> map = new HashMap<>();
        for (TraderOrderDetailReq req : detailReq) {
            map.put(req.getDetailId(), req);
        }
        return map;
    }

    //7.待业务员审批 8.待工程部审批 9.待运管部审批 3.待匹配
    private String getOptException(String status) {
        if (ApplyOrderConstants.WAIT_YW_AUDIT.equals(status)) {
            return "7";
        } else if (ApplyOrderConstants.WAIT_GC_AUDIT.equals(status)) {
            return "8";
        } else if (ApplyOrderConstants.WAIT_YG_AUDIT.equals(status)) {
            return "9";
        } else if (ApplyOrderConstants.WAIT_MATCH.equals(status)) {
            return "3";
        }
        throw new IllegalArgumentException("illegal status");
    }
}
