package thyyxxk.webserver.dao.his.zhuyuanyisheng.emr;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mapstruct.Mapper;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrAuditDetail;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.LinkQualityControl;

import java.util.Date;
import java.util.List;

@Mapper
public interface EmrAuditDao {

    @Select("select id, " +
            "       approver, " +
            "       approver_name = (select rtrim(name) from a_employee_mi where code = approver)," +
            "       review_time," +
            "       name, " +
            "       scoring_criteria, " +
            "       remark, " +
            "       remediation_status, " +
            "       modify_person, " +
            "       modify_person_name = (select rtrim(name) from a_employee_mi where modify_person = code), " +
            "       modification_time, " +
            "       emr_id," +
            "       number_of_defects, " +
            "       history " +
            "from emr_audit_detail" +
            " where emr_id = #{id}" +
            " and history = 0 and final_control = #{finalControl} and number_of_defects > 0 ")
    List<EmrAuditDetail> selectedMessage(String id, Integer finalControl);

    @Select("select count(1) " +
            "from emr_audit_detail " +
            "where history = 1 " +
            "  and emr_id = #{id} " +
            "group by emr_id")
    Integer selectIsHistoryCount(String id);

    @Select("select  id,emr_code, scoring_criteria, name , 1 as numberOfDefects,sort " +
            "from link_quality_control where emr_code = #{code} order by sort ")
    List<EmrAuditDetail> selectedLink(String code);

    @Update("update emr_audit_detail set history = 1 where emr_id = #{id} and final_control = #{finalControl} ")
    void setAuditHistoryByEmrId(Integer id, Integer finalControl);

    @Insert("<script>" +
            "insert into emr_audit_detail (pat_no,times,approver, review_time, name, scoring_criteria, remark, " +
            "number_of_defects, emr_id, history , doctor , final_control) " +
            "values" +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{patNo},#{times},#{item.approver},#{item.reviewTime},#{item.name},#{item.scoringCriteria},#{item.remark}," +
            "#{item.numberOfDefects},#{id},0,#{doctor},#{item.finalControl})" +
            "</foreach>" +
            "</script>")
    void insertAudit(@Param("patNo") String patNo,
                     @Param("times") Integer times,
                     @Param("id") Integer id,
                     @Param("doctor") String doctor,
                     @Param("list") List<EmrAuditDetail> list);

    @Update("UPDATE emr_audit_detail " +
            "SET remediation_status = #{state}," +
            "    modify_person = #{code}," +
            "    modification_time = #{date} " +
            "WHERE id = #{id};")
    void rectifyMedicalRecords(Integer id,
                               Integer state,
                               String code,
                               Date date);

    @Select("select id, " +
            "       pat_no, " +
            "       times, " +
            "       approver, " +
            "       approver_name      = (select rtrim(name) from a_employee_mi where approver = code), " +
            "       review_time, " +
            "       name, " +
            "       scoring_criteria, " +
            "       remark, " +
            "       remediation_status, " +
            "       number_of_defects, " +
            "       modify_person, " +
            "       modify_person_name = (select rtrim(name) from a_employee_mi where modify_person = code), " +
            "       modification_time, " +
            "       emr_id, " +
            "       history, " +
            "       doctor,final_control " +
            "from emr_audit_detail " +
            "where emr_id = #{emrId} " +
            "  and history = 1 " +
            " order by review_time desc ")
    List<EmrAuditDetail> getHistoricalAudit(Integer emrId);


    @Select("select id, " +
            "       pat_no, " +
            "       times, " +
            "       approver, " +
            "       approver_name      = (select rtrim(name) from a_employee_mi where approver = code), " +
            "       review_time, " +
            "       name, " +
            "       scoring_criteria, " +
            "       remark, " +
            "       remediation_status, " +
            "       number_of_defects, " +
            "       modify_person, " +
            "       modify_person_name = (select rtrim(name) from a_employee_mi where modify_person = code), " +
            "       modification_time, " +
            "       emr_id, " +
            "       history, " +
            "       doctor,final_control " +
            "from emr_audit_detail " +
            "where emr_id = #{emrId} " +
            "  and history = 0 " +
            "  and final_control = 0 " +
            " order by review_time desc ")
    List<EmrAuditDetail> getLinkQualityControl(Integer emrId);

    @Update("update zy_actpatient " +
            "set final_control = 1 " +
            "where inpatient_no = #{patNo} " +
            "  and admiss_times = #{times}; " +
            "update zy_inactpatient " +
            "set final_control = 1 " +
            "where inpatient_no = #{patNo} " +
            "  and admiss_times = #{times};")
    void updateFinalControl(String patNo, Integer times);

    @Select("select count(1) from t_case_frontsheet_main where file_status = 1 and bah = #{patNo} and admiss_times = #{times}")
    int whetherToSignForTheMedicalRecord(String patNo, Integer times);

    @Update("update batj_ba2 " +
            "set bazl = #{bazl}, " +
            "    zkys = #{userCode}, " +
            "    barq = getdate() " +
            "where zyh = #{patNo} " +
            "  and zycs = #{times}")
    void updateControlLevel(String patNo, Integer times, Integer bazl, String userCode);

}
