<?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.ComputeItemCostMapper">

	<!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kcim.dao.model.ComputeItemCost" id="computeItemCostMap">
        <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="code" column="code"/>
        <result property="name" column="name"/>
        <result property="itemTypeCode" column="item_type_code"/>
        <result property="itemType" column="item_type"/>
        <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.ComputeItemCostPageDto">
        SELECT
        a.id as id,
        a.hosp_id as hosp_id,
        a.compute_date as compute_date,
        a.visit_no as visit_no,
        a.`code` as `code`,
        a.`name` as `name`,
        a.item_type_code as item_type_code,
        a.item_type as item_type,
        b.type as type,
        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_item_cost a
        LEFT JOIN compute_item_cost_detail b ON a.id = b.item_cost_id AND b.del_flag = 0
        WHERE
        a.hosp_id = #{hospId}
        AND a.del_flag = 0
        AND a.compute_date = #{computeDate}
        <if test="itemType != null and itemType != ''">
           and item_type_code = #{itemType,jdbcType=VARCHAR}
        </if>
        <if test="itemName != null and itemType != ''">
            and name like concat('%',#{itemName,jdbcType=VARCHAR},'%')
        </if>
        GROUP BY a.code
    </select>
    <select id="getListGroupByVisitNo" resultType="com.kcim.dao.model.dto.ComputeItemGroupByVisitNoDto">
        SELECT
        a.id as id,
        a.hosp_id as hosp_id,
        a.compute_date as compute_date,
        a.visit_no as visit_no,
        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_item_cost a
        LEFT JOIN compute_item_cost_detail b ON a.id = b.item_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 visit_no

    </select>
    <select id="getListGroupByOrder" resultType="com.kcim.dao.model.dto.ComputeItemGroupByOrder">
        SELECT
            a.id as id,
            a.hosp_id as hosp_id,
            a.compute_date as compute_date,
            a.visit_no as visit_no,
            a.order_code as order_code,
            a.order_name as order_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_item_cost a
                LEFT JOIN compute_item_cost_detail b ON a.id = b.item_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 visit_no,order_code
    </select>


</mapper>