package thyyxxk.webserver.dao.his.inpatient.casefrontsheet;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mapstruct.Mapper;
import thyyxxk.webserver.entity.casefrontsheet.FrontsheetQualityVerification;
import thyyxxk.webserver.entity.casefrontsheet.state.AuditState;
import thyyxxk.webserver.entity.dictionary.CodeName;

import java.util.List;

@Mapper
public interface QualityVerificationDao extends BaseMapper<FrontsheetQualityVerification> {

    @Select("select user_code from dj_user_role where role_id=66")
    List<String> selectAuditors();

    @Select("select * from ( " +
            "select distinct pat_no,times,pat_name,pat_gender,doctor_code,doctor_name, " +
            "max(application_time) as applicationTime, " +
            "approved=(select count(1) from t_frontsheet_quality_verification d where d.pat_no=a.pat_no and d.times=a.times and d.audit_state='APPROVED') " +
            "from t_frontsheet_quality_verification a " +
            "where audit_state='REJECTED' and application_time>=#{start} and application_time<=#{end} " +
            "${statement} " +
            "group by pat_no, times, pat_name, pat_gender, doctor_code, doctor_name" +
            ") t where t.approved=0")
    List<FrontsheetQualityVerification> selectRejectedRecords(String start, String end, String statement);

    @Select("select code,name,py_code from zd_unit_code where isnull(del_flag,'0')!='1' " +
            "and code not like '8%' and inpatient_flag='1' order by code")
    List<CodeName> selectAllSmallDeptWithInpatientFlag();

    @Update("update t_frontsheet_quality_verification set audit_state='INITIAL' where id=#{id} ")
    int revokeApprovedAudit(String id);

    @Select("select top 1 audit_state from t_frontsheet_quality_verification " +
            "where pat_no=#{patNo} and times=#{times} order by application_time desc ")
    AuditState selectStates(String patNo, int times);
}
