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

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.orderWarehouseAdjust.OrderWarehouseAdjustDO;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.order.vo.orderWarehouseAdjust.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 调仓记录 Mapper
 * @author yanghao
 */
@Mapper
public interface OrderWarehouseAdjustMapper extends AbstractMapper<OrderWarehouseAdjustDO> {
    @Override
    default PageResult<OrderWarehouseAdjustDO> selectPage(PageVO page, Object object) {
        if (object instanceof OrderWarehouseAdjustQueryVO) {
            OrderWarehouseAdjustQueryVO vo = (OrderWarehouseAdjustQueryVO)object;
            return selectPage(page, new LambdaQuery<OrderWarehouseAdjustDO>()
                .eqIfPresent(OrderWarehouseAdjustDO::getOrderId, vo.getOrderId())
                .eqIfPresent(OrderWarehouseAdjustDO::getWarehouseOutId, vo.getWarehouseOutId())
                .eqIfPresent(OrderWarehouseAdjustDO::getWarehouseInId, vo.getWarehouseInId())
                .eqIfPresent(OrderWarehouseAdjustDO::getLogisticsCompany, vo.getLogisticsCompany())
                .eqIfPresent(OrderWarehouseAdjustDO::getLogisticsNo, vo.getLogisticsNo())
                .eqIfPresent(OrderWarehouseAdjustDO::getPhone, vo.getPhone())
                .betweenIfPresent(OrderWarehouseAdjustDO::getDeliveryDate, vo.getBeginDeliveryDate(), vo.getEndDeliveryDate())
                .betweenIfPresent(OrderWarehouseAdjustDO::getArrivalDate, vo.getBeginArrivalDate(), vo.getEndArrivalDate())
                .eqIfPresent(OrderWarehouseAdjustDO::getStatus, vo.getStatus())
                .betweenIfPresent(OrderWarehouseAdjustDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderWarehouseAdjustDO::getId));
        }

        return null;
    }

    @Override
    default List<OrderWarehouseAdjustDO> selectList(Object object) {
        if (object instanceof OrderWarehouseAdjustQueryVO) {
            OrderWarehouseAdjustQueryVO vo = (OrderWarehouseAdjustQueryVO)object;
            return selectList(new LambdaQuery<OrderWarehouseAdjustDO>()
                .likeIfPresent(OrderWarehouseAdjustDO::getOrderId, vo.getOrderId() != null ? vo.getOrderId().toString() : null)
                .eqIfPresent(OrderWarehouseAdjustDO::getWarehouseOutId, vo.getWarehouseOutId())
                .eqIfPresent(OrderWarehouseAdjustDO::getWarehouseInId, vo.getWarehouseInId())
                .eqIfPresent(OrderWarehouseAdjustDO::getLogisticsCompany, vo.getLogisticsCompany())
                .eqIfPresent(OrderWarehouseAdjustDO::getLogisticsNo, vo.getLogisticsNo())
                .eqIfPresent(OrderWarehouseAdjustDO::getPhone, vo.getPhone())
                .betweenIfPresent(OrderWarehouseAdjustDO::getDeliveryDate, vo.getBeginDeliveryDate(), vo.getEndDeliveryDate())
                .betweenIfPresent(OrderWarehouseAdjustDO::getArrivalDate, vo.getBeginArrivalDate(), vo.getEndArrivalDate())
                .eqIfPresent(OrderWarehouseAdjustDO::getStatus, vo.getStatus())
                .betweenIfPresent(OrderWarehouseAdjustDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderWarehouseAdjustDO::getId));
        }

        return null;
    }


    @Select({
            "<script>",
            "select wa.*, u.nickname as creator_name, ",
            "if(#{lang} = 0, a.title_zh, a.title_en) as warehouse_out_name, ",
            "a.shi as warehouse_out_shi, ",
            "if(#{lang} = 0, b.title_zh, b.title_en) as warehouse_in_name, ",
            "b.shi as warehouse_in_shi ",
            "from ecw_order_warehouse_adjust wa ",
            "left join ecw_warehouse a on wa.warehouse_out_id = a.id ",
            "left join ecw_warehouse b on wa.warehouse_in_id = b.id ",
            "left join system_user u on wa.creator = u.id ",
            "where wa.order_id = #{orderId} and wa.deleted = 0 order by id asc",
            "</script>"
    })
    List<OrderWarehouseAdjustDO> selectByOrderId(@Param("orderId") Long orderId, @Param("lang") Integer lang);

}
