package org.ccpit.business.questionsManage;

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;

@Transactional
@Service
public class QuestionService {
	@Autowired
	private QuestionDao questionDao;
	@Autowired
	private LogInfoService logInfoService;
	
	public boolean addOrUpdateQuestion(Question question, String operate,
                                       String questionId, User user, HttpServletRequest request) {
		boolean flag = false;
		if ("add".equals(operate)) {
			question.setCreateTime(new Date());
			question.setCreator(user);
			flag = questionDao.save(question);
			if (flag) {
				// 记录用户操作日志信息
				logInfoService.recordLog(request, "新增问题数据信息");
			}
		} else if ("edit".equals(operate)) {
			Question question1 = queryQuestionById(Long.parseLong(questionId));
			PageUtil.setterValues(question, question1);
			question1.setModifyTime(new Date());
			question1.setModifyBy(user);
			flag = questionDao.update(question1);
			if (flag) {
				// 记录用户操作日志信息
				logInfoService.recordLog(request, "修改问题数据信息");
			}
		}
		return flag;
	}

	public boolean deleteQuestionById(String jsonIds) {
		JSONArray jsonArray = JSONArray.fromObject(jsonIds);
		Object[] ids = jsonArray.toArray();
		boolean flag = false;
		for (Object metadataId : ids) {
			Question question = questionDao.queryById(Long.valueOf(String
					.valueOf(metadataId)));
			question.setDeleted(true);
			flag = questionDao.update(question);
		}
		return flag;
	}

	public Question queryQuestionById(long id) {
		return questionDao.queryById(id);
	}

	public List<Question> queryQuestionList(){
		List<Question> questionList = questionDao.queryAll();
		return questionList;
	}

	public void update(Question question){
		questionDao.update(question);
	}
	
	/**
	 * 
	 * isExistQuestion:(根据标题判断问题信息是否存在). <br/>
	 * @author 
	 * @param
	 * @since JDK 1.8.0
	 */
	public boolean queryQuestionByTitle(String questionTitle) {
		if (null != questionTitle && !"".equals(questionTitle)) {
			try {
				questionTitle = java.net.URLDecoder.decode(questionTitle, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		List<Question> questionList = questionDao.query("from Question where title = '" + questionTitle + "'", null);
		if (null != questionList && questionList.size() > 0) {
			return true;
		}
		return false;
	}
	
	public Page<Question> queryQuestions(PageRequest pageRequest, String questionTitle) {
		StringBuffer hql = new StringBuffer("from Question where 1=1");
		if (questionTitle != null && !"".equals(questionTitle)) {
			hql.append(" and title like '%" + questionTitle + "%'");
		}
		hql.append(" order by createTime desc");
		return questionDao.findPage(pageRequest, hql.toString(), null);
	}

	public Map<String, Object> convertToMap(Question question) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (question == null) {
			return map;
		}
		map.put("id", question.getId());
		map.put("title", question.getTitle());
		map.put("createTime", StringUtil.DATE_FORMATE.format(question.getCreateTime()));
		map.put("clickVolume", question.getClickVolume());
		map.put("creator", question.getCreator().getUsername());

		return map;
	}

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