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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.AbstractMapper;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQuery;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderSplit.OrderSplitDO;
import cn.iocoder.yudao.module.order.vo.orderSplit.OrderSplitBackVO;
import cn.iocoder.yudao.module.order.vo.orderSplit.OrderSplitQueryVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 订单拆单 Mapper
 *
 * @author yanghao
 */
@Mapper
public interface OrderSplitMapper extends AbstractMapper<OrderSplitDO> {
    @Override
    default PageResult<OrderSplitDO> selectPage(PageVO page, Object object) {
        if (object instanceof OrderSplitQueryVO) {
            OrderSplitQueryVO vo = (OrderSplitQueryVO) object;
            return selectPage(page, new LambdaQuery<OrderSplitDO>()
                    .betweenIfPresent(OrderSplitDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .eqIfPresent(OrderSplitDO::getParentOrderNo, vo.getParentOrderNo())
                    .eqIfPresent(OrderSplitDO::getOrderNo, vo.getOrderNo())
                    .eqIfPresent(OrderSplitDO::getParentOrderId, vo.getParentOrderId())
                    .eqIfPresent(OrderSplitDO::getTransportId, vo.getTransportId())
                    .eqIfPresent(OrderSplitDO::getDstWarehouseId, vo.getDstWarehouseId())
                    .orderByDesc(OrderSplitDO::getId));
        }

        return null;
    }

    @Override
    default List<OrderSplitDO> selectList(Object object) {
        if (object instanceof OrderSplitQueryVO) {
            OrderSplitQueryVO vo = (OrderSplitQueryVO) object;
            return selectList(new LambdaQuery<OrderSplitDO>()
                    .betweenIfPresent(OrderSplitDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .eqIfPresent(OrderSplitDO::getParentOrderNo, vo.getParentOrderNo())
                    .eqIfPresent(OrderSplitDO::getOrderNo, vo.getOrderNo())
                    .eqIfPresent(OrderSplitDO::getParentOrderId, vo.getParentOrderId())
                    .eqIfPresent(OrderSplitDO::getTransportId, vo.getTransportId())
                    .eqIfPresent(OrderSplitDO::getDstWarehouseId, vo.getDstWarehouseId())
                    .orderByDesc(OrderSplitDO::getId));
        }

        return null;
    }


    @ResultType(OrderSplitBackVO.class)
    @Select({
            "select ",
            "a.*, ",
            "if(#{lang} = 0, w.title_zh, w.title_en) as dst_warehouse_name, ",
            "if(#{lang} = 0, dd.label, dd.label_en) as transport_name, ",
            "if(#{lang} = 0, c.name_zh, c.name_en) as channel_name ",
            "from ecw_order_split a  ",
            "left join ecw_warehouse w on a.dst_warehouse_id = w.id ",
            "left join ecw_channel c on a.channel_id = c.channel_id ",
            "left join (select * from system_dict_data d where d.dict_type = 'transport_type' and d.deleted = 0) dd on a.transport_id = dd.value ",
            "where a.deleted = 0 and a.used = false and a.parent_order_id = #{orderId} ",
            "order by a.id asc"
    })
    List<OrderSplitBackVO> listByOrderId(@Param("orderId") Long orderId, @Param("lang") Integer lang);


    @Delete("delete from ecw_order_split s where s.parent_order_id = #{orderId}")
    int deleteByOrderId(@Param("orderId") Long orderId);

    @Delete("delete from ecw_order_split s where s.used = false and s.parent_order_id = #{orderId}")
    int deleteByOrderIdAndUsed(@Param("orderId") Long orderId);

}
