package thyyxxk.webserver.dao.his.datamodify;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.datamodify.ZyDetailCharge;

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

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author 肖蟾
 * @description: 住院费用修改
 * @since 2021-03-13
 */
@Mapper
public interface ZyDetailChargeDao {


    /**
     * 修改单个住院费用的 金额
     *
     * @param data 需要修改的数据
     * @return 数据
     */
    @Update("update zy_detail_charge set charge_fee = #{newChargeFee} " +
            "where inpatient_no=#{inpatientNo} and detail_sn=#{detailSn} " +
            "and admiss_times=#{admissTimes} and ledger_sn=#{ledgerSn} ")
    boolean totalModify(ZyDetailCharge data);

    /**
     * 根据条件 来查询 住院费用
     *
     * @param page         分页的条件
     * @param chargeCodeMx 收费项目码
     * @param inpatientNo  住院号
     */
    @Select("select admiss_times,ledger_sn,detail_sn,charge_date,rtrim(inpatient_no) as inpatient_no," +
            "a.serial,infant_flag,charge_status, " +
            "op_id_code=rtrim((select name from a_employee_mi where code=op_id_code)), " +
            "case when ((select name from zd_charge_item where  code = charge_code_mx) is null) then (b.name) " +
            "else (select name from zd_charge_item where  code = charge_code_mx ) " +
            "end as charge_code, " +
            "charge_fee,a.self_flag, " +
            "ward_code=(select name from zd_unit_code where code = ward_code), " +
            "dept_code=(select name from zd_unit_code where code = dept_code), " +
            "order_no,ope_flag, " +
            "exec_unit=(select name from zd_unit_code where code=exec_unit), " +
            "gen_time,a.charge_code_mx,charge_amount " +
            "from zy_detail_charge as a " +
            "inner join yp_zd_dict as b on (a.charge_code_mx = code and a.serial = b.serial) " +
            "where inpatient_no=#{inpatientNo} and charge_code_mx=#{chargeCodeMx} and " +
            "admiss_times=(select admiss_times from zy_actpatient where inpatient_no=a.inpatient_no) " +
            "and ledger_sn=(select max(ledger_sn) from zy_ledger_file where inpatient_no=a.inpatient_no and admiss_times=a.admiss_times)")
    void queryHospitalizationExpenses(IPage<ZyDetailCharge> page,
                                                       @Param("inpatientNo") String inpatientNo,
                                                       @Param("chargeCodeMx") String chargeCodeMx);
    @Insert("<script>" +
            "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, doctor_code,  " +
            "                              refer_physician," +
            "                              consult_physician, gen_time, yb_self_flag) values " +
            "<foreach collection='list' item='item' index='index'  separator=','>" +
            "( #{item.inpatientNo},#{item.admissTimes},#{item.ledgerSn},#{item.detailSn},#{item.chargeDate},#{item.opIdCode},#{item.chargeCode}" +
            "  ,#{item.infantFlag},#{item.chargeStatus},#{item.chargeFee},#{item.selfFlag} " +
            " ,#{item.separateFlag},#{item.suppressFlag},#{item.wardCode},#{item.deptCode} " +
            " ,#{item.orderNo},#{item.opeFlag},#{item.execUnit},#{item.chargeAmount},#{item.chargeCodeMx},#{item.serial},#{item.doctorCode}" +
            " ,#{item.referPhysician},#{item.consultPhysician},#{item.genTime},#{item.ybSelfFlag}  )" +
            "</foreach>" +
            "</script>")
    int insertZyDetailCharge(@Param("list") List<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);

    @Select(" select sum ( charge_fee ) from zy_detail_charge where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes}")
    BigDecimal selectSumChargeFee(@Param("inpatientNo") String inpatientNo, @Param("admissTimes")Integer admissTimes);
}
