package org.ta.pddserver.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.ta.pdd.entity.UnpackNoticeEntity;
import org.ta.pddserver.model.api.request.UnpackNoticeListRequest;
import org.ta.pddserver.model.api.response.UnpackNoticeListResponse;

import java.util.List;

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

    @Select("SELECT * FROM `unpack_notice` WHERE `orderCode` = #{orderCode} ORDER BY `uid` DESC LIMIT 1;")
    UnpackNoticeEntity selectByOrderCode(String orderCode);


    @Select("SELECT count(`uid`) FROM `unpack_notice` WHERE ${str}")
    Integer countUsable(UnpackNoticeListRequest param);

    @Select("SELECT `orderCode`, `status`, `mailDetails`, `num`, `created` FROM `unpack_notice` WHERE ${str} ORDER BY `uid` DESC LIMIT #{offset}, #{size}; ")
    List<UnpackNoticeListResponse> getListByRequest(UnpackNoticeListRequest param);

    @Select("SELECT `orderCode`, `status`, `num`, `created` FROM `unpack_notice` WHERE `status` = 1 AND `num` = 0 ORDER BY `uid` ASC; ")
    List<UnpackNoticeListResponse> getUnprintListByRequest(UnpackNoticeListRequest param);

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

    @Update("UPDATE `unpack_notice` SET `num` = `num` + #{num} WHERE `uid` = #{uid};")
    int updatePrintNum(@Param("num") int num, @Param("uid") String uid);

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

    @Delete("DELETE FROM `unpack_notice` WHERE `orderCode` = #{orderCode}")
    Integer deleteByOrderCode(@Param("orderCode") String orderCode);
}
