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

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmOtherCostEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="year_and_month" property="yearAndMonth"/>
        <result column="xm_id" property="xmId"/>
        <result column="xmmc" property="xmmc"/>
        <result column="xmbh" property="xmbh"/>
        <result column="cost_type" property="costType"/>
        <result column="record_date" property="recordDate"/>
        <result column="subject_id" property="subjectId"/>
        <result column="subject_name" property="subjectName"/>
        <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_other_cost
        WHERE ID in <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>


    <select id="selectXmOtherCostPageByYearAndMonth" resultMap="ResultMap" parameterType="org.sky.scientific.pojo.dto.XmOtherCostQuery">
        select xoc.* ,xm.XMBH, xm.XMMC
        from kd_xm_other_cost xoc
        LEFT JOIN
        (
            SELECT
                xm.id,xm.XMBH,
                COALESCE(get_xm_info(xm.id,'XMMC', #{ew.yearAndMonth}),XMMC) AS XMMC
            FROM kd_xm xm
         ) xm on xm.id = xoc.xm_id
        where
        xoc.tenant_id = #{ew.tenantId}
        and date_format(record_date, '%Y%m') = #{ew.yearAndMonth}
        <if test="ew.xmId != null">and xoc.xm_id = #{ew.xmId}</if>
        <if test="ew.xmmc != null and ew.xmmc!=''">and XMMC like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.subjectId!=null">and xoc.subject_id = #{ew.subjectId}</if>
        <if test="ew.recordDateMin!=null and ew.recordDateMax!=null">and date_format(xoc.record_date,'%Y-%m-%d') between #{ew.recordDateMin} and #{ew.recordDateMax}</if>
    </select>

    <select id="selectXmOtherCostPageByYear" resultMap="ResultMap" parameterType="org.sky.scientific.pojo.dto.XmOtherCostQuery">
        select xoc.* ,xm.XMBH, xm.XMMC
        from kd_xm_other_cost xoc
        LEFT JOIN
        (
            SELECT
                xm.id,xm.XMBH,
                COALESCE(get_xm_info(xm.id,'XMMC', #{ew.yearAndMonth}),XMMC) AS XMMC
            FROM kd_xm xm
        ) xm on xm.id = xoc.xm_id
        where
        xoc.tenant_id = #{ew.tenantId}
        and date_format(record_date, '%Y%m') between concat(#{ew.yearAndMonth},'01') and concat(#{ew.yearAndMonth},'12')
        <if test="ew.xmId != null">and xoc.xm_id = #{ew.xmId}</if>
        <if test="ew.xmmc != null and ew.xmmc!=''">and XMMC like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.subjectId!=null">and xoc.subject_id = #{ew.subjectId}</if>
        <if test="ew.recordDateMin!=null and ew.recordDateMax!=null">and date_format(xoc.record_date,'%Y-%m-%d') between #{ew.recordDateMin} and #{ew.recordDateMax}</if>
        order by xoc.create_time desc
    </select>
</mapper>
