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.query.FindsStaffLocationStatusSyncService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class FindsStaffMappingSyncJob implements Job {
    private final FindsStaffLocationStatusSyncService locationStatusSyncService;

    @Override
    @JobRegister(cron = "0 */5 * * * ?", jobDesc = "FindS人员映射和定位状态同步", author = "系统", triggerStatus = 1)
    @XxlJob("com.zcloud.personnel.positioning.plan.FindsStaffMappingSyncJob")
    public ReturnT<String> execute(String param) {
        try {
            FindsStaffLocationStatusSyncService.SyncSummary summary = locationStatusSyncService.synchronize();
            String message = "FindS人员映射和定位状态同步完成，人员总数="
                    + summary.getRemoteStaffCount()
                    + "，全量同步=" + summary.isFullSync()
                    + "，本次补充=" + summary.getFullSyncCount()
                    + "，在线终端=" + summary.getOnlineTerminalCount();
            XxlJobHelper.log(message);
            return new ReturnT<>(ReturnT.SUCCESS_CODE, message);
        } catch (Exception e) {
            String message = "FindS人员映射和定位状态同步失败：" + e.getMessage();
            XxlJobHelper.log(message);
            return new ReturnT<>(ReturnT.FAIL_CODE, message);
        }
    }
}
