package com.yihu.wlyy.service.app.health;

import com.yihu.wlyy.entity.education.HealthEduArticle;
import com.yihu.wlyy.entity.education.HealthEduArticleDoctor;
import com.yihu.wlyy.entity.education.HealthEduArticleLabel;
import com.yihu.wlyy.entity.education.HealthEduArticlePatient;
import com.yihu.wlyy.repository.education.HealthEduArticleDoctorDao;
import com.yihu.wlyy.repository.education.HealthEduArticleLabelDao;
import com.yihu.wlyy.service.BaseService;
import com.yihu.wlyy.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

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

/**
 * 健康教育患者业务逻辑类
 * @author linz
 *
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class HealthEduArticleLabelService extends BaseService {

	@Autowired
	private HealthEduArticleLabelDao healthEduArticleLabelDao;

	/**
	 * 更新标签
	 * @param keyword 关键字
	 * @param user 人员
	 * @return
	 *
	 */
	public void saveOrUpdateLabel(String keyword,String user) {
		 HealthEduArticleLabel healthEduArticleLabel =  healthEduArticleLabelDao.findByPublicKeyword(keyword);
		 if(healthEduArticleLabel==null){//公共标签不存在则添加
			 HealthEduArticleLabel publicLabel = new HealthEduArticleLabel();
			 publicLabel.setAmount(1);
			 publicLabel.setCreater("public");
			 publicLabel.setKeyword(keyword);
			 publicLabel.setCzrq(DateUtil.getNow());
			 this.healthEduArticleLabelDao.save(publicLabel);
		 }else{//存在更新次数
			 int amount = healthEduArticleLabel.getAmount();
			 healthEduArticleLabel.setAmount(amount+1);
			 healthEduArticleLabel.setCzrq(DateUtil.getNow());
			 this.healthEduArticleLabelDao.save(healthEduArticleLabel);
		 }
		HealthEduArticleLabel userLabel =  healthEduArticleLabelDao.findByKeywordAndCreater(keyword, user);
		if(userLabel==null){//个人标签
			userLabel = new HealthEduArticleLabel();
			userLabel.setAmount(1);
			userLabel.setCreater(user);
			userLabel.setKeyword(keyword);
			userLabel.setCzrq(DateUtil.getNow());
			this.healthEduArticleLabelDao.save(userLabel);
		}else{//存在更新次数
			int amount = userLabel.getAmount();
			userLabel.setAmount(amount+1);
			userLabel.setCzrq(DateUtil.getNow());
			this.healthEduArticleLabelDao.save(userLabel);
		}
	}

	/**
	 * 获取文章列表
	 * @param page 页码
	 * @param pagesize 分页大小
	 * @return 列表
	 */
	public Page<HealthEduArticleLabel> findAll(int page, int pagesize,int type,String user) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		if(page<0){
			page = 0;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC,"amount","czrq");
		// 分页信息
		PageRequest pageRequest = new PageRequest(page, pagesize, sort);
		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		if(type==1){
			filters.put("creater", new SearchFilter("creater", SearchFilter.Operator.EQ, "public"));
		}else if(type==2){
			filters.put("creater", new SearchFilter("creater", SearchFilter.Operator.EQ, user));
		}
		Specification<HealthEduArticleLabel> spec = DynamicSpecifications.bySearchFilter(filters.values(), HealthEduArticleLabel.class);
		return healthEduArticleLabelDao.findAll(spec, pageRequest);
	}

}
