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

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.HealthEduArticlePatientDao;
import com.yihu.wlyy.service.BaseService;
import org.json.JSONArray;
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.jdbc.core.JdbcTemplate;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

	@Autowired
	private HealthEduArticleDoctorDao healthEduArticleDoctorDao;

	@Autowired
	private HealthEduArticleLabelService healthEduArticleLabelService;

	@Autowired
	private HealthEduArticleOpHistoryService healthEduArticleOpHistoryService;

	@Autowired
	private JdbcTemplate jdbcTemplate;
	/**
	 * 根据文章标示获取数据
	 * @param article
	 * @return
	 *
	 */
	public HealthEduArticleDoctor findByArticle(String article,String doctor) {
		return healthEduArticleDoctorDao.findByArticleAndDoctor(article, doctor);
	}

	/**
	 * 根据文章删除
	 * @param article
	 * @return
	 *
	 */
	public int delete(String article,String doctor) {
		//删除收藏统计
		healthEduArticleOpHistoryService.delCollectAmount(article,doctor);
		return healthEduArticleDoctorDao.deleteByArticleAndDoctor(article, doctor);
	}

	/**
	 * 保存
	 * @param healthEduArticleDoctor
	 * @return
	 */
	public HealthEduArticleDoctor save(HealthEduArticleDoctor healthEduArticleDoctor){
		return this.healthEduArticleDoctorDao.save(healthEduArticleDoctor);
	}

	/**
	 * 根据文章id获取数据
	 * @param id
	 * @return
	 */
	public HealthEduArticleDoctor findById(Long id) {
		return healthEduArticleDoctorDao.findOne(id);
	}

	/**
	 * 查询收藏的文章
	 * @param page 第几页
	 * @param pagesize 分页大小
	 * @return
	 */
	public List<Map<String, Object>> list(int page, int pagesize,String filter,String doctor,String patient) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		if(page<0){
			page = 0;
		}
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append("select a.id,a.article,a.doctor,a.title,a.url,a.czrq,b.content,b.keyword");
		sql.append(",(select count(1) from wlyy_health_edu_article_op_history c where c.code = a.article and c.status = 1) as readAmount");
		sql.append(",(select count(1) from wlyy_health_edu_article_op_history c where c.code = a.article and c.status = 2) as collectionAmount");
		sql.append(",(select count(1) from wlyy_health_edu_article_op_history c where c.code = a.article and c.status = 3) as repeatAmount");
		if(StringUtils.isNotBlank(patient)) {
			params.add(doctor);
			params.add(patient);
			sql.append(",(select count(1) from wlyy_health_edu_article_patient c where c.article = a.article and c.doctor=? and c.patient = ?) as send");
		}
		sql.append(" from wlyy_health_edu_article_doctor a left join wlyy_health_edu_article b on a.article = b.code ");//内联取文章内容
		String where = " where a.doctor = ?";
		params.add(doctor);
		if(StringUtils.isNotBlank(filter)){
			//记录搜索记录
			healthEduArticleLabelService.saveOrUpdateLabel(filter,doctor);
			filter="%"+filter+"%";
			params.add(filter);
			params.add(filter);
			where+=" and ( a.title like ? or b.keyword like ? )";
		}
		int start = page * pagesize;

		String pageInfo = " limit "+start+","+pagesize;

		String orderBy = " order by a.czrq,a.id desc";

		sql.append(where).append(orderBy).append(pageInfo);
		List<Map<String, Object>> result = jdbcTemplate.queryForList(sql.toString(),params.toArray());
		return result;
	}


}
