package shop.alien.mapper.second;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.second.SecondTradeRecord;
import shop.alien.entity.second.vo.SecondTradeRecordVo;
import shop.alien.entity.store.vo.LifeFansVo;

import java.util.List;

/**
 * <p>
 * 二手交易记录表 Mapper 接口
 * </p>
 *
 * @author qrs
 * @since 2025-07-07
 */
@Mapper
public interface SecondTradeRecordMapper extends BaseMapper<SecondTradeRecord> {

    @Select("select trade.id, goods.title, goods.home_image, goods.price, trade.transaction_amount, trade.trade_status, trade.transaction_time, trade.seller_id, " +
            "       trade.transaction_latitude_longitude, trade.transaction_latitude_longitude_address, trade.transaction_location, trade.created_user_id, " +
            "       if (now() >= trade.transaction_time, 1, 0) timeOutFlag " +
            "from second_trade_record trade " +
            "left join second_goods goods on goods.id = trade.goods_id " +
            "${ew.customSqlSegment} ")
    List<SecondTradeRecordVo> getTradeRecord(@Param(Constants.WRAPPER) QueryWrapper<SecondTradeRecord> wrapper);

    @Select("select trade.id, trade.trade_no, trade.goods_id, goods.title, goods.home_image, goods.price, trade.transaction_amount, trade.trade_status, " +
            "trade.transaction_time, trade.seller_id, trade.transaction_latitude_longitude, trade.transaction_latitude_longitude_address, " +
            "trade.transaction_location, trade.created_user_id, if (now() >= trade.transaction_time, 1, 0) timeOutFlag " +
            "from second_trade_record trade " +
            "left join second_goods goods on goods.id = trade.goods_id " +
            "${ew.customSqlSegment} ")
    IPage<SecondTradeRecordVo> getTradeRecordPage(IPage<SecondTradeRecord> page, @Param(Constants.WRAPPER) QueryWrapper<SecondTradeRecord> wrapper);

    @Select("select trade.*, buyer.user_name buyerName, buyer.user_phone buyerPhone, seller.user_name sellerName, seller.user_phone sellerPhone " +
            "from second_trade_record trade " +
            "left join life_user buyer on buyer.id = trade.buyer_id " +
            "left join life_user seller on seller.id = trade.seller_id " +
            "where trade.id = #{id}")
    SecondTradeRecordVo getTradeRecordById(@Param("id") Integer id);
}
