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

import java.math.BigDecimal;
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.orderSplitItem.OrderSplitItemDO;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.module.order.dto.OrderSplitWorthDto;
import org.apache.ibatis.annotations.*;
import cn.iocoder.yudao.module.order.vo.orderSplitItem.*;

/**
 * 订单拆单产品项 Mapper
 *
 * @author yanghao
 */
@Mapper
public interface OrderSplitItemMapper extends AbstractMapper<OrderSplitItemDO> {
    @Override
    default PageResult<OrderSplitItemDO> selectPage(PageVO page, Object object) {
        if (object instanceof OrderSplitItemQueryVO) {
            OrderSplitItemQueryVO vo = (OrderSplitItemQueryVO) object;
            return selectPage(page, new LambdaQuery<OrderSplitItemDO>()
                    .betweenIfPresent(OrderSplitItemDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .eqIfPresent(OrderSplitItemDO::getOrderSplitId, vo.getOrderSplitId())
                    .eqIfPresent(OrderSplitItemDO::getOrderItemId, vo.getOrderItemId())
                    .eqIfPresent(OrderSplitItemDO::getProdId, vo.getProdId())
                    .eqIfPresent(OrderSplitItemDO::getProdType, vo.getProdType())
                    .eqIfPresent(OrderSplitItemDO::getProdTitleZh, vo.getProdTitleZh())
                    .eqIfPresent(OrderSplitItemDO::getProdTitleEn, vo.getProdTitleEn())
                    .eqIfPresent(OrderSplitItemDO::getBrand, vo.getBrand())
                    .eqIfPresent(OrderSplitItemDO::getBrandType, vo.getBrandType())
                    .eqIfPresent(OrderSplitItemDO::getNum, vo.getNum())
                    .eqIfPresent(OrderSplitItemDO::getUnit, vo.getUnit())
                    .eqIfPresent(OrderSplitItemDO::getBoxGauge, vo.getBoxGauge())
                    .eqIfPresent(OrderSplitItemDO::getVolume, vo.getVolume())
                    .eqIfPresent(OrderSplitItemDO::getWeight, vo.getWeight())
                    .orderByDesc(OrderSplitItemDO::getId));
        }

        return null;
    }

    @Override
    default List<OrderSplitItemDO> selectList(Object object) {
        if (object instanceof OrderSplitItemQueryVO) {
            OrderSplitItemQueryVO vo = (OrderSplitItemQueryVO) object;
            return selectList(new LambdaQuery<OrderSplitItemDO>()
                    .betweenIfPresent(OrderSplitItemDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .eqIfPresent(OrderSplitItemDO::getOrderSplitId, vo.getOrderSplitId())
                    .eqIfPresent(OrderSplitItemDO::getOrderItemId, vo.getOrderItemId())
                    .eqIfPresent(OrderSplitItemDO::getProdId, vo.getProdId())
                    .eqIfPresent(OrderSplitItemDO::getProdType, vo.getProdType())
                    .eqIfPresent(OrderSplitItemDO::getProdTitleZh, vo.getProdTitleZh())
                    .eqIfPresent(OrderSplitItemDO::getProdTitleEn, vo.getProdTitleEn())
                    .eqIfPresent(OrderSplitItemDO::getBrand, vo.getBrand())
                    .eqIfPresent(OrderSplitItemDO::getBrandType, vo.getBrandType())
                    .eqIfPresent(OrderSplitItemDO::getNum, vo.getNum())
                    .eqIfPresent(OrderSplitItemDO::getUnit, vo.getUnit())
                    .eqIfPresent(OrderSplitItemDO::getBoxGauge, vo.getBoxGauge())
                    .eqIfPresent(OrderSplitItemDO::getVolume, vo.getVolume())
                    .eqIfPresent(OrderSplitItemDO::getWeight, vo.getWeight())
                    .orderByDesc(OrderSplitItemDO::getId));
        }

        return null;
    }

    @ResultType(OrderSplitItemBackVO.class)
    @Select({
            "select a.*, ",
            "IF(#{lang} = 0, pd.title_zh, pd.title_en) as brand_name ",
            " from ecw_order_split_item a",
            "LEFT JOIN ecw_product_brank pd on a.brand = pd.id where a.deleted = 0 and a.used = false and a.order_split_id = #{orderSplitId}"
    })
    List<OrderSplitItemBackVO> getListByOrderSplitId(@Param("orderSplitId") Long orderSplitId, @Param("lang") Integer lang);

    @Delete("delete from ecw_order_split_item item where exists(select 1 from ecw_order_split s where item.order_split_id = s.id and s.parent_order_id = #{orderId}) ")
    int deleteByOrderId(@Param("orderId") Long orderId);

    @Delete("delete from ecw_order_split_item item where exists(select 1 from ecw_order_split s where s.used = false and item.order_split_id = s.id and s.parent_order_id = #{orderId}) and item.used = false")
    int deleteByOrderIdAndUsed(@Param("orderId") Long orderId);

    @ResultType(OrderSplitWorthDto.class)
    @Select({
            "<script>",
            "select ",
            "sum(IFNULL(a.`num`, 0)) as split_num, ",
            "sum(IFNULL(a.`worth`, 0)) as split_worth",
            " from ecw_order_split_item a",
            "LEFT JOIN ecw_order_split s on s.id = a.order_split_id ",
            "where a.deleted = false and s.deleted = false and a.used = false and s.used = false and s.parent_order_id = #{orderId}",
            "<when test = 'orderSplitItemId != null '>",
            "AND a.id != #{orderSplitItemId}",
            "</when>",
            "</script>"
    })
    OrderSplitWorthDto getOrderAllSplitWorthDtoByOrderId(@Param("orderId") Long orderId, @Param("orderSplitItemId") Long orderSplitItemId);

    @ResultType(BigDecimal.class)
    @Select({
            "select " ,
            "sum(IFNULL(a.`worth`, 0)) as split_worth",
            " from ecw_order_split_item a",
            "LEFT JOIN ecw_order_split s on s.id = a.order_split_id where a.deleted = false and s.deleted = false and s.is_master = false and a.used = false and s.used = false and s.parent_order_id = #{parentOrderId}"
    })
    BigDecimal getSonSplitTotalWorth(Long parentOrderId);
}
