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

import cn.hnthyy.thmz.entity.his.zy.ZyDetailCharge;
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.Date;
import java.util.List;
import java.util.Map;

public interface ZyDetailChargeMapper {


    /**
     *  查询住院费用明细
     * @param inpatientNo
     * @param admissTimes
     * @param ledgerSn
     * @return
     */
    @Select("select inpatient_no , admiss_times , ledger_sn , detail_sn , charge_date , op_id_code , charge_code , infant_flag ," +
            " charge_status , charge_fee , self_flag , separate_flag , suppress_flag , ward_code , dept_code , order_no , ope_flag ," +
            " exec_unit , charge_amount , charge_code_mx , serial , zy_serial_no , gen_time  from  zy_detail_charge " +
            "  where inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ledger_sn =#{ledgerSn} ")
    List<ZyDetailCharge> selectZyDetailCharge(@Param("inpatientNo")String inpatientNo, @Param("admissTimes")Integer admissTimes, @Param("ledgerSn")Integer ledgerSn);


    /**
     * 查询住院明细
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @param infantFlag 是否婴儿
     * @param chargeStatuses 状态集合
     * @return
     */
    @Select({"<script>",
            "SELECT rtrim(inpatient_no) inpatientNo," +
            "admiss_times admissTimes," +
            "rtrim(ledger_sn) ledgerSn," +
            "rtrim(detail_sn) detailSn," +
            "charge_date chargeDate," +
            "rtrim(op_id_code) opIdCode," +
            "rtrim(charge_code) chargeCode," +
            "rtrim(charge_code_mx) chargeCodeMx," +
            "rtrim(infant_flag) infantFlag," +
            "rtrim(charge_status) chargeStatus," +
            "charge_fee chargeFee," +
            "rtrim(self_flag) selfFlag," +
            "rtrim(separate_flag) separateFlag," +
            "rtrim(suppress_flag) suppressFlag," +
            "rtrim(ward_code) wardCode," +
            "rtrim(dept_code) deptCode," +
            "orderNo=abs(order_no)," +
            "ope_flag opeFlag," +
            "rtrim(exec_unit) execUnit," +
            "        charge_amount chargeAmount," +
            "        gen_time genTime," +
                    "serial " +
            "FROM zy_detail_charge (nolock) " +
            "       " +
            "WHERE inpatient_no = #{inpatientNo}  and " +
            "         admiss_times = #{admissTimes} and " +
            "         infant_flag=0",
            "<when test='chargeStatuses!=null'>",
            " and charge_status in",
            "<foreach item='item' index='index' collection='chargeStatuses' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>"
            ,"</script>"})
    List<Map<String, Object>> selectZyDetailCharges(@Param("inpatientNo")String inpatientNo, @Param("admissTimes")Integer admissTimes, @Param("infantFlag")String infantFlag, @Param("chargeStatuses")List<String> chargeStatuses);

    /**
     * 保存单条记录
     * @param zyDetailCharge
     * @return
     */
    @Insert("INSERT INTO zy_detail_charge ( inpatient_no , admiss_times , ledger_sn , detail_sn , charge_date , op_id_code , charge_code , infant_flag , charge_status , " +
            "charge_fee , self_flag , separate_flag , suppress_flag , ward_code , dept_code , order_no , ope_flag , exec_unit , charge_amount , charge_code_mx , serial ," +
            " zy_serial_no , gen_time ) VALUES (#{inpatientNo},#{admissTimes},#{ledgerSn},#{detailSn},#{chargeDate},#{opIdCode},#{chargeCode},#{infantFlag},#{chargeStatus}," +
            "#{chargeFee},#{selfFlag},#{separateFlag},#{suppressFlag},#{wardCode},#{deptCode},#{orderNo},#{opeFlag},#{execUnit},#{chargeAmount},#{chargeCodeMx}," +
            "#{serial},#{zySerialNo},#{genTime})")
    int insertZyDetailCharge(ZyDetailCharge zyDetailCharge);

    /**
     * 插入费用冲销的住院明细
     * @param inpatientNo
     * @param admissTimes
     * @param ledgerSn
     * @param amountParams
     * @param genTime
     * @param newLedgerSn
     * @return
     */
    @Insert("INSERT INTO zy_detail_charge ( inpatient_no , admiss_times , ledger_sn , detail_sn , charge_date , op_id_code , charge_code , infant_flag , charge_status , charge_fee , " +
            "self_flag , separate_flag , suppress_flag , ward_code , dept_code , order_no , ope_flag , exec_unit , charge_amount , charge_code_mx , serial , zy_serial_no , gen_time ) " +
            "select inpatient_no , admiss_times , #{newLedgerSn} , detail_sn , charge_date , op_id_code , charge_code , infant_flag , charge_status , charge_fee *#{amountParams} , self_flag , " +
            "separate_flag , suppress_flag , ward_code , dept_code , order_no , ope_flag , exec_unit , charge_amount *#{amountParams} , charge_code_mx , serial , zy_serial_no ," +
            "#{genTime} from zy_detail_charge WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ledger_sn =#{ledgerSn} ")
    int insertForCancelCashi(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes, @Param("ledgerSn") Integer ledgerSn,@Param("amountParams") Integer amountParams,@Param("genTime") Date genTime, @Param("newLedgerSn") Integer newLedgerSn);


    /**
     * 更新账页信息，取消结算
     * @param inpatientNo
     * @param admissTimes
     * @param ledgerSn
     * @return
     */
    @Update("update zy_detail_charge SET charge_status ='2' WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ledger_sn =#{ledgerSn} AND charge_status ='3'  ")
    int updateZyDetailChargeForCanCel(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes, @Param("ledgerSn") Integer ledgerSn);
}
