<?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="org.sky.scientific.mapper.XmDirectCostMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmDirectCostEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="year_and_month" property="yearAndMonth"/>
        <result column="use_date" property="useDate"/>
        <result column="xm_id" property="xmId"/>
        <result column="xmmc" property="xmmc"/>
        <result column="xmbh" property="xmbh"/>
        <result column="type" property="type"/>
        <result column="amount" property="amount"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <delete id="physicalDeleteByIds">
        Delete FROM kd_xm_direct_cost
        WHERE id IN
        <foreach item="item" index="index" collection="list" separator="," close=")" open="(">
            #{item}
        </foreach>
    </delete>

    <select id="selectXmDirectCostPage" resultMap="ResultMap">
        select kxl.*, xm.xmmc, xm.xmbh
        from kd_xm_direct_cost kxl
        left join
        (
            SELECT
                m.id, m.xmbh,
                COALESCE(get_xm_info(m.id, 'XMMC', #{ew.yearAndMonth}),XMMC) AS XMMC
            FROM kd_xm m
        ) xm on xm.id = kxl.xm_id
        where kxl.is_deleted = 0 and kxl.year_and_month = #{ew.yearAndMonth}
        <if test="ew.xmId!=null and ew.xmId>0">and kxl.xm_id =#{ew.xmId}</if>
        <if test="ew.xmmc!=null and ew.xmbh!=''">and xm.xmmc like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.xmbh!=null and ew.xmbh!=''">and xm.xmbh like concat('%',#{ew.xmbh},'%')</if>
        <if test="ew.type!=null and ew.type!=''">and kxl.type = #{ew.type}</if>
    </select>

    <select id="tourufeiyonghuizongbiao" resultType="org.sky.scientific.pojo.vo.XmDirectCostSummary" parameterType="org.sky.scientific.pojo.entity.XmDirectCostEntity">
        SELECT
        id,
        XMBH,
        COALESCE(get_xm_info(xm.id, 'XMMC', #{yearAndMonth}),XMMC) AS XMMC,
        xm.XMKSSJ,
        COALESCE(get_xm_info(xm.id, 'XMJSSJ', #{yearAndMonth}),XMJSSJ) AS XMJSSJ,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_goods g WHERE xm_id=xm.id and g.year_and_month=#{yearAndMonth} and g.type ='材料') as cailiaofy,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_goods g WHERE xm_id=xm.id and g.year_and_month=#{yearAndMonth} and g.type ='燃料') as ranliaofy,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_goods g WHERE xm_id=xm.id and g.year_and_month=#{yearAndMonth} and g.type ='动力') as donglify,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_lease xl WHERE xm_id=xm.id and xl.year_and_month=#{yearAndMonth} and xl.type ='仪器设备') as yiqishebeify,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_lease xl WHERE xm_id=xm.id and xl.year_and_month=#{yearAndMonth} and xl.type ='房屋建筑物') as fawuzulinfy,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_direct_cost xdc WHERE xm_id=xm.id and xdc.year_and_month=#{yearAndMonth} and xdc.type ='1') as fy6,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_direct_cost xdc WHERE xm_id=xm.id and xdc.year_and_month=#{yearAndMonth} and xdc.type ='2') as fy7,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_direct_cost xdc WHERE xm_id=xm.id and xdc.year_and_month=#{yearAndMonth} and xdc.type ='3') as fy8
        FROM kd_xm xm
        where #{yearAndMonth} between DATE_FORMAT(XMKSSJ, '%Y%m') and DATE_FORMAT(COALESCE(get_xm_info(xm.id, 'XMJSSJ', #{yearAndMonth}),XMJSSJ),'%Y%m') and xm.`status` != 0
        <if test="xmmc!=null">and xm.XMMC like concat('%',#{xmmc},'%')</if>
    </select>

    <select id="tourufeiyonghuizongbiaoByYear" resultType="org.sky.scientific.pojo.vo.XmDirectCostSummary" parameterType="org.sky.scientific.pojo.entity.XmDirectCostEntity">
        SELECT
        id,
        XMBH,
        COALESCE(get_xm_info(xm.id, 'XMMC', #{yearAndMonth}),XMMC) AS XMMC,
        xm.XMKSSJ,
        COALESCE(get_xm_info(xm.id, 'XMJSSJ', #{yearAndMonth}),XMJSSJ) AS XMJSSJ,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_goods g WHERE xm_id=xm.id and left(g.year_and_month,4)=#{yearAndMonth} and g.type ='材料') as cailiaofy,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_goods g WHERE xm_id=xm.id and left(g.year_and_month,4)=#{yearAndMonth} and g.type ='燃料') as ranliaofy,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_goods g WHERE xm_id=xm.id and left(g.year_and_month,4)=#{yearAndMonth} and g.type ='动力') as donglify,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_lease xl WHERE xm_id=xm.id and left(xl.year_and_month,4)=#{yearAndMonth} and xl.type ='仪器设备') as yiqishebeify,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_lease xl WHERE xm_id=xm.id and left(xl.year_and_month,4)=#{yearAndMonth} and xl.type ='房屋建筑物') as fawuzulinfy,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_direct_cost xdc WHERE xm_id=xm.id and left(xdc.year_and_month,4)=#{yearAndMonth} and xdc.type ='1') as fy6,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_direct_cost xdc WHERE xm_id=xm.id and left(xdc.year_and_month,4)=#{yearAndMonth} and xdc.type ='2') as fy7,
        (SELECT IFNULL(sum(amount),0) FROM kd_xm_direct_cost xdc WHERE xm_id=xm.id and left(xdc.year_and_month,4)=#{yearAndMonth} and xdc.type ='3') as fy8
        FROM kd_xm xm
        where #{yearAndMonth} between DATE_FORMAT(XMKSSJ, '%Y') and DATE_FORMAT(COALESCE(get_xm_info(xm.id, 'XMJSSJ', #{yearAndMonth}),XMJSSJ),'%Y') and xm.`status` != 0
        <if test="xmmc!=null">and XMMC like concat('%',#{xmmc},'%')</if>
    </select>

</mapper>
