package com.goafanti.admin.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

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

import com.goafanti.admin.bo.KnowledgeBaseBo;
import com.goafanti.admin.service.CommunityService;
import com.goafanti.common.dao.JtCollectSearchMapper;
import com.goafanti.common.dao.JtKnowledgeBaseMapper;
import com.goafanti.common.enums.collectType;
import com.goafanti.common.model.JtCollectSearch;
import com.goafanti.common.model.JtKnowledgeBase;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
@Service
public class AdminCommunityServiceImpl extends BaseMybatisDao<JtKnowledgeBaseMapper> implements CommunityService {
	
	@Autowired
	private JtKnowledgeBaseMapper jtKnowledgeBaseMapper;
	
	@Autowired
	private JtCollectSearchMapper collectSearchMapper;
	
	@Autowired
	JtCollectSearchMapper	jtCollectSearchMapper;

	@Override
	public Pagination<KnowledgeBaseBo> getJtKnowledgeBaseList(KnowledgeBaseBo knowledgeBase, Integer pageNo,
			Integer pageSize, Integer ob) {
		Map<String,Object> params = new HashMap<String, Object>();
		if(null != knowledgeBase) params.put("v", knowledgeBase);
		params.put("ob", ob);//按创建时间排序
		@SuppressWarnings("unchecked")
		Pagination<KnowledgeBaseBo> kb = (Pagination<KnowledgeBaseBo>) findPage("getKnowledgeBaseListBySome","getKnowledgeBaseCountBySome",params,pageNo,pageSize);
		return kb;
		
	}

	@Override
	public KnowledgeBaseBo getCommunintyById(JtKnowledgeBase o) {
		return jtKnowledgeBaseMapper.getKnowledgeBaseById(o);
	}

	@Override
	public int insertCommunity(JtKnowledgeBase o) {
		//序号
		o.setId(UUID.randomUUID().toString());
		//创建人
		o.setOwner(TokenManager.getUserId());
		//关键字处理
		if (null != o.getKeyword() && o.getKeyword().length()>0) {
			addKeyword(o);
		}
		//发布时间处理
		if(1 == o.getStatus()){
			o.setCreateTime(new Date());
			o.setReleaseTime(new Date());
		}
		return jtKnowledgeBaseMapper.insertSelective(o);
	}

	@Override
	public int updateCommunity(JtKnowledgeBase o) {
		if(o.getStatus() == 1){
			o.setReleaseTime(new Date());
		}else
		//删除关键字
		if( "".equals(o.getKeyword())){
			updateJtCollectSearch(o.getId());
		}
		//新增关键字
		if (null != o.getKeyword() && o.getKeyword().length()>0) {
			updateJtCollectSearch(o.getId());
			addKeyword(o);
		}
		return jtKnowledgeBaseMapper.updateBySome(o);
	}

	@Override
	public int nameCheck(String name) {
		return jtKnowledgeBaseMapper.nameCheck(name);
	}

	@Override
	public int addRead(String id) {
		return jtKnowledgeBaseMapper.addOneRead(id);
	}

	@Override
	public int addComment(String id) {
		return jtKnowledgeBaseMapper.addOneComment(id);
	}

	@Override
	public int addStar(String id) {
		return jtKnowledgeBaseMapper.addOneStar(id);
	}
	
	
	//关键字放在关键字表中
	private int addKeyword(JtKnowledgeBase o){
		JtCollectSearch r=new JtCollectSearch();
		r.setFromTable(collectType.KNOWLEDGE_BASE.getDesc());
		r.setKeyword(o.getKeyword());
		r.setTitle(o.getTitle());
		r.setRowId(o.getId().toString());
		String sum = o.getSummary();
		if(null != sum && sum.length() > 40){
			sum = sum.substring(0,40) + "...";
		}
		r.setSummary(sum);
		
		return jtCollectSearchMapper.insertSelective(r);
	}
	
	private int updateJtCollectSearch(String id) {
		return jtCollectSearchMapper.deleteByrowId(id);
	}
}
