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.SkuMatchRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface SkuMatchRecordRepository extends BaseMapper<SkuMatchRecord> {
    
    @Select("<script>" +
            "SELECT * FROM sku_match_record WHERE matched = 1 " +
            "<if test='platform != null and platform != \"\"'> AND EXISTS (SELECT 1 FROM platform_sku ps WHERE ps.id = sku_match_record.platform_sku_id AND ps.platform_code = #{platform}) </if>" +
            "<if test='matchType != null and matchType != \"\"'> AND match_type = #{matchType} </if>" +
            "<if test='keyword != null and keyword != \"\"'> AND (platform_sku_name LIKE CONCAT('%', #{keyword}, '%') OR platform_sku_code LIKE CONCAT('%', #{keyword}, '%') OR erp_sku_name LIKE CONCAT('%', #{keyword}, '%') OR erp_sku_code LIKE CONCAT('%', #{keyword}, '%')) </if>" +
            "ORDER BY updated_at DESC" +
            "</script>")
    IPage<SkuMatchRecord> selectMatchList(Page<SkuMatchRecord> page, @Param("platform") String platform, @Param("matchType") String matchType, @Param("keyword") String keyword);
}
