package org.ta.pddserver.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
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 org.ta.pdd.entity.OutboundNoticeEntity;
import org.ta.pddserver.model.api.request.OutboundNoticeListRequest;
import org.ta.pddserver.model.api.response.OutboundNoticeListResponse;

import java.util.List;

@Mapper
public interface OutboundNoticeMapper extends BaseMapper<OutboundNoticeEntity> {
    // 根据用户名查找用户
    @Select("SELECT * FROM `outbound_notice` WHERE `username` = #{username};")
    List<OutboundNoticeEntity> selectByUsername(String username);

    @Select("SELECT count(`uid`) FROM `outbound_notice` WHERE `status` = 1")
    Integer countUsable(OutboundNoticeListRequest param);

    @Select("SELECT `orderCode`, `status` FROM `outbound_notice` WHERE `status` = 1 ORDER BY `uid` DESC LIMIT #{offset}, #{size}; ")
    List<OutboundNoticeListResponse> getListByRequest(OutboundNoticeListRequest param);

    @Update("UPDATE `outbound_notice` SET `status` = ${status} WHERE `orderCode` = #{orderCode} ")
    void updateStatusByOrderCode(@Param("orderCode") String orderCode, @Param("status") int status);

    @Select("SELECT * FROM `outbound_notice` WHERE `orderCode` = #{orderCode}")
    OutboundNoticeEntity findByOrderCode(String orderCode);

    @Select("SELECT COUNT(*) FROM `outbound_notice` WHERE `created` > #{date}")
    Long countFromDate(String today);
}
