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

import cn.hnthyy.thmz.entity.his.zy.ZyLedgerFile;
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 ZyLedgerFileMapper {

    /**
     * 根据病人的住院号和住院次数和账页号查询账页信息
     *
     * @param inpatientNo
     * @param admissTimes
     * @return
     */
    @Select("select * from zy_ledger_file where inpatient_no=#{inpatientNo} and admiss_times = #{admissTimes} AND ledger_sn =#{ledgerSn} ")
    ZyLedgerFile selectZyLedgerFileByInpatientNoAndTimesAndLedgerSn(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes, @Param("ledgerSn") Integer ledgerSn);


    /**
     * 根据病人的住院号和住院次数查询账页信息
     *
     * @param inpatientNo
     * @param admissTimes
     * @return
     */
    @Select("select * from zy_ledger_file where inpatient_no=#{inpatientNo} and admiss_times = #{admissTimes}  ")
    List<ZyLedgerFile> selectZyLedgerFileByInpatientNoAndTimes(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);


    /**
     * 插入账页信息
     *
     * @param zyLedgerFile
     * @return
     */
    @Insert("INSERT INTO zy_ledger_file (inpatient_no , admiss_times , ledger_sn , op_id_code , deposit , balance , 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 , " +
            "settle , last_balance , settle_type , dept_code , ward_code , dept_x , ward_x , account_date , cash_date , cash_id , dcount_no , dcount_date , zy_serial_no )" +
            " VALUES ( #{inpatientNo} , #{admissTimes},#{ledgerSn} , #{opIdCode}, #{deposit} , #{balance} , #{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} ," +
            " #{settle} ,#{lastBalance}, #{settleType} , #{deptCode} , #{wardCode} , #{deptX} , #{wardX} , #{accountDate} , #{cashDate} , #{cashId} , #{dcountNo} , #{dcountDate} , #{zySerialNo} ) ")
    int insetZyLedgerFile(ZyLedgerFile zyLedgerFile);

    /**
     * 查询当前未结算的账页信息
     *
     * @param inpatientNo
     * @param admissTimes
     * @return
     */
    @Select("select ledger_sn from  zy_ledger_file where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes}  and  account_date is null ")
    Integer selectCurrentLedgerSn(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);


    /**
     * 查询当前最大的可以结算的账页信息
     *
     * @param inpatientNo
     * @param admissTimes
     * @return
     */
    @Select("select isnull(max ( ledger_sn ) , min ( ledger_sn )) from  zy_ledger_file where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} ")
    Integer selectMaxLedgerSn(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);

    /**
     * 获取最后一个账页信息
     *
     * @param inpatientNo
     * @param admissTimes
     * @return
     */
    @Select("select top 1* from zy_ledger_file where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} order by ledger_sn desc ")
    ZyLedgerFile selectLastLedgerSn(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);


    /**
     * 更新金额
     *
     * @param zyLedgerFile
     * @return
     */
    @Update("update zy_ledger_file SET deposit =#{deposit} , balance =#{balance} WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ledger_sn =#{ledgerSn} ")
    int updateZyLedgerFile(ZyLedgerFile zyLedgerFile);


    @Update({"<script>",
            "update zy_ledger_file ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ledger_sn =#{ledgerSn}' >",
            "<when test='deposit!=null'>",
            "deposit =#{deposit}",
            "</when>",
            "<when test='cashDate!=null'>",
            ",cash_date =#{cashDate}",
            "</when>",
            "<when test='settleType!=null'>",
            ",settle_type =#{settleType}",
            "</when>",
            "<when test='cashId!=null'>",
            ",cash_id =#{cashId}",
            "</when>",
            "<when test='balance!=null'>",
            ",balance =#{balance}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateZyLedgerFileCommon(ZyLedgerFile zyLedgerFile);


    /**
     * 更新账页信息，取消结算
     * @param inpatientNo
     * @param admissTimes
     * @param ledgerSn
     * @return
     */
    @Update("update zy_ledger_file SET account_date =NULL , ward_code =NULL , dept_code =NULL , balance =deposit - total_charge , settle =0 , settle_type ='0' WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ledger_sn =#{ledgerSn} ")
    int updateZyLedgerFileForCanCel(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes, @Param("ledgerSn") Integer ledgerSn);


    /**
     * 更新账页信息，取消出纳
     * @param inpatientNo
     * @param admissTimes
     * @param ledgerSn
     * @return
     */
    @Update("update zy_ledger_file set settle_type ='1' , cash_date =null , cash_id =null , dcount_no ='0' , dcount_date =null , receipt_no ='00000000' where inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ledger_sn =#{ledgerSn}")
    int  updateZyLedgerFileForCancelCashi(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes, @Param("ledgerSn") Integer ledgerSn);


    /**
     * 查询当前最小的账页信息
     *
     * @param inpatientNo
     * @param admissTimes
     * @return
     */
    @Select("select  min( ledger_sn ) from  zy_ledger_file where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} ")
    Integer selectMinLedgerSn(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);

    /**
     * 出纳信息查询
     * @param inpatientNo
     * @param admissTimes
     * @param ledgerSn
     * @return
     */
    @Select("select account_date , cash_date , settle , last_balance , deposit , total_charge,ybjzje=total_charge - ( deposit - settle ),zfje=( deposit - settle ) from zy_ledger_file where inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ledger_sn =#{ledgerSn} ")
    Map<String,Object> selectTellerInfo(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes, @Param("ledgerSn") Integer ledgerSn);

}
