AutomatedHandleOrder.java 5.4 KB
Newer Older
Z
zengqiao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
package com.xiaojukeji.kafka.manager.task.dispatch.op;

import com.xiaojukeji.kafka.manager.bpm.OrderService;
import com.xiaojukeji.kafka.manager.bpm.common.OrderStatusEnum;
import com.xiaojukeji.kafka.manager.bpm.common.OrderTypeEnum;
import com.xiaojukeji.kafka.manager.common.constant.Constant;
import com.xiaojukeji.kafka.manager.common.constant.LogConstant;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.common.utils.DateUtils;
import com.xiaojukeji.kafka.manager.common.utils.ValidateUtils;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ConfigDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.bpm.order.impl.ApplyAppOrder;
import com.xiaojukeji.kafka.manager.service.service.ConfigService;
import com.xiaojukeji.kafka.manager.common.utils.SpringTool;
import com.xiaojukeji.kafka.manager.task.component.AbstractScheduledTask;
import com.xiaojukeji.kafka.manager.task.component.CustomScheduled;
import com.xiaojukeji.kafka.manager.task.component.EmptyEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
23
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Z
zengqiao 已提交
24 25 26 27 28 29 30 31 32 33 34
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 工单自动化审批
 * @author zhongyuankai
 * @date 2020/6/12
 */
@Component
@CustomScheduled(name = "automatedHandleOrder", cron = "0 0/1 * * * ?", threadNum = 1)
35
@ConditionalOnProperty(prefix = "task.op.order-auto-exec", name = "app-enabled", havingValue = "true", matchIfMissing = false)
Z
zengqiao 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
public class AutomatedHandleOrder extends AbstractScheduledTask<EmptyEntry> {
    private static final Logger LOGGER = LoggerFactory.getLogger(LogConstant.SCHEDULED_TASK_LOGGER);

    @Autowired
    private OrderService orderService;

    @Autowired
    private ConfigService configService;

    @Override
    public List<EmptyEntry> listAllTasks() {
        EmptyEntry emptyEntry = new EmptyEntry();
        emptyEntry.setId(System.currentTimeMillis() / 1000);
        return Arrays.asList(emptyEntry);
    }

    @Override
    public void processTask(EmptyEntry entryEntry) {
        List<OrderDO> waitDealOrderList = orderService.getWaitDealOrder();
        if (ValidateUtils.isEmptyList(waitDealOrderList)) {
56
            LOGGER.info("class=AutomatedHandleOrder||method=processTask||msg=waiting deal order is empty");
Z
zengqiao 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70
            return;
        }

        List<OrderDO> passedOrderList = orderService.getPassedOrder(new Date(DateUtils.getDayStarTime(0)));
        Map<Integer, List<OrderDO>> waitDealMap = getOrderTypeMap(waitDealOrderList);
        Map<Integer, List<OrderDO>> passedMap = getOrderTypeMap(passedOrderList);

        handleAppApplyOrder(
                waitDealMap.getOrDefault(OrderTypeEnum.APPLY_APP.getCode(), new ArrayList<>()),
                passedMap.getOrDefault(OrderTypeEnum.APPLY_APP.getCode(), new ArrayList<>())
        );
    }

    private void handleAppApplyOrder(List<OrderDO> waitDealOrderList, List<OrderDO> passedOrderList) {
71
        LOGGER.info("class=AutomatedHandleOrder||method=processTask||msg=start handle app apply order");
Z
zengqiao 已提交
72 73 74
        if (ValidateUtils.isEmptyList(waitDealOrderList)) {
            return;
        }
75

Z
zengqiao 已提交
76 77 78 79 80 81
        Integer maxNum = Constant.HANDLE_APP_APPLY_MAX_NUM_DEFAULT;
        ConfigDO configDO = configService.getByKey(Constant.HANDLE_APP_APPLY_MAX_NUM);
        if (!ValidateUtils.isNull(configDO)) {
            try {
                maxNum = Integer.parseInt(configDO.getConfigValue());
            } catch (Exception e) {
82
                LOGGER.error("class=AutomatedHandleOrder||method=processTask||configDO={}||msg=config value illegal", configDO, e);
Z
zengqiao 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
            }
        }
        int handleNum = Math.min(maxNum - passedOrderList.size(), waitDealOrderList.size());
        if (handleNum <= 0) {
            return;
        }
        OrderHandleBaseDTO baseDTO = new OrderHandleBaseDTO();
        baseDTO.setStatus(OrderStatusEnum.PASSED.getCode());
        baseDTO.setOpinion("通过");
        baseDTO.setDetail("{}");
        ApplyAppOrder applyAppOrder = (ApplyAppOrder) SpringTool.getBean(OrderTypeEnum.APPLY_APP.getOrderName());

        for (int i = 0; i < handleNum; i++) {
            OrderDO orderDO = waitDealOrderList.get(i);
            try {
                ResultStatus resultStatus =
                        applyAppOrder.handleOrderDetail(orderDO, baseDTO, Constant.AUTO_HANDLE_USER_NAME);
                if (ResultStatus.SUCCESS.equals(resultStatus)) {
                    applyAppOrder.updateOrder(orderDO, baseDTO, Constant.AUTO_HANDLE_USER_NAME);
                }
            } catch (Exception e) {
104
                LOGGER.error("class=AutomatedHandleOrder||method=processTask||orderDO={}||msg=auto handle app order failed", orderDO, e);
Z
zengqiao 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
            }
        }
    }

    private Map<Integer, List<OrderDO>> getOrderTypeMap(List<OrderDO> orderList) {
        if (ValidateUtils.isEmptyList(orderList)) {
            return new HashMap<>();
        }
        Map<Integer, List<OrderDO>> orderMap = new HashMap<>();
        for (OrderDO orderDO : orderList) {
            List<OrderDO> list = orderMap.getOrDefault(orderDO.getType(), new ArrayList<>());
            list.add(orderDO);
            orderMap.put(orderDO.getType(), list);
        }
        return orderMap;
    }
}