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

    <resultMap type="com.kxs.store.api.model.KxsMachineAdvance" id="KxsMachineAdvanceMap">
        <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="brand_id" property="brandId"/>
        <result column="brand_name" property="brandName"/>
        <result column="send_user_id" property="sendUserId"/>
        <result column="to_user_id" property="toUserId"/>
        <result column="warehouse_id" property="warehouseId"/>
        <result column="warehouse_name" property="warehouseName"/>

    </resultMap>
    <select id="getByPage" resultType="com.kxs.store.api.vo.kxsapp.store.StoreAdvanceMachineVO">
        select a.id, a.create_time, info.is_use_amount, info.pos_sn from kxs_machine_advance a
        left join kxs_machine_advance_info info on info.order_id = a.id
        <where>
            and a.del_flag = 0
            and info.is_confirm = 1
            and info.is_return = 0
            and info.is_apply = 0
            and a.warehouse_id = #{warehouseId}
            and a.brand_id = #{brandId}
        </where>

    </select>
    <select id="getUserWareHouseList" resultType="com.kxs.store.api.vo.kxsapp.storeAdvance.UserWarehouseListVO">
        select to_user_id as user_id
        from (select to_user_id
              from kxs_machine_advance
              where del_flag = 0 and warehouse_id = #{userId}
              group by to_user_id) as warehouse_user


    </select>
    <select id="selectUserByCount" resultType="java.lang.Long">
        select count(b.id)
        from kxs_machine_advance a
                 left join kxs_machine_advance_info b on a.id = b.order_id
        where a.del_flag = 0
          and b.is_apply = 0
          and b.is_return = 0
          and b.is_confirm in (0, 1)
          and a.to_user_id = #{userId}
    </select>
    <select id="selectToUserList" resultType="java.util.Map">
        select a.id,
               a.brand_id      as brandId,
               a.brand_name    as brandName,
               b.id            as infoId,
               b.pos_sn        as posSn,
               b.is_use_amount as isUseAmount,
               b.create_time   as createTime,
               b.is_confirm    as isConfirm
        from kxs_machine_advance a
                 left join kxs_machine_advance_info b on a.id = b.order_id
        where a.del_flag = 0
          and a.send_user_id = #{sendUserId}
          and a.to_user_id = #{toUserId}
          and b.is_apply = 0
          and b.is_return = 0
          and b.is_confirm = 1
    </select>

    <resultMap type="com.kxs.store.api.vo.kxsapp.userAdvance.AdvanceDevModalVO" id="AdvanceDevModalMap">
        <result column="id" property="id"/>
        <result column="warehouse_id" property="warehouseId"/>
        <result column="warehouse_name" property="warehouseName"/>
        <result column="brand_name" property="brandName"/>
        <collection property="snList" ofType="com.kxs.store.api.vo.kxsapp.userAdvance.AdvanceDevModalInfoVO">
            <result column="pos_sn" property="posSn"/>
        </collection>
    </resultMap>
    <select id="selectToUserNeedConfirmList" resultMap="AdvanceDevModalMap">

        select a.id,
               a.brand_name,
               a.warehouse_id,
               a.warehouse_name,
               b.pos_sn
        from kxs_machine_advance a
                 left join kxs_machine_advance_info b on a.id = b.order_id
        where a.del_flag = 0
          and a.to_user_id = #{toUserId}
          and b.is_apply = 0
          and b.is_return = 0
          and b.is_confirm = 0
    </select>

    <resultMap type="com.kxs.store.api.vo.kxsapp.userAdvance.UserAdvanceListVO" id="UserAdvanceListMap">
        <result column="id" property="id"/>
        <result column="brand_name" property="brandName"/>
        <collection property="snList" ofType="com.kxs.store.api.vo.kxsapp.userAdvance.UserAdvanceListInfoVO">
            <result column="create_time" property="createTime"/>
            <result column="is_use_amount" property="isUseAmount"/>
            <result column="pos_sn" property="posSn"/>
            <result column="is_advance" property="isAdvance"/>
        </collection>
    </resultMap>
    <select id="advanceUserList" resultMap="UserAdvanceListMap">
        select a.id,
               a.brand_name,
               b.pos_sn,
               b.create_time,
               b.is_use_amount,
               b.is_advance
        from kxs_machine_advance a
                 left join kxs_machine_advance_info b on a.id = b.order_id
        where a.del_flag = 0
          and a.to_user_id = #{userId}
          and b.is_apply = 0
          and b.is_return = 0
          and b.is_confirm = 1

    </select>
    <select id="selectWarehouseUserCount" resultType="java.lang.Long">
        select count(a.id)
        from kxs_machine_advance a
                 left join kxs_machine_advance_info b on a.id = b.order_id
        where a.del_flag = 0
          and a.warehouse_id = #{warehouseId}
          and b.is_apply = 0
          and b.is_return = 0
          and b.is_confirm = 1
    </select>

</mapper>
