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.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;
import java.time.YearMonth;

@Component
@Slf4j
public class KingdeeOrderAbleSyncIdJob implements JobHandler {

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

    @Resource
    private KingdeeApi kingdeeApi;

    @Override
    @TenantIgnore
    public String execute(String jsonParam) {
        logger.info("KingdeeOrderAbleSyncIdJob 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();

        String billingMonth = YearMonth.now().toString();
        // 应收单
        Integer syncCount = kingdeeApi.syncArReceivable(tenantId, billingMonth);
        return String.format("租户{}同步金蝶用户账单数量{}", tenantId, syncCount);
    }

}
