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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.model.finance.FinanceBusinessSettlementInfo">
        <id column="id" property="id" />
        <result column="order_id" property="orderId" />
        <result column="suggested_discount" property="suggestedDiscount" />
        <result column="extra_charge" property="extraCharge" />
        <result column="extra_charge_illustration" property="extraChargeIllustration" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, order_id, suggested_discount, extra_charge, extra_charge_illustration, create_time, update_time
    </sql>

    <select id="selectListPage" resultType="com.casic.missiles.dto.finance.SettlementInfoListResponse">
        SELECT bo.id AS order_id, fbsi.id, bo.order_code, bo.customer_id, bo.customer_no, bo.customer_name, bo.deliverer, bo.deliverer_tel,
            bo.is_urgent, bo.create_time, fbsi.suggested_discount, fbsi.extra_charge, fbsi.extra_charge_illustration
        FROM finance_business_settlement_info fbsi
        LEFT JOIN business_order bo ON fbsi.order_id = bo.id
        WHERE bo.is_del = 0
        <if test="request.orderCode != null and request.orderCode !=''">
            and bo.order_code like concat('%',#{request.orderCode},'%')
        </if>
        <if test="request.customerNo != null and request.customerNo !=''">
            and bo.customer_no like concat('%',#{request.customerNo},'%')
        </if>
        <if test="request.customerName != null and request.customerName !=''">
            and bo.customer_name like concat('%',#{request.customerName},'%')
        </if>
        <if test="request.deliverer != null and request.deliverer !=''">
            and bo.deliverer like concat('%',#{request.deliverer},'%')
        </if>
    </select>

    <select id="selectList" resultType="com.casic.missiles.dto.finance.SettlementInfoListResponse">
        SELECT bo.id AS order_id, fbsi.id, bo.order_code, bo.customer_id, bo.customer_no, bo.customer_name, bo.deliverer, bo.deliverer_tel,
        bo.is_urgent, bo.create_time, fbsi.suggested_discount, fbsi.extra_charge, fbsi.extra_charge_illustration
        FROM finance_business_settlement_info fbsi
        LEFT JOIN business_order bo ON fbsi.order_id = bo.id
        WHERE bo.is_del = 0
        <if test="request.orderCode != null and request.orderCode !=''">
            and bo.order_code like concat('%',#{request.orderCode},'%')
        </if>
        <if test="request.customerNo != null and request.customerNo !=''">
            and bo.customer_no like concat('%',#{request.customerNo},'%')
        </if>
        <if test="request.customerName != null and request.customerName !=''">
            and bo.customer_name like concat('%',#{request.customerName},'%')
        </if>
        <if test="request.deliverer != null and request.deliverer !=''">
            and bo.deliverer like concat('%',#{request.deliverer},'%')
        </if>
    </select>

    <select id="selectDetailById" resultType="com.casic.missiles.dto.finance.SettlementInfoDetailResponse">
        SELECT fbsi.id, fbsi.order_id, bo.order_code, bo.customer_id, bo.customer_no, bo.customer_name, bo.customer_phone, bo.deliverer,
        bo.deliverer_tel, bo.create_time, bo.plan_deliver_time, bo.require_over_time, bo.is_urgent, fbsi.suggested_discount,
        fbsi.extra_charge, fbsi.extra_charge_illustration
        FROM finance_business_settlement_info fbsi
        LEFT JOIN business_order bo ON fbsi.order_id = bo.id
        WHERE bo.is_del = 0 AND fbsi.id = #{id}
    </select>

    <select id="selectListByIds" resultType="com.casic.missiles.dto.finance.SettlementInfoListResponse">
        SELECT bo.id AS order_id, fbsi.id, bo.order_code, bo.customer_id, bo.customer_no, bo.customer_name, bo.deliverer, bo.deliverer_tel,
        bo.is_urgent, bo.create_time, fbsi.suggested_discount, fbsi.extra_charge, fbsi.extra_charge_illustration
        FROM finance_business_settlement_info fbsi
        LEFT JOIN business_order bo ON fbsi.order_id = bo.id
        WHERE bo.is_del = 0
        AND fbsi.id IN
        <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="selectListByDimension" resultType="com.casic.missiles.dto.finance.SettlementInfoListResponse">
        SELECT fbsi.*
        FROM finance_business_settlement_info fbsi
        JOIN business_order bo ON fbsi.order_id = bo.id
        WHERE 1=1
        <if test="dimension != null and dimension !='' and dimension == 'year'">
            AND YEAR(fbsi.create_time) = YEAR(CURDATE());
        </if>
        <if test="dimension != null and dimension !='' and dimension == 'month'">
            AND DATE_FORMAT(fbsi.create_time, '%Y%m') = DATE_FORMAT(CURDATE( ),'%Y%m')
        </if>
    </select>

    <select id="selectPreMonthAmountByYear" resultType="com.casic.missiles.dto.cockpit.ManageTrendResponse">
        <!--查询委托单和对应所有样品标价和-->
        SELECT DATE_FORMAT(fbsi.create_time,'%Y-%m') AS dimension, fbsi.order_id AS orderId,
            fbsi.suggested_discount, fbsi.extra_charge, SUM(bosr.posted_price) AS postPrice
        FROM finance_business_settlement_info fbsi
        JOIN business_order bo ON fbsi.order_id = bo.id
        JOIN business_order_sample_relation bosr ON fbsi.order_id = bosr.order_id
        WHERE bo.is_del = 0
        AND DATE_SUB(CURDATE(), INTERVAL 365 DAY) &lt;= DATE(fbsi.create_time)
        GROUP BY DATE_FORMAT(fbsi.create_time,'%Y-%m'), fbsi.order_id, fbsi.suggested_discount, fbsi.extra_charge
    </select>
</mapper>
