package com.palmnest.service.notice.impl;

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

import lombok.extern.slf4j.Slf4j;

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

import com.palmnest.application.dto.DeptGroupDto;
import com.palmnest.dao.notice.NoticeApprovalsetDao;
import com.palmnest.dao.notice.NoticeSubGroupDao;
import com.palmnest.model.notice.NoticeSubGroupModel;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.notice.NoticeSubGroupManager;

@Slf4j
@Service("noticeSubGroupManager")
public class NoticeSubGroupManagerImpl extends GenericManagerImpl<NoticeSubGroupModel, Integer> implements NoticeSubGroupManager{
	private NoticeSubGroupDao noticeSubGroupDao;
	
	@Autowired
	public void setNoticeSubGroupDao(NoticeSubGroupDao noticeSubGroupDao) {
		this.noticeSubGroupDao = noticeSubGroupDao;
		this.dao=noticeSubGroupDao;
	}


	/**
	 * 保存公告与分组的关系
	 * @param type 
	 * @param sourceId
	 * @param dtoList
	 * @param createBy
	 */
	@Override
	public void saveByDeptGroup(Integer type, Integer sourceId, List<DeptGroupDto> dtoList,Integer createBy) {
		if(null == sourceId || CollectionUtils.isEmpty(dtoList)){
			return;
		}
		noticeSubGroupDao.delBySourceId(type, sourceId, createBy);
		for(DeptGroupDto dto:dtoList){
			if(null == dto || null == dto.getId() || null == dto.getType()){
				continue;
			}
			NoticeSubGroupModel model=new NoticeSubGroupModel();
			model.setType(type);
			model.setSource_id(sourceId);
			model.setDept_group_type(dto.getType());
			model.setDept_group_id(dto.getId());
			model.setDept_group_name(dto.getName());
			model.setCreated_at(new Date());
			model.setCreated_by(createBy);
			model.setEnabled(1);
			noticeSubGroupDao.save(model);
		}
		log.info("添加成功");
		
	}


	@Override
	public List<DeptGroupDto> getDeptGroupDto(Integer type, Integer sourceId) {
		List<DeptGroupDto> list=new ArrayList<DeptGroupDto>();
		List<NoticeSubGroupModel> modelList=noticeSubGroupDao.findBySourceId(type, sourceId);
		if(CollectionUtils.isEmpty(modelList)){
			return list;
		}
		for(NoticeSubGroupModel model:modelList){
			if(null == model || null == model.getDept_group_type() || null == model.getDept_group_id()){
				continue;
			}
			DeptGroupDto dto=new DeptGroupDto();
			dto.setType(model.getDept_group_type());
			dto.setId(model.getDept_group_id());
			dto.setName(model.getDept_group_name());
			list.add(dto);
		}
		
		return list;
	}


	@Override
	public List<NoticeSubGroupModel> findBySourceId(Integer type,
			Integer sourceId) {
		return noticeSubGroupDao.findBySourceId(type, sourceId);
	}

}
