package com.zcloud.personnel.positioning.plan;

import com.jjb.saas.framework.job.Job;
import com.jjb.saas.framework.job.annotation.JobRegister;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zcloud.personnel.positioning.command.AlarmRuleFindsPullService;
import com.zcloud.personnel.positioning.command.FenceFindsPullService;
import com.zcloud.personnel.positioning.dto.clientobject.FenceSyncCO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class FindsReferenceDataSyncJob implements Job {
    private final FenceFindsPullService fenceFindsPullService;
    private final AlarmRuleFindsPullService alarmRuleFindsPullService;

    @Override
    @JobRegister(cron = "0 */5 * * * ?", jobDesc = "FindS围栏和告警规则快照拉取", author = "申智丹", triggerStatus = 1)
    @XxlJob("com.zcloud.personnel.positioning.plan.FindsReferenceDataSyncJob")
    public ReturnT<String> execute(String param) {
        try {
            FenceSyncCO fenceResult = fenceFindsPullService.syncAllConfigured();
            int ruleCount = alarmRuleFindsPullService.refreshAllConfigured();
            String message = "FindS参考数据拉取完成，围栏处理 " + fenceResult.getPulledCount()
                    + " 条，告警规则处理 " + ruleCount + " 条";
            XxlJobHelper.log(message);
            return new ReturnT<>(ReturnT.SUCCESS_CODE, message);
        } catch (Exception e) {
            XxlJobHelper.log("FindS参考数据拉取失败：" + e.getMessage());
            return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage());
        }
    }
}
