package cn.iocoder.yudao.module.order.dal.mysql.orderPickup;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQuery;
import cn.iocoder.yudao.framework.mybatis.core.mapper.AbstractMapper;
import cn.iocoder.yudao.module.order.dal.dataobject.orderPickup.OrderPickupDO;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.order.vo.orderPickup.*;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

/**
 * 提货 Mapper
 * @author 系统管理员
 */
@Mapper
public interface OrderPickupMapper extends AbstractMapper<OrderPickupDO> {
    @Override
    default PageResult<OrderPickupDO> selectPage(PageVO page, Object object) {
        if (object instanceof OrderPickupQueryVO) {
            OrderPickupQueryVO vo = (OrderPickupQueryVO)object;
            return selectPage(page, new LambdaQuery<OrderPickupDO>()
                .betweenIfPresent(OrderPickupDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                .eqIfPresent(OrderPickupDO::getOrderId, vo.getOrderId())
                .eqIfPresent(OrderPickupDO::getPickType, vo.getPickType())
                .betweenIfPresent(OrderPickupDO::getPickTime, vo.getBeginPickTime(), vo.getEndPickTime())
                .eqIfPresent(OrderPickupDO::getTrackingNumber, vo.getTrackingNumber())
                .betweenIfPresent(OrderPickupDO::getDeliverTime, vo.getBeginDeliverTime(), vo.getEndDeliverTime())
                .eqIfPresent(OrderPickupDO::getAttachment, vo.getAttachment())
                .eqIfPresent(OrderPickupDO::getRemark, vo.getRemark())
                    .orderByDesc(OrderPickupDO::getId));
        }

        return null;
    }

    @Override
    default List<OrderPickupDO> selectList(Object object) {
        if (object instanceof OrderPickupQueryVO) {
            OrderPickupQueryVO vo = (OrderPickupQueryVO)object;
            return selectList(new LambdaQuery<OrderPickupDO>()
                .betweenIfPresent(OrderPickupDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                .eqIfPresent(OrderPickupDO::getOrderId, vo.getOrderId())
                .eqIfPresent(OrderPickupDO::getPickType, vo.getPickType())
                .betweenIfPresent(OrderPickupDO::getPickTime, vo.getBeginPickTime(), vo.getEndPickTime())
                .eqIfPresent(OrderPickupDO::getTrackingNumber, vo.getTrackingNumber())
                .betweenIfPresent(OrderPickupDO::getDeliverTime, vo.getBeginDeliverTime(), vo.getEndDeliverTime())
                .eqIfPresent(OrderPickupDO::getAttachment, vo.getAttachment())
                .eqIfPresent(OrderPickupDO::getRemark, vo.getRemark())
                    .orderByDesc(OrderPickupDO::getId));
        }

        return null;
    }

    @ResultType(Integer.class)
//    @Select("select IFNULL(sum(t.pick_num),0) from ecw_order_pickup t where t.order_id=#{orderId} and t.deleted=0 GROUP BY t.order_id")
    @Select(" SELECT IFNULL(a.num,0) FROM (select sum(IF(t.pick_num is null,0,t.pick_num)) as num from ecw_order_pickup t where t.order_id = #{orderId} and t.deleted=0 ) as a")
    int selectPickedNum(String orderId);

    @ResultType(OrderPickupDO.class)
    @Select("select id, order_id, pick_type, pick_time, tracking_number, deliver_time, attachment, remark, pick_num, deliver_address, create_time, update_time, creator, updater, deleted FROM ecw_order_pickup WHERE order_id=#{orderNo}")
    List<OrderPickupDO> getAllPickUpListByOrderNo(String orderNo);

    @ResultType(Date.class)
    @Select("select t2.ul_box_time from ecw_order t LEFT JOIN ecw_box t1 on t.container_number=t1.self_no LEFT JOIN ecw_box_cabinet_unload t2 on t1.id=t2.shipment_id where t.order_id=#{orderNo} limit 1")
    Date getUnloadTime(Long orderId);

    @ResultType(OrderPickupDO.class)
    @Select("select * from ecw_order_pickup t where t.order_id = #{orderId} and t.deleted=1 and t.order_status >0 order by t.create_time desc limit 1")
    OrderPickupDO selectDeletedPickup(String orderId);
}
