package com.eactive.eai.common.pushinfo;

import java.util.ArrayList;
import java.util.List;

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

import com.eactive.eai.common.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.pushinfo.loader.PushInfoDetailLoader;
import com.eactive.eai.common.pushinfo.loader.PushInfoLoader;
import com.eactive.eai.common.pushinfo.mapper.PushInfoDetailMapper;
import com.eactive.eai.common.pushinfo.mapper.PushInfoMapper;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.data.entity.onl.pushinfo.PushInfo;
import com.eactive.eai.data.entity.onl.pushinfo.PushInfoDetail;

@Service
@Transactional
public class PushInfoDAO extends BaseDAO {
	private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
	
	@Autowired
	private PushInfoLoader pushInfoLoader;

	@Autowired
	private PushInfoMapper pushInfoMapper;
	
	@Autowired
	private PushInfoDetailLoader pushInfoDetailLoader;

	@Autowired
	private PushInfoDetailMapper pushInfoDetailMapper;
	
	public void insertPushInfo(PushInfoVO pushInfoVO) throws DAOException {
		try {
			PushInfo entity = pushInfoMapper.toEntity(pushInfoVO);
			pushInfoLoader.save(entity);
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICKE118"), e);
		}
	}

	public void batchInsertPushInfoDetail(List<PushInfoDetailVO> pushInfoDetailVoList) throws DAOException {
		try {
			List<PushInfoDetail> list = new ArrayList<>();
			pushInfoDetailVoList.forEach(vo -> list.add(pushInfoDetailMapper.toEntity(vo)) );
			pushInfoDetailLoader.saveAll(list);
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICKE118"), e);
		}
	}
	
	public long getPushInfoDetailCount() {
		return pushInfoDetailLoader.count();
	}
}
