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.ta.pddserver.entity.LogOrderMailDetailIoEntity;
import org.ta.pddserver.model.api.request.LogOrderMailDetailIoRequest;
import org.ta.pddserver.model.api.response.LogOrderMailDetailIoListResponse;
import org.ta.pddserver.model.api.response.MailDetailIoSelectResponse;

import java.util.List;

@Mapper
public interface LogOrderMailDetailIoMapper extends BaseMapper<LogOrderMailDetailIoEntity> {

    @Select("SELECT count(t.uid) FROM ( SELECT a.uid  FROM  `log_order_mail_detail_io` a WHERE  a.`logisticsOrderCode` LIKE '%${logisticsOrderCode}%'  AND `mailNo` LIKE '%${mailNo}%'  AND `status` = #{status} ) t")
    Integer countUsable(LogOrderMailDetailIoRequest param);

    @Select("SELECT `uid`, `created`,`status`, `logisticsOrderCode`, `expressCode`, `mailNo`, `action`  FROM `log_order_mail_detail_io` WHERE  `logisticsOrderCode` LIKE '%${logisticsOrderCode}%' AND `mailNo` LIKE '%${mailNo}%'  AND `status` = #{status} ORDER BY uid DESC LIMIT #{offset}, #{size}; ")
    List<LogOrderMailDetailIoListResponse> getListByRequest(LogOrderMailDetailIoRequest param);

    @Select("SELECT `expressCode`, `mailNo` FROM `log_order_mail_detail_io` WHERE `logisticsOrderCode` = #{orderCode} AND `status` = 60 AND `action` = 'FINISHOUTBOUND';")
    List<LogOrderMailDetailIoEntity> findOutByOrderCode(String orderCode);

    @Select("SELECT `uid`, `created`,`status`, `logisticsOrderCode`, `expressCode`, `mailNo`, `action` FROM `log_order_mail_detail_io` WHERE `mailNo` = #{mailNo} AND `status` = 60 AND `action` = 'FINISHOUTBOUND';")
    LogOrderMailDetailIoEntity findOutboundByMailNo(String mailNo);

    @Select("SELECT `uid`, `created`, `updated`, `logisticsOrderCode`, `expressCode`, `action` FROM `log_order_mail_detail_io` WHERE `mailNo` = #{mailNo} AND `action` = #{action}")
    MailDetailIoSelectResponse findInfoByMailNoAction(@Param("mailNo") String mailNo, @Param("action") String action);
}
