<?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="com.dayou.mapper.ProductionFundMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.ProductionFund">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="business_type" property="businessType" />
        <result column="business_id" property="businessId" />
        <result column="business_sub_id" property="businessSubId" />
        <result column="should_amount" property="shouldAmount" />
        <result column="real_amount" property="realAmount" />
        <result column="standard_amount" property="standardAmount" />
        <result column="discount" property="discount" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        business_type, business_id, business_sub_id, should_amount, real_amount, standard_amount, discount
    </sql>

    <select id="deepQueryProductionFund" parameterType="java.util.List" resultType="com.dayou.entity.ProductionFund">
        select id,business_id,should_amount from production_fund where business_id in (
            <foreach collection="majorIds" item="item" index="index" separator=",">
                #{item}
            </foreach>
            ) and deleted = 0
    </select>

    <select id="getProductionFundByOrderFundId" parameterType="java.lang.Long" resultType="com.dayou.dto.ReportDTO">
        SELECT
            mp.id ,
            (case mp.production when 'STATEMENT' THEN '价值意见书' when 'REPORT' THEN '报告' else '复评函' end ) as productionType,
            mp.evaluate_amount,
            sum(pf.real_amount) as claimedAmount,
            pf.standard_amount,
            mp.report_no as businessSubId
        FROM
            ( SELECT id, business_id FROM order_fund WHERE business_type = 'MAJOR_BUSINESS' AND deleted = 0 AND id = #{orderFundId} ) orf
                INNER JOIN ( SELECT id, major_id, report_no, production, evaluate_amount FROM major_production WHERE  deleted = 0 and name is not null ) mp ON orf.business_id = mp.major_id
                LEFT JOIN ( SELECT id, business_id, production_no, order_fund_id, real_amount, standard_amount,production_type FROM production_fund WHERE deleted = 0 ) pf ON (
                        pf.business_id = mp.major_id
                    AND pf.production_no = mp.report_no and pf.production_type = mp.production)
        GROUP BY mp.id
        order by  mp.report_no
    </select>
</mapper>
