package cn.hnthyy.thmz.mapper.his.mz;

import cn.hnthyy.thmz.entity.his.mz.MzYjReq;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface MzYjReqMapper {
    @Select("select req_no,patient_id,p_name patient_name,birth_date,times,receipt_no,order_no,item_no,p_bar_code,group_name,order_code,order_name,page_no,req_comment,req_doctor," +
            "req_ward,exec_dept,req_date,inspect_part,inspect_stuff,sample_status,rtrim(print_flag) print_flag,rtrim(receive_flag) receive_flag,req_type,result_comment,sys_date," +
            "rtrim(pay_mark) pay_mark,rtrim(status_flag) status_flag,serial_no,cash_id,jy_remark,req_other_result,original_list_no,req_tz_comment,rtrim(jz_flag) jz_flag,req_studyid," +
            "percent_ft,rtrim(confirm_flag) confirm_flag,confirm_time,confirm_id,rtrim(print_flag_yj) print_flag_yj from mz_yj_req")
    List<MzYjReq> selectMzYjReq(MzYjReq mzYjReq);

    @Insert("INSERT INTO mz_yj_req(req_no,patient_id,p_name,birth_date,times,receipt_no,order_no,item_no,p_bar_code,group_name,order_code,order_name,page_no,req_comment,req_doctor," +
            "req_ward,exec_dept,req_date,inspect_part,inspect_stuff,sample_status,print_flag,receive_flag,req_type,result_comment,sys_date,pay_mark,status_flag,serial_no,cash_id," +
            "jy_remark,req_other_result,original_list_no,req_tz_comment,jz_flag,req_studyid,percent_ft,confirm_flag,confirm_time,confirm_id,print_flag_yj) " +
            " VALUES(#{reqNo,jdbcType=INTEGER},#{patientId,jdbcType=VARCHAR},#{patientName,jdbcType=VARCHAR},#{birthDate,jdbcType=TIMESTAMP},#{times,jdbcType=SMALLINT}," +
            "#{receiptNo,jdbcType=SMALLINT},#{orderNo,jdbcType=SMALLINT},#{itemNo,jdbcType=SMALLINT},#{pBarCode,jdbcType=VARCHAR},#{groupName,jdbcType=VARCHAR},#{orderCode,jdbcType=VARCHAR}," +
            "#{orderName,jdbcType=VARCHAR},#{pageNo,jdbcType=VARCHAR},#{reqComment,jdbcType=VARCHAR},#{reqDoctor,jdbcType=VARCHAR},#{reqWard,jdbcType=VARCHAR}," +
            "#{execDept,jdbcType=VARCHAR},#{reqDate,jdbcType=TIMESTAMP},#{inspectPart,jdbcType=VARCHAR},#{inspectStuff,jdbcType=VARCHAR},#{sampleStatus,jdbcType=VARCHAR}," +
            "#{printFlag,jdbcType=CHAR},#{receiveFlag,jdbcType=CHAR},#{reqType,jdbcType=VARCHAR},#{resultComment,jdbcType=VARCHAR},#{sysDate,jdbcType=TIMESTAMP}," +
            "#{payMark,jdbcType=CHAR},#{statusFlag,jdbcType=CHAR},#{serialNo,jdbcType=INTEGER},#{cashId,jdbcType=VARCHAR},#{jyRemark,jdbcType=VARCHAR}," +
            "#{reqOtherResult,jdbcType=VARCHAR},#{originalListNo,jdbcType=VARCHAR},#{reqTzComment,jdbcType=VARCHAR},#{jzFlag,jdbcType=CHAR},#{reqStudyid,jdbcType=VARCHAR}," +
            "#{percentFt,jdbcType=DECIMAL},#{confirmFlag,jdbcType=CHAR},#{confirmTime,jdbcType=TIMESTAMP},#{confirmId,jdbcType=VARCHAR},#{printFlagYj,jdbcType=CHAR})")
    int insertMzYjReq(MzYjReq mzYjReq);

    /**
     * 根据医技申请单号查询申请
     * @param reqNo
     * @return
     */
    @Select("select req_no,patient_id,p_name patient_name,birth_date,times,receipt_no,order_no,item_no,p_bar_code,group_name,order_code,order_name,page_no,req_comment,req_doctor," +
            "req_ward,exec_dept,req_date,inspect_part,inspect_stuff,sample_status,rtrim(print_flag) print_flag,rtrim(receive_flag) receive_flag,req_type,result_comment,sys_date," +
            "rtrim(pay_mark) pay_mark,rtrim(status_flag) status_flag,serial_no,cash_id,jy_remark,req_other_result,original_list_no,req_tz_comment,rtrim(jz_flag) jz_flag,req_studyid," +
            "percent_ft,rtrim(confirm_flag) confirm_flag,confirm_time,confirm_id,rtrim(print_flag_yj) print_flag_yj from mz_yj_req where req_no=#{reqNo}")
    MzYjReq selectMzYjReqByReqNo(@Param(value = "reqNo") String reqNo);


    /**
     * 查询未缴费的医技申请单
     *
     * @param mzYjReq
     * @return
     */
    @Select({"<script>",
            "select req_no,patient_id,p_name patient_name,birth_date,times,receipt_no,order_no,item_no,p_bar_code,group_name,order_code,order_name,page_no,req_comment,req_doctor,",
            "req_ward,exec_dept,req_date,inspect_part,inspect_stuff,sample_status,rtrim(print_flag) print_flag,rtrim(receive_flag) receive_flag,req_type,result_comment,sys_date,",
            "rtrim(pay_mark) pay_mark,rtrim(status_flag) status_flag,serial_no,cash_id,jy_remark,req_other_result,original_list_no,req_tz_comment,rtrim(jz_flag) jz_flag,req_studyid,",
            "percent_ft,rtrim(confirm_flag) confirm_flag,confirm_time,confirm_id,rtrim(print_flag_yj) print_flag_yj from mz_yj_req where pay_mark='5' and status_flag <![CDATA[<>]]> 'z' ",
            "<when test='times!=null'>",
            " and times =#{times,jdbcType=SMALLINT}",
            "</when>",
            "<when test='patientId!=null'>",
            " and patient_id =#{patientId,jdbcType=VARCHAR}",
            "</when>",
            "</script>"})
    List<MzYjReq> selectNotPayMzYjReq(MzYjReq mzYjReq);



    /**
     * 查询近三天内没有缴费的医技处方
     * @param patientId
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,doctor_code=min(req_doctor),cf_type='2',order_count=count(distinct page_no),receipt_no =min(receipt_no), visit_date =min(req_date)," +
                    " name=min(p_name) from   mz_yj_req where 1=1",
            "<when test='patientId!=null'>",
            " and patient_id=#{patientId,jdbcType=CHAR}",
            "</when>",
            " and pay_mark='5' and status_flag <![CDATA[ <> ]]> 'z' and   (datediff(day,req_date,getdate()) <![CDATA[ <= ]]> 3) group by patient_id,times",
            "</script>"})
    List<Map<String, Object>> selectUnPaidYjReq(@Param(value = "patientId") String patientId);





    /**
     * 修改医技申请记录
     * @param mzYjReq
     * @return
     */
    @Update({"<script>",
            "update mz_yj_req " +
                    "<trim prefix='set' prefixOverrides=',' suffix=' where patient_id = #{patientId} and times=#{times}' >",
            "<when test='payMark!=null'>",
            "pay_mark =#{payMark,jdbcType=CHAR}",
            "</when>",
            "<when test='receiptNo!=null'>",
            ",receipt_no =#{receiptNo,jdbcType=SMALLINT}",
            "</when>",
            "<when test='serialNo!=null'>",
            ",serial_no =#{serialNo,jdbcType=INTEGER}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateMzYjReq(MzYjReq mzYjReq);


    /**
     * 查询未退费的医技申请单
     *
     * @param orderCodes
     * @return
     */
    @Select({"<script>",
            "select req_no,patient_id,p_name patient_name,birth_date,times,receipt_no,order_no,item_no,p_bar_code,group_name,order_code,order_name,page_no,req_comment,req_doctor,",
            "req_ward,exec_dept,req_date,inspect_part,inspect_stuff,sample_status,rtrim(print_flag) print_flag,rtrim(receive_flag) receive_flag,req_type,result_comment,sys_date,",
            "rtrim(pay_mark) pay_mark,rtrim(status_flag) status_flag,serial_no,cash_id,jy_remark,req_other_result,original_list_no,req_tz_comment,rtrim(jz_flag) jz_flag,req_studyid,",
            "percent_ft,rtrim(confirm_flag) confirm_flag,confirm_time,confirm_id,rtrim(print_flag_yj) print_flag_yj from mz_yj_req where pay_mark='0' and status_flag <![CDATA[<>]]> 'z'",
            "<when test='times!=null'>",
            " and times =#{times,jdbcType=SMALLINT}",
            "</when>",
            "<when test='patientId!=null'>",
            " and patient_id =#{patientId,jdbcType=VARCHAR}",
            "</when>",
            "<when test='orderCodes!=null and orderCodes.size>0'>",
            " and order_code not in ",
            "<foreach item='item' index='index' collection='orderCodes' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            "</script>"})
    List<MzYjReq> selectNotRefundYjReq(@Param("patientId") String patientId, @Param("times") Integer times,@Param("orderCodes") List<String> orderCodes);




    /**
     * 修改病人姓名
     * @param name
     * @param patientId
     * @param times 120病人缴费时调用时需传
     * @return
     */
    @Update({"<script>",
            "update mz_yj_req set p_name =#{name} where patient_id = #{patientId}",
            "<when test='times!=null'>",
            "and times = #{times}",
            "</when>",
            "</script>"})
    int updateNameByPatientIdAndTimes(@Param("name") String name,@Param("patientId") String patientId,@Param("times") Integer times);


    /**
     * 删除未缴费医技申请
     * @param patientId
     * @return
     */
    @Delete("delete  from  mz_yj_req where patient_id =#{patientId,jdbcType=CHAR} and pay_mark = 5 ")
    int deleteNotPayYjReByPatientId(@Param("patientId") String patientId);

    /**
     * 查询执行科室
     * @param mzYjReq
     * @return
     */
    @Select("select rtrim(exec_dept) from  mz_yj_req where  patient_id = #{patientId} and times = #{times} and receipt_no=#{receiptNo} GROUP BY exec_dept ")
    List<String> selectExecDept(MzYjReq mzYjReq);
}
