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 ORDER BY `uid` DESC LIMIT 1;")
    LogOrderMailDetailEntity findByMailNo(String mailNo);

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

    @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` = #{status}  AND `state` > 0 ORDER BY uid DESC LIMIT #{offset}, #{size};")
    List<LogOrderMailDetailListResponse> getListByCodeNo(LogOrderMailDetailListRequest param);



    @Select("SELECT a.`uid`, a.`logisticsOrderCode` , a.`expressCode` ,a.`mailNo`, a.`updated`, a.`status`, b.`num` FROM `log_order_mail_detail` a, `log_order` b WHERE a.`mailNo` = #{mailNo} AND a.`logisticsOrderCode` = b.`logisticsOrderCode` ORDER BY a.`uid` DESC LIMIT 1;")
    MailNoResponse getInfoByMailNo(@Param("mailNo") String mailNo);


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

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

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

    @Select("SELECT COUNT(*) FROM `log_order_mail_detail` WHERE `logisticsOrderCode` = #{logisticsOrderCode}")
    int findCountByLogisticsOrderCode(@Param("logisticsOrderCode") String logisticsOrderCode);
}
