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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.Assets">
        <result column="id" property="id"/>
        <result column="deleted" property="deleted"/>
        <result column="created" property="created"/>
        <result column="modified" property="modified"/>
        <result column="name" property="name"/>
        <result column="order_id" property="orderId"/>
        <result column="financial" property="financial"/>
        <result column="allot_type" property="allotType"/>
        <result column="client_manager_id" property="clientManagerId"/>
        <result column="principal_id" property="principalId"/>
        <result column="department_id" property="departmentId"/>
        <result column="assets_business_gener" property="assetsBusinessGener"/>
        <result column="entrust_again" property="entrustAgain"/>
        <result column="clientele_type" property="clienteleType"/>
        <result column="clientele_id" property="clienteleId"/>
        <result column="clientele_sub_id" property="clienteleSubId"/>
        <result column="clientele_contact_id" property="clienteleContactId"/>
        <result column="security" property="security"/>
        <result column="significant_assets_reorganization" property="significantAssetsReorganization"/>
        <result column="state_assets" property="stateAssets"/>
        <result column="foreign_assets_involved" property="foreignAssetsInvolved"/>
        <result column="dispense_benefit" property="dispenseBenefit"/>
        <result column="bailor" property="bailor"/>
        <result column="bailor_address" property="bailorAddress"/>
        <result column="bailor_contact_name" property="bailorContactName"/>
        <result column="bailor_contact_tel" property="bailorContactTel"/>
        <result column="terminal_clientele_id" property="terminalClienteleId"/>
        <result column="terminal_clientele_type" property="terminalClienteleType"/>
        <result column="terminal_clientele_contact_id" property="terminalClienteleContactId"/>
        <result column="members" property="members"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        name, order_id, financial, allot_type, client_manager_id, principal_id, department_id, assets_business_gener, entrust_again, clientele_type, clientele_id, clientele_sub_id, clientele_contact_id, security, significant_assets_reorganization, state_assets, foreign_assets_involved, dispense_benefit, bailor, bailor_address, bailor_contact_name, bailor_contact_tel, terminal_clientele_id, terminal_clientele_type, terminal_clientele_contact_id, members, remark
    </sql>

    <!--条件查询资产任务列表-->
    <select id="selectPage" resultType="com.dayou.vo.AssetsVO">
        SELECT DISTINCT
#             (@i :=  @i + 1) AS id,
        nodeInfo.recordId AS id,
        assets.id AS assetsId,
        assets.name,
        order_id,
        assets_business_gener,
        allot_type,
        (SELECT production_no FROM assets_production WHERE assets_production.production_no = nodeInfo.business_sub_id AND business_id = nodeInfo.businessId) AS statementNo,
        (SELECT assets_name FROM assets_production WHERE assets_production.production_no = nodeInfo.business_sub_id AND business_id = nodeInfo.businessId) AS statementName,
        (SELECT production_no FROM assets_production WHERE assets_production.production_no = nodeInfo.business_min_id AND business_id = nodeInfo.businessId) AS reportNo,
        (SELECT assets_name FROM assets_production WHERE assets_production.production_no = nodeInfo.business_min_id AND business_id = nodeInfo.businessId) AS reportName,
        (SELECT production_type FROM assets_production WHERE assets_production.production_no = nodeInfo.business_sub_id AND business_id = nodeInfo.businessId) AS productionType,
        clientele_id,
        customer.name AS customerName,
        bailor,
        bailor_contact_tel,
        assets.created,
        #查询客户经理
        (SELECT name FROM user WHERE id = assets.client_manager_id) AS clientManagerName,
        #查询项目负责人
        (SELECT name FROM user WHERE id = assets.principal_id) AS principalName,
        #查询业务来源(即子级客户)
        (SELECT customer_company.name FROM customer_company WHERE customer_company.id = clientele_sub_id) AS customerSubName,
        nodeInfo.businessId,
        nodeInfo.currentNodeName,
        nodeInfo.currentNodeCode,
        nodeInfo.handlerName,
        nodeInfo.currentNodeId,
        nodeInfo.recordId,
        nodeInfo.handlerId
        FROM assets
        LEFT JOIN user ON user.id = assets.principal_id
        LEFT JOIN customer_company AS customer ON customer.id = assets.clientele_id
        #查询当前进行的节点信息(并使用左连接)
        LEFT JOIN (SELECT nodeInstance.id AS currentNodeId,
        user.name AS handlerName,
        node.name AS currentNodeName,
        node.code AS currentNodeCode,
        nodeInstance.business_id AS businessId,
        nodeInstance.business_sub_id,
        nodeInstance.business_min_id,
        nodeInstance.state,
        record.id AS recordId,
        record.handler_id AS handlerId
        FROM work_flow_node_instance AS nodeInstance,
        work_task_record AS record,
        user,
        work_node AS node
        WHERE state = 'PENDING'
#             (state = 'PENDING' OR( state = 'FINISHED' AND node.`code` = 'BUSINESS_ARCHIVING' ))
        AND nodeInstance.node_id = node.id
        AND record.handler_id = user.id
        AND nodeInstance.deleted = 0
        AND record.instance_id = nodeInstance.id
        AND nodeInstance.business_type = 'ASSET_BUSINESS'
        ) AS nodeInfo ON nodeInfo.businessId = assets.id
        <if test="assetsSelectDTO != null and assetsSelectDTO.selectByDepartment">
            INNER JOIN (
            SELECT DISTINCT
            department.id,
            department.parent_id,
            user.id AS userId
            FROM
            user,
            post,
            department,
            user_post ,
            work_task_record AS record
            WHERE
            user.id = user_post.user_id
            AND user_post.post_id = post.id
            AND post.department_id = department.id
            AND user_post.deleted = 0
            ) AS department ON (department.id IN
            <foreach collection="assetsSelectDTO.departmentIdList" index="index" item="item" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
            )
            AND department.userId = nodeInfo.handlerId
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.selectByAll">
            INNER JOIN (
            SELECT DISTINCT
            department.id,
            department.parent_id,
            user.id AS userId
            FROM
            user,
            post,
            department,
            user_post ,
            work_task_record AS record
            WHERE
            user.id = user_post.user_id
            AND user_post.post_id = post.id
            AND post.department_id = department.id
            AND user_post.deleted = 0
            ) AS department ON (department.id IN
            <foreach collection="assetsSelectDTO.departmentIdList" index="index" item="item" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
            )
            AND (department.userId = assets.principal_id OR department.userId = assets.client_manager_id)
        </if>
        WHERE assets.deleted = 0
        AND nodeInfo.state = 'PENDING'
        #评估业务类别(业务类型)查询
        <if test="assetsSelectDTO != null and assetsSelectDTO.assetsBusinessGener != null and assetsSelectDTO.assetsBusinessGener != '' ">
            AND assets.assets_business_gener = #{assetsSelectDTO.assetsBusinessGener}
        </if>
        #项目负责人查询
        <if test="assetsSelectDTO != null and assetsSelectDTO.principalId != null">
            AND assets.principal_id = #{assetsSelectDTO.principalId}
        </if>
        #客户经理查询
        <if test="assetsSelectDTO != null and assetsSelectDTO.clientManagerId != null">
            AND assets.client_manager_id = #{assetsSelectDTO.clientManagerId}
        </if>
        #关键字模糊查询
        <if test="assetsSelectDTO != null and assetsSelectDTO.keyWord != null and assetsSelectDTO.keyWord != '' ">
            AND (
            assets.order_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            assets.name LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            nodeInfo.business_sub_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            nodeInfo.business_min_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            customer.name LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%')
            )
        </if>
        # 根据处理节点查询(节点code)
        <if test="assetsSelectDTO != null and assetsSelectDTO.nodeCode != null and assetsSelectDTO.nodeCode != '' ">
            AND nodeInfo.currentNodeCode = #{assetsSelectDTO.nodeCode}
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.handlerId != null ">
            AND nodeInfo.handlerId = #{assetsSelectDTO.handlerId}
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.departmentId">
            AND assets.department_id = #{assetsSelectDTO.departmentId}
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.getNodeIsBookbindingStamp">
            AND (nodeInfo.currentNodeCode = 'STATEMENT_BOOKBINDING_STAMP' OR nodeInfo.currentNodeCode = 'REPORT_BOOKBINDING_STAMP')
        </if>
        ORDER BY assets.created DESC
    </select>

    <sql id="selectMyOrderSql">
        SELECT (@i :=  @i + 1) AS id,
        assets.id AS assetsId,
        CASE
            WHEN reportInfo.assets_name IS NOT NULL THEN reportInfo.assets_name
            WHEN (SELECT assets_name FROM assets_production WHERE assets_production.production_no = nodeInfo.business_sub_id) IS NOT NULL THEN (SELECT assets_name FROM assets_production WHERE assets_production.production_no = nodeInfo.business_sub_id)
            ELSE assets.name
        END AS name,
        assets.order_id,
        assets_business_gener,
        allot_type,
        (SELECT production_no FROM assets_production WHERE assets_production.production_no = nodeInfo.business_sub_id) AS statementNo,
        (SELECT production_type FROM assets_production WHERE assets_production.production_no = nodeInfo.business_sub_id) AS productionType,
        reportInfo.production_no AS reportNo,
        reportInfo.repertory_state AS reportRepertoryState,
        reportInfo.delivery AS reportDelivery,
        reportInfo.assets_name AS reportName,
        clientele_id,
        customer.name AS customerName,
        bailor,
        bailor_contact_name,
        bailor_contact_tel,
        assets.created,
        ( SELECT SUM(estimated_value) FROM assets_evaluation_target WHERE assets_evaluation_target.assets_id = assets.id ) AS estimated_value,
        fund.id AS orderFundId,
        fund.should_amount,
        fund.real_amount,
        fund.discount,
        pFund.id AS productionFundId,
        pFund.standard_amount,
        pFund.production_should_amount,
        pFund.real_amount AS productionRealAmount,
        # invoice.real_amount AS invoiceRealAmount,
        (SELECT SUM(real_amount) FROM finance_invoice WHERE order_fund_id = fund.id AND finance_invoice.production_fund_id = pFund.id AND deleted = 0) AS invoiceRealAmount,
        #查询客户经理
        (SELECT name FROM user WHERE id = assets.client_manager_id) AS clientManagerName,
        #查询项目负责人
        (SELECT name FROM user WHERE id = assets.principal_id) AS principalName,
        #查询业务来源(即子级客户)
        (SELECT customer_company.name FROM customer_company WHERE customer_company.id = clientele_sub_id) AS customerSubName,
        nodeInfo.businessId,
        nodeInfo.currentNodeName,
        nodeInfo.currentNodeCode,
        nodeInfo.handlerName,
        nodeInfo.recordId,
        nodeInfo.currentNodeId,
        nodeInfo.handlerId
        FROM (SELECT @i := 0) AS sort,assets
        LEFT JOIN user ON user.id = assets.principal_id
        LEFT JOIN customer_company AS customer ON customer.id = assets.clientele_id
        LEFT JOIN order_fund AS fund ON fund.business_id = assets.id AND business_type = 'ASSET_BUSINESS'
        #         LEFT JOIN finance_invoice AS invoice ON invoice.order_fund_id = fund.id
        #查询当前进行的节点信息
        LEFT JOIN (SELECT nodeInstance.id AS currentNodeId,
        user.name AS handlerName,
        node.name AS currentNodeName,
        node.code AS currentNodeCode,
        nodeInstance.business_id AS businessId,
        nodeInstance.business_sub_id,
        nodeInstance.business_min_id,
        record.id AS recordId,
        record.handler_id AS handlerId
        FROM work_flow_node_instance AS nodeInstance,
        work_task_record AS record,
        user,
        work_node AS node
        WHERE (state = 'PENDING' OR( state = 'FINISHED' AND node.`code` = 'BUSINESS_ARCHIVING' ))
        AND nodeInstance.node_id = node.id
        AND record.handler_id = user.id
        AND nodeInstance.deleted = 0
        AND record.instance_id = nodeInstance.id
        AND nodeInstance.business_type = 'ASSET_BUSINESS'
        ) AS nodeInfo ON nodeInfo.businessId = assets.id
        LEFT JOIN production_fund AS pFund ON pFund.order_fund_id = fund.id AND pFund.production_no = nodeInfo.business_min_id
        LEFT JOIN(SELECT production_no,repertory_state,production_type,delivery,assets_name FROM assets_production) AS reportInfo ON reportInfo.production_no = nodeInfo.business_min_id
        WHERE assets.deleted = 0
        AND assets.client_manager_id = #{assetsSelectDTO.clientManagerId}
        #送达状态查询
        <if test="assetsSelectDTO != null and assetsSelectDTO.delivery != null">
            AND reportInfo.delivery = #{assetsSelectDTO.delivery}
        </if>
        #项目负责人查询
        <if test="assetsSelectDTO != null and assetsSelectDTO.principalId != null">
            AND assets.principal_id = #{assetsSelectDTO.principalId}
        </if>
        #关键字模糊查询
        <if test="assetsSelectDTO != null and assetsSelectDTO.keyWord != null and assetsSelectDTO.keyWord != '' ">
            AND (
            assets.order_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            assets.name LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            nodeInfo.business_sub_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            nodeInfo.business_min_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            customer.name LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%')
            )
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.startTime != null and assetsSelectDTO.endTime != null ">
            AND assets.created BETWEEN #{assetsSelectDTO.startTime} AND #{assetsSelectDTO.endTime}
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.departmentId != null ">
            AND assets.department_id = #{assetsSelectDTO.departmentId}
        </if>
        ORDER BY assets.created DESC
    </sql>

    <!--条件查询我的资产订单列表-->
    <select id="selectMyOrderPage" resultType="com.dayou.vo.AssetsVO">
        <include refid="selectMyOrderSql" />
    </select>

    <!--导出我的资产订单列表-->
    <select id="exportMyOrder" resultType="com.dayou.vo.AssetsVO">
        <include refid="selectMyOrderSql" />
    </select>

    <!--条件查询资产已归档列表-->
    <select id="selectArchivedPage" resultType="com.dayou.vo.AssetsVO">
        SELECT (@i :=  @i + 1) AS id,
        assets.id AS assetsId,
        assets.name,
        assets.order_id,
        assets_business_gener,
        allot_type,
        (SELECT production_no FROM assets_production WHERE assets_production.production_no = nodeInfo.business_sub_id) AS statementNo,
        (SELECT assets_name FROM assets_production WHERE assets_production.production_no = nodeInfo.business_sub_id) AS statementName,
        (SELECT production_no FROM assets_production WHERE assets_production.production_no = nodeInfo.business_min_id limit 1) AS reportNo,
        (SELECT assets_name FROM assets_production WHERE assets_production.production_no = nodeInfo.business_min_id limit 1) AS reportName,
        (SELECT production_type FROM assets_production WHERE assets_production.production_no = nodeInfo.business_sub_id) AS productionType,
        clientele_id,
        customer.name AS customerName,
        bailor,
        bailor_contact_name,
        bailor_contact_tel,
        assets.created,
        ( SELECT SUM(estimated_value) FROM assets_evaluation_target WHERE assets_evaluation_target.assets_id = assets.id ) AS estimated_value,
        fund.id AS orderFundId,
        fund.should_amount,
        fund.real_amount,
#         fund.discount,
        pFund.standard_amount,
#         invoice.real_amount AS invoiceRealAmount,
        (SELECT SUM(real_amount) FROM finance_invoice WHERE order_fund_id = fund.id AND deleted = 0) AS invoiceRealAmount,
        #查询客户经理
        (SELECT name FROM user WHERE id = assets.client_manager_id) AS clientManagerName,
        #查询项目负责人
        (SELECT name FROM user WHERE id = assets.principal_id) AS principalName,
        #查询业务来源(即子级客户)
        (SELECT customer_company.name FROM customer_company WHERE customer_company.id = clientele_sub_id) AS customerSubName,
        nodeInfo.businessId,
        nodeInfo.currentNodeName,
        nodeInfo.currentNodeCode,
        nodeInfo.handlerName,
        nodeInfo.recordId,
        nodeInfo.currentNodeId,
        nodeInfo.handlerId
        FROM (SELECT @i := 0) AS sort,assets
        LEFT JOIN user ON user.id = assets.principal_id
        LEFT JOIN customer_company AS customer ON customer.id = assets.clientele_id
        LEFT JOIN order_fund AS fund ON fund.business_id = assets.id AND business_type = 'ASSET_BUSINESS'
        #查询当前进行的节点信息
        LEFT JOIN (SELECT nodeInstance.id AS currentNodeId,
        user.name AS handlerName,
        node.name AS currentNodeName,
        node.code AS currentNodeCode,
        nodeInstance.business_id AS businessId,
        nodeInstance.business_sub_id,
        nodeInstance.business_min_id,
        record.id AS recordId,
        record.handler_id AS handlerId
        FROM work_flow_node_instance AS nodeInstance,
        work_task_record AS record,
        user,
        work_node AS node
        WHERE state = 'FINISHED'
        AND nodeInstance.node_id = node.id
        AND record.handler_id = user.id
        AND nodeInstance.deleted = 0
        AND record.instance_id = nodeInstance.id
        AND nodeInstance.business_type = 'ASSET_BUSINESS'
        ) AS nodeInfo ON nodeInfo.businessId = assets.id
        LEFT JOIN production_fund AS pFund ON pFund.business_id = assets.id AND pFund.production_no = nodeInfo.business_min_id
#         LEFT JOIN finance_invoice AS invoice ON pFund.order_fund_id = fund.id
        WHERE assets.deleted = 0
        AND nodeInfo.currentNodeCode = 'BUSINESS_ARCHIVING'
        #评估业务类别(业务类型)查询
        <if test="assetsSelectDTO != null and assetsSelectDTO.assetsBusinessGener != null and assetsSelectDTO.assetsBusinessGener != '' ">
            AND assets.assets_business_gener = #{assetsSelectDTO.assetsBusinessGener}
        </if>
        #项目负责人查询
        <if test="assetsSelectDTO != null and assetsSelectDTO.principalId != null">
            AND assets.principal_id = #{assetsSelectDTO.principalId}
        </if>
        #客户经理查询
        <if test="assetsSelectDTO != null and assetsSelectDTO.clientManagerId != null">
            AND assets.client_manager_id = #{assetsSelectDTO.clientManagerId}
        </if>
        #关键字模糊查询
        <if test="assetsSelectDTO != null and assetsSelectDTO.keyWord != null and assetsSelectDTO.keyWord != '' ">
            AND (
            assets.order_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            assets.name LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            nodeInfo.business_sub_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            nodeInfo.business_min_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            customer.name LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%')
            )
        </if>
        # 根据处理节点查询(节点code)
        <if test="assetsSelectDTO != null and assetsSelectDTO.nodeCode != null and assetsSelectDTO.nodeCode != '' ">
            AND nodeInfo.currentNodeCode = #{assetsSelectDTO.nodeCode}
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.handlerId != null ">
            AND nodeInfo.handlerId = #{assetsSelectDTO.handlerId}
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.departmentId != null ">
            AND assets.department_id = #{assetsSelectDTO.departmentId}
        </if>
        ORDER BY assets.created DESC
    </select>

    <!--条件查询资产已终止列表-->
    <select id="selectTerminatedPage" resultType="com.dayou.vo.AssetsVO">
        SELECT (@i := @i + 1) AS id,
        assets.id AS assetsId,
        assets.name,
        assets.order_id,
        assets_business_gener,
        allot_type,
        (SELECT production_no FROM assets_production WHERE assets_production.production_no = nodeInfo.business_sub_id) AS statementNo,
        (SELECT assets_name FROM assets_production WHERE assets_production.production_no = nodeInfo.business_sub_id) AS statementName,
        (SELECT production_no FROM assets_production WHERE assets_production.production_no = nodeInfo.business_min_id) AS reportNo,
        (SELECT assets_name FROM assets_production WHERE assets_production.production_no = nodeInfo.business_min_id) AS reportName,
        clientele_id,
        customer.name AS customerName,
        bailor,
        bailor_contact_name,
        bailor_contact_tel,
        assets.created,
        #查询客户经理
        (SELECT name FROM user WHERE id = assets.client_manager_id) AS clientManagerName,
        #查询项目负责人
        (SELECT name FROM user WHERE id = assets.principal_id) AS principalName,
        #查询业务来源(即子级客户)
        (SELECT customer_company.name
        FROM customer_company
        WHERE customer_company.id = clientele_sub_id) AS customerSubName,
        nodeInfo.businessId,
        nodeInfo.comment
        FROM (SELECT @i := 0) AS sort,
        assets
        LEFT JOIN user ON user.id = assets.principal_id
        LEFT JOIN customer_company AS customer ON customer.id = assets.clientele_id
        #查询当前进行的节点信息
        RIGHT JOIN (SELECT nodeInstance.id AS currentNodeId,
        nodeInstance.business_id AS businessId,
        nodeInstance.business_sub_id,
        nodeInstance.business_min_id,
        nodeInstance.state AS state,
        record.id AS recordId,
        nodeInstance.comment AS comment
        FROM work_flow_node_instance AS nodeInstance,
        work_task_record AS record,
        user,
        work_node AS node
        WHERE nodeInstance.state = 'CLOSED'
        AND nodeInstance.node_id = node.id
        AND record.handler_id = user.id
        AND nodeInstance.deleted = 0
        AND record.instance_id = nodeInstance.id
        AND business_type = 'ASSET_BUSINESS') AS nodeInfo ON nodeInfo.businessId = assets.id
        WHERE assets.deleted = 0
        AND #{assetsSelectDTO.handlerId} IN (SELECT record.handler_id
        FROM work_flow_node_instance AS nodeInstance,
        work_task_record AS record
        WHERE business_id = assets.id
        AND record.instance_id = nodeInstance.id)
        <if test="assetsSelectDTO != null and assetsSelectDTO.assetsBusinessGener != null and assetsSelectDTO.assetsBusinessGener != '' ">
            AND assets.assets_business_gener = #{assetsSelectDTO.assetsBusinessGener}
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.principalId != null">
            AND assets.principal_id = #{assetsSelectDTO.principalId}
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.clientManagerId != null">
            AND assets.client_manager_id = #{assetsSelectDTO.clientManagerId}
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.keyWord != null and assetsSelectDTO.keyWord != '' ">
            AND (
            assets.order_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            assets.name LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            nodeInfo.business_sub_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            nodeInfo.business_min_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            customer.name LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%')
            )
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.departmentId != null ">
            AND assets.department_id = #{assetsSelectDTO.departmentId}
        </if>
        ORDER BY assets.created DESC
    </select>

    <!--条件查询资产入库统计列表(分页查询)-->
    <select id="selectRepertoryPage" resultType="com.dayou.vo.AssetsRepertoryVo">
        SELECT (@i := @i + 1) AS id,
        assets.order_id AS orderId,
        production.production_no AS reportNo,
        production.assets_name AS orderName,
        purpose.purpose_name AS purposeName,
        method.method_name AS methodName,
        (SELECT instance.created
        FROM work_flow_node_instance AS instance,
        work_node AS node
        WHERE instance.business_id = assets.id
        AND instance.node_id = node.id
        AND instance.deleted = 0
        AND node.code = 'SPOT_RECONNAISSANCE_DETERMINE_PRICE') AS startTime,
        (SELECT instance.created
        FROM work_flow_node_instance AS instance,
        work_node AS node
        WHERE business_min_id = reportNo
        AND instance.node_id = node.id
        AND instance.deleted = 0
        AND node.code = 'REPORT_OUT') AS endTime,
        production.valuation_basis_date AS valuationBasisDate,
        production.evaluate_amount AS evaluateAmount,
        production.evaluate_price AS evaluatePrice,
        assets.bailor,
        assets.bailor_contact_tel AS bailorContactTel,
        (SELECT user.name FROM user WHERE user.id = assets.principal_id) AS principalName,
        (SELECT user.name FROM user WHERE user.id = assets.principal_id) AS firstReporter,
        (SELECT user.name FROM user WHERE user.id = assets.client_manager_id) AS managerName,
        (SELECT customer_company.name
        FROM customer_company
        WHERE customer_company.id = assets.clientele_id) AS customerName,
        (SELECT customer_company.name
        FROM customer_company
        WHERE customer_company.id = clientele_sub_id) AS customerSubName,
        production.repertory_in_time AS repertoryInTime,
        production.repertory_out_time AS repertoryOutTime,
        (SELECT instance.modified
        FROM work_flow_node_instance AS instance,
        work_node AS node
        WHERE business_min_id = reportNo
        AND instance.node_id = node.id
        AND instance.deleted = 0
        AND node.code = 'BUSINESS_ARCHIVING') AS archivedTime,
        (SELECT department.name FROM department WHERE department.id = assets.department_id) AS evaluateDepartment,
        (SELECT GROUP_CONCAT( department.name SEPARATOR '、' )
        FROM department,
        user,
        user_post,
        post
        WHERE user.id = user_post.user_id
        AND user_post.post_id = post.id
        AND post.department_id = department.id
        AND user.id = assets.client_manager_id
        AND user_post.deleted = 0) AS marketDepartment,
        fund.real_amount AS productionRealAmount
        FROM (SELECT @i := 0) AS sort,
        assets_production AS production
        LEFT JOIN assets ON assets.id = production.business_id
        LEFT JOIN assets_evaluation_target AS target ON target.report_no = production.production_no
        LEFT JOIN assets_evaluation_target_purpose AS purpose ON purpose.id = target.evaluation_purpose_id
        LEFT JOIN assets_evaluation_method AS method ON method.id = target.choice_evaluation_method_id
        LEFT JOIN production_fund AS fund ON fund.production_no = production.production_no
        WHERE production.deleted = 0
        AND (production.repertory_state = 0 OR production.repertory_state = 1)
        AND production.production_type != 'STATEMENT'
        <if test="assetsSelectDTO != null and assetsSelectDTO.keyWord != null and assetsSelectDTO.keyWord != '' ">
            AND (
            assets.order_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            assets.name LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            production.production_no LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%')
            )
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.departmentId != null ">
            AND assets.department_id = #{assetsSelectDTO.departmentId}
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.startTime != null and assetsSelectDTO.endTime != null ">
            HAVING startTime &gt;= #{assetsSelectDTO.startTime}
            AND endTime &lt;= #{assetsSelectDTO.endTime}
        </if>
    </select>

    <!--条件查询资产入库统计集合(用于导出)-->
    <select id="selectRepertoryList" resultType="com.dayou.vo.AssetsRepertoryVo">
        SELECT (@i := @i + 1) AS id,
        assets.order_id AS orderId,
        production.production_no AS reportNo,
        production.assets_name AS orderName,
        purpose.purpose_name AS purposeName,
        method.method_name AS methodName,
        (SELECT instance.created
        FROM work_flow_node_instance AS instance,
        work_node AS node
        WHERE instance.business_id = assets.id
        AND instance.node_id = node.id
        AND instance.deleted = 0
        AND node.code = 'SPOT_RECONNAISSANCE_DETERMINE_PRICE') AS startTime,
        (SELECT instance.created
        FROM work_flow_node_instance AS instance,
        work_node AS node
        WHERE business_min_id = reportNo
        AND instance.node_id = node.id
        AND instance.deleted = 0
        AND node.code = 'REPORT_OUT') AS endTime,
        production.valuation_basis_date AS valuationBasisDate,
        production.evaluate_amount AS evaluateAmount,
        production.evaluate_price AS evaluatePrice,
        assets.bailor,
        assets.bailor_contact_tel AS bailorContactTel,
        (SELECT user.name FROM user WHERE user.id = assets.principal_id) AS principalName,
        (SELECT user.name FROM user WHERE user.id = assets.principal_id) AS firstReporter,
        (SELECT user.name FROM user WHERE user.id = assets.client_manager_id) AS managerName,
        (SELECT customer_company.name
        FROM customer_company
        WHERE customer_company.id = assets.clientele_id) AS customerName,
        (SELECT customer_company.name
        FROM customer_company
        WHERE customer_company.id = clientele_sub_id) AS customerSubName,
        production.repertory_in_time AS repertoryInTime,
        production.repertory_out_time AS repertoryOutTime,
        (SELECT instance.modified
        FROM work_flow_node_instance AS instance,
        work_node AS node
        WHERE business_min_id = reportNo
        AND instance.node_id = node.id
        AND instance.deleted = 0
        AND node.code = 'BUSINESS_ARCHIVING') AS archivedTime,
        (SELECT department.name FROM department WHERE department.id = assets.department_id) AS evaluateDepartment,
        (SELECT department.name
        FROM department,
        user,
        user_post,
        post
        WHERE user.id = user_post.user_id
        AND user_post.post_id = post.id
        AND post.department_id = department.id
        AND user.id = assets.client_manager_id) AS marketDepartment,
        fund.real_amount AS productionRealAmount
        FROM (SELECT @i := 0) AS sort,
        assets_production AS production
        LEFT JOIN assets ON assets.id = production.business_id
        LEFT JOIN assets_evaluation_target AS target ON target.report_no = production.production_no
        LEFT JOIN assets_evaluation_target_purpose AS purpose ON purpose.id = target.evaluation_purpose_id
        LEFT JOIN assets_evaluation_method AS method ON method.id = target.choice_evaluation_method_id
        LEFT JOIN production_fund AS fund ON fund.production_no = production.production_no
        WHERE production.deleted = 0
        AND (production.repertory_state = 0 OR production.repertory_state = 1)
        AND production.production_type != 'STATEMENT'
        <if test="assetsSelectDTO != null and assetsSelectDTO.departmentId != null ">
            AND assets.department_id = #{assetsSelectDTO.departmentId}
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.startTime != null and assetsSelectDTO.endTime != null ">
            HAVING startTime &gt;= #{assetsSelectDTO.startTime}
            AND endTime &lt;= #{assetsSelectDTO.endTime}
        </if>
    </select>

    <!--根据业务id获取资产业务详情-->
    <select id="getAssetsDetailById" resultType="com.dayou.vo.AssetsVO">
        SELECT assets.id,
        #查询客户经理
        (SELECT name FROM user WHERE id = assets.client_manager_id) AS clientManagerName,
        user.name AS principalName,
        allot_type,
        assets.department_id,
        assets.order_id,
        assets.name,
        assets.assets_business_gener,
        assets.entrust_again,
        assets.clientele_type,
        assets.principal_id,
        assets.terminal_clientele_contact_id AS terminalClienteleContactId,
        customer.id AS clienteleId,
        customer.name AS customerName,
        linkman.id AS clienteleContactId,
        linkman.name AS linkmanName,
        linkman.department AS linkmanDepartment,
        linkman.duty AS linkmanDuty,
        linkman.mobile AS linkmanMobile,
        assets.security,
        assets.significant_assets_reorganization,
        assets.state_assets,
        assets.foreign_assets_involved,
        assets.dispense_benefit,
        assets.bailor,
        assets.bailor_address,
        assets.bailor_contact_name,
        assets.bailor_contact_tel,
        assets.terminal_clientele_id,
        assets.terminal_clientele_type,
        assets.remark,
        tLinkman.id AS tLinkmanId,
        tLinkman.name AS tLinkmanName,
        tLinkman.mobile AS tLinkmanMobile,
        assets.members AS members,
        #查询终端客户名称
        (SELECT customer_company.name FROM customer_company WHERE customer_company.id = assets.terminal_clientele_id) AS
        terminalClienteleName,
        assets.clientele_sub_id
        FROM assets
        LEFT JOIN user ON user.id = assets.principal_id
        LEFT JOIN department ON department.id = assets.department_id
        LEFT JOIN customer_company AS customer ON customer.id = assets.clientele_id
        LEFT JOIN customer_linkman AS linkman ON linkman.id = assets.clientele_contact_id
        LEFT JOIN (SELECT tLinkman.id, tLinkman.name, tLinkman.mobile FROM customer_linkman AS tLinkman) AS tLinkman ON
        tLinkman.id = assets.terminal_clientele_contact_id
        <where>
            <if test="id != null">
                AND assets.id = #{id}
            </if>
        </where>
    </select>

    <!--根据业务id获取资产业务款项信息-->
    <select id="getOrderFundInvoiceById" resultType="com.dayou.vo.AssetsOrderFundInvoiceVO">
        SELECT order_fund.id,
               business_id,
               should_amount,
               order_fund.real_amount
        FROM order_fund
        WHERE order_fund.business_id = #{id}
          AND order_fund.deleted = 0
          AND business_type = 'ASSET_BUSINESS'
    </select>

    <!--根据业务类型与取号类型获取业务取号信息-->
    <select id="getBusinessNum" resultType="com.dayou.entity.BusinessNumber">
        SELECT id, year, month, next_no
        FROM business_number
        WHERE take_type = #{takeType}
          AND business_type = #{businessType}
          AND year = #{year}
          AND deleted = 0
    </select>

    <!--新增取号信息-->
    <insert id="addBusinessNum">
        INSERT INTO business_number(id, take_type, business_type, year, month, next_no)
            VALUE (NULL, #{takeType}, #{businessType}, #{year}, #{month}, #{nextNo})
    </insert>

    <!--更新取号信息-->
    <update id="updateBusinessNum">
        UPDATE business_number
        SET month = #{month},
            next_no = #{nextNo}
        WHERE id = #{id}
    </update>

    <!--获取项目参与人-->
    <select id="getBusinessMembers" resultType="com.dayou.entity.Assets">
        SELECT id, principal_id, members
        FROM assets
        WHERE deleted = 0
        AND id = #{businessId}
    </select>

    <!--获取报告签字师-->
    <select id="getSignatorys" resultType="java.lang.String">
        SELECT signatory
        FROM assets_production
        WHERE deleted = 0
        AND production_no = #{reportNo}
    </select>

    <!--获取用户信息-->
    <select id="getAssetsBusinessMember" resultType="com.dayou.vo.AssetsBusinessMember">
        SELECT id AS memberId, name AS memberName
        FROM user
        WHERE deleted = 0
        AND id = #{userId}
    </select>

    <!--通过客户经理id获取下单部门-->
    <select id="getManagerDepId" resultType="java.lang.Long">
        SELECT post.department_id
        FROM user,user_post,post
        WHERE user.id = user_post.user_id
        AND user_post.post_id = post.id
        AND user_post.deleted = 0
        AND post.deleted = 0
        AND user.id = #{managerId}
        LIMIT 1
    </select>

    <!--获取资产下单部门-->
    <select id="getMarkeDepartment" resultType="com.dayou.entity.Department">
        SELECT department.id,department.name
        FROM assets,department
        WHERE assets.market_department_id = department.id
        AND department.deleted = 0
        AND assets.deleted = 0
        GROUP BY department.id
    </select>

    <!--更新资产订单基本信息-->
    <update id="updateOrderBaseInfo">
        UPDATE assets
        SET name = #{assets.name},
            assets_business_gener = #{assets.assetsBusinessGener},
            entrust_again = #{assets.entrustAgain},
            security = #{assets.security},
            significant_assets_reorganization = #{assets.significantAssetsReorganization},
            state_assets = #{assets.stateAssets},
            foreign_assets_involved = #{assets.foreignAssetsInvolved},
            dispense_benefit = #{assets.dispenseBenefit},
            bailor = #{assets.bailor},
            bailor_address = #{assets.bailorAddress},
            bailor_contact_name = #{assets.bailorContactName},
            bailor_contact_tel = #{assets.bailorContactTel}
        WHERE id = #{assets.id}
        AND deleted = 0
    </update>

</mapper>