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

import cn.hnthyy.thmz.entity.his.mz.MzyReqrec;
import cn.hnthyy.thmz.pageDto.MzyReqrecPageDto;
import cn.hnthyy.thmz.vo.ThmzmxsrParamsVo;
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 java.util.List;
import java.util.Map;

/**
 * 门诊挂号记录
 */
public interface MzyReqrecMapper {
    @Insert({"<script>",
            "<when test='cancelMark eq 0 and canRepeatedly eq 0' >",
            "if not exists (select patient_id  from mzy_reqrec where patient_id=#{patientId,jdbcType=CHAR} and ampm = #{ampm,jdbcType=CHAR} and unit_code=#{unitCode,jdbcType=CHAR} and charge_type=#{chargeType,jdbcType=CHAR} and doctor_code=#{doctorCode,jdbcType=CHAR} and CONVERT(varchar(100), request_day, 23)=#{requestDayStr} and cancel_mark=0) " +
            "</when>",
            "INSERT INTO mzy_reqrec(patient_id,times,name,req_type,request_day,ampm,unit_code,group_code,doctor_code,charge_type,req_order,req_fee,oth_fee,cancel_mark,admiss_time,op_id," +
            "op_day,clinic_fee,visited_mark,plus_mark,closing_date,printer_id,print_flag,windows_no,serial_no,receipt_bill,dcount_no,brochure_fee,dept_no,visit_dept,visit_doctor,visit_date,paymode,psordnum,agtordnum,bl_fee,ck_fee,print_closing_date,print_dcount_no)" +
            " VALUES (#{patientId,jdbcType=CHAR},#{times,jdbcType=INTEGER},#{name,jdbcType=CHAR},#{reqType,jdbcType=CHAR},#{requestDay,jdbcType=TIMESTAMP},#{ampm,jdbcType=CHAR}," +
            "#{unitCode,jdbcType=CHAR},#{groupCode,jdbcType=CHAR},#{doctorCode,jdbcType=CHAR},#{chargeType,jdbcType=CHAR},#{reqOrder,jdbcType=INTEGER},#{reqFee,jdbcType=DOUBLE}," +
            "#{othFee,jdbcType=DOUBLE},#{cancelMark,jdbcType=CHAR},#{admissTime,jdbcType=CHAR},#{opId,jdbcType=CHAR},#{opDay,jdbcType=TIMESTAMP},#{clinicFee,jdbcType=DOUBLE},#{visitedMark,jdbcType=CHAR},#{plusMark,jdbcType=CHAR}," +
            "#{closingDate,jdbcType=TIMESTAMP},#{printerId,jdbcType=CHAR},#{printFlag,jdbcType=CHAR},#{windowsNo,jdbcType=CHAR},#{serialNo,jdbcType=INTEGER},#{receiptBill,jdbcType=CHAR},#{dcountNo,jdbcType=INTEGER}," +
            "#{brochureFee,jdbcType=DOUBLE},#{deptNo,jdbcType=VARCHAR},#{visitDept,jdbcType=VARCHAR},#{visitDoctor,jdbcType=VARCHAR},#{visitDate,jdbcType=TIMESTAMP},#{paymode,jdbcType=VARCHAR}," +
            "#{psordnum,jdbcType=VARCHAR},#{agtordnum,jdbcType=VARCHAR},0.00,0.00,#{printClosingDate,jdbcType=TIMESTAMP},#{printDcountNo,jdbcType=INTEGER})"
            ,"</script>"})
    int insertMzyReqrec(MzyReqrec mzyReqrec);

    /**
     * 根据病人编号查询最后一次挂号信息
     *
     * @param patientId
     * @return
     */
    @Select("select top 1* from mzy_reqrec where times >0 and patient_id=#{patientId,jdbcType=CHAR} order by times desc")
    MzyReqrec selectLastMzyReqrecByPatientId(@Param("patientId") String patientId);


    /**
     * 查询符合条件的挂号数据总数
     *
     * @param mzyReqrecPageDto
     * @return
     */
    @Select({"<script>",
            "select count(*) count from ${tableName}  where times >0 ",
            "<when test='patientIds!=null'>",
            " and patient_id in ",
            "<foreach item='item' index='index' collection='patientIds' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            "<when test='mzyReqrec.serialNo!=null'>",
            " and serial_no =#{mzyReqrec.serialNo,jdbcType=INTEGER}",
            "</when>",
            "<when test='mzyReqrec.unitCode!=null'>",
            " and unit_code =#{mzyReqrec.unitCode,jdbcType=CHAR}",
            "</when>",
            "<when test='mzyReqrec.doctorCode!=null'>",
            " and doctor_code =#{mzyReqrec.doctorCode,jdbcType=CHAR}",
            "</when>",
            "<when test='mzyReqrec.name!=null'>",
            " and (name like #{mzyReqrec.name,jdbcType=CHAR} or patient_id like #{mzyReqrec.name,jdbcType=CHAR})",
            "</when>",

            "<when test='mzyReqrec.visitedMark!=null and mzyReqrec.visitedMark == 0'>",
            " and visit_date is null ",
            "</when>",

            "<when test='mzyReqrec.visitedMark!=null and mzyReqrec.visitedMark == 1'>",
            " and visit_date is not null ",
            "</when>",

            "<when test='mzyReqrec.cancelMark!=null'>",
            " and cancel_mark =#{mzyReqrec.cancelMark,jdbcType=CHAR}",
            "</when>",
            "<when test='beginTime!=null'>",
            " and op_day &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null'>",
            " and op_day &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='mzyReqrec.opId!=null'>",
            " and op_id =#{mzyReqrec.opId}",
            "</when>",
            "</script>"})
    Integer countMzyReqrec(MzyReqrecPageDto mzyReqrecPageDto);

    /**
     * 分页查询挂号信息
     *
     * @param mzyReqrecPageDto
     * @return
     */
    @Select({"<script>",
            "select top ${pageSize} rtrim(patient_id) patient_id,times,rtrim(name) name,rtrim(req_type) req_type,request_day,rtrim(ampm) ampm,rtrim(unit_code) unit_code,",
            "rtrim(group_code) group_code,rtrim(doctor_code) doctor_code,rtrim(charge_type) charge_type,req_order,req_fee,oth_fee,rtrim(cancel_mark) cancel_mark,",
            "rtrim(admiss_time) admiss_time,rtrim(op_id) op_id,op_day,clinic_fee,rtrim(visited_mark) visited_mark,rtrim(plus_mark) plus_mark,closing_date,",
            "rtrim(printer_id) printer_id,rtrim(print_flag) print_flag,rtrim(windows_no) windows_no,serial_no,rtrim(receipt_bill) receipt_bill,dcount_no,brochure_fee,dept_no,visit_dept,",
            "visit_doctor,visit_date,paymode,psordnum,agtordnum,print_closing_date,print_dcount_no   FROM (SELECT ROW_NUMBER() OVER (ORDER BY mzy_reqrec_page.request_day desc) AS RowNumber,",
            "* from dbo.${tableName} mzy_reqrec_page where times >0 ",
            "<when test='patientIds!=null'>",
            " and patient_id in ",
            "<foreach item='item' index='index' collection='patientIds' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            "<when test='mzyReqrec.serialNo!=null'>",
            " and serial_no =#{mzyReqrec.serialNo,jdbcType=INTEGER}",
            "</when>",
            "<when test='mzyReqrec.unitCode!=null'>",
            " and unit_code =#{mzyReqrec.unitCode,jdbcType=CHAR}",
            "</when>",
            "<when test='mzyReqrec.doctorCode!=null'>",
            " and doctor_code =#{mzyReqrec.doctorCode,jdbcType=CHAR}",
            "</when>",
            "<when test='mzyReqrec.name!=null'>",
            " and (name like #{mzyReqrec.name,jdbcType=CHAR} or patient_id like #{mzyReqrec.name,jdbcType=CHAR}) ",
            "</when>",

            "<when test='mzyReqrec.visitedMark!=null and mzyReqrec.visitedMark == 0'>",
            " and visit_date is null ",
            "</when>",

            "<when test='mzyReqrec.visitedMark!=null and mzyReqrec.visitedMark == 1'>",
            " and visit_date is not null ",
            "</when>",

            "<when test='mzyReqrec.cancelMark!=null'>",
            " and cancel_mark =#{mzyReqrec.cancelMark,jdbcType=CHAR}",
            "</when>",
            "<when test='beginTime!=null'>",
            " and op_day &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null'>",
            " and op_day &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='mzyReqrec.opId!=null'>",
            " and op_id =#{mzyReqrec.opId}",
            "</when>",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber asc",
            "</script>"})
    List<MzyReqrec> selectMzyReqrecWithPage(MzyReqrecPageDto mzyReqrecPageDto);

    /**
     * 查询所有退号信息
     *
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,rtrim(name) name,rtrim(req_type) req_type,request_day,rtrim(ampm) ampm,rtrim(unit_code) unit_code,",
            "rtrim(group_code) group_code,rtrim(doctor_code) doctor_code,rtrim(charge_type) charge_type,req_order,req_fee,oth_fee,rtrim(cancel_mark) cancel_mark,",
            "rtrim(admiss_time) admiss_time,rtrim(op_id) op_id,op_day,clinic_fee,rtrim(visited_mark) visited_mark,rtrim(plus_mark) plus_mark,closing_date,",
            "rtrim(printer_id) printer_id,rtrim(print_flag) print_flag,rtrim(windows_no) windows_no,serial_no,rtrim(receipt_bill) receipt_bill,dcount_no,brochure_fee,dept_no,visit_dept,",
            "visit_doctor,visit_date,paymode,psordnum,agtordnum,print_closing_date,print_dcount_no   from dbo.mzy_reqrec where cancel_mark=1",
            "</script>"})
    List<MzyReqrec> selectAllCancel();

    /**
     * 修改挂号信息
     *
     * @param mzyReqrec
     * @return
     */
    @Update({"<script>",
            "update mzy_reqrec ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where patient_id = #{patientId} and times=#{times}' >",
            "<when test='cancelMark!=null'>",
            "cancel_mark =#{cancelMark,jdbcType=CHAR}",
            "</when>",
            "<when test='receiptBill!=null'>",
            ",receipt_bill =#{receiptBill,jdbcType=CHAR}",
            "</when>",
            "<when test='printerId!=null'>",
            ",printer_id =#{printerId,jdbcType=CHAR}",
            "</when>",
            "<when test='paymode!=null'>",
            ",paymode =#{paymode}",
            "</when>",
            "</trim>"
            , "</script>"})
    int updateMzyReqrec(MzyReqrec mzyReqrec);

    /**
     * 按照病人id 和次数查询病人挂号信息
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,rtrim(name) name,rtrim(req_type) req_type,request_day,rtrim(ampm) ampm,rtrim(unit_code) unit_code,",
            "rtrim(group_code) group_code,rtrim(doctor_code) doctor_code,rtrim(charge_type) charge_type,req_order,req_fee,oth_fee,rtrim(cancel_mark) cancel_mark,",
            "rtrim(admiss_time) admiss_time,rtrim(op_id) op_id,op_day,clinic_fee,rtrim(visited_mark) visited_mark,rtrim(plus_mark) plus_mark,closing_date,",
            "rtrim(print_flag) print_flag,rtrim(windows_no) windows_no,serial_no,rtrim(receipt_bill) receipt_bill,dcount_no,brochure_fee,dept_no,visit_dept,",
            "visit_doctor,visit_date,paymode,psordnum,agtordnum,print_closing_date,print_dcount_no   from dbo.mzy_reqrec where patient_id=#{patientId,jdbcType=CHAR} and times = #{times,jdbcType=INTEGER}",
            "</script>"})
    MzyReqrec selectMzyReqrecByPatientIdAndTimes(@Param("patientId") String patientId, @Param("times") Integer times);


    /**
     * 按照缴费流水号查询病人挂号信息
     *
     * @param serialNo
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,rtrim(name) name,rtrim(req_type) req_type,request_day,rtrim(ampm) ampm,rtrim(unit_code) unit_code,",
            "rtrim(group_code) group_code,rtrim(doctor_code) doctor_code,rtrim(charge_type) charge_type,req_order,req_fee,oth_fee,rtrim(cancel_mark) cancel_mark,",
            "rtrim(admiss_time) admiss_time,rtrim(op_id) op_id,op_day,clinic_fee,rtrim(visited_mark) visited_mark,rtrim(plus_mark) plus_mark,closing_date,",
            "rtrim(print_flag) print_flag,rtrim(windows_no) windows_no,serial_no,rtrim(receipt_bill) receipt_bill,dcount_no,brochure_fee,dept_no,visit_dept,",
            "visit_doctor,visit_date,paymode,psordnum,agtordnum,print_closing_date,print_dcount_no   from dbo.mzy_reqrec where serial_no = #{serialNo}",
            "</script>"})
    MzyReqrec selectMzyReqrecBySerialNo(@Param("serialNo") Integer serialNo);

    /**
     * 老系统的退号数据清洗 临时用法
     *
     * @return
     */
    @Update("update  mzy_reqrec set cancel_mark =1 where serial_no in  (select serial_no from mzy_reqrec where serial_no in(select -serial_no from  mzy_reqrec where cancel_mark =1 and times <0 ) and cancel_mark =0)")
    int updateCanCelMark();

    /**
     * 门诊号别统计表
     *
     * @param thmzmxsrParamsVo
     * @return
     */
    @Select({"<script>" +
            "   SELECT dept_name=mz_unit_code.name, " +
            "         mz_unit_code.d_code,  " +
            " mzy_reqrec.unit_code," +
            " mzy_reqrec.charge_type," +
            "         charge_type_name=mzy_zd_charge_type.name,  " +
            "         mzy_zd_charge_type.code," +
            "         patient_count=count((case mzy_reqrec.cancel_mark when '1' then null else mzy_reqrec.patient_id end))," +
            "         req_fee=sum(mzy_reqrec.req_fee + mzy_reqrec.clinic_fee + mzy_reqrec.oth_fee + isnull(bl_fee,0) + isnull(ck_fee,0))" +
            "    FROM zd_unit_code mz_unit_code,   " +
            "         ${firstTableName} mzy_reqrec,   " +
            "         mzy_zd_charge_type  " +
            "   WHERE ( mz_unit_code.code = mzy_reqrec.unit_code ) and  " +
            "         ( mzy_reqrec.charge_type *= mzy_zd_charge_type.code ) and " +
            "         (case when 1=#{caseType}  then mzy_reqrec.op_day else mzy_reqrec.closing_date end) <![CDATA[>=]]>#{beginDate,jdbcType=TIMESTAMP} and " +
            "         (case when 1=#{caseType}  then mzy_reqrec.op_day else mzy_reqrec.closing_date end) <![CDATA[<=]]>#{endDate,jdbcType=TIMESTAMP}  " +
            "<when test='jzSqDept!=null'>" +
            " and mzy_reqrec.unit_code =#{jzSqDept}" +
            "</when>" +
            "<when test='jzSqDoctor!=null'>" +
            " and mzy_reqrec.doctor_code =#{jzSqDoctor}" +
            "</when>" +
            "group by mz_unit_code.name," +
            " mz_unit_code.d_code," +
            "         mzy_zd_charge_type.name," +
            "         mzy_zd_charge_type.code," +
            " mzy_reqrec.unit_code," +
            " mzy_reqrec.charge_type  "
            , "</script>"})
    List<Map<String, Object>> selectMzhbCount(ThmzmxsrParamsVo thmzmxsrParamsVo);


    /**
     * 按照日期分组查询号数与金额
     *
     * @param mzyReqrecPageDto
     * @return
     */
    @Select({"<script>",
            "select CONVERT(varchar(100), op_day, 23) request_day," +
                    "count ( case when times <![CDATA[>]]> 0 then 1 else null end ) -count ( case when  times  <![CDATA[<]]> 0 then 1 else null end ) count ," +
                    "sum(req_fee+clinic_fee+oth_fee+bl_fee+ck_fee) amount," +
                    "count ( case when  times  <![CDATA[<]]> 0 then 1 else null end ) thcount," +
                    "abs(sum ( case when times  <![CDATA[<]]> 0  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end )) thAmount from mzy_reqrec  where  (paymode <![CDATA[<>]]> 'hospital_z' or paymode is null)  ",
            "<when test='beginTime!=null'>",
            " and op_day &gt;=#{beginTime,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='endTime!=null'>",
            " and op_day &lt;=#{endTime,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='mzyReqrec.visitedMark!=null and mzyReqrec.visitedMark == 1'>",
            " and visit_date is not null ",
            "</when>",
            " group by CONVERT(varchar(100), op_day, 23) ",
            "</script>"})
    List<Map<String, Object>> countAndAmountMzyReqrec(MzyReqrecPageDto mzyReqrecPageDto);




    /**
     * 判断手机自助缴费平台对应的流水号有无成功挂号
     * @param psordnum
     * @param agtordnum
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,rtrim(name) name,rtrim(req_type) req_type,request_day,rtrim(ampm) ampm,rtrim(unit_code) unit_code,"+
            "rtrim(group_code) group_code,rtrim(doctor_code) doctor_code,rtrim(charge_type) charge_type,req_order,req_fee,oth_fee,rtrim(cancel_mark) cancel_mark,"+
            "rtrim(admiss_time) admiss_time,rtrim(op_id) op_id,op_day,clinic_fee,rtrim(visited_mark) visited_mark,rtrim(plus_mark) plus_mark,closing_date,"+
            "rtrim(printer_id) printer_id,rtrim(print_flag) print_flag,rtrim(windows_no) windows_no,serial_no,rtrim(receipt_bill) receipt_bill,dcount_no,brochure_fee,dept_no,visit_dept,"+
            "visit_doctor,visit_date,paymode,psordnum,agtordnum,print_closing_date,print_dcount_no   from dbo.${tableName} where psordnum=#{psordnum} and cancel_mark =0 ",
            "<when test='agtordnum!=null'>",
            " and agtordnum = #{agtordnum}",
            "</when>",
            "</script>"})
    MzyReqrec selectMzyReqrecByAgtordnum(@Param("psordnum") String psordnum,@Param("agtordnum") String agtordnum,@Param("tableName") String tableName);



    /**
     * 修改病人姓名
     * @param name
     * @param patientId
     * @param times
     * @return
     */
    @Update({"<script>",
            "update mzy_reqrec set 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);
}
