package com.erp.platformsync.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.erp.platformsync.entity.OrderPullConfig;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单拉取配置Mapper
 */
@Mapper
public interface OrderPullConfigMapper extends BaseMapper<OrderPullConfig> {

    /**
     * 查询启用的配置列表
     */
    @Select("SELECT * FROM order_pull_config WHERE enabled = 1 AND deleted_at IS NULL ORDER BY id")
    List<OrderPullConfig> findAllEnabled();

    /**
     * 查询店铺的配置
     */
    @Select("SELECT * FROM order_pull_config WHERE platform = #{platform} AND shop_id = #{shopId} AND deleted_at IS NULL")
    List<OrderPullConfig> findByPlatformAndShopId(@Param("platform") String platform, @Param("shopId") Long shopId);

    /**
     * 更新状态
     */
    @Update("UPDATE order_pull_config SET status = #{status}, updated_at = #{now} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") String status, @Param("now") LocalDateTime now);

    /**
     * 更新上次拉取时间
     */
    @Update("UPDATE order_pull_config SET last_pull_time = #{lastPullTime}, next_pull_time = #{nextPullTime}, updated_at = #{now} WHERE id = #{id}")
    int updatePullTime(@Param("id") Long id, @Param("lastPullTime") LocalDateTime lastPullTime, 
                       @Param("nextPullTime") LocalDateTime nextPullTime, @Param("now") LocalDateTime now);

    /**
     * 更新上次成功拉取时间
     */
    @Update("UPDATE order_pull_config SET last_success_time = #{lastSuccessTime}, updated_at = #{now} WHERE id = #{id}")
    int updateLastSuccessTime(@Param("id") Long id, @Param("lastSuccessTime") LocalDateTime lastSuccessTime, @Param("now") LocalDateTime now);
}
