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

import com.yihu.wlyy.entity.IdEntity;
import com.yihu.wlyy.entity.education.HealthEduArticleOpHistory;
import com.yihu.wlyy.entity.education.HealthEduArticlePatient;
import com.yihu.wlyy.repository.education.HealthEduArticleOpHistoryDao;
import com.yihu.wlyy.repository.education.HealthEduArticlePatientDao;
import com.yihu.wlyy.service.BaseService;
import com.yihu.wlyy.util.DateUtil;
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 org.springside.modules.persistence.SearchFilter.Operator;

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

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

	@Autowired
	private HealthEduArticleOpHistoryDao healthEduArticleOpHistoryDao;

	/**
	 * 汇总阅读量
	 * @param code
	 * @return
	 */
	public  int countReadAmount(String code){
		return healthEduArticleOpHistoryDao.countByCodeStatus(code,HealthEduArticleOpHistory.READ_STATUS);
	}



	/**
	 * 汇总转发量
	 * @param code
	 * @return
	 */
	public int countRepeatAmount(String code){
		return healthEduArticleOpHistoryDao.countByCodeStatus(code,HealthEduArticleOpHistory.REPEAT_STATUS);
	}

	/**
	 * 获取收藏量
	 * @param code
	 * @return
	 */
	public  int countCollectionAmount(String code){
		return healthEduArticleOpHistoryDao.countByCodeStatus(code,HealthEduArticleOpHistory.COLLECTION_STATUS);
	}

	public int delCollectAmount(String code,String user){
		return healthEduArticleOpHistoryDao.deleteByCodeAndStatusAndCreater(code,HealthEduArticleOpHistory.COLLECTION_STATUS,user);
	}

	public int countByUserStatus(String user,String status){
		return healthEduArticleOpHistoryDao.countByCodeStatus(user,status);
	}

	/**
	 *
	 * @param status 状态
	 * @param code 文章编号
	 * @param title 文章标题
	 * @param user 操作人
	 * @return
	 */
	public HealthEduArticleOpHistory saveByStatus(String status,String code,String title,String user){

		HealthEduArticleOpHistory healthEduArticleOpHistory = new HealthEduArticleOpHistory();
		healthEduArticleOpHistory.setCode(code);
		healthEduArticleOpHistory.setCreatedTime(DateUtil.getNow());
		healthEduArticleOpHistory.setStatus(status);
		healthEduArticleOpHistory.setTitle(title);
		healthEduArticleOpHistory.setCreater(user);
		healthEduArticleOpHistory.setId(0L);
		healthEduArticleOpHistoryDao.save(healthEduArticleOpHistory);
		return healthEduArticleOpHistory;
	}

}
