<?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.yc.ship.module.otc.dal.mysql.storetradelog.StoreTradeLogMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <delete id="deleteStoreTradeLogById">
        DELETE FROM otc_agency_store_trade_log WHERE id = #{id}
    </delete>


    <select id="getStoreTradeLogById" resultType="com.yc.ship.module.otc.controller.admin.storetradelog.vo.StoreTradeLogRespVO">
        SELECT t1.*,t2.order_no orderNo
        FROM otc_agency_store_trade_log t1
        LEFT JOIN trade_order t2 ON t1.order_id = t2.id
        WHERE t1.id = #{id} LIMIT 1
    </select>

    <select id="selectStorePage"  resultType="com.yc.ship.module.otc.controller.admin.storetradelog.vo.StoreTradeLogRespVO">
        SELECT t1.*,t2.order_no orderNo,t3.store_name storeName
        FROM otc_agency_store_trade_log t1
        LEFT JOIN trade_order t2 ON t1.order_id = t2.id
        INNER join otc_agency_store t3 on t1.store_id = t3.id
        where t1.deleted =0
        <if test="param.storeId != null and param.storeId != ''">
            AND t1.store_id = #{param.storeId}
        </if>
        <if test="param.distributorId != null and param.distributorId != ''">
            AND t1.distributor_id = #{param.distributorId}
        </if>
        <if test="param.tradeName != null and param.tradeName != ''">
        AND t1.trade_name = #{param.tradeName}
    </if>
        <if test="param.type != null and param.type != '' and param.type=='1'">
            AND t1.trade_type in (1, 3, 5, 7)
        </if>
        <if test="param.type != null and param.type != '' and param.type=='2'">
            AND t1.trade_type in (2, 4, 6, 8)
        </if>
        <if test="param.tradeType != null and param.tradeType != ''">
            AND t1.trade_type = #{param.tradeType}
        </if>
        <if test="param.tradeAmount != null and param.tradeAmount != ''">
            AND t1.trade_amount = #{param.tradeAmount}
        </if>
        <if test="param.tradeMode != null and param.tradeMode != ''">
            AND t1.trade_mode = #{param.tradeMode}
        </if>
        <if test="param.orderNo != null and param.orderNo != ''">
            AND t2.order_no = #{param.orderNo}
        </if>
        <if test="param.createTime != null">
            AND t1.create_time BETWEEN #{param.createTime[0]} AND #{param.createTime[1]}
        </if>
        order by t1.create_time desc
    </select>
</mapper>
