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;

public class SynCostCenterForSAPTask extends AbstractTask implements StopTask {
    private static final Log logger = LogFactory.getLog(SynSupplierFromSAPTask.class);
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        SynSapServiceImpl synSapService = new SynSapServiceImpl();
        //使用状态=启用
        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());

        //轮询组织，将成本信息缓存到Map中
        for(DynamicObject org : orgLst) {
            String orgNumber = org.getString("number");
            logger.info("------同步成本中心 " + orgNumber + "----------");
            Map<String, String> result = synSapService.synCostCenterForSap(orgNumber);

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

    }

}
