/**
 * Company Name : 中贸促信息技术有限责任公司
 * Project Name:sfzxWebsite
 * File Name:NoticeService.java
 * Package Name:ccpit.business.noticeManage
 * Date:2017年4月12日上午11:22:49
 * Copyright (c) 2017, dingwei@ccpit.org All Rights Reserved.
 *
*/

package org.ccpit.business.noticeManage;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import net.sf.json.JSONArray;

import org.ccpit.base.controller.Convert;
import org.ccpit.base.controller.Page;
import org.ccpit.base.controller.PageBo;
import org.ccpit.base.controller.PageRequest;
import org.ccpit.base.logManage.LogInfoService;
import org.ccpit.base.user.User;
import org.ccpit.base.utils.StringUtil;
import org.ccpit.base.utils.page.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ClassName:NoticeService <br/>
 * Function: TODO 通知公告信息管理业务逻辑层. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2017年4月12日 上午11:22:49 <br/>
 * @author   dingwei
 * @version  
 * @since    JDK 1.8.0
 * @see 	 
 */
@Transactional
@Service
public class NoticeService {

	@Autowired
	private NoticeDao noticeDao;
	@Autowired
	private LogInfoService logInfoService;
	
	public boolean addOrUpdateNotice(Notice Notice, String operate,
                                     String NoticeId, User user, HttpServletRequest request) {
		boolean flag = false;
		if ("add".equals(operate)) {
			Notice.setCreateTime(new Date());
			Notice.setCreator(user);
			flag = noticeDao.save(Notice);
			if (flag) {
				// 记录用户操作日志信息
				logInfoService.recordLog(request, "新增通知公告数据信息");
			}
		} else if ("edit".equals(operate)) {
			Notice Notice1 = queryNoticeById(Long.parseLong(NoticeId));
			PageUtil.setterValues(Notice, Notice1);
			Notice1.setModifyTime(new Date());
			Notice1.setModifyBy(user);
			flag = noticeDao.update(Notice1);
			if (flag) {
				// 记录用户操作日志信息
				logInfoService.recordLog(request, "修改通知公告数据信息");
			}
		}
		return flag;
	}

	public List<Notice> queryNoticeList(int noticeType){
		List<Notice> noticeList = noticeDao.query("from Notice where noticeType ="+noticeType + "order by createTime desc", null);
		return noticeList;
	}
	
	public boolean deleteNoticeById(String jsonIds) {
		JSONArray jsonArray = JSONArray.fromObject(jsonIds);
		Object[] ids = jsonArray.toArray();
		boolean flag = false;
		for (Object metadataId : ids) {
			Notice Notice = noticeDao.queryById(Long.valueOf(String
					.valueOf(metadataId)));
			Notice.setDeleted(true);
			flag = noticeDao.update(Notice);
		}
		return flag;
	}

	public Notice queryNoticeById(long id) {
		return noticeDao.queryById(id);
	}

	/**
	 * 
	 * isExistNotice:(根据标题判断新闻信息是否存在). <br/>
	 * 
	 * @author dingwei
	 * @param noticeTitle
	 * @return Date:2017年3月22日上午9:30:39
	 * @since JDK 1.8.0
	 */
	public boolean queryNoticeByTitle(String noticeTitle) {
		if (null != noticeTitle && !"".equals(noticeTitle)) {
			try {
				noticeTitle = java.net.URLDecoder.decode(noticeTitle, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		List<Notice> NoticeList = noticeDao.query("from Notice where noticeTitle = '" + noticeTitle + "'", null);
		if (null != NoticeList && NoticeList.size() > 0) {
			return true;
		}
		return false;
	}
	
	public Page<Notice> queryNotices(PageRequest pageRequest, String noticeTitle,
                                     String newsType) {
		StringBuffer hql = new StringBuffer("from Notice where 1=1");
		if (noticeTitle != null && !"".equals(noticeTitle)) {
			hql.append(" and noticeTitle like '%" + noticeTitle + "%'");
		}
		if (newsType != null && !"".equals(newsType)) {
			hql.append(" and noticeType = " + Integer.parseInt(newsType));
		}
		hql.append(" order by createTime desc");
		return noticeDao.findPage(pageRequest, hql.toString(), null);
	}

	public Map<String, Object> convertToMap(Notice notice) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (notice == null) {
			return map;
		}
		map.put("id", notice.getId());
		map.put("noticeTitle", notice.getNoticeTitle());
		map.put("noticeType", null == notice.getNoticeType() ? "" : 1 == notice.getNoticeType() ? "通知公告" : "政策法规");
		map.put("createTime", StringUtil.DATE_FORMATE.format(notice.getCreateTime()));
		map.put("clickVolume", notice.getClickVolume());
		map.put("creator", notice.getCreator().getUsername());

		return map;
	}

	public PageBo<Notice> convert(Page<Notice> page) {
		return new PageBo<Notice>(page, new Convert<Notice>() {
			@Override
			public Map<String, Object> convert(Notice obj) {
				return convertToMap(obj);
			}
		});
	}
}

