package cn.hnthyy.thmz.mapper.thmz;

import cn.hnthyy.thmz.entity.thmz.Mzmxsr;
import cn.hnthyy.thmz.vo.ThmzmxsrParamsVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

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

public interface MzmxsrMapper {
    /**
     * 单独插入数据
     * @param mzmxsr
     * @return
     */
    @Insert("INSERT INTO t_mzmxsr(xm, patient_id, jz_sq_dept, jz_sq_doctor, exec_dept, xmm, hsm, je, charge_date, dcount_date, quty, price, parent_code, input_date) VALUES" +
            " (#{xm,jdbcType=VARCHAR}, #{patientId,jdbcType=VARCHAR}, #{jzSqDept,jdbcType=VARCHAR}, #{jzSqDoctor,jdbcType=VARCHAR}, #{execDept,jdbcType=VARCHAR}, #{xmm,jdbcType=VARCHAR}, #{hsm,jdbcType=VARCHAR}, #{je,jdbcType=DECIMAL}," +
            " #{chargeDate,jdbcType=TIMESTAMP}, #{dcountDate,jdbcType=TIMESTAMP}, #{quty,jdbcType=DOUBLE}, #{price,jdbcType=DECIMAL}, #{parentCode,jdbcType=VARCHAR}, #{inputDate,jdbcType=TIMESTAMP})")
    int insertMzmxsr(Mzmxsr mzmxsr);


    /**
     * 批量保存门诊收费明细记录
     *
     * @param mzmxsrList
     * @return
     */
    @Insert({
            "<script>",
            "INSERT INTO  t_mzmxsr(xm, patient_id, jz_sq_dept, jz_sq_doctor, exec_dept, xmm, hsm, je, charge_date, dcount_date, quty, price, parent_code, input_date) VALUES ",
            "<foreach collection='mzmxsrList' item='item' index='index' separator=','>",
            " (#{item.xm,jdbcType=VARCHAR}, #{item.patientId,jdbcType=VARCHAR}, #{item.jzSqDept,jdbcType=VARCHAR}, #{item.jzSqDoctor,jdbcType=VARCHAR}," +
                    " #{item.execDept,jdbcType=VARCHAR}, #{item.xmm,jdbcType=VARCHAR}, #{item.hsm,jdbcType=VARCHAR}, #{item.je,jdbcType=DECIMAL}," +
                    "#{item.chargeDate,jdbcType=TIMESTAMP}, #{item.dcountDate,jdbcType=TIMESTAMP}, #{item.quty,jdbcType=DOUBLE}, " +
                    "#{item.price,jdbcType=DECIMAL}, #{item.parentCode,jdbcType=VARCHAR}, #{item.inputDate,jdbcType=TIMESTAMP}) ",
            "</foreach>",
            "</script>"
    })
    int batchInsertMzmxsr(@Param(value = "mzmxsrList") List<Mzmxsr> mzmxsrList);

    /**
     * 分页查询门诊收入明细
     *
     *
     * @param thmzmxsrParamsVo
     * @return
     */
    @Select({"<script>" +
            "select a.id,xm, patient_id, jz_sq_dept, jz_sq_doctor, exec_dept, xmm, hsm, je, charge_date, dcount_date, quty, price, parent_code, " +
            "input_date from t_mzmxsr a join  ("+
            "select id from t_mzmxsr where 1=1"+
            "<when test='caseType==1'>" +
            " and charge_date  <![CDATA[>=]]>#{beginDate,jdbcType=TIMESTAMP} and charge_date  <![CDATA[<=]]>#{endDate,jdbcType=TIMESTAMP}" +
            "</when>" +
            "<when test='caseType==0'>" +
            " and dcount_date  <![CDATA[>=]]>#{beginDate,jdbcType=TIMESTAMP} and dcount_date  <![CDATA[<=]]>#{endDate,jdbcType=TIMESTAMP}" +
            "</when>" +
            "<when test='patientIds!=null'>" +
                    " and patient_id in" +
                    "<foreach item='item' index='index' collection='patientIds' open='(' separator=',' close=')'>" +
                    "#{item}" +
                    "</foreach>" +
                    "</when>"+
                    "<when test='jzSqDept!=null'>" +
                    " and jz_sq_dept =#{jzSqDept}" +
                    "</when>" +
                    "<when test='jzSqDoctor!=null'>" +
                    " and jz_sq_doctor =#{jzSqDoctor}" +
                    "</when>" +
                    "<when test='execDept!=null'>" +
                    " and exec_dept =#{execDept}" +
                    "</when>" +
                    "<when test='hsm!=null'>" +
                    " and hsm =#{hsm}" +
                    "</when>" +
                    "<when test='parentCode!=null'>" +
                    " and parent_code =#{parentCode}" +
                    "</when>" +
                    "<when test='xmm!=null'>" +
                    " and xmm =#{xmm}" +
                    "</when>"+
            " order by id limit #{offset},#{pageSize} )b on a.id=b.id"
            + "</script>"})
    List<Mzmxsr> selectMzmxsrWithPage(ThmzmxsrParamsVo thmzmxsrParamsVo);




    /**
     * 查询符合条件的总数
     *
     *
     * @param thmzmxsrParamsVo
     * @return
     */
    @Select({"<script>" +
            "select count(1) from t_mzmxsr where 1=1"+
            "<when test='caseType==1'>" +
            " and charge_date  <![CDATA[>=]]>#{beginDate,jdbcType=TIMESTAMP} and charge_date  <![CDATA[<=]]>#{endDate,jdbcType=TIMESTAMP}" +
            "</when>" +
            "<when test='caseType==0'>" +
            " and dcount_date  <![CDATA[>=]]>#{beginDate,jdbcType=TIMESTAMP} and dcount_date  <![CDATA[<=]]>#{endDate,jdbcType=TIMESTAMP}" +
            "</when>" +
            "<when test='patientIds!=null'>" +
            " and patient_id in" +
            "<foreach item='item' index='index' collection='patientIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</when>"+
            "<when test='jzSqDept!=null'>" +
            " and jz_sq_dept =#{jzSqDept}" +
            "</when>" +
            "<when test='jzSqDoctor!=null'>" +
            " and jz_sq_doctor =#{jzSqDoctor}" +
            "</when>" +
            "<when test='execDept!=null'>" +
            " and exec_dept =#{execDept}" +
            "</when>" +
            "<when test='hsm!=null'>" +
            " and hsm =#{hsm}" +
            "</when>" +
            "<when test='parentCode!=null'>" +
            " and parent_code =#{parentCode}" +
            "</when>" +
            "<when test='xmm!=null'>" +
            " and xmm =#{xmm}" +
            "</when>"
            + "</script>"})
    Integer selectCountMzmxsr(ThmzmxsrParamsVo thmzmxsrParamsVo);



    /**
     * 查询符合条件的总金额
     *
     *
     * @param thmzmxsrParamsVo
     * @return
     */
    @Select({"<script>" +
            "select sum(je) from t_mzmxsr where 1=1"+
            "<when test='caseType==1'>" +
            " and charge_date  <![CDATA[>=]]>#{beginDate,jdbcType=TIMESTAMP} and charge_date  <![CDATA[<=]]>#{endDate,jdbcType=TIMESTAMP}" +
            "</when>" +
            "<when test='caseType==0'>" +
            " and dcount_date  <![CDATA[>=]]>#{beginDate,jdbcType=TIMESTAMP} and dcount_date  <![CDATA[<=]]>#{endDate,jdbcType=TIMESTAMP}" +
            "</when>" +
            "<when test='patientIds!=null'>" +
            " and patient_id in" +
            "<foreach item='item' index='index' collection='patientIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</when>"+
            "<when test='jzSqDept!=null'>" +
            " and jz_sq_dept =#{jzSqDept}" +
            "</when>" +
            "<when test='jzSqDoctor!=null'>" +
            " and jz_sq_doctor =#{jzSqDoctor}" +
            "</when>" +
            "<when test='execDept!=null'>" +
            " and exec_dept =#{execDept}" +
            "</when>" +
            "<when test='hsm!=null'>" +
            " and hsm =#{hsm}" +
            "</when>" +
            "<when test='parentCode!=null'>" +
            " and parent_code =#{parentCode}" +
            "</when>" +
            "<when test='xmm!=null'>" +
            " and xmm =#{xmm}" +
            "</when>"
            + "</script>"})
    BigDecimal selectSumAmount(ThmzmxsrParamsVo thmzmxsrParamsVo);


    /**
     * 按天查询不所有的收费病人id
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select("select patient_id from t_mzmxsr where dcount_date>=#{beginDate}  and dcount_date<=#{endDate} GROUP BY patient_id")
    List<String> selectAllPatientId(@Param(value = "beginDate") String beginDate,@Param(value = "endDate") String endDate);

}
