package jnpf.personnels.utils;

import jnpf.database.util.TenantDataSourceUtil;
import jnpf.model.personnels.dto.staff.roster.FtbPersonnelsStaffRosterDto;
import jnpf.model.personnels.po.FtbPersonnelsStaffRoster;
import jnpf.personnels.service.FtbPersonnelsStaffRegistrationFormDataService;
import jnpf.personnels.service.FtbPersonnelsStaffRosterService;
import jnpf.utils.FeignHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class PersonnelAsyncOldData {

    @Autowired
    @Lazy
    private FtbPersonnelsStaffRegistrationFormDataService registrationFormDataService;

    @Autowired
    @Lazy
    private FtbPersonnelsStaffRosterService rosterService;

    @Async
    public void dealOldData(FtbPersonnelsStaffRosterDto record, String tenantCode, Map<String, String> headers) {
        try {
//            FeignHolder.set(headers);
            TenantDataSourceUtil.switchTenant(tenantCode);
            FtbPersonnelsStaffRoster roster = new FtbPersonnelsStaffRoster();
            roster.setId(record.getId());
            roster.setCurrOrg(record.getCurrOrg());
            roster.setCurrPosition(record.getCurrPosition());
            roster.setCurrRank(record.getCurrRank());
            roster.setCurrShopId(record.getCurrShopId());
            roster.setCurrReportsTo(record.getCurrReportsTo());
            roster.setCurrGroupId(record.getCurrGroupId());
            rosterService.updateById(roster);
            Map<String, Object> formDataMap = new HashMap<>();
            formDataMap.put("currOrg", record.getCurrOrg());
            formDataMap.put("currPosition", record.getCurrPosition());
            formDataMap.put("currRank", record.getCurrRank());
            formDataMap.put("currReportsTo", record.getCurrReportsTo());
            formDataMap.put("currShopId", record.getCurrShopId());
            formDataMap.put("onboardingTeam", record.getCurrGroupId());
            registrationFormDataService.syncData(formDataMap, record.getPhone(), record.getId());
        } catch (Exception e) {
            log.error("异步处理组织岗位异常");
        } finally {
            FeignHolder.clear();
        }
    }
}



