package com.erp.skumatch.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.skumatch.entity.PlatformSku;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface PlatformSkuRepository extends BaseMapper<PlatformSku> {
    
    @Select("SELECT * FROM platform_sku WHERE erp_sku_id IS NULL AND status = 'onsale' ORDER BY updated_at DESC")
    IPage<PlatformSku> selectUnmatchedPage(Page<PlatformSku> page);
    
    @Select("SELECT * FROM platform_sku WHERE erp_sku_id IS NULL AND status = 'onsale' AND platform_code = #{platformCode} ORDER BY updated_at DESC")
    IPage<PlatformSku> selectUnmatchedByPlatform(Page<PlatformSku> page, @Param("platformCode") String platformCode);
    
    @Select("SELECT * FROM platform_sku WHERE erp_sku_id IS NOT NULL AND status = 'onsale' ORDER BY updated_at DESC")
    IPage<PlatformSku> selectMatchedPage(Page<PlatformSku> page);
    
    @Select("<script>" +
            "SELECT * FROM platform_sku WHERE erp_sku_id IS NOT NULL AND status = 'onsale' " +
            "<if test='platform != null and platform != \"\"'> AND platform_code = #{platform} </if>" +
            "<if test='keyword != null and keyword != \"\"'> AND (sku_name LIKE CONCAT('%', #{keyword}, '%') OR sku_code LIKE CONCAT('%', #{keyword}, '%')) </if>" +
            "ORDER BY updated_at DESC" +
            "</script>")
    IPage<PlatformSku> selectMatchedByCondition(Page<PlatformSku> page, @Param("platform") String platform, @Param("keyword") String keyword);
    
    @Select("SELECT * FROM platform_sku WHERE shop_id = #{shopId} ORDER BY updated_at DESC")
    List<PlatformSku> selectByShopId(@Param("shopId") Long shopId);
    
    @Select("<script>" +
            "SELECT * FROM platform_sku WHERE erp_sku_id IS NULL AND status = 'onsale' " +
            "<if test='platform != null and platform != \"\"'> AND platform_code = #{platform} </if>" +
            "<if test='shopId != null'> AND shop_id = #{shopId} </if>" +
            "<if test='keyword != null and keyword != \"\"'> AND (sku_name LIKE CONCAT('%', #{keyword}, '%') OR sku_code LIKE CONCAT('%', #{keyword}, '%')) </if>" +
            "</script>")
    IPage<PlatformSku> selectUnmatchedByCondition(Page<PlatformSku> page, @Param("platform") String platform, @Param("shopId") Long shopId, @Param("keyword") String keyword);
}
