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

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmDesignCostEntity">
        <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="contract_name" property="contractName"/>
        <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>

    <resultMap id="ResultVoMap" type="org.sky.scientific.pojo.vo.XmDesignCostVO" extends="ResultMap">

    </resultMap>

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

    <select id="selectXmDesignCostPageByYearAndMonth" resultMap="ResultVoMap" parameterType="org.sky.scientific.pojo.dto.XmDesignCostQuery">
        select
            dc.*, xm.XMMC, xm.XMBH
        from kd_xm_design_cost dc
        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 = dc.xm_id
        WHERE dc.tenant_id = #{ew.tenantId} and date_format(dc.record_date, '%Y%m') = #{ew.yearAndMonth}
        <if test="ew.xmId != null">and dc.xm_id = #{ew.xmId}</if>
        <if test="ew.xmmc != null and ew.xmmc!=''">
           and xm.XMMC like concat('%',#{ew.xmmc},'%')
        </if>
        <if test="ew.contractName!=null and ew.contractName!=''">
           and dc.contract_name like concat('%',#{ew.contractName},'%')
        </if>
        <if test="ew.costType!=null and ew.costType!=''">
           and dc.cost_type = #{ew.costType}
        </if>
        <if test="ew.amountMin!=null and ew.amountMax!=null">
           and dc.amount between #{ew.amountMin} and #{ew.amountMax}
        </if>
        <if test="ew.recordDateMin!=null and ew.recordDateMax!=null">
           and date_format(dc.record_date,'%Y-%m-%d') between #{ew.recordDateMin} and #{ew.recordDateMax}
        </if>
    </select>

    <select id="selectXmDesignCostPageByYear" resultMap="ResultVoMap" parameterType="org.sky.scientific.pojo.dto.XmDesignCostQuery">
        select
        dc.*, xm.XMMC, xm.XMBH
        from kd_xm_design_cost dc
        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 = dc.xm_id
        WHERE
        dc.tenant_id = #{ew.tenantId}
        and date_format(dc.record_date, '%Y%m')  between concat(#{ew.yearAndMonth},"01") and concat(#{ew.yearAndMonth}, "12")
        <if test="ew.xmId != null">and dc.xm_id = #{ew.xmId}</if>
        <if test="ew.xmmc != null and ew.xmmc!=''">
            and xm.XMMC like concat('%',#{ew.xmmc},'%')
        </if>
        <if test="ew.contractName!=null and ew.contractName!=''">
            and dc.contract_name like concat('%',#{ew.contractName},'%')
        </if>
        <if test="ew.costType!=null and ew.costType!=''">
            and dc.cost_type = #{ew.costType}
        </if>
        <if test="ew.amountMin!=null and ew.amountMax!=null">
            and dc.amount between #{ew.amountMin} and #{ew.amountMax}
        </if>
        <if test="ew.recordDateMin!=null and ew.recordDateMax!=null">
            and date_format(dc.record_date,'%Y-%m-%d') between #{ew.recordDateMin} and #{ew.recordDateMax}
        </if>
    </select>

</mapper>
