<?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.cool.store.mapper.order.StoreOrderMapper">

    <resultMap id="BaseResultMap" type="com.cool.store.entity.order.StoreOrderDO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="order_no" property="orderNo" jdbcType="VARCHAR"/>
        <result column="shop_id" property="shopId" jdbcType="BIGINT"/>
        <result column="line_id" property="lineId" jdbcType="BIGINT"/>
        <result column="partner_name" property="partnerName" jdbcType="VARCHAR"/>
        <result column="partner_mobile" property="partnerMobile" jdbcType="VARCHAR"/>
        <result column="investment_manager" property="investmentManager" jdbcType="VARCHAR"/>
        <result column="store_type_id" property="storeTypeId" jdbcType="BIGINT"/>
        <result column="store_type_code" property="storeTypeCode" jdbcType="VARCHAR"/>
        <result column="store_type_name" property="storeTypeName" jdbcType="VARCHAR"/>
        <result column="store_type" property="storeType" jdbcType="INTEGER"/>
        <result column="brand" property="brand" jdbcType="VARCHAR"/>
        <result column="franchise_fee" property="franchiseFee" jdbcType="DECIMAL"/>
        <result column="original_franchise_fee" property="originalFranchiseFee" jdbcType="DECIMAL"/>
        <result column="brand_usage_fee" property="brandUsageFee" jdbcType="DECIMAL"/>
        <result column="original_brand_usage_fee" property="originalBrandUsageFee" jdbcType="DECIMAL"/>
        <result column="management_fee" property="managementFee" jdbcType="DECIMAL"/>
        <result column="original_management_fee" property="originalManagementFee" jdbcType="DECIMAL"/>
        <result column="system_usage_fee" property="systemUsageFee" jdbcType="DECIMAL"/>
        <result column="original_system_usage_fee" property="originalSystemUsageFee" jdbcType="DECIMAL"/>
        <result column="first_order_fee" property="firstOrderFee" jdbcType="DECIMAL"/>
        <result column="contract_deposit" property="contractDeposit" jdbcType="DECIMAL"/>
        <result column="original_contract_deposit" property="originalContractDeposit" jdbcType="DECIMAL"/>
        <result column="device_deposit" property="deviceDeposit" jdbcType="DECIMAL"/>
        <result column="original_device_deposit" property="originalDeviceDeposit" jdbcType="DECIMAL"/>
        <result column="design_fee" property="designFee" jdbcType="DECIMAL"/>
        <result column="original_design_fee" property="originalDesignFee" jdbcType="DECIMAL"/>
        <result column="store_fixed_amount" property="storeFixedAmount" jdbcType="DECIMAL"/>
        <result column="option_amount" property="optionAmount" jdbcType="DECIMAL"/>
        <result column="total_amount" property="totalAmount" jdbcType="DECIMAL"/>
        <result column="paid_amount" property="paidAmount" jdbcType="DECIMAL"/>
        <result column="unpaid_amount" property="unpaidAmount" jdbcType="DECIMAL"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, order_no, shop_id, line_id, partner_name, partner_mobile, investment_manager,
        store_type_id, store_type_code, store_type_name, store_type, brand,
        franchise_fee, original_franchise_fee,
        brand_usage_fee, original_brand_usage_fee,
        management_fee, original_management_fee,
        system_usage_fee, original_system_usage_fee,
        contract_deposit, original_contract_deposit,
        device_deposit, original_device_deposit,
        design_fee, original_design_fee,
        first_order_fee,
        store_fixed_amount, option_amount, total_amount, paid_amount, unpaid_amount,
        status, create_time, update_time
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM zxjp_store_order
        WHERE id = #{id}
    </select>

    <select id="queryPage" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM zxjp_store_order
        <where>
            <if test="shopId != null">
                shop_id = #{shopId}
            </if>
            <if test="status != null">
                <if test="shopId != null">AND</if>
                status = #{status}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="queryPageByLineId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM zxjp_store_order
        <where>
            <if test="lineId != null">
                line_id = #{lineId}
            </if>
            <if test="status != null">
                <if test="lineId != null">AND</if>
                status = #{status}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="countByShopIdAndStatusIn" resultType="int">
        SELECT COUNT(1)
        FROM zxjp_store_order
        WHERE shop_id = #{shopId}
        AND status IN
        <foreach collection="statuses" item="st" open="(" separator="," close=")">
            #{st}
        </foreach>
    </select>

    <select id="countByShopIdAndStatus" resultType="int">
        SELECT COUNT(1)
        FROM zxjp_store_order
        WHERE shop_id = #{shopId}
          AND status = #{status}
    </select>

    <select id="getLatestDraftByShopId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM zxjp_store_order
        WHERE shop_id = #{shopId}
        AND status = #{status}
        ORDER BY update_time DESC, create_time DESC
        LIMIT 1
    </select>

    <select id="getLatestOrderByShopId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM zxjp_store_order
        WHERE shop_id = #{shopId}
        AND status != 25
        ORDER BY update_time DESC, create_time DESC
        LIMIT 1
    </select>

    <select id="queryPCPage" resultType="com.cool.store.vo.order.PCStoreOrderListVO">
        SELECT
        a.id AS orderId,
        a.order_no AS orderNo,
        a.store_type AS storeType,
        a.brand AS brand,
        a.store_type_name AS name,
        a.total_amount AS totalAmount,
        a.paid_amount AS paidAmount,
        a.unpaid_amount AS unpaidAmount,
        a.store_fixed_amount as storeFixedAmount,
        a.status AS status,
        a.create_time AS orderTime,
        c.username AS partnerName,
        c.mobile AS partnerMobile,
        b.shop_code as shopCode,
        b.shop_name as shopName
        FROM zxjp_store_order a
        LEFT JOIN xfsg_shop_info b ON a.shop_id = b.id
        LEFT JOIN xfsg_line_info c ON a.line_id = c.id
        <where>
            <if test="orderNo != null and orderNo != ''">
                AND a.order_no LIKE CONCAT('%', #{orderNo}, '%')
            </if>
            <if test="partnerName != null and partnerName != ''">
                AND c.username LIKE CONCAT('%', #{partnerName}, '%')
            </if>
            <if test="partnerMobile != null and partnerMobile != ''">
                AND c.mobile LIKE CONCAT('%', #{partnerMobile}, '%')
            </if>
            <if test="brand != null and brand != ''">
                AND a.brand = #{brand}
            </if>
            <if test="storeTypeId != null">
                AND a.store_type_id = #{storeTypeId}
            </if>
            <if test="orderTimeStart != null">
                AND a.create_time &gt;= #{orderTimeStart}
            </if>
            <if test="orderTimeEnd != null">
                AND a.create_time &lt;= #{orderTimeEnd}
            </if>
            <if test="payTimeStart != null">
                AND a.pay_time &gt;= #{payTimeStart}
            </if>
            <if test="payTimeEnd != null">
                AND a.pay_time &lt;= #{payTimeEnd}
            </if>
            <if test="status != null">
                AND a.status = #{status}
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>

    <select id="count" resultType="java.lang.Long">
        SELECT
        count(1)
        FROM zxjp_store_order a
        LEFT JOIN xfsg_shop_info b ON a.shop_id = b.id
        LEFT JOIN xfsg_line_info c ON a.line_id = c.id
        <where>
            <if test="orderNo != null and orderNo != ''">
                AND a.order_no LIKE CONCAT('%', #{orderNo}, '%')
            </if>
            <if test="partnerName != null and partnerName != ''">
                AND c.username LIKE CONCAT('%', #{partnerName}, '%')
            </if>
            <if test="partnerMobile != null and partnerMobile != ''">
                AND c.mobile LIKE CONCAT('%', #{partnerMobile}, '%')
            </if>
            <if test="brand != null and brand != ''">
                AND a.brand = #{brand}
            </if>
            <if test="storeTypeId != null">
                AND a.store_type_id = #{storeTypeId}
            </if>
            <if test="orderTimeStart != null">
                AND a.create_time &gt;= #{orderTimeStart}
            </if>
            <if test="orderTimeEnd != null">
                AND a.create_time &lt;= #{orderTimeEnd}
            </if>
            <if test="payTimeStart != null">
                AND a.pay_time &gt;= #{payTimeStart}
            </if>
            <if test="payTimeEnd != null">
                AND a.pay_time &lt;= #{payTimeEnd}
            </if>
            <if test="status != null">
                AND a.status = #{status}
            </if>
        </where>
    </select>

</mapper>
