package thyyxxk.webserver.dao.his.zygl;

import org.apache.ibatis.annotations.Delete;
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.ZyReceipt;

import java.util.List;

/**
 * 住院发票数据库接口
 */
public interface ZyReceiptDao {
    /**
     * 删除住院发票
     * @param inpatientNo
     * @param admissTimes
     * @param ledgerSn
     * @return
     */
    @Delete("delete from zy_receipt WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ledger_sn =#{ledgerSn} ")
    int deleteZyReceipt(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes, @Param("ledgerSn") Integer ledgerSn);



    /**
     * 获取最后一个住院发票信息
     *
     * @param inpatientNo
     * @param admissTimes
     * @return
     */
    @Select("select top 1* from zy_receipt where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} order by ledger_sn desc ")
    ZyReceipt selectLastZyReceipt(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);


    /**
     * 查询当前病人本次住院所有有效发票信息
     * @param inpatientNo
     * @param admissTimes
     * @return
     */
    @Select("select * from zy_receipt where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn >0  ")
    List<ZyReceipt> selectAllValidReceipt(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);


    /**
     * 查询当前病人本次住院所有有效且未打印发票信息
     * @param inpatientNo
     * @param admissTimes
     * @return
     */
    @Select("select * from zy_receipt where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn >0  and (receipt_no is null or receipt_no ='00000000')")
    List<ZyReceipt> selectAllValidAndNotPrintReceipt(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);


    /**
     * 查询当前病人本次住院所有有效且已经打印发票信息
     * @param inpatientNo
     * @param admissTimes
     * @return
     */
    @Select("select * from zy_receipt where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn >0  and (receipt_no is not null and receipt_no <>'00000000')")
    List<ZyReceipt> selectAllPrintReceipt(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);


    /**
     * 根据住院号与住院次数以及账页号获取住院发票信息
     *
     * @param inpatientNo
     * @param admissTimes
     * @return
     */
    @Select("select * from zy_receipt where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn =#{ledgerSn}  ")
    ZyReceipt selectZyReceiptByLedgerSn(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes, @Param("ledgerSn") Long ledgerSn);


    /**
     * 根据发票号获取住院发票信息
     *
     * @param receiptNo
     * @return
     */
    @Select("select * from zy_receipt where receipt_no =#{receiptNo} and  ledger_sn >0 ")
    ZyReceipt selectZyReceiptByReceiptNo(@Param("receiptNo") String receiptNo);

    /**
     * 插入住院收费发票
     * @param zyReceipt
     * @return
     */
    @Insert("INSERT INTO zy_receipt ( inpatient_no , admiss_times , ledger_sn , receipt_sn , receipt_no , account_date , date_1 , date_2 , ward , dept , print_date , " +
            "responce_unit , op_id_code , total_charge , charge1 , charge2 , charge3 , charge4 , charge5 , charge6 , charge7 , charge8 , charge9 , charge10 , charge11 , " +
            "charge12 , charge13 , charge14 , charge15 , charge16 , charge17 , charge18 , charge19 , charge20 , charge21 , charge22 , charge23 , charge24 , charge25 ," +
            " charge26 , charge27 , charge28 , charge29 , charge30 ) VALUES (#{inpatientNo},#{admissTimes},#{ledgerSn},#{receiptSn},#{receiptNo},#{accountDate},#{date1}," +
            "#{date2},#{ward},#{dept},#{printDate},#{responceUnit},#{opIdCode},#{totalCharge},#{charge1}, #{charge2} ,#{charge3} ,#{charge4},#{charge5} , #{charge6}, #{charge7} ," +
            " #{charge8}, #{charge9} , #{charge10} ,#{charge11} , #{charge12}, #{charge13} , #{charge14}, #{charge15} ,#{charge16} , #{charge17},#{charge18}, #{charge19}," +
            " #{charge20} , #{charge21} , #{charge22} , #{charge23} , #{charge24} , #{charge25} , #{charge26} ,#{charge27} ,#{charge28} , #{charge29} , #{charge30})")
    int insertZyReceipt(ZyReceipt zyReceipt);



    /**
     * 插入作废住院收费发票
     * @param zyReceipt
     * @return
     */
    @Insert("INSERT INTO zy_receipt_obsolete ( inpatient_no , admiss_times , ledger_sn , receipt_sn , receipt_no , account_date , date_1 , date_2 , ward , dept , print_date , " +
            "responce_unit , op_id_code , total_charge , charge1 , charge2 , charge3 , charge4 , charge5 , charge6 , charge7 , charge8 , charge9 , charge10 , charge11 , " +
            "charge12 , charge13 , charge14 , charge15 , charge16 , charge17 , charge18 , charge19 , charge20 , charge21 , charge22 , charge23 , charge24 , charge25 ," +
            " charge26 , charge27 , charge28 , charge29 , charge30,receipt_status,update_time,update_id ) VALUES (#{inpatientNo},#{admissTimes},#{ledgerSn},#{receiptSn},#{receiptNo},#{accountDate},#{date1}," +
            "#{date2},#{ward},#{dept},#{printDate},#{responceUnit},#{opIdCode},#{totalCharge},#{charge1}, #{charge2} ,#{charge3} ,#{charge4},#{charge5} , #{charge6}, #{charge7} ," +
            " #{charge8}, #{charge9} , #{charge10} ,#{charge11} , #{charge12}, #{charge13} , #{charge14}, #{charge15} ,#{charge16} , #{charge17},#{charge18}, #{charge19}," +
            " #{charge20} , #{charge21} , #{charge22} , #{charge23} , #{charge24} , #{charge25} , #{charge26} ,#{charge27} ,#{charge28} , #{charge29} , #{charge30},#{receiptStatus} , #{updateTime} , #{updateId})")
    int insertZyReceiptObsolete(ZyReceipt zyReceipt);

    /**
     * 清空住院发票发票号与打印时间以及打印人信息
     * @param zyReceipt
     * @return
     */
    @Update("update zy_receipt set receipt_no =#{receiptNo},print_date=#{printDate},op_id_code=#{opIdCode} where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn=#{ledgerSn} ")
    int clearZyReceipt(ZyReceipt zyReceipt);


    /**
     * 打印发票
     * @param zyReceipt
     * @return
     */
    @Update("update zy_receipt set receipt_no =#{receiptNo},print_date=#{printDate},op_id_code=#{opIdCode} where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn=#{ledgerSn}  and (receipt_no is null or receipt_no ='00000000')")
    int printReceipt(ZyReceipt zyReceipt);

    /**
     * 修改发票信息
     * @param zyReceipt
     * @return
     */
    @Update({"<script>",
            "update zy_receipt ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ledger_sn =#{ledgerSn}' >",
            "<when test='receiptNo!=null'>",
            "receipt_no =#{receiptNo}",
            "</when>",
            "<when test='printDate!=null'>",
            ",print_date =#{printDate}",
            "</when>",
            "<when test='opIdCode!=null'>",
            ",op_id_code =#{opIdCode}",
            "</when>",
            "<when test='receiptStatus!=null'>",
            ",receipt_status =#{receiptStatus}",
            "</when>",
            "<when test='updateTime!=null'>",
            ",update_time =#{updateTime}",
            "</when>",
            "<when test='updateId!=null'>",
            ",update_id =#{updateId}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateZyReceiptCommon(ZyReceipt zyReceipt);
}
