package thyyxxk.webserver.dao.his.zygl;

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 thyyxxk.webserver.entity.inpatient.charge.ZyDepositFile;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public interface ZyDepositFileDao {
    /**
     * 根据住院号和次数查询缴费记录
     *
     * @param zyDepositFile
     * @return
     */
    @Select("  SELECT inpatient_no," +
            "         admiss_times," +
            "         depo_times," +
            "         depo_date," +
            "         depo_amount," +
            "         depo_type," +
            "         status=case when window_no='2' and status='2' then '4' " +
            "                     when window_no='2' and status='4' then '3' " +
            "                     else status end," +
            "         cheque_no ," +
            "         receipt_no," +
            "         op_id_code," +
            "         ledger_sn ," +
            "         printed   ," +
            "         window_no,report_flag,deleted,psordnum,agtordnum,par_channel,trans_date,trace_no  " +
            "    FROM zy_deposit_file " +
            "   WHERE  " +
            "          inpatient_no = #{inpatientNo}  AND  " +
            "          admiss_times = #{admissTimes}   order by ledger_sn asc")
    List<ZyDepositFile> selectZyDepositFileByInpatientNoAndTimes(ZyDepositFile zyDepositFile);

    /**
     * 查询当前最大缴费次数
     *
     * @param zyDepositFile
     * @return
     */
    @Select("select max(depo_times) from zy_deposit_file where inpatient_no =#{inpatientNo} and  admiss_times =#{admissTimes} ")
    Integer selectMaxDepoTimes(ZyDepositFile zyDepositFile);

    /**
     * 入库住院收费记录
     *
     * @param zyDepositFile
     * @return
     */
    @Insert("insert into zy_deposit_file ( inpatient_no , admiss_times , depo_times , depo_date , op_id_code , depo_amount , depo_type , status , cheque_no , printed , receipt_no , ledger_sn , window_no , deleted,psordnum,agtordnum,par_channel,trans_date,trace_no ) values" +
            " ( #{inpatientNo} , #{admissTimes} , #{depoTimes} , #{depoDate} , #{opIdCode} , #{depoAmount} , #{depoType} , #{status} , #{chequeNo} , #{printed} , #{receiptNo} , #{ledgerSn} , #{windowNo}, #{deleted},#{psordnum},#{agtordnum},#{parChannel},#{transDate},#{traceNo} ) ")
    int insetZyDepositFile(ZyDepositFile zyDepositFile);


    /**
     * 按照收据号查询未退费的收据内容
     *
     * @param receiptNo
     * @return
     */
    @Select("select * from zy_deposit_file where receipt_no=#{receiptNo} and depo_amount>0")
    ZyDepositFile selectZyDepositFileByReceiptNo(@Param("receiptNo") String receiptNo);


    /**
     * 按照收据号查询该收据号的总费用  ==0 说明已经退了费  >0 说明未退费
     *
     * @param receiptNo
     * @return
     */
    @Select("select sum(depo_amount) from zy_deposit_file where receipt_no=#{receiptNo}")
    BigDecimal selectSumDepoAmountByReceiptNo(@Param("receiptNo") String receiptNo);
    /**
     * @description: 根据住院号查询  总费用  ==0 说明已经退了费  >0 说明未退费
     * @author: lihong
     * @date: 2023/11/15 14:39
     * @param: inpatientNo
     * @param: admissTimes
     * @return: java.math.BigDecimal
     **/
    @Select(" select sum(depo_amount) from zy_deposit_file where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} ")
    BigDecimal selectSumDepoAmountByInpatientNo(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);

    /**
     * 获取最后一个收据信息
     *
     * @param inpatientNo
     * @param admissTimes
     * @return
     */
    @Select("select top 1* from zy_deposit_file where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} order by depo_times desc ")
    ZyDepositFile selectLastZyDepositFile(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Double admissTimes);

    /**
     * 根据收据号将状态标记为已退费
     *
     * @param receiptNo
     * @return
     */
    @Update("update zy_deposit_file set report_flag=1 where receipt_no=#{receiptNo}")
    int updateReportFlag(@Param("receiptNo") String receiptNo);


    /**
     * 根据收据号将发票标记为已打印发票
     *
     * @param receiptNo
     * @return
     */
    @Update("update zy_deposit_file set printed=1 where receipt_no=#{receiptNo}")
    int updatePrinted(@Param("receiptNo") String receiptNo);


    /**
     * 修改收据信息
     *
     * @param zyDepositFile
     * @return
     */
    @Update({"<script>",
            "update zy_deposit_file ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND depo_times =#{depoTimes}' >",
            "<when test='ledgerSn!=null'>",
            "ledger_sn =#{ledgerSn}",
            "</when>",
            "<when test='deleted!=null'>",
            ",deleted =#{deleted}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateZyDepositFileCommon(ZyDepositFile zyDepositFile);


    /**
     * 更新核算人和核算时间
     *
     * @param inpatientNo
     * @param admissTimes
     * @param opId
     * @param hsTime
     * @return
     */
    @Update("update zy_deposit_file SET hsr =#{opId} , hs_time =#{hsTime} WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ( hs_time is null or hs_time =null ) AND window_no ='1' and status in ( '1' , '2' ) ")
    int updateHsrAndHsTime(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Double admissTimes, @Param("opId") String opId, @Param("hsTime") Date hsTime);


    /**
     * 判断手机自助缴费平台对应的流水号有无成功住院预交金
     *
     * @param psordnum
     * @param agtordnum
     * @return
     */
    @Select({"<script>",
            "select * from dbo.${tableName} where psordnum=#{psordnum}  and depo_amount>0 ",
            "<when test='agtordnum!=null'>",
            " and agtordnum = #{agtordnum}",
            "</when>",
            "</script>"})
    ZyDepositFile selectZyDepositFileByAgtordnum(@Param("psordnum") String psordnum, @Param("agtordnum") String agtordnum, @Param("tableName") String tableName);
    @Select(" select count(*) from zy_deposit_file where receipt_no=#{receiptNo} ")
    Integer countReceiptNo(String receiptNo);
}
