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.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 `uid`, `logisticsOrderCode` , `expressCode` , `mailNo`, `length` , `width` , `height`, `weight` FROM `log_order_mail_detail` WHERE `logisticsOrderCode` = #{logisticsOrderCode} AND `mailNo` = #{mailNo} AND `state` > 0;")
    List<LogOrderMailDetailListResponse> getListByCodeNo(@Param("logisticsOrderCode") String logisticsOrderCode, @Param("mailNo") String mailNo);

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

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

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