package com.brilliance.isc.common.transaction.cecm.service.impl;

import com.brilliance.isc.bo.Trn;
import com.brilliance.isc.common.context.SpringEnvContext;
import com.brilliance.isc.common.exception.CommonServiceException;
import com.brilliance.isc.common.transaction.cecm.service.AbstractBatchStoreService;
import com.brilliance.isc.common.transaction.cecm.service.BatchStoreService;
import com.brilliance.isc.common.trnmod.vo.TrnmodVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Service("cecm.execute")
public class CecmBatchStoreExecuteServiceImpl extends AbstractBatchStoreService {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private static List<String> batchList = null;

    @PostConstruct
    public void initBatchSrvList(){
        batchList = new ArrayList<>();
        batchList.add("git_opn");
    }

    @Override
    public void execute(TrnmodVo trnmod){
        //保函开立交易
        for(String srv :batchList ){
            this.getCecmBatchService(srv).txnSave(trnmod);
        }
    }

    @Override
    public void executeRepair(Trn trn){
        //保函开立交易
        for(String srv :batchList ){
            this.getCecmBatchService(srv).repair(trn);
        }
    }

    public BatchStoreService getCecmBatchService(String srv){
        BatchStoreService cecmBatchService = SpringEnvContext.getBean(srv + ".cecmbatch");
        if (cecmBatchService == null) {
            throw new CommonServiceException("cecmBatchService [" + srv + "] unknown");
        }
        return cecmBatchService;
    }
}
