package com.yihu.wlyy.repository.doctor;

import com.yihu.wlyy.entity.doctor.team.sign.SignPatientLabelInfo;
import com.yihu.wlyy.entity.patient.SignFamily;
import io.swagger.models.auth.In;
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 java.util.List;

/**
 * Created by lyr on 2016/10/9.
 */
public interface SignPatientLabelInfoDao extends
        PagingAndSortingRepository<SignPatientLabelInfo, Long>,
        JpaSpecificationExecutor<SignPatientLabelInfo> {

    /**
     * 查询某个患者的标签
     *
     * @param patient 患者
     * @param status  状态
     * @return
     */
    List<SignPatientLabelInfo> findByPatientAndStatus(String patient, Integer status);

    /**
     * 查询某个患者的标签
     *
     * @param patient 患者
     * @param status  状态
     * @return
     */
    @Query("select f from SignPatientLabelInfo f,SignPatientLabel l where f.patient = ?1 and f.status = ?2 and f.label = l.labelCode and f.labelType =l.labelType" +
            " and l.status = ?2 and (l.labelType <> '4' or l.teamCode = ?3 or ( l.labelType = '4' and l.labelCode in (1,2)) )")
    List<SignPatientLabelInfo> findByPatientAndStatusByTeam(String patient, Integer status, Long teamCode);

    /**
     * 查询某个患者的某个类型标签
     *
     * @param patient   患者
     * @param labelType 标签类型
     * @param status    状态
     * @return
     */
    List<SignPatientLabelInfo> findByPatientAndLabelTypeAndStatus(String patient, String labelType, Integer status);

    /**
     * 查询某个患者的某个类型的某个标签
     *
     * @param patient   患者
     * @param labelCode  标签代码
     * @param labelType 标签类型
     * @param status    状态
     * @return
     */
    SignPatientLabelInfo findByPatientAndLabelAndLabelTypeAndStatus(String patient, String labelCode, String labelType, Integer status);

    /**
     * 删除患者的某个标签类型的所有指定状态标签
     *
     * @param patient
     * @param labelType
     * @param status
     * @return
     */
    @Modifying
    @Query("update SignPatientLabelInfo set status = 0 where patient = ?1 and labelType = ?2 and status = ?3")
    int deleteByPatientAndLabelTypeAndStatus(String patient, String labelType, Integer status);

    @Modifying
    @Query("update SignPatientLabelInfo set status = 0 where label = ?1 and status = 1")
    int deleteByLabel(String labelCode);

    /**
     * 更新某个标签名称
     *
     * @param labelName
     * @param labelCode
     * @return
     */
    @Modifying
    @Query("update SignPatientLabelInfo a set a.labelName = ?1 where a.label= ?2 and a.labelType = '4' ")
    int updateLabelName(String labelName,String labelCode);

}
