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

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
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 java.util.Date;
import java.util.List;

@Mapper
public interface EmrAuditDao extends BaseMapper<EmrAuditDetail> {

    @Insert("insert into emr_audit_detail (pat_no,times,approver, review_time, name, scoring_criteria, remark, " +
            "number_of_defects, emr_id, history , doctor , final_control,level,minus_points) " +
            "values" +
            "(#{patNo},#{times},#{item.approver},#{item.reviewTime},#{item.name},#{item.scoringCriteria},#{item.remark}," +
            "#{item.numberOfDefects},#{id},0,#{doctor},#{item.finalControl},#{level},#{item.minusPoints})")
    void insertAudit(@Param("patNo") String patNo,
                     @Param("times") Integer times,
                     @Param("id") Integer id,
                     @Param("doctor") String doctor,
                     @Param("level") Integer level,
                     @Param("item") 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);

}
