package com.brilliance.isc.common.cbsmod.service.impl;

import com.brilliance.isc.bo.Cbe;
import com.brilliance.isc.bo.Lsm;
import com.brilliance.isc.common.cbsmod.service.LscmodService;
import com.brilliance.isc.common.util.SettleExceptionUtils;
import com.brilliance.isc.mda.dao.LsmMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.brilliance.mda.runtime.mda.Constants.*;

@Service
public class LscmodServiceImpl implements LscmodService {

    @Autowired
    private LsmMapper lsmMapper;

    @Override
    public void lsiDeleteCbeEntry(Cbe cbe) {

    }

    @Override
    public void lsiDeleteObject(String objTyp, String objInr) {

    }

    /**
     *   source:lscmod.0034.script
     */
    public String lsiGetCBEStatus(Cbe argCbe) {
        //! Function to determine and return the limitstatus of a single LSM request
        //! ArgCbe specifies the CBE-Entry of the owning contract for which the status is requested
        // Used in LIAONE to decide whether there are pending limit requests and to
        // optionally include the SRVPDL into the workflow
        Lsm lsm = lscReadLSMByCbe(argCbe.getInr());
        if (lsm == null) {
            return  LscAccepted;
        }
        switch(lsm.getLsmflg() ) {
            case "N":
            case  " ":
                return  LscPending;
            case "A":
            case  "O":
                return  LscAccepted;
            case "R":
                return  LscRejected;
            default:
                SettleExceptionUtils.sysDump("LSCMOD.LsiGetCBEStatus: LscReadLSMByCbe return unknown LSMFLG '" + lsm.getLsmflg() + "'.");
        }
        return null;
    }

    /**
     *   source:lscmod.0020.script
     */
    public Lsm lscReadLSMByCbe(String cbeinr) {
        return lsmMapper.getLsmByCbeinr(cbeinr);
    }
}
