package kr.co.jinwoosi.clfd.framework.cd.service.impl;


import kr.co.jinwoosi.clfd.framework.base.service.impl.BaseServiceImpl;
import kr.co.jinwoosi.clfd.framework.cd.mapper.CdDtlMapper;
import kr.co.jinwoosi.clfd.framework.cd.model.Cd;
import kr.co.jinwoosi.clfd.framework.cd.model.CdDtl;
import kr.co.jinwoosi.clfd.framework.cd.service.CdDtlService;
import kr.co.jinwoosi.clfd.framework.cd.service.CdService;
import kr.co.jinwoosi.clfd.framework.util.ApplicationHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(value="cdDtlService")
public class CdDtlServiceImpl extends BaseServiceImpl<CdDtl, CdDtl, CdDtlMapper> implements CdDtlService {
	
	@Resource (name="cdService")
	private CdService cdService;
	
	@Override
	@Resource (name="cdDtlMapper")
	protected void setMapper (CdDtlMapper mapper) {
		super.setMapper(mapper);
	}

	@Override
	public Integer existCount(CdDtl cdDtl) {
		return mapper.existCount(cdDtl);
	}
	
	@Override
	public Map<String, List<CdDtl>> selectJson() {
		if(ApplicationHelper.getCdDtlMap() == null) {
			initJson();
		}
		
		return ApplicationHelper.getCdDtlMap();
	}
	
	@Override
	public List<CdDtl> selectJson(String cdId) {
		Map<String, List<CdDtl> > cdDtlMap = selectJson();
		return cdDtlMap.get(cdId);
	}

	@Override
	public void initJson() {
		Map<String, List<CdDtl>> cdDtlMap = new HashMap<String, List<CdDtl>>();
		Cd cdSearch = new Cd();
		CdDtl cdDtl;
		List<Cd> cdList = cdService.selectList(cdSearch);
		for(Cd cd : cdList) {
			cdDtl = new CdDtl();
			cdDtl.setCdId(cd.getCdId());
			cdDtl.setSort("CD_DTL_ORDER");
			cdDtlMap.put(cd.getCdId(), selectList(cdDtl));
		}
		
		ApplicationHelper.setCdDtlMap(cdDtlMap);
	}

	@Override
	public Integer countChek(CdDtl cdDtl) {
		return mapper.countChek(cdDtl);
	}

	@Override
	public List<CdDtl> selectCdDtlList(String cdId){
		return mapper.selectCdDtlList(cdId);
	}

}
