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.LogOrderEntity;
import org.ta.pddserver.model.api.request.LogOrderListRequest;
import org.ta.pddserver.model.api.response.LogOrderInfoResponse;
import org.ta.pddserver.model.api.response.LogOrderListResponse;

import java.util.List;

@Mapper
public interface LogOrderMapper extends BaseMapper<LogOrderEntity> {
    @Update("UPDATE `log_order` SET `packageQuantity` = ${packageQuantity} WHERE `logisticsOrderCode` = #{logisticsOrderCode};")
    Boolean savePackageQuantity(@Param("logisticsOrderCode") String logisticsOrderCode, @Param("packageQuantity") long packageQuantity);

    @Select("SELECT * FROM `log_order` WHERE `logisticsOrderCode` = #{logisticsOrderCode};")
    LogOrderEntity selectByOrderCode(String logisticsOrderCode);

    @Update("UPDATE `log_order` SET `status` = #{status} WHERE `uid` = #{uid};")
    int updateOrderStatusById(int status, String uid);

    @Select("SELECT `uid`, `updated`, `logisticsOrderCode`, `bizType`, `buyerCode`, `consoType`, `dereRecogCode`, `packageQuantity` FROM `log_order` WHERE  `logisticsOrderCode` LIKE '%${logisticsOrderCode}%' ")
    List<LogOrderListResponse> getListByRequest1(@Param("logisticsOrderCode") String logisticsOrderCode);


    @Select("SELECT count(t.uid) FROM ( SELECT a.uid  FROM  `log_order` a WHERE  a.`logisticsOrderCode` LIKE '%${logisticsOrderCode}%'  ) t")
    Integer countUsable(LogOrderListRequest param);

    @Select("SELECT `uid`, `updated`, `logisticsOrderCode`, `bizType`, `buyerCode`, `consoType`, `dereRecogCode`, `packageQuantity`, `num` FROM `log_order` WHERE  `logisticsOrderCode` LIKE '%${logisticsOrderCode}%' ORDER BY uid DESC LIMIT #{offset}, #{size}; ")
    List<LogOrderListResponse> getListByRequest(LogOrderListRequest param);

    @Update("UPDATE `log_order` SET `status` = ${status} WHERE  `logisticsOrderCode` = #{logisticsOrderCode}")
    void updateStatusByOrderCode(@Param("logisticsOrderCode") String logisticsOrderCode, @Param("status") int status);

    @Select("SELECT `logisticsOrderCode`,`bizType`, `consoType`, `dereRecogCode`, `num`, `packageQuantity`, `updated`, `status`, `buyerCode` FROM `log_order` WHERE `logisticsOrderCode` = #{logisticsOrderCode} ")
    LogOrderInfoResponse getLogOrderInfo(@Param("logisticsOrderCode") String logisticsOrderCode);
}
