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

	<!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kcim.dao.model.ComputeProjectCost" id="computeProjectCostMap">
        <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="itemType" column="item_type"/>
        <result property="orderCode" column="order_code"/>
        <result property="orderName" column="order_name"/>
        <result property="responsibilityCode" column="responsibility_code"/>
        <result property="responsibilityName" column="responsibility_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.vo.ComputeProjectCostPageDto">
        SELECT
            a.id as id,
            a.hosp_id as hosp_id,
            a.compute_date as compute_date,
            a.`code` as `code`,
            a.`name` as `name`,
            a.item_type as item_type,
            a.responsibility_code as responsibility_code,
            a.responsibility_name as responsibility_name,
            b.type,
            sum((IF(b.compute_result is null , 0.00,b.compute_result))) as compute_result,
            sum((IF(b.compute_single_result is null , 0.00,b.compute_single_result))) as compute_single_result
        FROM
            compute_project_cost a
                LEFT JOIN compute_project_group_cost_detail b
                          ON a.id = b.project_cost_id and b.del_flag = 0
        WHERE
            a.hosp_id = #{hospId,jdbcType=BIGINT}
          AND b.hosp_id= #{hospId,jdbcType=BIGINT}
          AND a.del_flag = 0
          AND a.compute_date = #{computeDate,jdbcType=VARCHAR}
          and b.compute_date = #{computeDate,jdbcType=VARCHAR}
        <if test="itemType != null and itemType != ''">
            and a.item_type = #{itemType,jdbcType=VARCHAR}
        </if>
        <if test="responsibility != null and responsibility != ''">
            and a.responsibility_code = #{responsibility,jdbcType=VARCHAR}
        </if>
        <if test="itemFilter != null and itemFilter != ''">
            and (a.code like concat('%',#{itemFilter,jdbcType=VARCHAR},'%') or a.name like
            concat('%',#{itemFilter,jdbcType=VARCHAR},'%'))
        </if>
        GROUP BY responsibility_code,`code`,item_type,type
    </select>
    <select id="getSingleByPage" resultType="com.kcim.vo.ComputeProjectCostPageDto">
        SELECT
        a.id as id,
        a.hosp_id as hosp_id,
        a.compute_date as compute_date,
        a.`code` as `code`,
        a.`name` as `name`,
        a.item_type as item_type,
        a.responsibility_code as responsibility_code,
        a.responsibility_name as responsibility_name,
        sum((IF(b.type = '11', b.compute_single_result, 0.00))) as managerShareCost,
        sum((IF(b.type = '10', b.compute_single_result, 0.00))) as publicShareCost,
        sum((IF(b.type = '6', b.compute_single_result, 0.00))) as itemCost,
        sum((IF(b.type = '7', b.compute_single_result, 0.00))) as empCost,
        sum((IF(b.type = '8', b.compute_single_result, 0.00))) as equipmentCost,
        sum((IF(b.type = '9', b.compute_single_result, 0.00))) as spaceCost
        FROM
        compute_project_cost a
        LEFT JOIN compute_project_group_cost_detail b
        ON a.id = b.project_cost_id and b.del_flag = 0
        WHERE
        a.hosp_id = #{hospId,jdbcType=BIGINT}
        AND b.hosp_id= #{hospId,jdbcType=BIGINT}
        AND a.del_flag = 0
        AND a.compute_date = #{computeDate,jdbcType=VARCHAR}
        and b.compute_date = #{computeDate,jdbcType=VARCHAR}
        <if test="itemType != null and itemType != ''">
            and a.item_type = #{itemType,jdbcType=VARCHAR}
        </if>
        <if test="responsibility != null and responsibility != ''">
            and a.responsibility_code = #{responsibility,jdbcType=VARCHAR}
        </if>
        <if test="itemFilter != null and itemFilter != ''">
            and (a.code like concat('%',#{itemFilter,jdbcType=VARCHAR},'%') or a.name like
            concat('%',#{itemFilter,jdbcType=VARCHAR},'%'))
        </if>
        GROUP BY responsibility_code,`code`,
        order_code
    </select>
    <select id="getColumnList" resultType="com.kcim.vo.ComputeProjectCostPageDto">
        SELECT
        a.id as id,
        a.hosp_id as hosp_id,
        a.compute_date as compute_date,
        a.`code` as `code`,
        a.`name` as `name`,
        a.item_type as item_type,
        a.responsibility_code as responsibility_code,
        a.responsibility_name as responsibility_name
        FROM
        compute_project_cost a
        LEFT JOIN compute_project_group_cost_detail b
        ON a.id = b.project_cost_id and b.del_flag = 0
        WHERE
        a.hosp_id = #{hospId,jdbcType=BIGINT}
        AND b.hosp_id= #{hospId,jdbcType=BIGINT}
        AND a.del_flag = 0
        AND a.compute_date = #{computeDate,jdbcType=VARCHAR}
        and b.compute_date = #{computeDate,jdbcType=VARCHAR}
        <if test="itemType != null and itemType != ''">
            and a.item_type = #{itemType,jdbcType=VARCHAR}
        </if>
        <if test="responsibility != null and responsibility != ''">
            and a.responsibility_code = #{responsibility,jdbcType=VARCHAR}
        </if>
        <if test="itemFilter != null and itemFilter != ''">
            and (a.code like concat('%',#{itemFilter,jdbcType=VARCHAR},'%') or a.name like
            concat('%',#{itemFilter,jdbcType=VARCHAR},'%'))
        </if>
        GROUP BY responsibility_code,`code`,item_type
    </select>
    <select id="getProjectCost" resultType="com.kcim.vo.ProjectCostVo">

        SELECT
            a.id as id,
            a.hosp_id as hosp_id,
            a.compute_date as compute_date,
            a.`code` as `code`,
            a.`name` as `name`,
            a.item_type as item_type,
            a.responsibility_code as responsibility_code,
            a.responsibility_name as responsibility_name,
            b.cost_column_code ,
            b.cost_column_type,
            sum(b.compute_result) as compute_result,
            sum(b.compute_single_result) as compute_single_result

        FROM
            compute_project_cost a
                LEFT JOIN compute_project_cost_detail b
                          ON a.id = b.project_cost_id and b.del_flag = 0
        WHERE
            a.hosp_id = #{hospId}
          AND b.hosp_id= #{hospId}
          AND a.del_flag = 0
          AND a.compute_date = #{computeDate}
          and b.compute_date = #{computeDate}
        GROUP BY code,responsibility_code,cost_column_code,item_type


    </select>


</mapper>