package com.yonge.cooleshow.cms.service.impl;

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

import com.yonge.cooleshow.cms.dal.entity.NewsStatusEnum;
import com.yonge.cooleshow.cms.dto.SysNewsInformationDto;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.mybatis.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonge.cooleshow.cms.dal.dao.HelpCenterContentDao;
import com.yonge.cooleshow.cms.dal.entity.HelpCenterContent;
import com.yonge.cooleshow.cms.service.HelpCenterContentService;
import com.yonge.toolset.mybatis.dal.BaseDAO;
import org.springframework.transaction.annotation.Transactional;

@Service
public class HelpCenterContentServiceImpl extends BaseServiceImpl<Long, HelpCenterContent> implements HelpCenterContentService {

	@Autowired
	private HelpCenterContentDao contentCenterDao;

	@Override
	public BaseDAO<Long, HelpCenterContent> getDAO() {
		return contentCenterDao;
	}

	@Override
	@Transactional
	public boolean updateContentByCatalog(Long catalogId, Integer status) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("catalogId", catalogId);
		map.put("status", status);
		contentCenterDao.updateContentByCatalog(map);
		return true;
	}

	@Override
	@Transactional
	public boolean deleteContentByCatalog(Long catalogId) {
		contentCenterDao.deleteContentByCatalog(catalogId);
		return true;
	}

    @Override
	@Transactional
    public boolean status(Long id) {
		HelpCenterContent helpCenterContent = contentCenterDao.get(id);
		if (helpCenterContent.getCatalogId() != 2) {
			contentCenterDao.status(id);
			return true;
		}
		if (helpCenterContent.getStatus().equals(NewsStatusEnum.SHOW.getCode())) {
			contentCenterDao.status(id);
			return true;
		}
		Integer count = contentCenterDao.selectByNoticeStatus(NewsStatusEnum.SHOW.getCode(),helpCenterContent.getCatalogType());

		if (count >0) {
			throw  new BizException("有正在启用的公告");
		}

		contentCenterDao.status(id);
		return true;
    }

    @Override
	@Transactional
    public boolean noticeRelease(HelpCenterContent helpCenterContent) {
        if (helpCenterContent.getCatalogId() == 1 || helpCenterContent.getReleaseStatus() == 1) {
			return false;
		}

		HelpCenterContent centerContent = contentCenterDao.get(helpCenterContent.getId());
		if (centerContent.getReleaseTime().compareTo(helpCenterContent.getReleaseTime()) > 0) {
			return false;
		}

		// 发送老师端
		if ("TEACHER".equals(helpCenterContent.getCatalogType())) {
			contentCenterDao.noticeReleaseToTeacher(helpCenterContent);
		} else if ("STUDENT".equals(helpCenterContent.getCatalogType())) {
			// 发送学生端
			contentCenterDao.noticeReleaseToStudent(helpCenterContent);
		}
		helpCenterContent.setReleaseStatus(1);
		return contentCenterDao.update(helpCenterContent)>0;
    }

	@Override
	public List<HelpCenterContent> getNeedReleaseNotice() {
		return contentCenterDao.selectNeedReleaseNotice();
	}

}
