package cn.iocoder.yudao.module.system.job;

import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.api.kingdee.KingdeeApi;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class KingdeeBDCustomerSyncIdJob implements JobHandler {

    private final static Logger logger = LoggerFactory.getLogger(KingdeeBDCustomerSyncIdJob.class);

    @Resource
    private KingdeeApi kingdeeApi;

    @Override
    @TenantIgnore
    public String execute(String jsonParam) {
        logger.info("kingdeeOrderBillSyncIdJob execute param:{}", jsonParam);
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(jsonParam, JsonObject.class);
        JsonElement tenantIdElement = jsonObject.get("tenantId");
        if (tenantIdElement == null) {
            return "参数错误，tenantId为空！";
        }
        Long tenantId = tenantIdElement.getAsLong();
        Integer syncCount = kingdeeApi.syncBDCustomer(tenantId);
        return String.format("租户{}同步金蝶用户账单数量{}", TenantContextHolder.getTenantId(), syncCount);
    }

    public static void main(String[] args) {

        String jsonParam = "{\"tenantId\":1}";
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(jsonParam, JsonObject.class);
        JsonElement tenantIdElement = jsonObject.get("tenantId");
        if (tenantIdElement == null || !tenantIdElement.isJsonPrimitive()) {
            System.out.println("参数错误，tenantId为空或格式不正确！");
            return;
        }
        Long tenantId = tenantIdElement.getAsLong();

        System.out.println("args = " + tenantId);
    }

}
