package shop.alien.mapper.second;


import com.alibaba.fastjson2.JSONObject;
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 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 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_record goods on goods.id = trade.goods_record_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, trade.failure_flag, trade.failure_reason, if (now() >= trade.transaction_time, 1, 0) timeOutFlag, " +
            "buyer.user_name buyerName, buyer.user_phone buyerPhone, seller.user_name sellerName, seller.user_phone sellerPhone " +
            "from second_trade_record trade " +
            "left join second_goods_record goods on goods.id = trade.goods_record_id " +
            "left join life_user buyer on buyer.id = trade.buyer_id " +
            "left join life_user seller on seller.id = trade.seller_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);

    @Select("with record as ( " +
            "    select buyer_id userId, count(1) num, 'buyer' flag, max(transaction_time) time " +
            "    from second_trade_record " +
            "    where delete_flag = 0 and trade_status = 4 and failure_flag = 0 " +
            "    and transaction_time >= #{beginTime} and transaction_time <= #{endTime} " +
            "    group by buyer_id " +
            "    union all " +
            "    select seller_id userId, count(1) num, 'seller' flag, max(transaction_time) time " +
            "    from second_trade_record " +
            "    where delete_flag = 0 and trade_status = 4 and failure_flag = 0 " +
            "    and transaction_time >= #{beginTime} and transaction_time <= #{endTime} " +
            "    group by seller_id " +
            ") " +
            "select a.*, ifnull(b.num, 0) buyer, ifnull(c.num, 0) seller, user.user_name userName, concat('user_', user.user_phone) phoneId, user.user_phone userPhone " +
            "from ( " +
            "    select userId, sum(num) num, max(time) time " +
            "    from record " +
            "    group by userId " +
            ") a " +
            "left join record b on a.userId = b.userId and b.flag = 'buyer' " +
            "left join record c on a.userId = c.userId and c.flag = 'seller' " +
            "join life_user user on user.id = a.userId and user.delete_flag = 0 " +
            "order by a.num desc, a.time " +
            "limit 20 ")
    List<JSONObject> getRankingList(@Param("beginTime") String beginTime, @Param("endTime") String endTime);
}
