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.pddserver.entity.LogOrderMailDetailEntity;
import org.ta.pddserver.model.api.request.LogOrderListRequest;
import org.ta.pddserver.model.api.request.LogOrderMailDetailListRequest;
import org.ta.pddserver.model.api.response.LogOrderListResponse;
import org.ta.pddserver.model.api.response.LogOrderMailDetailListResponse;
import org.ta.pddserver.model.api.response.MailNoResponse;

import java.util.List;

@Mapper
public interface LogOrderMailDetailMapper extends BaseMapper<LogOrderMailDetailEntity> {

    @Select("SELECT * FROM `log_order_mail_detail` WHERE `logisticsOrderCode` = #{logisticsOrderCode} AND `state` > 0;")
    List<LogOrderMailDetailEntity> getListByCode(String logisticsOrderCode);

    @Select("SELECT * FROM `log_order_mail_detail` WHERE `mailNo` = #{mailNo} AND `state` > 0;")
    LogOrderMailDetailEntity findByMailNo(String mailNo);


    @Select("SELECT count(t.uid) FROM ( SELECT a.uid  FROM  `log_order_mail_detail` a WHERE  `logisticsOrderCode` LIKE '%${logisticsOrderCode}%'  AND `mailNo` LIKE '%${mailNo}%' AND `status` LIKE '%${status}%'  AND `state` > 0  ) t")
    Integer countUsable(LogOrderMailDetailListRequest param);

    @Select("SELECT `uid`,`updated`,`status`,`logisticsOrderCode` , `expressCode` , `mailNo`, `length` , `width` , `height`, `weight` FROM `log_order_mail_detail` WHERE `logisticsOrderCode` LIKE '%${logisticsOrderCode}%'  AND `mailNo` LIKE '%${mailNo}%' AND `status` LIKE '%${status}%'  AND `state` > 0 ORDER BY uid DESC LIMIT #{offset}, #{size};")
    List<LogOrderMailDetailListResponse> getListByCodeNo(LogOrderMailDetailListRequest param);



    @Select("SELECT `uid`, `logisticsOrderCode` , `expressCode` ,`mailNo`, `updated`, `status` FROM `log_order_mail_detail` WHERE `mailNo` = #{mailNo} ;")
    MailNoResponse getInfoByMailNo(@Param("mailNo") String mailNo);

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

    @Select("SELECT COUNT(*) FROM  `log_order_mail_detail` WHERE `logisticsOrderCode` = #{logisticsOrderCode} AND `status` = ${status};")
    Long getSumByCodeStatus(@Param("logisticsOrderCode") String logisticsOrderCode, @Param("status") Long status);
}
