<?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.tmerclub.cloud.cuerecycle.mapper.CueOrderMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap id="cueOrderVOMap" type="com.tmerclub.cloud.cuerecycle.model.vo.CueOrderVO">
        <result property="orderId" column="order_id"/>
        <result property="userId" column="user_id"/>
        <result property="productId" column="product_id"/>
        <result property="productName" column="product_name"/>
        <result property="productPrice" column="product_price"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="cueOrderType" column="cue_order_type"/>
        <result property="orderStatus" column="order_status"/>
        <result property="flawImgUrl" column="flaw_img_url"/>
        <result property="evidenceOfPayment" column="evidence_of_payment"/>
        <result property="orderRemark" column="order_remark"/>
        <result property="estimatedAmount" column="estimated_amount"/>
        <result property="actualAmount" column="actual_amount"/>
        <result property="voucherAmount" column="voucher_amount"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="isPayed" column="is_payed"/>
        <result property="payType" column="pay_type"/>
        <result property="payTime" column="pay_time"/>
        <result property="deliveryTime" column="delivery_time"/>
        <result property="finallyTime" column="finally_time"/>
        <result property="finallyTime" column="finally_time"/>
        <result property="mallOrderId" column="mall_order_id"/>
        <result property="sendNumber" column="send_number"/>
        <result property="productImages" column="product_images"/>
        <result property="warehouseId" column="warehouse_id"/>
        <result property="shopId" column="shop_id"/>
    </resultMap>

    <sql id="Vo_Column_List">
        order_id,product_id,product_name,product_images,product_price,user_id,user_name,user_phone,cue_order_type,
        order_status,flaw_img_url,evidence_of_payment,order_remark,estimated_amount,actual_amount,voucher_amount,deleted,
        create_time,update_time,is_payed,pay_type,pay_time,delivery_time,finally_time,mall_order_id,send_number,order_addr_id,warehouse_id,shop_id
    </sql>

    <select id="list" resultType="com.tmerclub.cloud.cuerecycle.model.vo.AdminCueOrderVO">
        SELECT
            co.order_id,
            co.product_name,
            co.product_images,
            co.user_name,
            co.user_phone,
            co.cue_order_type,
            co.order_status,
            co.estimated_amount,
            co.actual_amount,
            co.voucher_amount,
            co.brokerage,
            co.service_charge,
            co.is_payed,
            co.pay_type,
            co.pay_time,
            co.delivery_time,
            co.finally_time,
            co.send_number,
            co.back_tracking_number,
            co.create_time,
            co.update_time,
            cb.brand_name,
            cs.series_name,
            ct.type_name
        FROM
            `cue_order` AS co
        LEFT JOIN cue_product AS cp ON co.product_id = cp.product_id
        LEFT JOIN cue_type AS ct ON cp.type_id = ct.type_id
        LEFT JOIN cue_brand AS cb ON cp.brand_id = cb.brand_id
        LEFT JOIN cue_series AS cs ON cp.series_id = cs.series_id
        <where>
            co.deleted = 0
            <if test="dto.userName != null and dto.userName != ''">
                AND co.user_name LIKE CONCAT('%', #{dto.userName}, '%')
            </if>
            <if test="dto.userPhone != null and dto.userPhone != ''">
                AND co.user_phone LIKE CONCAT('%', #{dto.userPhone}, '%')
            </if>
            <if test="dto.cueOrderType != null">
                AND co.cue_order_type = #{dto.cueOrderType}
            </if>
            <if test="dto.orderStatus != null">
                AND co.order_status = #{dto.orderStatus}
            </if>
            <if test="dto.isPayed != null">
                AND co.is_payed = #{dto.isPayed}
            </if>
            <if test="dto.payType != null">
                AND co.pay_type = #{dto.payType}
            </if>
        </where>
        ORDER BY
            co.order_id DESC
    </select>

    <select id="getById" resultMap="cueOrderVOMap">
        SELECT
            <include refid="Vo_Column_List" />
        FROM
            cue_order
        WHERE
            order_id = #{orderId}
    </select>

    <insert id="save" useGeneratedKeys="true" keyProperty="orderId">
        INSERT INTO cue_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="productId != null">product_id,</if>
            <if test="productName != null and productName != ''">product_name,</if>
            <if test="productImages != null and productImages != ''">product_images,</if>
            <if test="productPrice != null">product_price,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="userPhone != null and userPhone != ''">user_phone,</if>
            <if test="cueOrderType != null">cue_order_type,</if>
            <if test="orderStatus != null">order_status,</if>
            <if test="flawImgUrl != null and flawImgUrl != ''">flaw_img_url,</if>
            <if test="evidenceOfPayment != null and evidenceOfPayment != ''">evidence_of_payment,</if>
            <if test="orderRemark != null and orderRemark != ''">order_remark,</if>
            <if test="estimatedAmount != null">estimated_amount,</if>
            <if test="actualAmount != null">actual_amount,</if>
            <if test="voucherAmount != null">voucher_amount,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isPayed != null">is_payed,</if>
            <if test="payType != null">pay_type,</if>
            <if test="payTime != null">pay_time,</if>
            <if test="deliveryTime != null">delivery_time,</if>
            <if test="finallyTime != null">finally_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="productId != null">#{productId},</if>
            <if test="productName != null and productName != ''">#{productName},</if>
            <if test="productImages != null and productImages != ''">#{productImages},</if>
            <if test="productPrice != null">#{productPrice},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="userPhone != null and userPhone != ''">#{userPhone},</if>
            <if test="cueOrderType != null">#{cueOrderType},</if>
            <if test="orderStatus != null">#{orderStatus},</if>
            <if test="flawImgUrl != null and flawImgUrl != ''">#{flawImgUrl},</if>
            <if test="evidenceOfPayment != null and evidenceOfPayment != ''">#{evidenceOfPayment},</if>
            <if test="orderRemark != null and orderRemark != ''">#{orderRemark},</if>
            <if test="estimatedAmount != null">#{estimatedAmount},</if>
            <if test="actualAmount != null">#{actualAmount},</if>
            <if test="voucherAmount != null">#{voucherAmount},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isPayed != null">#{isPayed},</if>
            <if test="payType != null">#{payType},</if>
            <if test="payTime != null">#{payTime},</if>
            <if test="deliveryTime != null">#{deliveryTime},</if>
            <if test="finallyTime != null">#{finallyTime},</if>
        </trim>
    </insert>

    <update id="update">
        UPDATE cue_order
        <set>
            <if test="userName != null and userName != ''">
                user_name = #{userName},
            </if>
            <if test="userPhone != null and userPhone != ''">
                user_phone = #{userPhone},
            </if>
            <if test="cueOrderType != null">
                cue_order_type = #{cueOrderType},
            </if>
            <if test="orderStatus != null">
                order_status = #{orderStatus},
            </if>
            <if test="flawImgUrl != null and flawImgUrl != ''">
                flaw_img_url = #{flawImgUrl},
            </if>
            <if test="evidenceOfPayment != null and evidenceOfPayment != ''">
                evidence_of_payment = #{evidenceOfPayment},
            </if>
            <if test="orderRemark != null and orderRemark != ''">
                order_remark = #{orderRemark},
            </if>
            <if test="estimatedAmount != null">
                estimated_amount = #{estimatedAmount},
            </if>
            <if test="actualAmount != null">
                actual_amount = #{actualAmount},
            </if>
            <if test="voucherAmount != null">
                voucher_amount = #{voucherAmount},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="isPayed != null">
                is_payed = #{isPayed},
            </if>
            <if test="payType != null">
                pay_type = #{payType},
            </if>
            <if test="payTime != null">
                pay_time = #{payTime},
            </if>
            <if test="deliveryTime != null">
                delivery_time = #{deliveryTime},
            </if>
            <if test="finallyTime != null">
                finally_time = #{finallyTime},
            </if>
            <if test="mallOrderId != null">
                mall_order_id = #{mallOrderId},
            </if>
            <if test="mallOrderId != null">
                service_charge = #{serviceCharge},
            </if>
            <if test="brokerage != null">
                brokerage = #{brokerage},
            </if>
            <if test="sendNumber != null and sendNumber != ''">
                send_number = #{sendNumber},
            </if>
            <if test="warehouseId != null">
                warehouse_id = #{warehouseId},
            </if>
            <if test="orderAddrId != null">
                order_addr_id = #{orderAddrId},
            </if>
            <if test="outletConfigId != null">
                outlet_config_id = #{outletConfigId},
            </if>
            <if test="printerId != null">
                printer_id = #{printerId},
            </if>
            <if test="shopId != null">
                shop_id = #{shopId},
            </if>
            <if test="backTrackingNumber != null and backTrackingNumber != ''">
                back_tracking_number = #{backTrackingNumber},
            </if>
        </set>
        where
            order_id = #{orderId}
    </update>
</mapper>