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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.AssetsEvaluationTarget">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="evaluation_purpose_id" property="evaluationPurposeId" />
        <result column="evaluation_type_id" property="evaluationTypeId" />
        <result column="evaluation_type_sec_id" property="evaluationTypeSecId" />
        <result column="the_holder" property="theHolder" />
        <result column="assets_value_id" property="assetsValueId" />
        <result column="principal_id" property="principalId" />
        <result column="site_ids" property="siteIds" />
        <result column="valuation_basis_date" property="valuationBasisDate" />
        <result column="evaluation_method_id" property="evaluationMethodId" />
        <result column="choice_evaluation_method_id" property="choiceEvaluationMethodId" />
        <result column="total_asset_carrying_amount" property="totalAssetCarryingAmount" />
        <result column="total_liabilities_carrying_amount" property="totalLiabilitiesCarryingAmount" />
        <result column="carrying_amount_of_net_assets" property="carryingAmountOfNetAssets" />
        <result column="estimated_value" property="estimatedValue" />
        <result column="remark" property="remark" />
        <result column="file_path" property="filePath" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        evaluation_purpose_id, evaluation_type_id, evaluation_type_sec_id, the_holder, assets_value_id, principal_id, site_ids, valuation_basis_date, evaluation_method_id, choice_evaluation_method_id, total_asset_carrying_amount, total_liabilities_carrying_amount, carrying_amount_of_net_assets, estimated_value, remark, file_path
    </sql>

    <!--根据资产业务订单id获取评估对象信息-->
    <select id="getAETargetListByAssetsId" resultType="com.dayou.vo.AssetsEvaluationTargetVO">
        SELECT target.id,
        the_holder,
        total_asset_carrying_amount,
        total_liabilities_carrying_amount,
        carrying_amount_of_net_assets,
        estimated_value,
        check_value,
        purpose_name,
        type_name,
        target.valuation_basis_date,
        (SELECT assets_evaluation_target_type.type_name FROM assets_evaluation_target_type WHERE
        assets_evaluation_target_type.id = target.evaluation_type_id) AS targetTypename,
        (SELECT assets_evaluation_target_type.type_name FROM assets_evaluation_target_type WHERE
        assets_evaluation_target_type.id = target.evaluation_type_sec_id) AS secTargetTypaName,
        value_type.type_name AS valueTypeName,
        target.statement_no,
        target.report_no,
        (SELECT create_production_date FROM assets_production WHERE production_no = target.statement_no) AS statementCreateProductionDate,
        (SELECT create_production_date FROM assets_production WHERE production_no = target.report_no) AS reportCreateProductionDate
        FROM assets_evaluation_target AS target
        LEFT JOIN assets_evaluation_target_purpose AS purpose ON purpose.id = target.evaluation_purpose_id
        LEFT JOIN assets_value_type AS value_type ON value_type.id = target.assets_value_id
        WHERE target.assets_id = #{id}
        <if test="(statementNo != null and statementNo != '') and (reportNo != null and reportNo != '') ">
            AND statement_no = #{statementNo} AND report_no = #{reportNo}
        </if>
        <if test="statementNo != null and statementNo != '' ">
            AND statement_no = #{statementNo}
        </if>
        <if test="reportNo != null and reportNo != '' ">
            AND report_no = #{reportNo}
        </if>
        AND target.deleted = 0
    </select>

    <!--评估对象添加意见书产品号-->
    <update id="updateTargetStatementNo">
        UPDATE assets_evaluation_target
        SET statement_no = #{productionNo}
        WHERE id = #{id}
          AND deleted = 0
    </update>

    <!--评估对象添加报告产品号-->
    <update id="updateTargetReportNo">
        UPDATE assets_evaluation_target
        SET report_no = #{productionNo}
        WHERE id = #{id}
          AND deleted = 0
    </update>

    <!--根据业务id获取已取号评估对象的产品号-->
    <select id="getStatementNoList" resultType="java.lang.String">
        SELECT statement_no
        FROM assets_evaluation_target
        WHERE assets_id = #{businessId}
          AND deleted = 0
          AND statement_no IS NOT NULL
          AND statement_no != ''
    </select>

    <!--根据业务id获取已取号评估对象的产品号-->
    <select id="getReportNoList" resultType="java.lang.String">
        SELECT report_no
        FROM assets_evaluation_target
        WHERE assets_id = #{businessId}
          AND deleted = 0
          AND report_no IS NOT NULL
          AND report_no != ''
        AND assets_evaluation_target.id IN
        <foreach collection="targetIdList" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <!--评估对象审核定价-->
    <update id="checkValue">
        UPDATE assets_evaluation_target
        SET check_value = #{checkValue},
            evaluation_method_id = #{evaluationMethodId},
            choice_evaluation_method_id = #{choiceEvaluationMethodId}
        WHERE id = #{id}
          AND assets_id = #{assetsId}
          AND deleted = 0
    </update>

</mapper>
