package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.collect;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxThirdPartyDbCollectDataStrategyService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxThirdPartyDbCollectDataModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

@Slf4j
public class SxThirdPartyDbCollectDataProxyParitySyncService implements ISxThirdPartyDbCollectDataStrategyService {

    private ISxThirdPartyDbCollectDataStrategyService proxyObject;

    public SxThirdPartyDbCollectDataProxyParitySyncService(ISxThirdPartyDbCollectDataStrategyService proxyObject) {
        this.proxyObject = proxyObject;
    }

    @Override
    public void exec(SxThirdPartyDbCollectDataModel collect) {

        log.info("工厂{}第三方DB数据采集 --- CFG_CODE: {} EXEC --- CFG_KEY: {} EXEC --- EXEC PROXY PARITY SYNC --- START ---", collect.getOrganizeCode(), collect.getCfgCode(), collect.getObjectCode());

        if (!checkFieldIsNull(collect)) return;

        proxyObject.exec(collect);

        if (!saveParitySyncData(collect)) return;

        proxyObject.exec(collect);

        log.info("工厂{}第三方DB数据采集 --- CFG_CODE: {} EXEC --- CFG_KEY: {} EXEC --- EXEC PROXY PARITY SYNC --- END ---", collect.getOrganizeCode(), collect.getCfgCode(), collect.getObjectCode());
    }

    private Boolean checkFieldIsNull(SxThirdPartyDbCollectDataModel collect) {
        if (!checkFieldIsNull(collect, collect.getParitySyncTableName(), MesPcnExtConstWords.PARITY_SYNC_TABLE_NAME, "对等同步表名")) return false;
        return true;
    }

    @Override
    public Boolean checkFieldIsNull(SxThirdPartyDbCollectDataModel collect, String fieldValue, String field, String fieldName) {
        return proxyObject.checkFieldIsNull(collect, fieldValue, field, fieldName);
    }

    @Override
    public Boolean saveParitySyncData(SxThirdPartyDbCollectDataModel collect) {
        if (CollectionUtils.isEmpty(collect.getMapList())) return false;
        return proxyObject.saveParitySyncData(collect);
    }

}
