package com.xhpc.activity.task;


import com.xhpc.activity.domain.XhpcWorkOrderDomain;
import com.xhpc.activity.mapper.XhpcWorkOrderMapper;
import com.xhpc.common.api.PowerPileService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 自动处理工单
 */
@Component
public class AutoProcessOrderTask {

    private static final Logger log = LoggerFactory.getLogger(AutoProcessOrderTask.class);
    @Resource
    XhpcWorkOrderMapper workOrderMapper;

    @Resource
    PowerPileService powerPileService;

    //@Scheduled(fixedRate = 60000)
    protected void run() {

        List<XhpcWorkOrderDomain> orderList = workOrderMapper.selectByAutoRun();
        for (XhpcWorkOrderDomain orderDomain : orderList) {
            String pileNo = orderDomain.getSerialNumber();
            if (StringUtils.isEmpty(pileNo) || pileNo.length() < 14) continue;
            pileNo = pileNo.substring(0, 14);

            String processStr = "";
            R r = null;
            switch (orderDomain.getMeasure()) {
                case 1:  // 自动升级
                    r = powerPileService.pileSoftwareUpgrade(pileNo);
                    processStr = "自动升级指令";
                    break;
                case 2:     // 校时校价
                case 4:     // 下发新费率
                    r = powerPileService.configTimeNRateModel(pileNo);
                    processStr = "自动下发校时校价指令";
                    break;
                case 3:     // 重启
                    r = powerPileService.pileReboot(pileNo);
                    processStr = "自动下发重启指令";
                    break;
                default:
                    break;
            }

            if (r != null && r.getCode() == 200) {
                orderDomain.setStatus(Short.parseShort("2"));
                orderDomain.setDisposalMethod("运维系统执行自动处理措施：" + processStr);
                workOrderMapper.updatePressOrderByPrimaryKey(orderDomain);
            }
        }
    }

}
