<?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 = '')
    </update>

    <!--取子号情况下,再复制一份产品信息并添加-->
    <insert id="copyProductionInfo">
        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}
        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 != '') or (reportNo != null and reportNo != '') ">
            AND production_no = #{statementNo} OR production_no = #{reportNo}
        </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>


</mapper>
