<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.kcim.dao.mapper.ComputePatientCostMapper">

	<!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kcim.dao.model.ComputePatientCost" id="computePatientCostMap">
        <result property="id" column="id"/>
        <result property="hospId" column="hosp_id"/>
        <result property="computeDate" column="compute_date"/>
        <result property="visitNo" column="visit_no"/>
        <result property="patientNo" column="patient_no"/>
        <result property="name" column="name"/>
        <result property="departmentCode" column="department_code"/>
        <result property="departmentName" column="department_name"/>
        <result property="createUser" column="create_user"/>
        <result property="createTime" column="create_time"/>
        <result property="updateUser" column="update_user"/>
        <result property="updateTime" column="update_time"/>
        <result property="deleteUser" column="delete_user"/>
        <result property="deleteTime" column="delete_time"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>
    <select id="getByPage" resultType="com.kcim.dao.model.dto.ComputePatientCostPageDto">
        SELECT
            a.id as id,
            a.hosp_id as hosp_id,
            a.compute_date as compute_date,
            a.visit_no as visit_no,
            a.patient_no as patient_no,
            a.`name` as `name`,
            a.department_code as department_code,
            a.department_name as department_name,
            sum((IF(b.type = '1', b.compute_result, 0.00))) as drugIncome,
            sum((IF(b.type = '2', b.compute_result, 0.00))) as materialIncome,
            sum((IF(b.type = '3', b.compute_result, 0.00))) as itemIncome,
            sum((IF(b.type = '4', b.compute_result, 0.00))) as drugCost,
            sum((IF(b.type = '5', b.compute_result, 0.00))) as materialCost,
            sum((IF(b.type = '6', b.compute_result, 0.00))) as itemCost,
            sum((IF(b.type = '7', b.compute_result, 0.00))) as empCost,
            sum((IF(b.type = '8', b.compute_result, 0.00))) as equipmentCost,
            sum((IF(b.type = '9', b.compute_result, 0.00))) as spaceCost
        FROM
            compute_patient_cost a
                LEFT JOIN compute_patient_cost_detail b ON a.id = b.patient_cost_id
        WHERE
            a.hosp_id = #{hospId}
          AND b.hosp_id= #{hospId}
          AND a.del_flag = 0
          AND a.compute_date = #{computeDate}
          AND b.del_flag = 0
        <if test="patientName != null and patientName != ''">
            and a.name  like concat('%',#{patientName,jdbcType=VARCHAR},'%')
        </if>
        <if test="departmentName != null and departmentName != ''">
            and a.department_name like concat('%',#{departmentName,jdbcType=VARCHAR},'%')
        </if>
        GROUP BY visit_no,name
        order by name


    </select>
    <select id="getListGroupByDepartment" resultType="com.kcim.dao.model.dto.ComputePatientCostGroupByDepartmentDto">
        SELECT
            a.id as id,
            a.hosp_id as hosp_id,
            a.compute_date as compute_date,
            a.department_code as department_code,
            a.department_name as department_name,
            sum((IF(b.type = '1', b.compute_result, 0.00))) as drugIncome,
            sum((IF(b.type = '2', b.compute_result, 0.00))) as materialIncome,
            sum((IF(b.type = '3', b.compute_result, 0.00))) as itemIncome,
            sum((IF(b.type = '4', b.compute_result, 0.00))) as drugCost,
            sum((IF(b.type = '5', b.compute_result, 0.00))) as materialCost,
            sum((IF(b.type = '6', b.compute_result, 0.00))) as itemCost,
            sum((IF(b.type = '7', b.compute_result, 0.00))) as empCost,
            sum((IF(b.type = '8', b.compute_result, 0.00))) as equipmentCost,
            sum((IF(b.type = '9', b.compute_result, 0.00))) as spaceCost
        FROM
            compute_patient_cost a
                LEFT JOIN compute_patient_cost_detail b ON a.id = b.patient_cost_id
        WHERE
            a.hosp_id = #{hospId}
          AND b.hosp_id=#{hospId}
          AND a.del_flag = 0
          AND a.compute_date = #{computeDate}
          AND b.del_flag = 0
        GROUP BY department_code;
    </select>


</mapper>