package kr.co.jinwoosi.clfd.clfd.dgns.service;

import kr.co.jinwoosi.clfd.clfd.dgns.mapper.SkTyMapper;
import kr.co.jinwoosi.clfd.clfd.dgns.model.DgnsDtl;
import kr.co.jinwoosi.clfd.clfd.dgns.model.SkTy;
import kr.co.jinwoosi.clfd.clfd.dgns.model.SkTySearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SkTyService {

    public static List<SkTy> cashedSkTyList = null;

    @Autowired
    private SkTyMapper skTyMapper;

    public List<SkTy> selectList(SkTySearch skTySearch) {
        return skTyMapper.selectList(skTySearch);
    }

    public SkTy select(SkTySearch skTySearch) {
        return skTyMapper.select(skTySearch);
    }

    public SkTy selectSkTyByDgnsDtlList(List<DgnsDtl> dgnsDtlList) {
        StringBuilder skTyCdSb = new StringBuilder();
        for(DgnsDtl dgnsDtl : dgnsDtlList) {
            skTyCdSb.append(dgnsDtl.getDgnsSkTyDtlCd());
        }
        String skTyCd = skTyCdSb.toString();
        List<SkTy> skTyList = getCashedSkTyList(false);
        return skTyList.stream()
                       .filter(skTy -> skTy.getSkTyCd().equals(skTyCd))
                       .findFirst()
                       .orElse(null);
    }

    public List<SkTy> getCashedSkTyList(boolean refresh) {
        if (refresh || cashedSkTyList == null) {
            cashedSkTyList = selectList(new SkTySearch());
        }
        return cashedSkTyList;
    }
}
