/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.yihu.wlyy.repository.followup;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.yihu.wlyy.entity.follow.FollowUpPlan;

public interface FollowUpPlanDao extends PagingAndSortingRepository<FollowUpPlan, Long>, JpaSpecificationExecutor<FollowUpPlan> {

	// 查询已结束的随访计划
	@Query("SELECT a FROM FollowUpPlan a, FollowUpPlanDoctor b WHERE a.code = b.plan AND b.doctor = ?1 AND a.status = 1")
	Page<FollowUpPlan> findByDoctorFinish(String doctor, Pageable pageRequest);

	// 查询已结束的随访计划
	@Query("SELECT a FROM FollowUpPlan a, FollowUpPlanDoctor b WHERE a.code = b.plan AND b.doctor = ?1 AND a.status = 1 AND a.id < ?2")
	Page<FollowUpPlan> findByDoctorFinish(String doctor, long id, Pageable pageRequest);

	// 查询未结束的随访计划
	@Query("SELECT a FROM FollowUpPlan a, FollowUpPlanDoctor b WHERE a.code = b.plan AND b.doctor = ?1 AND a.status <> 1")
	Page<FollowUpPlan> findByDoctor(String doctor, Pageable pageRequest);

	// 查询未结束的随访计划
	@Query("SELECT a FROM FollowUpPlan a, FollowUpPlanDoctor b WHERE a.code = b.plan AND b.doctor = ?1 AND a.status <> 1 AND a.id < ?2")
	Page<FollowUpPlan> findByDoctor(String doctor, long id, Pageable pageRequest);

	// 根据code查询随访计
	FollowUpPlan findByCode(String code);

	@Query("select a from FollowUpPlan a where a.patient=?1 and  a.id < ?2  order by a.id desc, a.czrq desc")
	Page<FollowUpPlan> getfollowupPlanListLeft(String userid, long id, Pageable pageRequest);

	@Query("select a from FollowUpPlan a where a.patient=?1  order by a.id desc, a.czrq desc")
	Page<FollowUpPlan> getfollowupPlanListLeft(String userid, Pageable pageRequest);

	@Query("select a from FollowUpPlan a where a.patient=?1 and  a.id < ?2 and  a.title like ?3 order by a.id desc, a.czrq desc")
	Page<FollowUpPlan> getfollowupPlanListLeft(String userId, long id, String title, Pageable pageRequest);

	@Query("select a from FollowUpPlan a  where a.patient=?1 and  a.title like ?2 order by a.id desc, a.czrq desc")
	Page<FollowUpPlan> getfollowupPlanListLeft(String userId, String title, Pageable pageRequest);

	@Query("select a from FollowUpPlan a  where a.patient=?1 order by  a.czrq desc")
	List<FollowUpPlan> findByPatient(String userid);

	@Modifying
	@Query("update FollowUpPlan a set a.wjAmount = a.wjAmount + 1 where a.code = ?1")
	int increaseWjAmount(String plan);

	@Modifying
	@Query("update FollowUpPlan a set a.tcAmount = a.tcAmount + 1 where a.code = ?1")
	int increaseTcAmount(String plan);
}
