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

    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmWwxmCost">
        <id column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="year_and_month" property="yearAndMonth"/>
        <result column="ww_id" property="wwId"/>
        <result column="pay_date" property="payDate"/>
        <result column="pay_amount" property="payAmount"/>
        <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.XmWwxmCostVO" extends="ResultMap">
        <result column="xm_id" property="xmId"/>
        <result column="xmmc" property="xmmc"/>
        <result column="xmbh" property="xmbh"/>
        <result column="STFQC" property="stfqc"/>
        <result column="STFLX" property="stflx"/>
        <result column="HTMC" property="htmc"/>
        <result column="SFWCJSHTDJ" property="sfwcjshtdj"/>
        <result column="JSHTDJLX" property="jshtdjlx"/>
        <result column="JSHTDJJE" property="jshtdjje"/>
    </resultMap>

    <select id="selectPageByYearAndMonth" resultMap="ResultVoMap">
        SELECT
            xa.*,
            m.xm_id, m.STFQC, m.STFLX,m.HTMC,m.SFWCJSHTDJ,m.JSHTDJLX,m.JSHTDJJE,
            xm.xmmc, xm.xmbh
        FROM kd_xm_wwxm_cost xa
        LEFT JOIN kd_xm_wwxm m on m.id = xa.ww_id
        LEFT JOIN
        (
            SELECT
                xm.id,
                xm.XMBH,
                COALESCE(get_xm_info(xm.id, 'XMMC', #{ew.yearAndMonth}),XMMC) AS XMMC
            FROM kd_xm xm
            WHERE xm.is_deleted = 0
        ) xm on xm.id = m.xm_id
        where
            xa.year_and_month = #{ew.yearAndMonth}
        <if test="ew.xmId != null">and m.xm_id = #{ew.xmId}</if>
        <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.xmmc like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.sfwcjshtdj!=null and ew.sfwcjshtdj!=''">and m.SFWCJSHTDJ like concat('%',#{ew.sfwcjshtdj},'%')</if>
        <if test="ew.jshtdjlx!=null and ew.jshtdjlx!=''">and m.JSHTDJLX like concat('%',#{ew.jshtdjlx},'%')</if>
        <if test="ew.stflx!=null and ew.stflx!=''">and m.STFLX like concat('%',#{ew.stflx},'%')</if>
    </select>

    <select id="selectPageByYear" resultMap="ResultVoMap">
        SELECT xa.*,xm.xmmc, xm.xmbh, m.STFQC, m.STFLX,m.HTMC,m.SFWCJSHTDJ,m.JSHTDJLX,m.JSHTDJJE
        FROM kd_xm_wwxm_cost xa
        LEFT JOIN kd_xm_wwxm m on m.id = xa.ww_id
        LEFT JOIN
        (
            SELECT
                xm.id,
                xm.XMBH,
                COALESCE(get_xm_info(xm.id,'XMMC', #{ew.yearAndMonth}),XMMC) AS XMMC
            FROM kd_xm xm
            WHERE xm.is_deleted = 0
        ) xm on xm.id = m.xm_id
        where
            left(xa.year_and_month,4) = #{ew.yearAndMonth}
        <if test="ew.xmId != null">and m.xm_id = #{ew.xmId}</if>
        <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.xmmc like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.sfwcjshtdj!=null and ew.SFWCJSHTDJ!=''">and m.SFWCJSHTDJ like concat('%',#{ew.sfwcjshtdj},'%')</if>
        <if test="ew.jshtdjlx!=null and ew.JSHTDJLX!=''">and m.JSHTDJLX like concat('%',#{ew.jshtdjlx},'%')</if>
        <if test="ew.stflx!=null and ew.stflx!=''">and m.STFLX like concat('%',#{ew.stflx},'%')</if>
    </select>

    <select id="getAmountByXmAndYear">
        SELECT IFNULL(sum(pay_amount),0)
        FROM kd_xm_wwxm_cost c
                 LEFT JOIN kd_xm_wwxm w on w.id = c.ww_id
        WHERE LEFT(c.year_and_month,4) = #{year} and w.XM_ID = #{xmId} and w.STFLX in ('境内研究机构','境内高等学校','境内企业','境内个人','境外机构')
    </select>

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