package org.ta.pddserver.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.ta.pdd.entity.StorageEntity;
import org.ta.pddserver.model.api.request.StorageListRequest;
import org.ta.pddserver.model.api.request.StorageListVORequest;
import org.ta.pddserver.model.api.request.UnpackNoticeListRequest;
import org.ta.pddserver.model.api.response.StorageListResponse;
import org.ta.pddserver.model.api.response.StorageResponse;
import org.ta.pddserver.model.api.response.UnpackNoticeListResponse;

import java.util.List;

@Mapper
public interface StorageMapper extends BaseMapper<StorageEntity> {

    @Select("SELECT * FROM `storage` WHERE `logisticsOrderCode` = #{logisticsOrderCode} ORDER BY `uid` DESC LIMIT 1; ")
    StorageEntity getInfoByLogisticsOrderCode(@Param("logisticsOrderCode") String logisticsOrderCode);

    @Select("SELECT * FROM `storage` WHERE  `mailNo` = #{mailNo};")
    StorageEntity getInfoByMailNo(@Param("mailNo") String mailNo);

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

    @Update("UPDATE `storage` SET `num` = `num` + 1 WHERE  `logisticsOrderCode` = #{logisticsOrderCode}")
    void updateNumByLogisticsOrderCode(String logisticsOrderCode);

    @Select("SELECT COUNT(`uid`) FROM `storage` WHERE `code` = #{code}")
    int findAllCount(@Param("code") String code);

    @Select("SELECT SUM(IFNULL(`total`, 0) - IFNULL(`num`, 0)) FROM( SELECT `total`, `num` FROM `storage` WHERE `num` <> `total` AND `code` = #{code} GROUP BY `logisticsOrderCode`) t")
    Integer findReNumCount(@Param("code") String code);

    @Select("SELECT `code`, `place` FROM  `storage` WHERE `mailNo` = #{mailNo}")
    StorageResponse storageSelect(@Param("mailNo") String mailNo);

    @Select("SELECT b.`mailNo`, a.`code` , a.`place` FROM `unpack_mail_detail` b LEFT JOIN `storage` a ON b.`mailNo` = a.`mailNo` WHERE b.`orderCode` = #{logisticsOrderCode} ORDER BY a.`place`")
    List<StorageListResponse> storageUnpackList(@Param("logisticsOrderCode") String logisticsOrderCode);

    @Select("SELECT b.`mailNo`, a.`code` , a.`place` FROM `collect_mail_detail` b LEFT JOIN `storage` a ON b.`mailNo` = a.`mailNo` WHERE b.`orderCode` = #{logisticsOrderCode} ORDER BY a.`place`")
    List<StorageListResponse> storageCollectList(@Param("logisticsOrderCode") String logisticsOrderCode);

    @Delete("DELETE FROM `storage` WHERE `mailNo` IN ${ids}")
    void storageDel(@Param("ids") String ids);

    @Select("SELECT * FROM `storage` WHERE `buyerCode` = #{buyerCode} LIMIT 1;")
    StorageEntity selectByBuyerCode(String buyerCode);

    @Select("SELECT COUNT(*) FROM `storage` WHERE `place` IS NOT NULL;")
    Long countOned();

    @Select("SELECT uid,mailNo,code,place,created,logisticsOrderCode,num,total,buyerCode FROM `storage` WHERE  place IS NULL AND (`mailNo` = #{mailNo} OR #{mailNo}='')  AND (`code` = #{code} OR #{code}='') ORDER BY `uid` DESC;")
    List<StorageEntity> notListedStorageList(StorageListVORequest param);

    @Select("SELECT count(`uid`) FROM `storage` WHERE (`mailNo` = #{mailNo} OR #{mailNo}='')  AND (`code` = #{code} OR #{code}='')")
    Integer countUsableStorageListPage(StorageListVORequest param);

    @Select("SELECT uid,mailNo,code,place,created,logisticsOrderCode,num,total,buyerCode FROM `storage` WHERE (`mailNo` = #{mailNo} OR #{mailNo}='')  AND (`code` = #{code} OR #{code}='') ORDER BY `uid` DESC LIMIT #{offset}, #{size};")
    List<StorageEntity> getStorageListPage(StorageListVORequest param);

    @Select("SELECT * FROM `storage` WHERE `place` = #{place};")
    List<StorageEntity> findListByPlace(@Param("place") String place);
}
