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

import cn.hnthyy.thmz.entity.his.mz.MzDepositFile;
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.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

public interface MzDepositFileMapper {
    /**
     * 新增缴费方式记录
     *
     * @param mzDepositFile
     * @return
     */
    @Insert("INSERT INTO  mz_deposit_file(patient_id,times,receipt_no,pay_mark,cheque_type,cheque_no,amount,charge_date,dcount_date,dcount_no," +
            "op_id,receipt_sn,serial_no,responce_type,yb_jlh,yb_ywh,dept_no,psordnum,agtordnum,hisrefundnum) VALUES (#{patientId,jdbcType=CHAR}," +
            "#{times,jdbcType=INTEGER},#{receiptNo,jdbcType=SMALLINT},#{payMark,jdbcType=CHAR},#{chequeType,jdbcType=CHAR},#{chequeNo,jdbcType=CHAR}" +
            ",#{amountStr,jdbcType=DECIMAL},#{chargeDate,jdbcType=TIMESTAMP},#{dcountDate,jdbcType=TIMESTAMP},#{dcountNo,jdbcType=CHAR},#{opId,jdbcType=CHAR}," +
            "#{receiptSn,jdbcType=INTEGER},#{serialNo,jdbcType=INTEGER},#{responceType,jdbcType=CHAR},#{ybJlh,jdbcType=VARCHAR},#{ybYwh,jdbcType=VARCHAR}," +
            "#{deptNo,jdbcType=VARCHAR},#{psordnum,jdbcType=VARCHAR},#{agtordnum,jdbcType=VARCHAR},#{hisrefundnum,jdbcType=VARCHAR})")
    int insertMzDepositFile(MzDepositFile mzDepositFile);


    /**
     * 批量保存缴费方式记录
     *
     * @param mzDepositFileList
     * @return
     */
    @Insert({
            "<script>",
            "INSERT INTO mz_deposit_file(patient_id,times,receipt_no,pay_mark,cheque_type,cheque_no,amount,charge_date,dcount_date,dcount_no,",
            "op_id,receipt_sn,serial_no,responce_type,yb_jlh,yb_ywh,dept_no,psordnum,agtordnum,hisrefundnum) VALUES ",
            "<foreach collection='mzDepositFileList' item='item' index='index' separator=','>",
            " (#{item.patientId,jdbcType=CHAR},#{item.times,jdbcType=INTEGER},#{item.receiptNo,jdbcType=SMALLINT},#{item.payMark,jdbcType=CHAR},#{item.chequeType,jdbcType=CHAR},#{item.chequeNo,jdbcType=CHAR}",
            ",#{item.amountStr,jdbcType=DECIMAL},#{item.chargeDate,jdbcType=TIMESTAMP},#{item.dcountDate,jdbcType=TIMESTAMP},#{item.dcountNo,jdbcType=CHAR},#{item.opId,jdbcType=CHAR},",
            "#{item.receiptSn,jdbcType=INTEGER},#{item.serialNo,jdbcType=INTEGER},#{item.responceType,jdbcType=CHAR},#{item.ybJlh,jdbcType=VARCHAR},#{item.ybYwh,jdbcType=VARCHAR},",
            "#{item.deptNo,jdbcType=VARCHAR},#{item.psordnum,jdbcType=VARCHAR},#{item.agtordnum,jdbcType=VARCHAR},#{item.hisrefundnum,jdbcType=VARCHAR}) ",
            "</foreach>",
            "</script>"
    })
    int batchInsertMzDepositFile(@Param(value = "mzDepositFileList") List<MzDepositFile> mzDepositFileList);


//    /**
//     * 查询所有的记账记录
//     * @param patientId
//     * @param times
//     * @return
//     */
//    @Select({"<script>",
//            "select rtrim(patient_id) patient_id,times,receipt_no,rtrim(pay_mark) pay_mark,rtrim(cheque_type) cheque_type,rtrim(cheque_no) cheque_no,amount,charge_date,dcount_date,",
//            "rtrim(dcount_no) dcount_no,rtrim(op_id) op_id,receipt_sn,serial_no,rtrim(responce_type) responce_type,yb_jlh,yb_ywh,dept_no,psordnum,agtordnum,hisrefundnum",
//            "from mz_deposit_file where patient_id =#{patientId,jdbcType=CHAR} and pay_mark=0  and times =#{times}  ",
//            "</script>"})
//    List<MzDepositFile> selectJiZhangDetails(@Param(value = "patientId") String patientId,@Param("times") Integer times);


    /**
     * 根据病人id和次数查询缴费记录
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,rtrim(pay_mark) pay_mark,rtrim(cheque_type) cheque_type,rtrim(cheque_no) cheque_no,amount,charge_date,dcount_date,",
            "rtrim(dcount_no) dcount_no,rtrim(op_id) op_id,receipt_sn,serial_no,rtrim(responce_type) responce_type,yb_jlh,yb_ywh,dept_no,psordnum,agtordnum,hisrefundnum",
            "from ${tableName} where patient_id =#{patientId,jdbcType=CHAR} and pay_mark=0 and times =  #{times}",
            "<when test='receiptNo!=null'>",
            " and receipt_no=#{receiptNo}",
            "</when>",
            "</script>"})
    List<MzDepositFile> selectMzDepositFileByTimes(@Param(value = "patientId") String patientId, @Param("times") Integer times, @Param("receiptNo") Integer receiptNo,@Param(value = "tableName") String tableName);


    /**
     * 根据病人id和次数查询缴费记录 再历史数据中查询
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,rtrim(pay_mark) pay_mark,rtrim(cheque_type) cheque_type,rtrim(cheque_no) cheque_no,amount,charge_date,dcount_date,",
            "rtrim(dcount_no) dcount_no,rtrim(op_id) op_id,receipt_sn,serial_no,rtrim(responce_type) responce_type,yb_jlh,yb_ywh,dept_no,psordnum,agtordnum,hisrefundnum",
            "from mz_deposit_file_b where patient_id =#{patientId,jdbcType=CHAR} and pay_mark=0 and times =  #{times}",
            "<when test='receiptNo!=null'>",
            " and receipt_no=#{receiptNo}",
            "</when>",
            "</script>"})
    List<MzDepositFile> selectMzDepositFileByTimesInHisData(@Param(value = "patientId") String patientId, @Param("times") Integer times, @Param("receiptNo") Integer receiptNo);

    /**
     * 根据发票流水号查询收费方式表
     *
     * @param serialNo
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,rtrim(pay_mark) pay_mark,rtrim(cheque_type) cheque_type,rtrim(cheque_no) cheque_no,amount,charge_date,dcount_date," +
            "rtrim(dcount_no) dcount_no,rtrim(op_id) op_id,receipt_sn,serial_no,rtrim(responce_type) responce_type,yb_jlh,yb_ywh,dept_no,psordnum,agtordnum,hisrefundnum " +
            " from mz_deposit_file where serial_no = #{serialNo}",
            "<when test='chequeType!=null'>",
            " and cheque_type = #{chequeType}",
            "</when>",
            "</script>"})
    List<MzDepositFile> selectMzDepositFileBySerialNo(@Param("serialNo") Integer serialNo,@Param("chequeType") String chequeType);

    /**
     * 更新收费表的退款标志等信息，发票作废重打时调用
     *
     * @param payMark
     * @param oriSerialNo
     * @return
     */
    @Update("update mz_deposit_file set pay_mark=#{payMark} where serial_no=#{oriSerialNo} and cheque_type=#{chequeType}")
    int updatePayMark(@Param("payMark") String payMark, @Param("oriSerialNo") Integer oriSerialNo,@Param("chequeType") String chequeType);


    /**
     * 更新收费表的收款方式，手机缴费打发票时修改
     *
     * @param chequeType
     * @param receiptSn
     * @param oldChequeType 原付款方式
     * @return
     */
    @Update({"<script>",
            "update mz_deposit_file set cheque_type=#{chequeType} where receipt_sn=#{receiptSn}",
            "<when test='oldChequeType!=null'>",
            " and cheque_type = #{oldChequeType}",
            "</when>"
            ,"</script>"})
    int updateChequeType(@Param("chequeType") String chequeType,@Param("receiptSn") Integer receiptSn,@Param("oldChequeType") String oldChequeType);

    /**
     * 判断是否已经入库，防止重复缴费
     * @param mzDepositFile
     * @return
     */
    @Select("select count(1) from mz_deposit_file where patient_id=#{patientId} and times = #{times} and receipt_no=#{receiptNo} and pay_mark=#{payMark} and cheque_type=#{chequeType} and amount=#{amount} and op_id=#{opId}")
    int selectBySameParams(MzDepositFile mzDepositFile);

    /**
     * 判断手机自助缴费平台对应的流水号有无成功缴费
     * @param psordnum
     * @param agtordnum
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,rtrim(pay_mark) pay_mark,rtrim(cheque_type) cheque_type,rtrim(cheque_no) cheque_no," +
            "amount,charge_date,dcount_date,rtrim(dcount_no) dcount_no,rtrim(op_id) op_id,receipt_sn,serial_no,rtrim(responce_type) responce_type,yb_jlh,yb_ywh,dept_no," +
            "psordnum,agtordnum,hisrefundnum from ${tableName} where psordnum=#{psordnum} and pay_mark = 0 ",
            "<when test='agtordnum!=null'>",
            " and agtordnum = #{agtordnum}",
            "</when>",
            "</script>"})
    MzDepositFile selectMzDepositFileByAgtordnum(@Param("psordnum") String psordnum,@Param("agtordnum") String agtordnum,@Param("tableName") String tableName);

    /**
     * 根据日期统计当天的收费总额
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select(" select sum(amount) from mz_deposit_file where charge_date >=#{beginDate,jdbcType=TIMESTAMP} and charge_date<=#{endDate,jdbcType=TIMESTAMP} and pay_mark <> 2 ")
    BigDecimal selectAmount(@Param("beginDate") Date beginDate,@Param("endDate") Date endDate);

    /**
     * 按照日期分组查询收费数与金额
     * @param beginTime
     * @param endTime
     * @param payMark
     * @return
     */
    @Select({"<script>",
            "select CONVERT(varchar(100), charge_date, 23) charge_date,sum(amount) amount from mz_deposit_file  where  1=1 ",
            "<when test='payMark!=null and payMark==0'>",
            " and pay_mark <![CDATA[<>]]> 2 ",
            "</when>",
            "<when test='payMark!=null and payMark==1'>",
            " and pay_mark = 1 ",
            "</when>",
            "<when test='beginTime!=null'>",
            " and charge_date &gt;=#{beginTime,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='endTime!=null'>",
            " and charge_date &lt;=#{endTime,jdbcType=TIMESTAMP} ",
            "</when>",
            " group by CONVERT(varchar(100), charge_date, 23) ",
            "</script>"})
    List<Map<String, Object>> countAndAmount(@Param("beginTime") Date beginTime,@Param("endTime") Date endTime,@Param("payMark") String payMark);


    /**
     * 分页查询缴费记录
     * @param mzDepositFile
     * @return
     */
    @Select({"<script>",
            "SELECT top ${pageSize} rtrim(patient_id) patient_id,times,receipt_no,rtrim(pay_mark) pay_mark,rtrim(cheque_type) cheque_type,rtrim(cheque_no) cheque_no," ,
                    "amount,charge_date,dcount_date,rtrim(dcount_no) dcount_no,rtrim(op_id) op_id,receipt_sn,serial_no,rtrim(responce_type) responce_type,yb_jlh,yb_ywh,dept_no," ,
                    "psordnum,agtordnum,hisrefundnum FROM (SELECT ROW_NUMBER() OVER (ORDER BY mz_deposit_file_page.${orderByCase} asc) ",
            "AS RowNumber,* from ${tableName} mz_deposit_file_page where 1=1 ",
            "<when test='patientId!=null'>",
            " and patient_id =#{patientId}",
            "</when>",
            "<when test='opId!=null'>",
            " and op_id =#{opId}",
            "</when>",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber asc",
            "</script>"})
    List<MzDepositFile> selectMzDepositFileWithPage(MzDepositFile mzDepositFile);

    /**
     *  查询缴费总数
     * @param mzDepositFile
     * @return
     */
    @Select({"<script>",
            "SELECT count(1)  from ${tableName} where 1=1 ",
            "<when test='patientId!=null'>",
            " and patient_id =#{patientId}",
            "</when>",
            "<when test='opId!=null'>",
            " and op_id =#{opId}",
            "</when>",
            "</script>"})
    int countMzDepositFile(MzDepositFile mzDepositFile);


}
