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.pdd.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`, `created`, `req`, `resp` FROM `log_order_mail_detail_io` WHERE `logisticsOrderCode` = #{orderCode} AND `status` = 60 AND `action` = 'FINISHOUTBOUND';")
    List<LogOrderMailDetailIoEntity> findOutByOrderCode(@Param("orderCode") String orderCode);

    @Select("SELECT * 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);

    @Select("SELECT COUNT(*) FROM(SELECT `uid` FROM `log_order_mail_detail_io` WHERE `action` = 'FINISHOUTBOUND' GROUP BY `logisticsOrderCode`) a;")
    Long countOutboundOrder();

    @Select("SELECT COUNT(*) FROM(SELECT `uid` FROM `log_order_mail_detail_io` WHERE `action` = 'FINISHOUTBOUND' AND `created` > #{date} GROUP BY `logisticsOrderCode`) a;")
    Long countOutboundOrderFromDate(@Param("date") String date);

    @Select("SELECT COUNT(*) FROM `log_order_mail_detail_io` WHERE `action` = 'FINISHOUTBOUND';")
    Long countOutboundPackage();

    @Select("SELECT COUNT(*) FROM `log_order_mail_detail_io` WHERE `action` = 'FINISHOUTBOUND' AND `created` > #{date};")
    Long countOutboundPackageDate(String today);

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

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

    @Select("SELECT `uid`, `created`, `updated`, `state`, `remarks`, `status`, `logisticsOrderCode`, `expressCode`, `mailNo`, `action`, `wxOrderCode`, `upMailNo` FROM `log_order_mail_detail_io` WHERE `req` LIKE '%${keyword}%';")
    List<LogOrderMailDetailIoEntity> findByReqKeyword(@Param("keyword") String keyword);
}
