package com.eactive.eai.rms.onl.loader.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.onl.loader.dao.RuleDeployJobDao;
import com.eactive.eai.rms.onl.loader.vo.RuleDeployJobItemVo;
import com.eactive.eai.rms.onl.loader.vo.RuleDeployJobVo;

@Service("ruleDeployJobService")
public class RuleDeployJobServiceImpl extends BaseService implements RuleDeployJobService {
	@Autowired
	private RuleDeployJobDao ruleDeployJobDao;

	@Override
	public void insert(RuleDeployJobVo jobVo) throws Exception {
		ruleDeployJobDao.insert(jobVo);

		List<RuleDeployJobItemVo> jobItemList = jobVo.getItemList();
		if (jobItemList == null) {
			return;
		}

		for (RuleDeployJobItemVo itemVo : jobItemList) {
			ruleDeployJobDao.insertItem(itemVo);
		}
	}

	@Override
	public void update(RuleDeployJobVo jobVo) throws Exception {
		ruleDeployJobDao.update(jobVo);
	}

	@Override
	public RuleDeployJobVo selectOne(String jobId) throws Exception {
		RuleDeployJobVo jobVo = ruleDeployJobDao.selectOne(jobId);
		List<RuleDeployJobItemVo> itemList = ruleDeployJobDao.selectItems(jobId);
		jobVo.setItemList(itemList);
		return jobVo;
	}

	@Override
	public List<RuleDeployJobVo> selectJobAll(RuleDeployJobVo searchJobVo, int startIdx, int endIdx) throws Exception {
		return ruleDeployJobDao.selectJobAll(searchJobVo, startIdx, endIdx);
	}

	@Override
	public void deleteHistory(String dateTime) throws Exception {
		ruleDeployJobDao.deleteItemHistory(dateTime);
		ruleDeployJobDao.deleteHistory(dateTime);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public HashMap<String, Object> selectList(int startNum, int endNum, String searchJobTitle, String searchStatus,
			String sortName, String sortOrder) throws Exception {
		HashMap paramMap = new HashMap();
		// 검색 조건
		paramMap.put("searchJobTitle", searchJobTitle);
		paramMap.put("searchStatus", searchStatus);
		paramMap.put("sortName", sortName);
		paramMap.put("sortOrder", sortOrder);

		// 전체 목록수 얻기
		int totalCount = ruleDeployJobDao.selectListCount(paramMap);

		paramMap.put("startNum", startNum);// 시작 record
		paramMap.put("endNum", endNum);// 끝 record

		List list = ruleDeployJobDao.selectList(paramMap);

		HashMap map = new HashMap();
		map.put("totalCount", totalCount);
		map.put("list", list);

		return map;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public HashMap selectJobDetail(String jobId) throws Exception {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("jobId", jobId);

		HashMap job = ruleDeployJobDao.selectJobDetail(paramMap);
		List<HashMap> jobItems = ruleDeployJobDao.selectJobItems(paramMap);

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("detail", job);
		map.put("rows", jobItems);

		return map;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void delete(HashMap paramMap) throws Exception {
		ruleDeployJobDao.deleteJobItems(paramMap);
		ruleDeployJobDao.delete(paramMap);
	}
}
