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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.AssetsProduction">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="business_id" property="businessId" />
        <result column="production_no" property="productionNo" />
        <result column="production_type" property="productionType" />
        <result column="signatory" property="signatory" />
        <result column="client_name" property="clientName" />
        <result column="client_tel" property="clientTel" />
        <result column="valuation_basis_date" property="valuationBasisDate" />
        <result column="evaluate_acreage" property="evaluateAcreage" />
        <result column="evaluate_amount" property="evaluateAmount" />
        <result column="evaluate_price" property="evaluatePrice" />
        <result column="owner" property="owner" />
        <result column="print_count" property="printCount" />
        <result column="create_production_date" property="createProductionDate" />
        <result column="comment" property="comment" />
        <result column="repertory_state" property="repertoryState" />
        <result column="repertory_in_time" property="repertoryInTime" />
        <result column="repertory_out_time" property="repertoryOutTime" />
        <result column="file_path" property="filePath" />
        <result column="save_file_date" property="saveFileDate" />
        <result column="delivery" property="delivery" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        business_id, production_no, production_type, signatory, client_name, client_tel, valuation_basis_date, evaluate_acreage, evaluate_amount, evaluate_price, owner, print_count, create_production_date, comment, repertory_state, repertory_in_time, repertory_out_time, file_path, save_file_date, delivery
    </sql>

    <!--根据业务id获取产品信息类型集合-->
    <select id="getProductionTypes" resultType="java.lang.String">
        SELECT production_type
        FROM assets_production
        WHERE business_id = #{businessId}
          AND deleted = 0
    </select>

    <!--根据业务id获取已选择产品-->
    <select id="getProductionByBusinessId" resultType="com.dayou.entity.AssetsProduction">
        SELECT id, production_type, print_count
        FROM assets_production
        WHERE business_id = #{businessId}
        AND deleted = 0
        AND create_production_date IS NULL
        LIMIT 1
    </select>

    <!--获取产品号集合(用于判断下一个子号)-->
    <select id="getProductionNo" resultType="java.lang.String">
        SELECT production_no
        FROM assets_evaluation_target
        WHERE assets_id = #{id}
          AND deleted = 0
          AND production_no IS NOT NULL
          AND production_no != ''
    </select>

    <!--更新产品的产品号信息-->
    <update id="updateProductionNo">
        UPDATE assets_production
        SET production_no = #{productionNo}
        WHERE deleted = 0
          AND business_id = #{businessId}
          AND production_type = #{productionType}
          AND (production_no IS NULL OR production_no = '')
          AND id = #{productionId}
    </update>

    <!--取子号情况下,再复制一份产品信息并添加-->
    <insert id="copyProductionInfo" useGeneratedKeys = "true" keyProperty = "id">
        INSERT INTO assets_production(id, business_id, production_type, print_count)
        VALUE (NULL, #{businessId}, #{productionType}, #{printCount});
    </insert>

    <!--撰写产品信息-->
    <update id="writeAssetsProduction">
        UPDATE assets_production
        SET signatory = #{signatory},
            client_name = #{clientName},
            client_tel = #{clientTel},
            valuation_basis_date = #{valuationBasisDate},
            evaluate_acreage = #{evaluateAcreage},
            evaluate_price = #{evaluatePrice},
            evaluate_amount = #{evaluateAmount},
            owner = #{owner},
            print_count = #{printCount},
            create_production_date = #{createProductionDate},
            comment = #{comment},
            file_path = #{filePath},
            assets_name = #{assetsName},
            qr_code = #{qrCode}
        WHERE production_no = #{productionNo}
          AND business_id = #{businessId}
    </update>

    <!--根据业务id与产品号查询产品信息-->
    <select id="getProductionList" resultType="com.dayou.entity.AssetsProduction">
        SELECT id,
        business_id,
        production_no,
        production_type,
        signatory,
        client_name,
        client_tel,
        valuation_basis_date,
        evaluate_acreage,
        evaluate_amount,
        evaluate_price,
        owner,
        print_count,
        create_production_date,
        comment,
        repertory_state,
        repertory_in_time,
        repertory_out_time,
        file_path,
        save_file_date,
        delivery,
        qr_code
        FROM assets_production
        WHERE business_id = #{businessId}
        <if test="statementNo != null and statementNo != ''">
            AND production_no = #{statementNo}
        </if>
        <if test="(statementNo != null and statementNo != '') and (reportNos != null and reportNos.size() > 0) ">
            OR
        </if>
        <if test="(statementNo == null or statementNo == '') and (reportNos != null and reportNos.size() > 0) ">
#             OR
            AND
        </if>
        <if test="reportNos != null and reportNos.size() > 0">
            production_no IN
            <foreach collection="reportNos" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        AND create_production_date IS NOT NULL
        AND deleted = 0
    </select>

    <!--根据业务id与产品号查询该产品是否已有提交的流程实例信息-->
    <select id="getInatanceCount" resultType="java.lang.Integer">
        SELECT COUNT(id)
        FROM work_flow_node_instance
        WHERE deleted = 0
          AND business_id = #{businessId}
          AND (business_sub_id = #{productionNo} OR business_min_id = #{productionNo})
    </select>

    <!--根据业务id与产品号集合获取每个产品是否有提交过的流程实例信息-->
    <select id="getInatanceCountByIdAndProNo" resultType="java.lang.Integer">
        SELECT COUNT(id)
        FROM work_flow_node_instance
        WHERE deleted = 0
          AND business_id = #{businessId}
          AND (business_sub_id = #{productionNo} OR business_min_id = #{productionNo})
    </select>

    <!--根据id获取产品出入库状态-->
    <select id="getRepertoryState" resultType="java.lang.Boolean">
        SELECT repertory_state
        FROM assets_production
        WHERE deleted = 0
          AND id = #{id}
    </select>

    <!--根据资产产品id更新文件路由信息-->
    <update id="updateProductionFileInfo">
        UPDATE assets_production
        SET file_path = #{filePath},
            comment = #{comment}
        WHERE id = #{id}
    </update>

    <!--获取产品实收款-->
    <select id="getProductionRealAmount" resultType="java.math.BigDecimal">
        SELECT real_amount
        FROM production_fund
        WHERE production_no = #{productionNo}
        AND deleted = 0
    </select>

    <!--产品标记送达-->
    <update id="markDelivery">
        UPDATE assets_production
        SET delivery = 1
        WHERE production_no = #{productionNo}
          AND deleted = 0
    </update>

    <!--查找实收款认领产品列表-->
    <select id="getAssetsProductionFundVoList" resultType="com.dayou.vo.AssetsProductionFundVo">
        SELECT (@i :=  @i + 1) AS id,
        assets.id AS assetsId,
        assets.name AS name,
        assets.order_id AS orderId,
        production.id AS productionId,
        production.production_no AS productionNo,
        production.production_type AS productionType,
        customer.name AS clienteleName,
        oFund.id AS orderFundId,
        oFund.should_amount AS orderShouldAmount,
        oFund.real_amount AS orderRealAmount,
        pFund.id AS productionFundId,
        pFund.production_should_amount AS productionShouldAmount,
        pFund.real_amount AS productionRealAmount,
        pFund.standard_amount AS standardAmount,
        eDeclare.declare_result AS eDeclareResult,
        mDeclare.declare_result AS mDeclareResult,
        (SELECT user.name FROM user WHERE user.id = assets.client_manager_id) AS marketUser,
        (SELECT user.name FROM user WHERE user.id = assets.principal_id) AS evaluateUser
        FROM (SELECT @i := 0) AS sort,assets
        LEFT JOIN assets_production AS production ON production.business_id = assets.id
        LEFT JOIN order_fund AS oFund ON oFund.business_id = assets.id AND oFund.order_id = assets.order_id
        LEFT JOIN production_fund AS pFund ON pFund.business_id = production.business_id AND pFund.production_no = production.production_no
        LEFT JOIN customer_company AS customer ON customer.id = assets.clientele_id
        LEFT JOIN (
        SELECT business_id,production_id,declare_result
        FROM commission_declare AS `declare`
        WHERE `declare`.business_type = 'COMMISSION_DECLARE_ASSET_EVALUATE'
        AND deleted = 0
        ) AS eDeclare ON eDeclare.business_id = assets.id AND eDeclare.production_id = production.id
        LEFT JOIN (
        SELECT business_id,production_id,declare_result
        FROM commission_declare AS `declare`
        WHERE `declare`.business_type = 'COMMISSION_DECLARE_ASSET_MARKET'
        AND deleted = 0
        ) AS mDeclare ON mDeclare.business_id = assets.id AND mDeclare.production_id = production.id
        WHERE production.production_type != 'STATEMENT'
        AND assets.client_manager_id = #{managerId}
        AND production.create_production_date IS NOT NULL
        <if test="apFundSelectDTO.oderName != null and apFundSelectDTO.oderName != ''">
            AND assets.name LIKE CONCAT('%',#{apFundSelectDTO.oderName},'%')
        </if>
        <if test="apFundSelectDTO.orderId != null and apFundSelectDTO.orderId != ''">
            AND assets.order_id LIKE CONCAT('%',#{apFundSelectDTO.orderId},'%')
        </if>
        <if test="apFundSelectDTO.reportNo != null and apFundSelectDTO.reportNo != ''">
            AND production.production_no LIKE CONCAT('%',#{apFundSelectDTO.reportNo},'%')
        </if>
        <if test="apFundSelectDTO.eDeclareResult != null and apFundSelectDTO.eDeclareResult != ''">
            AND eDeclare.declare_result = #{apFundSelectDTO.eDeclareResult}
        </if>
        <if test="apFundSelectDTO.mDeclareResult != null and apFundSelectDTO.mDeclareResult != ''">
            AND mDeclare.declare_result = #{apFundSelectDTO.mDeclareResult}
        </if>
        ORDER BY assets.created DESC
    </select>

    <!--查找实收款认领订单列表-->
    <select id="getAssetsOrderFundVoList" resultType="com.dayou.vo.AssetsProductionFundVo">
        SELECT (@i :=  @i + 1) AS id,
        assets.id AS assetsId,
        assets.name AS name,
        assets.order_id AS orderId,
        customer.name AS clienteleName,
        oFund.id AS orderFundId,
        oFund.should_amount AS orderShouldAmount,
        oFund.real_amount AS orderRealAmount,
        eDeclare.declare_result AS eDeclareResult,
        mDeclare.declare_result AS mDeclareResult,
        (SELECT user.name FROM user WHERE user.id = assets.client_manager_id) AS marketUser,
        (SELECT user.name FROM user WHERE user.id = assets.principal_id) AS evaluateUser
        FROM (SELECT @i := 0) AS sort,assets
        LEFT JOIN order_fund AS oFund ON oFund.business_id = assets.id AND oFund.order_id = assets.order_id
        LEFT JOIN customer_company AS customer ON customer.id = assets.clientele_id
        LEFT JOIN (
        SELECT business_id,declare_result
        FROM commission_declare AS `declare`
        WHERE `declare`.business_type = 'COMMISSION_DECLARE_ASSET_EVALUATE'
        AND deleted = 0
        ) AS eDeclare ON eDeclare.business_id = assets.id
        LEFT JOIN (
        SELECT business_id,declare_result
        FROM commission_declare AS `declare`
        WHERE `declare`.business_type = 'COMMISSION_DECLARE_ASSET_MARKET'
        AND deleted = 0
        ) AS mDeclare ON mDeclare.business_id = assets.id
        <where>
            AND assets.client_manager_id = #{managerId}
            AND (SELECT COUNT(1) FROM assets_production WHERE assets_production.business_id = assets.id AND assets_production.deleted = 0) = 0
            <if test="apFundSelectDTO.oderName != null and apFundSelectDTO.oderName != ''">
                AND assets.name LIKE CONCAT('%',#{apFundSelectDTO.oderName},'%')
            </if>
            <if test="apFundSelectDTO.orderId != null and apFundSelectDTO.orderId != ''">
                AND assets.order_id LIKE CONCAT('%',#{apFundSelectDTO.orderId},'%')
            </if>
            <if test="apFundSelectDTO.eDeclareResult != null and apFundSelectDTO.eDeclareResult != ''">
                AND eDeclare.declare_result = #{apFundSelectDTO.eDeclareResult}
            </if>
            <if test="apFundSelectDTO.mDeclareResult != null and apFundSelectDTO.mDeclareResult != ''">
                AND mDeclare.declare_result = #{apFundSelectDTO.mDeclareResult}
            </if>
        </where>
        ORDER BY assets.created DESC
    </select>

    <!--根据订单收款id获取产品收款信息-->
    <select id="getProductionFundByOrderFundId" resultType="com.dayou.dto.ReportDTO">
        SELECT
            ap.id,
            ( CASE ap.production_type WHEN 'STATEMENT' THEN '价值意见书' WHEN 'REPORT' THEN '评估报告' WHEN 'CONSULT' THEN '咨询报告' ELSE '意见函' END ) AS productionType,
            ap.evaluate_amount,
            sum( pf.real_amount ) AS claimedAmount,
            pf.standard_amount,
            ap.production_no AS businessSubId,
            pf.id AS productionFundId
        FROM ( SELECT id, business_id FROM order_fund WHERE business_type = 'ASSET_BUSINESS' AND deleted = 0 AND id = #{orderFundId} ) AS orf
            INNER JOIN ( SELECT id, business_id, production_no, production_type, evaluate_amount FROM assets_production WHERE production_type!='STATEMENT' AND deleted = 0 ) AS ap ON orf.business_id = ap.business_id
            LEFT JOIN ( SELECT id, business_id, production_no, order_fund_id, real_amount, standard_amount, production_type FROM production_fund WHERE deleted = 0 ) AS pf ON pf.business_id = ap.business_id
            AND pf.production_no = ap.production_no
            AND pf.production_type = ap.production_type
        GROUP BY
            ap.id,
            pf.standard_amount,
            pf.id
    </select>

    <!--产品归档-->
    <update id="prodSaveFile">
        UPDATE assets_production
        SET if_save_file = 1,
            save_file_date = #{saveFileDate}
        WHERE production_no = #{productionNo}
        AND deleted = 0
    </update>

</mapper>
