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

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmCostDetailsEntity">
        <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="XMBH" property="xmbh"/>
        <result column="XMMC" property="xmmc"/>
        <result column="record_date" property="recordDate"/>
        <result column="voucher_number" property="voucherNumber"/>
        <result column="e_subject_id" property="eSubjectId"/>
        <result column="e_subject_code" property="eSubjectCode"/>
        <result column="e_subject_name" property="eSubjectName"/>
        <result column="digest" property="digest"/>
        <result column="amount" property="amount"/>
        <result column="cashbook" property="cashbook"/>
        <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_cost_details
        WHERE id IN
        <foreach item="item" collection="list" separator="," open="(" close=")" index="">
            #{item}
        </foreach>
    </delete>


    <select id="selectPageByYearAndMonth" resultMap="ResultMap"
            parameterType="org.sky.scientific.pojo.dto.XmCostDetailsDTO">
        SELECT d.*, xm.XMBH, xm.XMMC
        FROM kd_xm_cost_details d
        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 = d.xm_id
        WHERE
        d.year_and_month = #{ew.yearAndMonth}
        <if test="ew.xmmc != null and ew.xmmc!=''">and xm.XMMC like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.amountMin!=null and ew.amountMax!=null">and d.amount between #{ew.amountMin} and #{ew.amountMax}
        </if>
    </select>

    <select id="selectPageByYear" resultMap="ResultMap" parameterType="org.sky.scientific.pojo.dto.XmCostDetailsDTO">
        SELECT d.*, xm.XMBH, xm.XMMC
        FROM kd_xm_cost_details d
        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 = d.xm_id
        WHERE
        LEFT(d.year_and_month,4) = #{ew.yearAndMonth}
        <if test="ew.xmmc != null and ew.xmmc!=''">and xm.XMMC like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.amountMin!=null and ew.amountMax!=null">and d.amount between #{ew.amountMin} and #{ew.amountMax}
        </if>
    </select>


    <resultMap id="FinanceCostDetailVO" type="org.sky.scientific.pojo.vo.FinanceCostDetailVO">
        <result column="xm_id" property="xmId"/>
        <result column="XMBH" property="xmbh"/>
        <result column="XMMC" property="xmmc"/>

        <collection property="costList" ofType="org.sky.scientific.pojo.vo.FinanceCostItemVO">
            <result column="record_date" property="recordDate"/>
            <result column="voucher_number" property="voucherNumber"/>
            <result column="digest" property="digest"/>
            <result column="e_subject_id" property="eSubjectId"/>
            <result column="ad_subject_enable" property="adSubjectEnable"/>
            <result column="L4Code" property="l4Code"/>
            <result column="L3Code" property="l3Code"/>
            <result column="L2Code" property="l2Code"/>
            <result column="amount" property="amount"/>
            <result column="cashbook" property="cashbook"/>
        </collection>
    </resultMap>
    <select id="financeCostDetail" resultMap="FinanceCostDetailVO">
        SELECT
        xm.id as xm_id, xm.XMBH,xm.XMMC,kxcd.record_date,kxcd.voucher_number,kxcd.digest,kxcd.amount,kxcd.cashbook,
        kxcd.e_subject_id,s.L3Code
        FROM kd_xm_cost_details kxcd
        LEFT JOIN
        (
        SELECT se.id, sub3.code as L3Code
        FROM kd_subject_enterprise se
        LEFT JOIN kd_subject sub4 on sub4.id = se.subject_id
        LEFT JOIN kd_subject sub3 on sub3.id = sub4.parent_id
        )s ON s.id = kxcd.e_subject_id
        LEFT JOIN
        (
        SELECT
        xm.id, xm.XMBH,
        COALESCE(get_xm_info(xm.id,'XMMC',#{yearAndMonth}),XMMC) AS XMMC
        FROM kd_xm xm
        WHERE xm.is_deleted = 0
        ) xm on xm.id = kxcd.xm_id
        WHERE kxcd.tenant_id = #{tenantId} and kxcd.year_and_month between concat(left(#{yearAndMonth},4),'01') and #{yearAndMonth}
            and xm.id in (select distinct(xm_id) from kd_xm_finance where type = 1 and tenant_id = #{tenantId})
            <if test="xmmc!=null and xmmc!=''">and xm.XMMC like concat('%',#{xmmc},'%')</if>
            <if test="xmId!=null">and xm.id = #{xmId}</if>
        order by kxcd.record_date asc
    </select>

    <resultMap id="HighTechCostDetailVO" type="org.sky.scientific.pojo.vo.HighTechCostDetailVO">
        <result column="xm_id" property="xmId"/>
        <result column="XMBH" property="xmbh"/>
        <result column="XMMC" property="xmmc"/>

        <collection property="costList" ofType="org.sky.scientific.pojo.vo.HighTechCostItemVO">
            <result column="record_date" property="recordDate"/>
            <result column="voucher_number" property="voucherNumber"/>
            <result column="digest" property="digest"/>
            <result column="e_subject_id" property="eSubjectId"/>
            <result column="rd_subject_enable" property="rdSubjectEnable"/>
            <result column="L4Code" property="l4Code"/>
            <result column="L3Code" property="l3Code"/>
            <result column="L2Code" property="l2Code"/>
            <result column="amount" property="amount"/>
            <result column="cashbook" property="cashbook"/>
        </collection>
    </resultMap>
    <select id="highTechCostDetail" resultMap="HighTechCostDetailVO" parameterType="org.sky.scientific.pojo.dto.XmCostDetailsDTO">
        SELECT
            xm.id as xm_id, xm.XMBH,xm.XMMC,kxcd.record_date,kxcd.voucher_number,kxcd.digest,kxcd.amount,kxcd.cashbook,
            kxcd.e_subject_id, s.L4Code ,s.rd_subject_enable, s.L3Code
        FROM kd_xm_cost_details kxcd
        LEFT JOIN
        (
            SELECT se.id, sub4.code as L4Code, sub4.rd_subject_enable,sub3.code as L3Code
            FROM kd_subject_enterprise se
            LEFT JOIN kd_subject sub4 on sub4.id = se.subject_id
            LEFT JOIN kd_subject sub3 on sub3.id = sub4.parent_id
        )s ON s.id = kxcd.e_subject_id
        LEFT JOIN
        (
            SELECT
                xm.id, xm.XMBH,
                COALESCE(get_xm_info(xm.id,'XMMC',#{yearAndMonth}),XMMC) AS XMMC
            FROM kd_xm xm
            WHERE xm.is_deleted = 0
        ) xm on xm.id = kxcd.xm_id
        WHERE
            kxcd.tenant_id = #{tenantId}
            and kxcd.year_and_month between concat(left(#{yearAndMonth},4),'01') and #{yearAndMonth}
            and xm.id in (select distinct(xm_id) from kd_xm_finance where type = 3 and tenant_id = #{tenantId})
            <if test="xmmc!=null and xmmc!=''">and xm.XMMC like concat('%',#{xmmc},'%')</if>
            <if test="xmId!=null">and xm.id = #{xmId}</if>
        order by kxcd.record_date asc
    </select>

    <resultMap id="DeductionCostDetailVO" type="org.sky.scientific.pojo.vo.DeductionCostDetailVO">
        <result column="xm_id" property="xmId"/>
        <result column="XMBH" property="xmbh"/>
        <result column="XMMC" property="xmmc"/>
        <result column="XMJSSJ" property="xmjssj"/>

        <collection property="costList" ofType="org.sky.scientific.pojo.vo.DeductionCostItemVO">
            <result column="record_date" property="recordDate"/>
            <result column="voucher_number" property="voucherNumber"/>
            <result column="digest" property="digest"/>
            <result column="e_subject_id" property="eSubjectId"/>
            <result column="ad_subject_enable" property="adSubjectEnable"/>
            <result column="L4Code" property="l4Code"/>
            <result column="L3Code" property="l3Code"/>
            <result column="L2Code" property="l2Code"/>
            <result column="amount" property="amount"/>
            <result column="cashbook" property="cashbook"/>
        </collection>
    </resultMap>
    <select id="deductionCostDetail" resultMap="DeductionCostDetailVO">
        SELECT
        xm.id as xm_id, xm.XMBH,xm.XMMC,xm.XMJSSJ,kxcd.record_date,kxcd.voucher_number,kxcd.digest,kxcd.amount,kxcd.cashbook,
        kxcd.e_subject_id, s.ad_subject_enable,s.L4Code, s.L3Code, s.L2Code
        FROM kd_xm_cost_details kxcd
        LEFT JOIN
        (
            SELECT
                se.id,sub4.code as L4Code, sub4.ad_subject_enable,sub3.code as L3Code, sub2.code as L2Code
            FROM kd_subject_enterprise se
            LEFT JOIN kd_subject sub4 on sub4.id = se.subject_id
            LEFT JOIN kd_subject sub3 on sub3.id = sub4.parent_id
            LEFT JOIN kd_subject sub2 on sub2.id = sub3.parent_id
        )s ON s.id = kxcd.e_subject_id
        LEFT JOIN
        (
            SELECT
                xm.id, xm.XMBH,
                COALESCE(get_xm_info(xm.id,'XMMC',#{yearAndMonth}),XMMC) AS XMMC,
                COALESCE(get_xm_info(xm.id,'XMJSSJ',#{yearAndMonth}),XMJSSJ) AS XMJSSJ
            FROM kd_xm xm
            WHERE xm.is_deleted = 0
        ) xm on xm.id = kxcd.xm_id
        WHERE
            kxcd.tenant_id = #{tenantId} and kxcd.year_and_month between concat(left(#{yearAndMonth},4),'01') and #{yearAndMonth}
            and xm.id in (select distinct(xm_id) from kd_xm_finance where type = 2 and tenant_id = #{tenantId})
            <if test="type==1">and s.L2Code = '530101'</if>
            <if test="type==2">and s.L2Code = '530102'</if>
            <if test="xmmc!=null and xmmc!=''">and xm.XMMC like concat('%',#{xmmc},'%')</if>
            <if test="xmId!=null">and xm.id = #{xmId}</if>
        order by kxcd.record_date asc
    </select>

    <select id="costSummary" resultType="com.alibaba.fastjson.JSONObject" parameterType="org.sky.scientific.pojo.dto.XmCostDetailsDTO">
        SELECT
        p.xm_id as xmId,xm.XMBH,xm.XMMC
        FROM
        (
            select distinct xm_id
            from kd_xm_cost_details
            where tenant_id = #{ew.tenantId} and year_and_month between concat(left(#{ew.yearAndMonth},4),'01') and #{ew.yearAndMonth}
            order by xm_id
        )as p
        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 = p.xm_id
        <where>
            <if test="ew.caliber==1 or ew.caliber==2 or ew.caliber==3">
                and xm.id in (select distinct(xm_id) from kd_xm_finance where type = #{ew.caliber} and tenant_id = #{ew.tenantId})
            </if>
            <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.XMMC like concat('%',#{ew.xmmc},'%')</if>
            <if test="ew.xmId!=null and ew.xmId>0">and xm.id = #{ew.xmId}</if>
        </where>

    </select>

    <resultMap id="CostItemVO" type="org.sky.scientific.pojo.vo.CostItemVO">
        <result column="record_date" property="recordDate"/>
        <result column="voucher_number" property="voucherNumber"/>
        <result column="amount" property="amount"/>
        <result column="cashbook" property="cashbook"/>
        <result column="digest" property="digest"/>
        <result column="e_subject_id" property="eSubjectId"/>
        <result column="rd_subject_enable" property="rdSubjectEnable"/>
        <result column="ad_subject_enable" property="adSubjectEnable"/>
        <result column="l4Code" property="l4Code"/>
        <result column="l3Code" property="l3Code"/>
        <result column="l2Code" property="l2Code"/>

    </resultMap>
    <select id="selectCostItemByCondition" resultMap="CostItemVO">
        select kxcd.record_date, kxcd.voucher_number, kxcd.digest, kxcd.amount,kxcd.cashbook,
               kxcd.e_subject_id, s.L4Code,s.ad_subject_enable,s.rd_subject_enable,s.L3Code,s.L2Code
        from kd_xm_cost_details kxcd
        LEFT JOIN
        (
            SELECT
                se.id, sub4.code as L4Code, sub4.ad_subject_enable, sub4.rd_subject_enable, sub3.code as L3Code, sub2.code as L2Code
            FROM kd_subject_enterprise se
            LEFT JOIN kd_subject sub4 on sub4.id = se.subject_id
            LEFT JOIN kd_subject sub3 on sub3.id = sub4.parent_id
            LEFT JOIN kd_subject sub2 on sub2.id = sub3.parent_id
        )s ON s.id = kxcd.e_subject_id
        where kxcd.xm_id = #{xmId} and year_and_month between concat(left(#{yearAndMonth}, 4), '01') and #{yearAndMonth}
    </select>

    <select id="statisticsCost" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            kxcd.xm_id as xmId,
            sum(amount) as amountTotal
        FROM kd_xm_cost_details kxcd
        WHERE
            kxcd.tenant_id = #{tenantId}
        <if test="yearAndMonth!=null and yearAndMonth!=''">
            and kxcd.year_and_month between concat(left(#{yearAndMonth},4),'01') and #{yearAndMonth}
        </if>
        GROUP BY xm_id
    </select>
    <select id="xmysygjqst" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            id,
            <if test="currentYear==1">COALESCE(get_xm_info(xm.id,'XMMC',#{year}),XMMC) AS XMMC,</if>
            <if test="currentYear==0">COALESCE(get_xm_info(xm.id,'XMMC',null),XMMC) AS XMMC,</if>
            (
                SELECT IFNULL(sum(amount), 0) FROM kd_xm_cost_details kxcd WHERE kxcd.xm_id = xm.id
                <if test="currentYear==1">and DATE_FORMAT(record_date,'%Y') = #{year}</if>
                and kxcd.tenant_id = #{tenantId}
            ) as cost,
            COALESCE(get_xm_info(xm.id,'XMYSZEMX', null),XMYSZEMX) AS XMYSZEMX
        FROM kd_xm xm
        WHERE xm.tenant_id = #{tenantId} and xm.status != 0
        and #{year} between DATE_FORMAT(XMKSSJ, '%Y') and DATE_FORMAT(COALESCE(get_xm_info(xm.id,'XMJSSJ',null),XMJSSJ), '%Y')
    </select>
</mapper>
