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 org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.second.SecondGoods;
import shop.alien.entity.second.vo.SecondGoodsVo;
import shop.alien.entity.second.vo.SellGoodsVo;

import java.util.List;

/**
 * 二手商品映射器
 */
public interface SecondGoodsMapper extends BaseMapper<SecondGoods> {
    /**
     * 自定义分页查询
     */
    List<SecondGoods> getSecondGoodsByPage(IPage<SecondGoods> page);
    
    /**
     * 自定义分页查询并按距离排序
     * @param page 分页信息
     * @param currentLatitude 当前位置纬度
     * @param currentLongitude 当前位置经度
     * @return 分页后的二手商品列表
     */
    List<SecondGoods> getSecondGoodsByPageAndDistance(IPage<SecondGoods> page, @Param("currentLatitude") Double currentLatitude, @Param("currentLongitude") Double currentLongitude);

    /**
     * 查询用户屏蔽的商品列表
     * @param page 分页参数
     * @param userId 用户ID
     * @return 分页后的屏蔽商品列表
     */
    IPage<SecondGoods> getShieldedGoodsListByType(IPage<SecondGoods> page, @Param("userId") Integer userId, @Param("shieldType") Integer shieldType);

    /**
     * 获取用户屏蔽的商品列表（分页）
     * @param page 分页参数
     * @param queryWrapper 查询条件
     * @return 屏蔽的商品列表
     */
    @Select("SELECT sg.* , ss.id as shieldId " +
            "FROM second_goods sg " +
            "INNER JOIN second_shield ss ON " +
            "(sg.id = ss.shield_id AND ss.shield_type = 1) "+
            "${ew.customSqlSegment}")
    IPage<SecondGoodsVo>  getShieldedGoodsPage(IPage<SecondGoodsVo> page, @Param(Constants.WRAPPER) QueryWrapper<SecondGoodsVo> queryWrapper);

    /**
     * 查询商品热卖排行榜
     * @param page 分页参数
     * @return 商品热卖排行榜列表
     */
    IPage<SecondGoods> getHotSellingRanking(IPage<SecondGoods> page);

    /**
     * 查询商品热卖排行榜（前10名），按标签聚合计算总点赞量
     * @return 热卖排行榜列表
     */
    List<SecondGoods> getHotSellingRankingTop10();

    /**
     * 获取商品收藏排行榜（前10名），按标签聚合计算总收藏量
     * @return 收藏排行榜列表
     */
    List<SecondGoods> getCollectTop10();

    /**
     * 商品列表
     * @return 结果列表
     */
    @Select("SELECT " +
            "sg.*, " +
            "sgc1.category_name as categoryOneName, " +
            "sgc2.category_name as categoryTwoName "+
            " FROM second_goods sg " +
            "left JOIN second_goods_category sgc1 " +
            "on sg.category_one_id = sgc1.id " +
            "left JOIN second_goods_category sgc2 " +
            "on sg.category_two_id = sgc2.id "+
            "${ew.customSqlSegment}")
    List<SecondGoodsVo> selectGoodsList(@Param(Constants.WRAPPER) QueryWrapper<SecondGoodsVo> queryWrapper);


    /**
     * 搜索结果-商品列表
     * @param page 分页参数
     * @return 搜索结果列表
     */
    @Select("SELECT " +
            "sg.*, " +
            "sgc1.category_name as categoryOneName, " +
            "sgc2.category_name as categoryTwoName, "+
            "ROUND(ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(',#{currentLongitude},' ',#{currentLatitude} , ')' )), ST_GeomFromText(CONCAT('POINT(', REPLACE(sg.position, ',', ' '), ')' ))) / 1000, 2) AS distance, "+
            "CONCAT('user_', u.user_phone) as user_phone"+
            " FROM second_goods sg " +
            "inner join life_user u " +
            "on u.id =  sg.user_id " +
            "and u.delete_flag = 0 " +
            "left JOIN second_goods_category sgc1 " +
            "on sg.category_one_id = sgc1.id " +
            "left JOIN second_goods_category sgc2 " +
            "on sg.category_two_id = sgc2.id "+
            "${ew.customSqlSegment}")
    IPage<SecondGoodsVo> searchGoodsList(IPage<SecondGoodsVo> page,@Param("currentLatitude") Double currentLatitude , @Param("currentLongitude") Double currentLongitude, @Param(Constants.WRAPPER) QueryWrapper<SecondGoodsVo> queryWrapper);

    /**
     * 获取商品详情
     * @param queryWrapper 查询条件
     * @return 商品详情
     */
    @Select("SELECT " + "sg.*, "
            + "sgc1.category_name as categoryOneName, "
            + "sgc2.category_name as categoryTwoName "
            + "FROM second_goods sg "
            + "left JOIN second_goods_category sgc1 "
            + "on sg.category_one_id = sgc1.id "
            + "left JOIN second_goods_category sgc2 "
            + "on sg.category_two_id = sgc2.id " +
            "${ew.customSqlSegment}")
    SecondGoodsVo getGoodsDetails(@Param(Constants.WRAPPER) QueryWrapper<SecondGoodsVo> queryWrapper);

    /**
     * 获取用户屏蔽的商品列表
     * @param userId 用户ID
     * @return 屏蔽的商品列表
     */
    List<SecondGoods> getShieldedGoodsList( @Param("userId") Integer userId);

    /**
     * 获取用户收藏的商品列表
     * @param page 分页参数
     * @param queryWrapper 查询条件
     * @return 收藏的商品列表
     */
    @Select("SELECT " +
            "sg.*, " +
            "sgc1.category_name as categoryOneName, " +
            "sgc2.category_name as categoryTwoName " +
            "FROM second_goods sg " +
            "LEFT JOIN life_collect lc ON sg.id = lc.business_id " +
            "LEFT JOIN second_goods_category sgc1 " +
            "on sg.category_one_id = sgc1.id " +
            "LEFT JOIN second_goods_category sgc2 " +
            "on sg.category_two_id = sgc2.id " +
            "${ew.customSqlSegment}")
    IPage<SecondGoodsVo> getCollectGoodsPage(IPage<SecondGoodsVo> page,  @Param(Constants.WRAPPER) QueryWrapper<SecondGoodsVo> queryWrapper);

    /**
     * 获取用户购买的商品列表
     * @param page 分页参数
     * @param queryWrapper 查询条件
     * @return 购买的商品列表
     */
    @Select("SELECT " +
            "sg.*, " +
            "sgc1.category_name as categoryOneName, " +
            "sgc2.category_name as categoryTwoName " +
            "FROM second_goods sg " +
            "LEFT JOIN second_trade_record str ON sg.id = str.goods_id " +
            "LEFT JOIN second_goods_category sgc1 " +
            "on sg.category_one_id = sgc1.id " +
            "LEFT JOIN second_goods_category sgc2 " +
            "on sg.category_two_id = sgc2.id " +
            "${ew.customSqlSegment}")
    IPage<SecondGoodsVo> getBuyGoodsPage(IPage<SecondGoodsVo> page,  @Param(Constants.WRAPPER) QueryWrapper<SecondGoodsVo> queryWrapper);

    /**
     * 获取用户出售的商品列表
     * @param page 分页参数
     * @param queryWrapper 查询条件
     * @return 出售的商品列表
     */
    @Select("SELECT " +
            "sg.*, " +
            "sgc1.category_name as categoryOneName, " +
            "sgc2.category_name as categoryTwoName " +
            "FROM second_goods sg " +
            "LEFT JOIN second_goods_category sgc1 " +
            "on sg.category_one_id = sgc1.id " +
            "LEFT JOIN second_goods_category sgc2 " +
            "on sg.category_two_id = sgc2.id " +
            "${ew.customSqlSegment}")
    IPage<SecondGoodsVo> getSellGoodsPage(IPage<SecondGoodsVo> page, @Param(Constants.WRAPPER) QueryWrapper<SecondGoodsVo> queryWrapper);

    /**
     * 获取用户商品列表
     * @param page 分页参数
     * @param queryWrapper 查询条件
     * @return 商品列表
     */
    @Select("<script>" +
            "SELECT " +
            "sg.*, " +
            "sgc1.category_name as categoryOneName, " +
            "sgc2.category_name as categoryTwoName, " +
            "<if test=\"currentLatitude != null and currentLongitude != null\">" +
            "ROUND(ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(',#{currentLongitude},' ',#{currentLatitude},')')), ST_GeomFromText(CONCAT('POINT(', REPLACE(sg.position, ',', ' '), ')' ))) / 1000, 2) AS distance " +
            "</if>" +
            "<if test=\"currentLatitude == null or currentLongitude == null\">" +
            "NULL AS distance " +
            "</if>" +
            "FROM second_goods sg " +
            "left JOIN second_goods_category sgc1 " +
            "on sg.category_one_id = sgc1.id " +
            "left JOIN second_goods_category sgc2 " +
            "on sg.category_two_id = sgc2.id " +
            "${ew.customSqlSegment}" +
            "</script>")
    IPage<SecondGoodsVo> getUserGoodsPage(IPage<SecondGoodsVo> page,@Param("currentLatitude") Double currentLatitude , @Param("currentLongitude") Double currentLongitude,  @Param(Constants.WRAPPER) QueryWrapper<SecondGoodsVo> queryWrapper);

    /**
     * 获取我的商品列表
     * @param page 分页参数
     * @param queryWrapper 查询条件
     * @return 商品列表
     */
    @Select("SELECT " +
            "sg.*, " +
            "sgc1.category_name as categoryOneName, " +
            "sgc2.category_name as categoryTwoName "+
//            "ROUND(ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(',#{currentLongitude},' ',#{currentLatitude} , ')' )), ST_GeomFromText(CONCAT('POINT(', REPLACE(sg.position, ',', ' '), ')' ))) / 1000, 2) AS distance "+
            " FROM second_goods sg " +
            "left JOIN second_goods_category sgc1 " +
            "on sg.category_one_id = sgc1.id " +
            "left JOIN second_goods_category sgc2 " +
            "on sg.category_two_id = sgc2.id "+
            "${ew.customSqlSegment}")
    IPage<SecondGoodsVo> getMyGoodsPage(IPage<SecondGoodsVo> page,  @Param(Constants.WRAPPER)  QueryWrapper<SecondGoodsVo> queryWrapper);

    /**
     * 获取我的草稿列表
     * @param page 分页参数
     * @param queryWrapper 查询条件
     * @return 商品列表
     */
    @Select("SELECT " +
            "sg.*, " +
            "sgc1.category_name as categoryOneName, " +
            "sgc2.category_name as categoryTwoName "+
//            "ROUND(ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(',#{currentLongitude},' ',#{currentLatitude} , ')' )), ST_GeomFromText(CONCAT('POINT(', REPLACE(sg.position, ',', ' '), ')' ))) / 1000, 2) AS distance "+
            " FROM second_goods sg " +
            "left JOIN second_goods_category sgc1 " +
            "on sg.category_one_id = sgc1.id " +
            "left JOIN second_goods_category sgc2 " +
            "on sg.category_two_id = sgc2.id "+
            "${ew.customSqlSegment}")
    IPage<SecondGoodsVo> getDraftList(IPage<SecondGoodsVo> page,  @Param(Constants.WRAPPER) QueryWrapper<SecondGoodsVo> queryWrapper);

    /**
     * 获取用户点赞的商品列表
     * @param page 分页参数
     * @param queryWrapper 查询条件
     * @return 点赞的商品列表
     */
    @Select("SELECT " +
            "sg.*, " +
            "sgc1.category_name as categoryOneName, " +
            "sgc2.category_name as categoryTwoName "+
//            "ROUND(ST_Distance_Sphere(ST_GeomFromText(CONCAT('POINT(',#{currentLongitude},' ',#{currentLatitude} , ')' )), ST_GeomFromText(CONCAT('POINT(', REPLACE(sg.position, ',', ' '), ')' ))) / 1000, 2) AS distance "+
            " FROM second_goods sg " +
            "left JOIN life_like_record lc " +
            "on sg.id = lc.huifu_id " +
            "left JOIN second_goods_category sgc1 " +
            "on sg.category_one_id = sgc1.id " +
            "left JOIN second_goods_category sgc2 " +
            "on sg.category_two_id = sgc2.id "+
            "${ew.customSqlSegment}")
    IPage<SecondGoodsVo> getLikeGoodsPage(IPage<SecondGoodsVo> page,  @Param(Constants.WRAPPER) QueryWrapper<SecondGoodsVo> queryWrapper);

    /**
     * 获取用户交易中的商品列表
     * @param page 分页参数
     * @param queryWrapper 查询条件
     * @return 交易中的商品列表
     */
    @Select("SELECT " +
            "str.goods_id, " + // 商品id
            "str.buyer_id, " + // 买家id
            "str.seller_id, " + // 卖家id
            "str.transaction_time, " + // 交易时间
            "str.transaction_latitude_longitude, " + // 交易地点(经纬度)
            "str.transaction_latitude_longitude_address, " + // 交易地点(经纬度地址)
            "str.transaction_location, " + // 交易地点(详细地址)
            "str.transaction_amount, " + // 交易金额
            "str.buyer_sign_in, " + // 买家是否签到  0-未签到  1-已签到
            "str.buyer_sign_in_time, " + // 买家签到时间
            "str.buyer_sign_in_latitude_longitude, " + // 买家签到地点(经纬度)
            "str.buyer_sign_in_latitude_longitude_address, " + // 买家签到地点(经纬度地址)
            "str.seller_sign_in, " + // 卖家是否签到  0-未签到  1-已签到
            "str.seller_sign_in_time, " + // 卖家签到时间
            "str.seller_sign_in_latitude_longitude, " + // 卖家签到地点(经纬度)
            "str.seller_sign_in_latitude_longitude_address, " + // 卖家签到地点(经纬度地址)
            "str.buyer_transaction_status, " + // 买家交易状态  1-交易成功  2-交易失败
            "str.seller_transaction_status, " + // 卖家交易状态  1-交易成功  2-交易失败
            "str.trade_status, " + // 交易状态  1-待确认  2-已拒绝  3-待交易  4-交易成功  5-交易失败  6-交易取消
            "str.buyer_evaluate, " + // 买家评价
            "str.seller_evaluate, " + // 卖家评价
            "str.cancel_user_id, " + // 取消交易id
            "str.cancel_reason, " + // 取消交易原因
            "str.cancel_reason_supplement, " + // 取消交易原因补充
            "   if (now() >= str.transaction_time, 1, 0) timeOutFlag, "+
            "sg.*, " +
            "    CASE " +
            "        WHEN str.buyer_id = #{userId} THEN str.seller_id " +
            "        ELSE str.buyer_id " +
            "    END AS other_party_id " +
            "FROM second_trade_record str " +
            "LEFT JOIN second_goods sg ON str.goods_id = sg.id "+
            "${ew.customSqlSegment}")
    IPage<SellGoodsVo> getTransactionList(IPage<SellGoodsVo> page, @Param("userId") Integer userId, @Param(Constants.WRAPPER) QueryWrapper<SellGoodsVo> queryWrapper);

    /**
     * 管理后台商品列表查询
     * @param page 分页参数
     * @param queryWrapper 查询条件
     * @return 商品列表
     */
    @Select("SELECT " +
            "sg.*, " +
            "sgc1.category_name as categoryOneName, " +
            "sgc2.category_name as categoryTwoName " +
            "FROM second_goods sg " +
            "left JOIN second_goods_category sgc1 " +
            "on sg.category_one_id = sgc1.id " +
            "left JOIN second_goods_category sgc2 " +
            "on sg.category_two_id = sgc2.id " +
            "${ew.customSqlSegment}")
    IPage<SecondGoodsVo> getAdminGoodsList(IPage<SecondGoodsVo> page, @Param(Constants.WRAPPER) QueryWrapper<SecondGoodsVo> queryWrapper);
    @Select("SELECT " +
            "sg.*, " +
            "lu.user_name as userName, " +
            "lu.user_phone as userPhone, " +
            "sgc1.category_name as categoryOneName, " +
            "sgc2.category_name as categoryTwoName " +
            "FROM second_goods sg " +
            "left JOIN second_goods_category sgc1 " +
            "on sg.category_one_id = sgc1.id " +
            "left JOIN second_goods_category sgc2 " +
            "on sg.category_two_id = sgc2.id " +
            "left JOIN life_user lu " +
            "on lu.id = sg.user_id " +
            "${ew.customSqlSegment}")
    SecondGoodsVo getGoodsById(@Param(Constants.WRAPPER) QueryWrapper<SecondGoodsVo> queryWrapper);

    @Select("SELECT " +
            "sg.*, " +
            "lu.user_name as userName, " +
            "lu.user_phone as userPhone, " +
            "sgc1.category_name as categoryOneName, " +
            "sgc2.category_name as categoryTwoName " +
            "FROM second_goods_record sg " +
            "join second_goods goods on goods.id = sg.goods_id " +
            "left JOIN second_goods_category sgc1 " +
            "on sg.category_one_id = sgc1.id " +
            "left JOIN second_goods_category sgc2 " +
            "on sg.category_two_id = sgc2.id " +
            "left JOIN life_user lu " +
            "on lu.id = goods.user_id " +
            "${ew.customSqlSegment}")
    SecondGoodsVo getGoodsRecordById(@Param(Constants.WRAPPER) QueryWrapper<SecondGoodsVo> queryWrapper);
}