package com.xisc.pm.infra.job;

import com.xisc.pm.domain.entity.PmSubLog;
import com.xisc.pm.domain.repository.PmSubLogRepository;
import org.hzero.boot.scheduler.infra.annotation.JobHandler;
import org.hzero.boot.scheduler.infra.enums.ReturnT;
import org.hzero.boot.scheduler.infra.handler.IJobHandler;
import org.hzero.boot.scheduler.infra.tool.SchedulerTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 余坯智能组板任务
 *
 * @author
 * @date
 * @description
 */
@JobHandler("SubDataJobHander")
public class SubDataJobHander implements IJobHandler {

    @Autowired
    private PmSubLogRepository pmSubLogRepository;

    private static final String TENANT_ID = "tenantId";

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnT execute(Map<String, String> map, SchedulerTool tool) {
        tool.info("测试定时任务执行开始");
        // 校验
        Long tenantId;
        try {
            tenantId = Long.parseLong(map.get(TENANT_ID));
        } catch (Exception e) {
            tool.info("=====参数有误: 请维护租户ID,且只能维护整数类型, 不执行本次任务=====");
            return ReturnT.FAILURE;
        }

        PmSubLog pmSubLog = new PmSubLog();
        pmSubLog.setSubDate(new java.util.Date());
        pmSubLog.setSlabNo("123456789");
        pmSubLog.setSubMode(1L);
        pmSubLog.setSubRslt("成功");
        pmSubLog.setSubLog("测试定时任务");
        pmSubLog.setTenantId(tenantId);
        pmSubLogRepository.insertSelective(pmSubLog);

        tool.info("测试定时任务执行结束");
        return ReturnT.SUCCESS;
    }
}
