package org.ta.pddserver.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.ta.pdd.entity.CollectMailDetailEntity;
import org.ta.pddserver.model.api.request.CollectMailDetailListRequest;
import org.ta.pddserver.model.api.response.CollectMailDetailListResponse;

import java.util.List;

@Mapper
public interface CollectMailDetailMapper extends BaseMapper<CollectMailDetailEntity> {
    @Select("SELECT * FROM `collect_mail_detail` WHERE `orderCode` = #{orderCode} AND `expressCode` = #{expressCode} AND `mailNo` = #{mailNo};")
    CollectMailDetailEntity selectByOrderCodeAndExpressCodeAndMailNo(@Param("orderCode") String orderCode, @Param("expressCode") String expressCode, @Param("mailNo") String mailNo);

    @Update("UPDATE `collect_mail_detail` SET `status` = #{status} WHERE `uid` = #{uid};")
    int updateStatusByUid(@Param("status") int status, @Param("uid") String uid);

    @Update("UPDATE `collect_mail_detail` SET `status` = #{status} WHERE `mailNo` = #{mailNo}")
    Boolean updateByMailNo(@Param("mailNo") String mailNo, @Param("status") int status);



    @Select("SELECT count(*) FROM `collect_mail_detail` WHERE `orderCode` LIKE '%${orderCode}%' AND `mailNo` LIKE '%${mailNo}%' AND `status` LIKE '%${status}%';")
    Integer countUsable(CollectMailDetailListRequest collectMailDetailListRequest);

    @Select("SELECT `uid`, `orderCode`, `expressCode`, `mailNo` FROM `collect_mail_detail` WHERE `orderCode` LIKE '%${orderCode}%' AND `mailNo` LIKE '%${mailNo}%' AND `status` LIKE '%${status}%' ORDER BY uid DESC LIMIT #{offset}, #{size}; ")
    List<CollectMailDetailListResponse> getListByRequest(CollectMailDetailListRequest collectMailDetailListRequest);

    @Select("SELECT * FROM `collect_mail_detail` WHERE `orderCode` = #{orderCode}")
    List<CollectMailDetailEntity> findListByOrderCode(@Param("orderCode") String orderCode);

    @Select("SELECT * FROM `collect_mail_detail` WHERE `mailNo` = #{mailNo} ORDER BY `uid` DESC LIMIT 1;")
    CollectMailDetailEntity findByMailNo(@Param("mailNo") String mailNo);

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

    @Update("UPDATE `collect_mail_detail` SET `state` = 2, `remarks` = '已经申报问题件' WHERE `mailNo` = #{mailNo};")
    Integer updateMailToProblem(@Param("mailNo") String mailNo);
}
