<?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.platform.dao.OrderDao">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.platform.entity.OrderEntity" id="orderMap">
        <result property="id" column="id"/>
        <result property="orderSn" column="order_sn"/>
        <result property="userId" column="user_id"/>
        <result property="orderStatus" column="order_status"/>
        <result property="shippingStatus" column="shipping_status"/>
        <result property="payStatus" column="pay_status"/>
        <result property="consignee" column="consignee"/>
        <result property="country" column="country"/>
        <result property="province" column="province"/>
        <result property="city" column="city"/>
        <result property="district" column="district"/>
        <result property="address" column="address"/>
        <result property="mobile" column="mobile"/>
        <result property="postscript" column="postscript"/>
        <result property="shippingId" column="shipping_id"/>
        <result property="shippingName" column="shipping_name"/>
        <result property="shippingNo" column="shipping_no"/>
        <result property="payId" column="pay_id"/>
        <result property="payName" column="pay_name"/>
        <result property="shippingFee" column="shipping_fee"/>
        <result property="actualPrice" column="actual_price"/>
        <result property="integral" column="integral"/>
        <result property="integralMoney" column="integral_money"/>
        <result property="orderPrice" column="order_price"/>
        <result property="goodsPrice" column="goods_price"/>
        <result property="addTime" column="add_time"/>
        <result property="confirmTime" column="confirm_time"/>
        <result property="payTime" column="pay_time"/>
        <result property="freightPrice" column="freight_price"/>
        <result property="couponId" column="coupon_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="couponPrice" column="coupon_price"/>
        <result property="callbackStatus" column="callback_status"/>
        <result property="orderType" column="order_type"/>
        <result property="userName" column="username"/>
    </resultMap>

    <select id="queryObject" resultType="com.platform.entity.OrderEntity">
		select o.*,u.username as username, u.nickname as nickName
        from nideshop_order o
        left join nideshop_user u on o.user_id = u.id
        where o.id = #{value}
	</select>

    <select id="getDealCount" resultType="java.lang.Long">
		select count(*)
        from nideshop_order where order_status >=201 and pay_time LIKE concat(#{day},'%')
	</select>

    <select id="queryList" resultType="com.platform.entity.OrderEntity">
        select o.* ,
        u.username as username, u.nickname as nickName,
        b.name as brandName, b.id AS brandId
        from nideshop_order o
        left join nideshop_user u on o.user_id = u.id
        left join nideshop_order_goods og on o.id = og.order_id
        left join nideshop_goods g on og.goods_id = g.id
        left join nideshop_brand b on g.brand_id = b.id
        left join nideshop_order_shipping os on os.brand_id = b.id AND os.order_id = o.id
        WHERE 1=1
        <if test="orderSn != null and orderSn.trim() != ''">
            AND o.order_sn LIKE concat('%',#{orderSn},'%')
        </if>
        <if test="orderSnList != null and orderSnList.size() > 0">
            AND o.order_sn in
            <foreach item="orderSn" collection="orderSnList" open="(" separator="," close=")">
                #{orderSn}
            </foreach>
        </if>
        <if test="shippingStatus != null and shippingStatus.trim() != ''">
            AND o.shipping_status = #{shippingStatus}
        </if>
        <if test="payStatus != null and payStatus.trim() != ''">
            AND o.pay_status = #{payStatus}
        </if>
        <if test="orderStatus != null and orderStatus.trim() != ''">
            <if test="orderStatus == 1 or orderStatus == 2">
                AND os.shipping_status = #{orderStatus}
            </if>
            <if test="orderStatus != 1 and orderStatus != 2">
                AND o.order_status = #{orderStatus}
            </if>
        </if>
        <if test="orderType != null and orderType.trim() != ''">
            AND o.order_type = #{orderType}
        </if>
        <if test="brandId != null and brandId.trim() != ''">
            AND b.id = #{brandId}
        </if>
        <if test="brandIdList != null and brandIdList.size() > 0">
            AND b.id in
            <foreach item="brandId" collection="brandIdList" open="(" separator="," close=")">
                #{brandId}
            </foreach>
        </if>
        GROUP BY b.name, o.id
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by o.${sidx} ${order}
            </when>
            <otherwise>
                order by o.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>


    <select id="queryTotal" resultType="int">
        select count(*) from nideshop_order o
        left join nideshop_user u on o.user_id = u.id
        left join nideshop_order_goods og on o.id = og.order_id
        left join nideshop_goods g on og.goods_id = g.id
        left join nideshop_brand b on g.brand_id = b.id
        WHERE 1=1
        <if test="orderSn != null and orderSn.trim() != ''">
            AND order_sn LIKE concat('%',#{orderSn},'%')
        </if>
        <if test="shippingStatus != null and shippingStatus.trim() != ''">
            AND shipping_status = #{shippingStatus}
        </if>
        <if test="payStatus != null and payStatus.trim() != ''">
            AND pay_status = #{payStatus}
        </if>
        <if test="orderStatus != null and orderStatus.trim() != ''">
            AND order_status = #{orderStatus}
        </if>
        <if test="orderType != null and orderType.trim() != ''">
            AND order_type = #{orderType}
        </if>
        <if test="brandId != null and brandId.trim() != ''">
            AND b.id = #{brandId}
        </if>
        <if test="brandIdList != null and brandIdList.size() > 0">
            AND b.id in
            <foreach item="brandId" collection="brandIdList" open="(" separator="," close=")">
                #{brandId}
            </foreach>
        </if>
    </select>

    <insert id="save" parameterType="com.platform.entity.OrderEntity" useGeneratedKeys="true" keyProperty="id">
		insert into nideshop_order
		(
			`order_sn`, 
			`user_id`, 
			`order_status`, 
			`shipping_status`, 
			`pay_status`, 
			`consignee`, 
			`country`, 
			`province`, 
			`city`, 
			`district`,
			`address`, 
			`mobile`, 
			`postscript`, 
			`shipping_id`, 
			`shipping_name`,
			`shipping_no`,
			`pay_id`, 
			`pay_name`, 
			`shipping_fee`, 
			`actual_price`, 
			`integral`, 
			`integral_money`, 
			`order_price`, 
			`goods_price`, 
			`add_time`, 
			`confirm_time`, 
			`pay_time`, 
			`freight_price`, 
			`coupon_id`, 
			`parent_id`, 
			`coupon_price`, 
			`callback_status`,
			`order_type`
		)
		values
		(
			#{orderSn}, 
			#{userId}, 
			#{orderStatus}, 
			#{shippingStatus}, 
			#{payStatus}, 
			#{consignee}, 
			#{country}, 
			#{province}, 
			#{city}, 
			#{district}, 
			#{address}, 
			#{mobile}, 
			#{postscript}, 
			#{shippingId}, 
			#{shippingName},
			#{shippingNo},
			#{payId}, 
			#{payName}, 
			#{shippingFee}, 
			#{actualPrice}, 
			#{integral}, 
			#{integralMoney}, 
			#{orderPrice}, 
			#{goodsPrice}, 
			#{addTime}, 
			#{confirmTime}, 
			#{payTime}, 
			#{freightPrice}, 
			#{couponId}, 
			#{parentId}, 
			#{couponPrice}, 
			#{callbackStatus},
			#{orderType}
		)
	</insert>

    <update id="update" parameterType="com.platform.entity.OrderEntity">
        update nideshop_order
        <set>
            <if test="orderSn != null">`order_sn` = #{orderSn},</if>
            <if test="userId != null">`user_id` = #{userId},</if>
            <if test="orderStatus != null">`order_status` = #{orderStatus},</if>
            <if test="shippingStatus != null">`shipping_status` = #{shippingStatus},</if>
            <if test="payStatus != null">`pay_status` = #{payStatus},</if>
            <if test="consignee != null">`consignee` = #{consignee},</if>
            <if test="country != null">`country` = #{country},</if>
            <if test="province != null">`province` = #{province},</if>
            <if test="city != null">`city` = #{city},</if>
            <if test="district != null">`district` = #{district},</if>
            <if test="address != null">`address` = #{address},</if>
            <if test="mobile != null">`mobile` = #{mobile},</if>
            <if test="postscript != null">`postscript` = #{postscript},</if>
            <if test="shippingId != null">`shipping_id` = #{shippingId},</if>
            <if test="shippingName != null">`shipping_name` = #{shippingName},</if>
            <if test="shippingNo != null">`shipping_no` = #{shippingNo},</if>
            <if test="payId != null">`pay_id` = #{payId},</if>
            <if test="payName != null">`pay_name` = #{payName},</if>
            <if test="shippingFee != null">`shipping_fee` = #{shippingFee},</if>
            <if test="actualPrice != null">`actual_price` = #{actualPrice},</if>
            <if test="integral != null">`integral` = #{integral},</if>
            <if test="integralMoney != null">`integral_money` = #{integralMoney},</if>
            <if test="orderPrice != null">`order_price` = #{orderPrice},</if>
            <if test="goodsPrice != null">`goods_price` = #{goodsPrice},</if>
            <if test="addTime != null">`add_time` = #{addTime},</if>
            <if test="confirmTime != null">`confirm_time` = #{confirmTime},</if>
            <if test="payTime != null">`pay_time` = #{payTime},</if>
            <if test="freightPrice != null">`freight_price` = #{freightPrice},</if>
            <if test="couponId != null">`coupon_id` = #{couponId},</if>
            <if test="parentId != null">`parent_id` = #{parentId},</if>
            <if test="couponPrice != null">`coupon_price` = #{couponPrice},</if>
            <if test="callbackStatus != null">`callback_status` = #{callbackStatus},</if>
            <if test="orderType != null">`order_type` = #{orderType}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		delete from nideshop_order where id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from nideshop_order where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>