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

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.mapstruct.Mapper;
import thyyxxk.webserver.entity.casefrontsheet.pojo.FrontsheetQualityVerification;
import thyyxxk.webserver.entity.casefrontsheet.pojo.GroupLeaderVerification;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.socketmessage.SocketMessage;

import java.util.List;

@Mapper
public interface QualityVerificationDao extends BaseMapper<FrontsheetQualityVerification> {

    @Select("select file_status from t_case_frontsheet_main with(nolock) where bah=#{bah} and admiss_times=#{times}")
    Integer selectFileStatus(String bah, int times);

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

    @Select("select * from ( " +
            "select distinct max(id) as id, 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 * from t_frontsheet_groupleader_verification " +
            "where pat_no=#{patNo} and times=#{times} order by create_time")
    List<GroupLeaderVerification> fetchGroupLeaderAudit(String patNo, Integer times);

    @Insert("insert into t_frontsheet_groupleader_verification " +
            "(id,pat_no,times,coder_name,audit_code,audit_name,audit_state," +
            "audit_remark,major_error) values " +
            "(#{id},#{patNo},#{times},#{coderName},#{auditCode},#{auditName}," +
            "#{auditState},#{auditRemark},#{majorError})")
    void insertLeaderAudit(GroupLeaderVerification request);

    @Insert("insert into t_socket_message (id,level,sender,title,content,pat_id) " +
            "values (#{id},0,#{sender},#{title},#{content},#{patId} )")
    void insertNewMessage(SocketMessage socketMessage);

    @Insert({"<script>" +
            "insert into t_socket_message_consumer (msg_id,sender,receiver) values " +
            "<foreach collection='list' item='item' separator=','>" +
            " (#{item.msgId}, #{item.sender}, #{item.receiver})" +
            "</foreach>" +
            "</script>"})
    void insertMessageConsumer(List<SocketMessage> list);

    @Select("select a.*,rtrim(b.name) as patName, " +
            "stateName=case when a.audit_state='APPROVED' then N'通过' else N'不通过' end " +
            "from t_frontsheet_groupleader_verification a,a_patient_mi b " +
            "where a.create_time>=#{start} and a.create_time<=#{end} " +
            "and a.pat_no=b.inpatient_no")
    List<GroupLeaderVerification> fetchLeaderExportData(String start, String end);
}
