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

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.order.OrderDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderGuanlian.OrderGuanlianDO;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.order.vo.orderGuanlian.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 订单关联 Mapper
 * @author 系统管理员
 */
@Mapper
public interface OrderGuanlianMapper extends AbstractMapper<OrderGuanlianDO> {
    @Override
    default PageResult<OrderGuanlianDO> selectPage(PageVO page, Object object) {
        if (object instanceof OrderGuanlianQueryVO) {
            OrderGuanlianQueryVO vo = (OrderGuanlianQueryVO)object;
            return selectPage(page, new LambdaQuery<OrderGuanlianDO>()
                .betweenIfPresent(OrderGuanlianDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                .eqIfPresent(OrderGuanlianDO::getOrderId, vo.getOrderId())
                .eqIfPresent(OrderGuanlianDO::getRelateOrderId, vo.getRelateOrderId())
                .betweenIfPresent(OrderGuanlianDO::getAddTime, vo.getBeginAddTime(), vo.getEndAddTime())
                    .orderByDesc(OrderGuanlianDO::getId));
        }

        return null;
    }

    @Override
    default List<OrderGuanlianDO> selectList(Object object) {
        if (object instanceof OrderGuanlianQueryVO) {
            OrderGuanlianQueryVO vo = (OrderGuanlianQueryVO)object;
            return selectList(new LambdaQuery<OrderGuanlianDO>()
                .betweenIfPresent(OrderGuanlianDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                .eqIfPresent(OrderGuanlianDO::getOrderId, vo.getOrderId())
                .eqIfPresent(OrderGuanlianDO::getRelateOrderId, vo.getRelateOrderId())
                .betweenIfPresent(OrderGuanlianDO::getAddTime, vo.getBeginAddTime(), vo.getEndAddTime())
                    .orderByDesc(OrderGuanlianDO::getId));
        }

        return null;
    }

    List<OrderGuanlianDO> getOrderRelationList(@Param("orderId") Long orderId);

    List<OrderDO> guanlianOrderList(@Param("orderNumbers") List<String> orderNumbers);

    @Select("update ecw_order_guanlian set deleted=0 where creator=#{query.creator} and create_time=#{query.createTime} " +
            "and updater=#{query.updater} and update_time=#{query.updateTime} and relate_order_id=#{query.relateOrderId}")
    void updateGuanlianBackByRelatId(@Param("query") OrderGuanlianDO item);

    @Select("update ecw_order_guanlian set deleted=0 where creator=#{query.creator} and create_time=#{query.createTime} " +
            "and updater=#{query.updater} and update_time=#{query.updateTime} and order_id=#{query.orderId}")
    void updateGuanlianBackByOrderId(@Param("query")OrderGuanlianDO item);

    @Select("select t.* " +
            "from ecw_order_guanlian t " +
            " LEFT JOIN ecw_order t1 on t.order_id=t1.order_id " +
            " LEFT JOIN ecw_order t2 on t.relate_order_id=t2.order_id " +
            "where (t.order_id=#{orderId} or t.relate_order_id =#{orderId})" +
            "and t1.deleted=0 and t2.deleted=0 and t.deleted=0")
    IPage<OrderGuanlianDO> getGuanlianOrderByOrderId(IPage<OrderGuanlianDO> pageVO, @Param("orderId") Long orderId);
}
