package nckd.jimin.jyyy.bd.task;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.exception.KDException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.api.StopTask;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import nckd.jimin.jyyy.bd.task.impl.SynSapServiceImpl;

import java.util.Map;

/**
 * 调度任务类
 * 插件说明：查询SAP的会计科目同步至星瀚
 * @author wanghaiwu_kd
 * @date 2025/04/23
 */
public class SynAccountForSAPTask extends AbstractTask implements StopTask {
    private static final Log logger = LogFactory.getLog(SynAccountForSAPTask.class);
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        //使用状态=启用
        QFilter qFilter = new QFilter("enable", QCP.equals, "1");
        //数据状态=已审核
        qFilter.and(new QFilter("status", QCP.equals, "C"));
        //属性核算组织=是
        qFilter.and(new QFilter("fisaccounting", QCP.equals, "1"));
        DynamicObject[] orgLst = BusinessDataServiceHelper.load("bos_org", "id, number", qFilter.toArray());

        SynSapServiceImpl synSapService = new SynSapServiceImpl();
        //轮询组织，将成本信息缓存到Map中
        for(DynamicObject org : orgLst) {
            String orgNumber = org.getString("number");
            logger.info("------同步会计科目 " + orgNumber + "----------");

            Map<String, String> result = synSapService.synAccountForSap(orgNumber);

            if(!result.get("code").equals("200")){
                logger.info("同步会计科目失败, " + "组织编码："+orgNumber +"，错误：" + result.get("msg"));
            } else{
                logger.info("同步会计科目成功, " + "组织编码："+orgNumber +"，信息：" + result.get("msg"));
            }
        }
    }
}
