<?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.kxs.store.biz.mapper.KxsMachineRecycleMapper">

    <resultMap type="com.kxs.store.api.model.KxsMachineRecycle" id="KxsMachineRecycleMap">
        <result column="id" property="id"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="del_flag" property="delFlag"/>
        <result column="version" property="version"/>
        <result column="order_sn" property="orderSn"/>
        <result column="user_id" property="userId"/>
        <result column="get_mode" property="getMode"/>
        <result column="warehouse_id" property="warehouseId"/>
        <result column="brand_id" property="brandId"/>
        <result column="brand_name" property="brandName"/>
        <result column="track_no" property="trackNo"/>
        <result column="warehouse_user_id" property="warehouseUserId"/>
        <result column="username" property="username"/>
        <result column="user_code" property="userCode"/>
        <result column="phone" property="phone"/>

    </resultMap>
    <!-- kxs app 回收订单列表分页 map -->
    <resultMap type="com.kxs.store.api.vo.kxsapp.storeRecycle.StoreRecycleMachineListVO" id="KxsRecycleMachineListVO">
        <result column="id" property="id"/>
        <result column="create_time" property="createTime"/>
        <result column="get_mode" property="getMode"/>
        <result column="brand_id" property="brandId"/>
        <result column="brand_name" property="brandName"/>
        <result column="track_no" property="trackNo"/>
        <result column="order_sn" property="orderSn"/>
        <result column="username" property="username"/>
        <result column="user_code" property="userCode"/>
        <result column="phone" property="phone"/>
        <result column="total_number" property="totalNumber"/>
        <collection property="snList" ofType="com.kxs.store.api.vo.kxsapp.storeRecycle.RecycleInfoVO">
            <result column="confirm_status" property="confirmStatus"/>
            <result column="pos_sn" property="posSn"/>
        </collection>
    </resultMap>
    <select id="getByPage" resultMap="KxsRecycleMachineListVO">
        SELECT a.*,
        t.pos_sn, t.status as confirm_status
        FROM (
        SELECT * FROM kxs_machine_recycle
        <where>
            and del_flag = 0
            and warehouse_user_id = #{query.warehouseUserId}
        </where>
        LIMIT #{start}, #{size}
        ) a
        LEFT JOIN kxs_machine_recycle_info t ON a.id = t.order_id
        <where>
            <if test="query.status != null and query.status == 0">
                and t.status = #{query.status}
            </if>
            <if test="query.status != null and query.status == 1">
                and t.status in (1,2)
            </if>
            <if test="query.keyword != null and query.keyword != ''">
                and (t.pos_sn like concat('%',#{query.keyword},'%') or a.user_code like
                concat('%',#{query.keyword},'%'))
            </if>
        </where>
    </select>
    <!-- kxs app 回收订单列表分页 map -->
    <resultMap type="com.kxs.store.api.vo.kxsapp.storeRecycle.RecycleUserListVO" id="KxsRecycleUserListVO">
        <result column="id" property="id"/>
        <result column="create_time" property="createTime"/>
        <result column="get_mode" property="getMode"/>
        <result column="brand_name" property="brandName"/>
        <result column="track_no" property="trackNo"/>
        <result column="order_sn" property="orderSn"/>
        <result column="warehouse_id" property="warehouseId"/>
        <result column="warehouse_name" property="warehouseName"/>
        <result column="contacts" property="contacts"/>
        <result column="warehouse_phone" property="warehousePhone"/>
        <result column="warehouse_address" property="warehouseAddress"/>
        <result column="update_time" property="updateTime"/>
        <collection property="snList" ofType="com.kxs.store.api.vo.kxsapp.storeRecycle.RecycleInfoVO">
            <result column="confirm_status" property="confirmStatus"/>
            <result column="pos_sn" property="posSn"/>
        </collection>
    </resultMap>
    <select id="userList" resultMap="KxsRecycleUserListVO">
        SELECT a.*,
        t.pos_sn, t.status as confirm_status,
        s.warehouse_name, s.contacts, s.warehouse_phone, s.warehouse_address
        FROM (
        SELECT * FROM kxs_machine_recycle
        <where>
            and del_flag = 0
            and user_id = #{userId}
        </where>
        LIMIT #{start}, #{size}
        ) a
        LEFT JOIN kxs_machine_recycle_info t ON a.id = t.order_id
        LEFT JOIN kxs_warehouse s ON a.warehouse_id = s.id
        <where>
            and t.del_flag = 0
        </where>
    </select>
    <select id="needConfirmList" resultType="com.kxs.store.api.vo.kxsapp.storeRecycle.NeedConfirmRecycleVO">
        select a.pos_sn, a.recycle_end_time
        from kxs_machine_recycle_info a
                 left join kxs_machine_recycle b on a.order_id = b.id
        where b.user_id = #{userId}
          and a.status = 0
          and a.brand_id = #{brandId}
    </select>
    <select id="recoverableData" resultType="java.lang.Integer">
        select count(*)
        from kxs_machine_recycle_info a
                 left join kxs_machine_recycle b on a.order_id = b.id
        where b.user_id = #{userId}
          and a.status = 0
          and a.del_flag = 0
    </select>
    <select id="storeRecoverableData" resultType="java.lang.Integer">
        select count(*)
        from kxs_machine_recycle_info a
        left join kxs_machine_recycle b on a.order_id = b.id
        <where>
            and a.del_flag = 0
            and b.warehouse_user_id = #{warehouseUserId}
            <if test="status == 0">
                and a.status = #{status}
            </if>
            <if test="status == 1">
                and a.status in (1,2)
            </if>
        </where>
    </select>
    <select id="recycleUserSnList" resultType="java.lang.String">
        select a.pos_sn
        from kxs_machine_recycle_info a
                 left join kxs_machine_recycle b on a.order_id = b.id
        where b.user_id = #{userId}
    </select>

</mapper>
