package com.brilliance.isc.common.transaction.help.impl;

import com.brilliance.isc.common.transaction.help.LiaallSet;
import com.brilliance.isc.common.transaction.help.TransactionHelpService;
import com.brilliance.isc.common.transaction.help.TransactionServiceSet;
import com.brilliance.isc.common.transaction.vo.BimdataVo;
import com.brilliance.isc.common.transaction.vo.TroStoreObjectVo;
import org.springframework.stereotype.Service;

@Service
public class TransactionHelpServiceImpl implements TransactionHelpService {

    private static ThreadLocal<TransactionServiceSet> transactionStoreSetSetLocal = new ThreadLocal<TransactionServiceSet>(){
        @Override
        protected TransactionServiceSet initialValue() {
            return new TransactionServiceSet();
        }
    };

    private static ThreadLocal<LiaallSet> liaallSetLocal = new ThreadLocal(){
        @Override
        protected LiaallSet initialValue() {
            return new LiaallSet();
        }
    };

    public void clearTransactionStoreSet(){
        transactionStoreSetSetLocal.remove();
    }

    public TransactionServiceSet getTransactionStoreSet(){
        return transactionStoreSetSetLocal.get();
    }

    public void clearLiaallSet(){
        liaallSetLocal.remove();
    }

    public LiaallSet getLiaallSet(){
        return liaallSetLocal.get();
    }

    @Override
    public String getTrninr() {
        if (getTransactionStoreSet().getTrnmodVo() != null &&
                getTransactionStoreSet().getTrnmodVo().getTrn() != null) {
            return getTransactionStoreSet().getTrnmodVo().getTrn().getInr();
        }
        return "";
    }

    @Override
    public int getGlenmb() {
        if (getTransactionStoreSet().getTrnmodVo() != null) {
            return getTransactionStoreSet().getTrnmodVo().getGlenmb();
        }
        return 0;
    }

    @Override
    public String getCmtflg() {
        if (getTransactionStoreSet().getTrnmodVo() != null) {
            return getTransactionStoreSet().getTrnmodVo().getCmtflg();
        }
        return "";
    }

    @Override
    public String getSptsta() {
        if (getTransactionStoreSet().getTrnmodVo() != null &&
                getTransactionStoreSet().getTrnmodVo().getSpt() != null) {
            return getTransactionStoreSet().getTrnmodVo().getSpt().getSta();
        }
        return "";
    }

    @Override
    public void addBimdataUpdateModel(Object... objects) {
        if (objects == null || objects.length == 0) {
            return;
        }
        BimdataVo bimdataVo = this.getTransactionStoreSet().getBimdataVo();
        if (bimdataVo != null) {
            for (Object object : objects) {
                bimdataVo.addModel(new TroStoreObjectVo(object));
            }
        }
    }

    @Override
    public void addBimdataDeleteModel(Object... objects) {
        if (objects == null || objects.length == 0) {
            return;
        }
        BimdataVo bimdataVo = this.getTransactionStoreSet().getBimdataVo();
        if (bimdataVo != null) {
            for (Object object : objects) {
                bimdataVo.addDeleteModel(new TroStoreObjectVo(object));
            }
        }
    }
}
