package com.goafanti.admin.service.impl;

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

import com.goafanti.common.utils.AsyncUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.admin.bo.NoticeBo;
import com.goafanti.admin.service.NoticeService;
import com.goafanti.common.dao.NoticeMapper;
import com.goafanti.common.enums.NoticeTypes;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;

@Service
public class NoticeServiceImpl extends BaseMybatisDao<NoticeMapper> implements NoticeService {
	@Autowired
	private NoticeMapper noticeMapper;
	@Autowired
	private AsyncUtils asyncUtils;


	@Override
	public List<Map<String, Object>> updateListUnreadNoticeByAid() {
		List<Map<String, Object>> list = new ArrayList<>();
		List<NoticeBo> nl = noticeMapper.findUnreadNoticeListByPage(TokenManager.getAdminId());
		List<String> dl = new ArrayList<String>();

		for (int i = NoticeTypes.getCount(); i >= 0; i--) {
			Map<String, Object> map = new HashMap<String, Object>();
			List<NoticeBo> cffk = new ArrayList<NoticeBo>();
			for (NoticeBo n : nl) {
				// 0专利提醒
				if (n.getType() == i) {
					cffk.add(n);
				} else if (n.getType() == i) {
					cffk.add(n);
				} else if (n.getType() == i) {
					cffk.add(n);
				} else if (n.getType() == i) {
					cffk.add(n);
				} else if (n.getType() == i) {
					cffk.add(n);
				} else if (n.getType() == i) {
					cffk.add(n);
				} else if (n.getType() == i) {
					cffk.add(n);
				}
			}
			map.put("name", NoticeTypes.getValueByCode(i));
			map.put("id", i);
			map.put("list", cffk);
			list.add(map);
		}
		if (!nl.isEmpty()) {
			for (NoticeBo n : nl) {
				// 添加已读
				dl.add(n.getId());
			}
			asyncUtils.batchUpdateUnreaded(dl);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<NoticeBo> ListReadedNoticeByAid(Integer type, Integer pageNo, Integer pageSize) {
		Map<String, Object> params = new HashMap<>();
		params.put("aid", TokenManager.getAdminId());
		params.put("type", type==null?0:type);
		return (Pagination<NoticeBo>) findPage("findReadedNoticeListByPage", "findReadedNoticeCount", params, pageNo,
				pageSize);
	}

	@Override
	public int findUnreadNoticeCount() {
		return noticeMapper.selectUnreadedCount(TokenManager.getAdminId());
	}

	@Override
	public List<NoticeBo> pushPublicReleaseUnread() {
		List<NoticeBo> nl=noticeMapper.publicReleaseUnread(TokenManager.getAdminId());
		List<String> dl=new  ArrayList<String>();
		if (!nl.isEmpty()) {
			for (NoticeBo n : nl) {
				// 添加已读
				dl.add(n.getId());
			}
			asyncUtils.batchUpdateUnreaded(dl);
		}
		return nl;
	}


}
