<?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.fzm.mall.server.front.asset.mapper.TranMapper">
    <resultMap id="PageResultMap" type="com.fzm.mall.server.front.asset.model.vo.GoodsTransVO">
        <id column="id" property="id" />
        <result column="uid" property="uid" />
        <result column="coin_name" property="coin" />
        <result column="price" property="price" />
        <result column="goods_id" property="goodsId" />
        <result column="sku_id" property="skuId" />
        <result column="coin_type" property="coinType" />
        <result column="status" property="status" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="goods_name" property="goodsName" />
        <result column="nickName" property="nickName" />
        <result column="tranType" property="type" />
        <result column="tranNum" property="number" />
        <result column="note" property="note" />
        <result column="tran_id" property="tranId" />
        <result column="saleThumb" property="saleThumb" />
        <result column="tranThumb" property="thumb" />
        <result column="category_id" property="categoryId" />
    </resultMap>

    <resultMap id="ResultMap" type="com.fzm.mall.server.front.asset.model.vo.UserAssetTransferVO">
        <id column="id" property="id" />
        <result column="tran_id" property="tranId" />
        <result column="uid" property="uid" />
        <result column="coin" property="coin" />
        <result column="stock" property="stock" />
        <result column="number" property="number" />
        <result column="type" property="type" />
        <result column="thumb" property="thumb" />
        <result column="price" property="price" />
        <result column="note" property="note" />
        <result column="goods_id" property="goodsId" />
        <result column="sku_id" property="skuId" />
        <result column="status" property="status" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="goods_name" property="goodsName" />
        <result column="head_url" property="uHeadUrl" />
        <result column="nick_name" property="uNickName" />
        <collection property="propValList" javaType="java.util.ArrayList"
                    ofType="string">
            <result column="prop_val" property="propVal"/>
        </collection>
    </resultMap>

    <select id="listByUid" resultMap="ResultMap">
        select t.*, s.name as goods_name, sp.prop_val, sk.thumb, u.head_url, u.nick_name
        from tran t
        left join goods_spu s on t.goods_id = s.goods_id
        left join goods_sku sk on sk.goods_id = t.goods_id and sk.sku_id = t.sku_id
        left join goods_sku_prop sp on sk.sku_id = sp.sku_id
        left join `user` u on t.uid = u.uid
        <where>
            t.uid = #{uid} and t.status = #{status}
            <if test="keyword != null and keyword != ''">
                and (
                s.name like CONCAT('%', #{keyword},'%')
                or
                t.coin like CONCAT('%', #{keyword},'%')
                )
            </if>
        </where>
        order by t.create_time desc
    </select>

    <select id="read" resultType="com.fzm.mall.server.front.asset.model.vo.UserAssetTransferVO">
        select t.*, s.name as goods_name, sk.thumb, u.head_url as uHeadUrl, u.nick_name as uNickName
        from tran t
        left join goods_spu s on t.goods_id = s.goods_id
        left join goods_sku sk on sk.goods_id = t.goods_id and sk.sku_id = t.sku_id
        left join `user` u on t.uid = u.uid
        where t.tran_id = #{tranId}
    </select>

    <select id="list" resultMap="PageResultMap">
        select distinct t.*,t.number tranNum,t.type tranType,u.nick_name nickName,u.head_url saleThumb, s.name as goods_name,sk.thumb tranThumb,sk.coin_name
        from tran t
        left join goods_spu s on t.goods_id = s.goods_id
        left join goods_sku sk on sk.goods_id = t.goods_id and sk.sku_id = t.sku_id
        left join `user` u on u.uid = t.uid
        <where>
            <if test="keyword != null and keyword != ''">
                and (
                s.name like CONCAT('%', #{keyword},'%')
                or
                sk.coin_name like CONCAT('%', #{keyword},'%')
                )
            </if>
        </where>
        and t.status = #{status}
        order by t.id desc,t.create_time desc
    </select>

    <select id="pageSerach" resultMap="PageResultMap">
        select t.*,t.number tranNum,t.type tranType,u.nick_name nickName,u.head_url saleThumb, s.name as goods_name,sk.thumb tranThumb,sk.coin_name
        from tran t
        left join goods_spu s on t.goods_id = s.goods_id
        left join goods_sku sk on sk.goods_id = t.goods_id and sk.sku_id = t.sku_id
        left join `user` u on u.uid = t.uid
        left join goods_count b on s.goods_id = b.goods_id
        <where>
            <if test="keyword != null and keyword != ''">
                and (
                s.name like CONCAT('%', #{keyword},'%')
                or
                sk.coin_name like CONCAT('%', #{keyword},'%')
                )
            </if>
            <if test="goodsIds != null and goodsIds != ''">
                and  t.goods_id in (${goodsIds})
            </if>
        </where>
        and t.status = #{status}
        order by s.default_price ${price},b.sales ${sale},t.create_time desc
    </select>
</mapper>
