<?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.casic.missiles.mapper.finance.FinanceAgreementInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.model.finance.FinanceAgreementInfo">
        <id column="id" property="id" />
        <result column="agreement_no" property="agreementNo" />
        <result column="agreement_name" property="agreementName" />
        <result column="agreement_kind" property="agreementKind" />
        <result column="agreement_type" property="agreementType" />
        <result column="dept_id" property="deptId" />
        <result column="dept_name" property="deptName" />
        <result column="estimate_sign_date" property="estimateSignDate" />
        <result column="agreement_amount" property="agreementAmount" />
        <result column="agreement_source" property="agreementSource" />
        <result column="agreement_start_date" property="agreementStartDate" />
        <result column="agreement_end_date" property="agreementEndDate" />
        <result column="customer_id" property="customerId" />
        <result column="first_party" property="firstParty" />
        <result column="director" property="director" />
        <result column="second_director" property="secondDirector" />
        <result column="second_party" property="secondParty" />
        <result column="agreement_important_content" property="agreementImportantContent" />
        <result column="minio_file_name" property="minioFileName" />
        <result column="status" property="status" />
        <result column="approval_status" property="approvalStatus" />
        <result column="process_id" property="processId" />
        <result column="remark" property="remark" />
        <result column="create_user" property="createUser" />
        <result column="create_user_name" property="createUserName" />
        <result column="is_del" property="isDel" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <select id="selectDraftListForApproval" resultType="com.casic.missiles.model.finance.FinanceAgreementInfo">
        SELECT *
        FROM finance_agreement_info
        WHERE is_del = 0
        AND approval_status = #{request.approvalStatus}
        AND create_user = #{request.createUserId}
        <if test="request.agreementNo != null and request.agreementNo !=''">
            and agreement_no like concat('%',#{request.agreementNo},'%')
        </if>
        <if test="request.agreementName != null and request.agreementName !=''">
            and agreement_name like concat('%',#{request.agreementName},'%')
        </if>
        <if test="request.agreementKind != null and request.agreementKind !=''">
            and agreement_kind = #{request.agreementKind}
        </if>
        <if test="request.agreementType != null and request.agreementType !=''">
            and agreement_type = #{request.agreementType}
        </if>
        <if test="request.customerId != null and request.customerId !=''">
            and customer_id = #{request.customerId}
        </if>
        <if test="request.firstParty != null and request.firstParty !=''">
            and first_party like concat('%',#{request.firstParty},'%')
        </if>
        <if test="request.estimateSignStartDate != null and request.estimateSignStartDate !=''">
            and estimate_sign_date &gt;= #{request.estimateSignStartDate}
        </if>
        <if test="request.estimateSignEndDate != null and request.estimateSignEndDate !=''">
            and estimate_sign_date &lt;= #{request.estimateSignEndDate}
        </if>
        <if test="request.status != null and request.status !=''">
            and status = #{request.status}
        </if>
    </select>

    <select id="selectBatchForApproval" resultType="com.casic.missiles.model.finance.FinanceAgreementInfo">
        SELECT *
        FROM finance_agreement_info
        WHERE is_del = 0
        <if test="request.agreementNo != null and request.agreementNo !=''">
            and agreement_no like concat('%',#{request.agreementNo},'%')
        </if>
        <if test="request.agreementName != null and request.agreementName !=''">
            and agreement_name like concat('%',#{request.agreementName},'%')
        </if>
        <if test="request.agreementKind != null and request.agreementKind !=''">
            and agreement_kind = #{request.agreementKind}
        </if>
        <if test="request.agreementType != null and request.agreementType !=''">
            and agreement_type = #{request.agreementType}
        </if>
        <if test="request.customerId != null and request.customerId !=''">
            and customer_id = #{request.customerId}
        </if>
        <if test="request.firstParty != null and request.firstParty !=''">
            and first_party like concat('%',#{request.firstParty},'%')
        </if>
        <if test="request.estimateSignStartDate != null and request.estimateSignStartDate !=''">
            and estimate_sign_date &gt;= #{request.estimateSignStartDate}
        </if>
        <if test="request.estimateSignEndDate != null and request.estimateSignEndDate !=''">
            and estimate_sign_date &lt;= #{request.estimateSignEndDate}
        </if>
        <if test="request.status != null and request.status !=''">
            and status = #{request.status}
        </if>
        and id in
        <foreach collection="businessKey" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="selectMaxAgreementNo" resultType="java.lang.Long">
        SELECT IFNULL(max(RIGHT(agreement_no, 12)), 0) FROM finance_agreement_info
    </select>

    <select id="selectCustomerExcellentList" resultType="com.casic.missiles.dto.customer.CustomerExcellentResponse">
        SELECT first_party AS customerName, SUM(agreement_amount) AS totalAmount
        FROM finance_agreement_info
        WHERE is_del = 0
        GROUP BY first_party
        ORDER BY SUM(agreement_amount) DESC
        LIMIT 10
    </select>

    <select id="selectCompleteAmount" resultType="java.lang.Integer">
        SELECT IFNULL(SUM(agreement_amount),0)
        FROM finance_agreement_info
        WHERE is_del = 0
        <if test="status != null and status !=''">
            and status = #{status}
        </if>
        <if test="dimension != null and dimension !='' and dimension == 'year'">
            and YEAR(create_time) = YEAR(CURDATE());
        </if>
        <if test="dimension != null and dimension !='' and dimension == 'month'">
            and DATE_FORMAT(create_time, '%Y%m') = DATE_FORMAT(CURDATE( ),'%Y%m')
        </if>
    </select>

    <select id="selectAmountByDept" resultType="com.casic.missiles.dto.business.order.labIncomeResponse">
        SELECT mo.organize_name AS labName, IFNULL(SUM(faii.amount),0) AS amount
        FROM meter_organize mo
        LEFT JOIN
            (SELECT fai.dept_id AS deptId, IFNULL(fai.agreement_amount,0) AS amount
            FROM finance_agreement_info fai
            WHERE fai.is_del = 0 AND fai.dept_name IS NOT NULL
            <if test="dimension != null and dimension !='' and dimension == 'year'">
                and YEAR(fai.create_time) = YEAR(CURDATE())
            </if>
            <if test="dimension != null and dimension !='' and dimension == 'quarter'">
                and QUARTER(fai.create_time) = QUARTER(CURDATE()) AND YEAR(fai.create_time) = YEAR(CURDATE())
            </if>
            <if test="dimension != null and dimension !='' and dimension == 'month'">
                and DATE_FORMAT(fai.create_time, '%Y%m') = DATE_FORMAT(CURDATE( ),'%Y%m')
            </if>
            ) faii ON (mo.dept_id = faii.deptId AND mo.is_lab = 1)
        GROUP BY mo.organize_name
    </select>

    <select id="selectCustomerExcellentForCockpit" resultType="com.casic.missiles.dto.customer.CustomerExcellentResponse">
        SELECT fai.first_party AS customerName, COUNT(bo.id) AS businessFrequency, SUM(fai.agreement_amount) AS totalAmount
        FROM finance_agreement_info fai
        JOIN business_order bo ON fai.customer_id = bo.customer_id
        WHERE fai.is_del = 0
        GROUP BY fai.first_party
        ORDER BY SUM(fai.agreement_amount) DESC
    </select>

    <select id="selectPreMonthCountByYear" resultType="com.casic.missiles.dto.cockpit.ManageTrendResponse">
        SELECT DATE_FORMAT(fai.estimate_sign_date,'%Y-%m') AS dimension, SUM(fai.agreement_amount) AS amount
        FROM finance_agreement_info fai
        JOIN business_order bo ON fai.customer_id = bo.customer_id
        WHERE fai.is_del = 0
        AND DATE_SUB(CURDATE(), INTERVAL 365 DAY) &lt;= DATE(fai.estimate_sign_date)
        GROUP BY DATE_FORMAT(fai.estimate_sign_date,'%Y-%m')
    </select>
</mapper>
