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

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.orderItem.OrderItemDO;
import cn.iocoder.yudao.module.order.vo.orderItem.OrderItemBackVO;
import cn.iocoder.yudao.module.order.vo.orderItem.OrderItemQueryVO;
import cn.iocoder.yudao.module.order.vo.orderItem.OrderItemSpecialBackVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 订单项 Mapper
 *
 * @author 系统管理员
 */
@Mapper
public interface OrderItemMapper extends AbstractMapper<OrderItemDO> {
    @Override
    default PageResult<OrderItemDO> selectPage(PageVO page, Object object) {
        if (object instanceof OrderItemQueryVO) {
            OrderItemQueryVO vo = (OrderItemQueryVO) object;
            return selectPage(page, new LambdaQuery<OrderItemDO>()
                    .eqIfPresent(OrderItemDO::getOrderId, vo.getOrderId())
                    .eqIfPresent(OrderItemDO::getOrderNo, vo.getOrderNo())
                    .eqIfPresent(OrderItemDO::getOrderItemType, vo.getOrderItemType())
                    .eqIfPresent(OrderItemDO::getTransportId, vo.getTransportId())
                    .eqIfPresent(OrderItemDO::getLineId, vo.getLineId())
                    .eqIfPresent(OrderItemDO::getChannelId, vo.getChannelId())
                    .eqIfPresent(OrderItemDO::getItemStatus, vo.getItemStatus())
                    .eqIfPresent(OrderItemDO::getProdId, vo.getProdId())
                    .eqIfPresent(OrderItemDO::getProdType, vo.getProdType())
                    .eqIfPresent(OrderItemDO::getProdTitleZh, vo.getProdTitleZh())
                    .eqIfPresent(OrderItemDO::getProdTitleEn, vo.getProdTitleEn())
                    .eqIfPresent(OrderItemDO::getBrand, vo.getBrand())
                    .eqIfPresent(OrderItemDO::getBrandType, vo.getBrandType())
                    .eqIfPresent(OrderItemDO::getFeeType, vo.getFeeType())
                    .eqIfPresent(OrderItemDO::getIsPayAdvance, vo.getIsPayAdvance())
                    .eqIfPresent(OrderItemDO::getQuantity, vo.getQuantity())
                    .eqIfPresent(OrderItemDO::getCharging, vo.getCharging())
                    .eqIfPresent(OrderItemDO::getNum, vo.getNum())
                    .eqIfPresent(OrderItemDO::getUnit, vo.getUnit())
                    .eqIfPresent(OrderItemDO::getBoxGauge, vo.getBoxGauge())
                    .eqIfPresent(OrderItemDO::getVolume, vo.getVolume())
                    .eqIfPresent(OrderItemDO::getWeight, vo.getWeight())
                    .eqIfPresent(OrderItemDO::getWorth, vo.getWorth())
                    .eqIfPresent(OrderItemDO::getWorthCurrency, vo.getWorthCurrency())
                    .eqIfPresent(OrderItemDO::getProdAttrIds, vo.getProdAttrIds())
                    .eqIfPresent(OrderItemDO::getResult, vo.getResult())
                    .eqIfPresent(OrderItemDO::getResultStatus, vo.getResultStatus())
                    .eqIfPresent(OrderItemDO::getSeaFreight, vo.getSeaFreight())
                    .eqIfPresent(OrderItemDO::getOriginalSeaFreight, vo.getOriginalSeaFreight())
                    .eqIfPresent(OrderItemDO::getSeaFreightCurrency, vo.getSeaFreightCurrency())
                    .eqIfPresent(OrderItemDO::getSeaFreightVolume, vo.getSeaFreightVolume())
                    .eqIfPresent(OrderItemDO::getClearanceFreight, vo.getClearanceFreight())
                    .eqIfPresent(OrderItemDO::getOriginalClearanceFreight, vo.getOriginalClearanceFreight())
                    .eqIfPresent(OrderItemDO::getClearanceFreightCurrency, vo.getClearanceFreightCurrency())
                    .eqIfPresent(OrderItemDO::getClearanceFreightVolume, vo.getClearanceFreightVolume())
                    .betweenIfPresent(OrderItemDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderItemDO::getOrderItemId));
        }

        return null;
    }

    @Override
    default List<OrderItemDO> selectList(Object object) {
        if (object instanceof OrderItemQueryVO) {
            OrderItemQueryVO vo = (OrderItemQueryVO) object;
            return selectList(new LambdaQuery<OrderItemDO>()
                    .eqIfPresent(OrderItemDO::getOrderId, vo.getOrderId())
                    .eqIfPresent(OrderItemDO::getOrderNo, vo.getOrderNo())
                    .eqIfPresent(OrderItemDO::getOrderItemType, vo.getOrderItemType())
                    .eqIfPresent(OrderItemDO::getTransportId, vo.getTransportId())
                    .eqIfPresent(OrderItemDO::getLineId, vo.getLineId())
                    .eqIfPresent(OrderItemDO::getChannelId, vo.getChannelId())
                    .eqIfPresent(OrderItemDO::getItemStatus, vo.getItemStatus())
                    .eqIfPresent(OrderItemDO::getProdId, vo.getProdId())
                    .eqIfPresent(OrderItemDO::getProdType, vo.getProdType())
                    .eqIfPresent(OrderItemDO::getProdTitleZh, vo.getProdTitleZh())
                    .eqIfPresent(OrderItemDO::getProdTitleEn, vo.getProdTitleEn())
                    .eqIfPresent(OrderItemDO::getBrand, vo.getBrand())
                    .eqIfPresent(OrderItemDO::getBrandType, vo.getBrandType())
                    .eqIfPresent(OrderItemDO::getFeeType, vo.getFeeType())
                    .eqIfPresent(OrderItemDO::getIsPayAdvance, vo.getIsPayAdvance())
                    .eqIfPresent(OrderItemDO::getQuantity, vo.getQuantity())
                    .eqIfPresent(OrderItemDO::getCharging, vo.getCharging())
                    .eqIfPresent(OrderItemDO::getNum, vo.getNum())
                    .eqIfPresent(OrderItemDO::getUnit, vo.getUnit())
                    .eqIfPresent(OrderItemDO::getBoxGauge, vo.getBoxGauge())
                    .eqIfPresent(OrderItemDO::getVolume, vo.getVolume())
                    .eqIfPresent(OrderItemDO::getWeight, vo.getWeight())
                    .eqIfPresent(OrderItemDO::getWorth, vo.getWorth())
                    .eqIfPresent(OrderItemDO::getWorthCurrency, vo.getWorthCurrency())
                    .eqIfPresent(OrderItemDO::getProdAttrIds, vo.getProdAttrIds())
                    .eqIfPresent(OrderItemDO::getResult, vo.getResult())
                    .eqIfPresent(OrderItemDO::getResultStatus, vo.getResultStatus())
                    .eqIfPresent(OrderItemDO::getSeaFreight, vo.getSeaFreight())
                    .eqIfPresent(OrderItemDO::getOriginalSeaFreight, vo.getOriginalSeaFreight())
                    .eqIfPresent(OrderItemDO::getSeaFreightCurrency, vo.getSeaFreightCurrency())
                    .eqIfPresent(OrderItemDO::getSeaFreightVolume, vo.getSeaFreightVolume())
                    .eqIfPresent(OrderItemDO::getClearanceFreight, vo.getClearanceFreight())
                    .eqIfPresent(OrderItemDO::getOriginalClearanceFreight, vo.getOriginalClearanceFreight())
                    .eqIfPresent(OrderItemDO::getClearanceFreightCurrency, vo.getClearanceFreightCurrency())
                    .eqIfPresent(OrderItemDO::getClearanceFreightVolume, vo.getClearanceFreightVolume())
                    .betweenIfPresent(OrderItemDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderItemDO::getOrderItemId));
        }

        return null;
    }

    /**
     * 特价申请商品列表
     *
     * @param orderNo 订单号
     * @return 特价申请商品列表
     */
    @ResultType(OrderItemSpecialBackVO.class)
    @Select({"select ",
            "oi.*, ",
            "oi.one_sea_freight as sea_freight, ",
            "(oi.one_sea_freight - IFNULL(oi.plain_commission,0)) as cost_sea_freight, ",
            "oi.original_sea_freight as original_sea_freight, ",
            "oi.one_clearance_freight as clearance_freight, ",
            "oi.one_clearance_freight as cost_clearance_freight, ",
            "oi.original_clearance_freight as original_clearance_freight, ",
            "IFNULL((select ap1.status from (select a1.* from ecw_order_approval a1 where a1.type = 1 and a1.order_id = oi.order_id and a1.details ->> '$.orderItemId' = oi.order_item_id order by a1.order_approval_id desc limit 1) ap1), 0) as discount_status,",
            "IFNULL((select ap2.status from (select a2.* from ecw_order_approval a2 where a2.type = 2 and a2.order_id = oi.order_id and a2.details ->> '$.orderItemId' = oi.order_item_id order by a2.order_approval_id desc limit 1) ap2), 0) as admin_status,",
            "IFNULL((select ap3.status from (select a3.* from ecw_order_approval a3 where a3.type = 3 and a3.order_id = oi.order_id and a3.details ->> '$.orderItemId' = oi.order_item_id order by a3.order_approval_id desc limit 1) ap3), 0) as commission_status,",
            "IFNULL((select ap4.status from (select a4.* from ecw_order_approval a4 where a4.type = 4 and a4.order_id = oi.order_id and a4.details ->> '$.orderItemId' = oi.order_item_id order by a4.order_approval_id desc limit 1) ap4), 0) as volume_status,",
            "IFNULL((select ap5.status from (select a5.* from ecw_order_approval a5 where a5.type = 5 and a5.order_id = oi.order_id and a5.details ->> '$.orderItemId' = oi.order_item_id order by a5.order_approval_id desc limit 1) ap5), 0) as weight_status",
            "from ecw_order_item oi",
            "where ",
            "order_no = #{orderNo}"
    })
    List<OrderItemSpecialBackVO> specialApplyOrderItem(@Param("orderNo") String orderNo);


    @Update({
            "<script>",
            "update ecw_order_item ",
            "set warehouse_in_info = null, ",
            "order_item_type = 1," ,
            "`brand` = null," ,
            "fee_type = 0," ,
            "is_priced = 0," ,
            "charge_weight = 0, " ,
            "charge_volume = 0, " ,
            "charge_quantity = 0",
             " where order_id = #{orderId} ",
            "<when test = 'orderItemId != null'>",
            "AND `order_item_id` = #{orderItemId}",
            "</when>",
            "</script>"
    })
    void updateOrderItemWarehouseInInfoToNull(@Param("orderId") Long orderId, @Param("orderItemId") Long orderItemId);

    @ResultType(OrderItemBackVO.class)
    @Select({"select ",
            "oi.*, ",
            "(oi.original_sea_freight + oi.packaging_freight_price + oi.brand_freight_price + oi.electrified_freight_price + oi.liquid_freight_price + oi.plain_commission  - oi.discount_freight_price ) as after_discount_sea_freight, ",
            "(oi.original_clearance_freight + oi.packaging_freight_price + oi.brand_clearance_fee_price + oi.electrified_clearance_fee_price + oi.liquid_clearance_fee_price  - oi.discount_clearance_fee_price) as after_discount_clearance_freight, ",
            "IF(o.order_type != 2 and (SELECT COUNT(1) FROM ecw_order_exception hoe WHERE hoe.order_id = o.order_id AND hoe.deleted = 0 AND hoe.order_exception_status = 0 AND hoe.order_exception_type ='order_heavy_cargo_exception') = 0, IFNULL(oi.warehouse_in_info ->> '$.volume', oi.volume), oi.w_volume ) as we_volume,",
            "IF(o.order_type != 3 and (SELECT COUNT(1) FROM ecw_order_exception boe WHERE boe.order_id = o.order_id AND boe.deleted = 0 AND boe.order_exception_status = 0 AND boe.order_exception_type ='order_bulky_cargo_exception') = 0, IFNULL(oi.warehouse_in_info ->> '$.weight', oi.weight), oi.v_weight ) as vo_weight,",
            "IF(#{lang} = 0, pd.title_zh, pd.title_en) as brand_name, ",
            "IF(#{lang} = 0, pdc.title_zh, pdc.title_en) as brand_name ",
            "from ecw_order_item oi",
            "left join ecw_product_brank pd",
            "on pd.id = oi.brand",
            "left join ecw_product_brank pdc",
            "on pdc.id = oi.check_brand",
            "left join ecw_order o",
            "on o.order_id = oi.order_id",
            "where  oi.deleted = 0 and ",
            "oi.order_id = #{orderId}"
    })
    List<OrderItemBackVO> infoItemList(@Param("orderId") Long orderId, @Param("lang") Integer lang);

    @ResultType(OrderItemBackVO.class)
    @Select({"select ",
            "oi.*, ",
            "(oi.original_sea_freight + oi.packaging_freight_price + oi.brand_freight_price + oi.electrified_freight_price + oi.liquid_freight_price + oi.plain_commission  - oi.discount_freight_price ) as after_discount_sea_freight, ",
            "(oi.original_clearance_freight + oi.packaging_freight_price + oi.brand_clearance_fee_price + oi.electrified_clearance_fee_price + oi.liquid_clearance_fee_price  - oi.discount_clearance_fee_price) as after_discount_clearance_freight, ",
            "IF(o.order_type != 2 and (SELECT COUNT(1) FROM ecw_order_exception hoe WHERE hoe.order_id = o.order_id AND hoe.deleted = 0 AND hoe.order_exception_status = 0 AND hoe.order_exception_type ='order_heavy_cargo_exception') = 0, IFNULL(oi.warehouse_in_info ->> '$.volume', oi.volume), oi.w_volume ) as we_volume,",
            "IF(o.order_type != 3 and (SELECT COUNT(1) FROM ecw_order_exception boe WHERE boe.order_id = o.order_id AND boe.deleted = 0 AND boe.order_exception_status = 0 AND boe.order_exception_type ='order_bulky_cargo_exception') = 0, IFNULL(oi.warehouse_in_info ->> '$.weight', oi.weight), oi.v_weight ) as vo_weight,",
            "IF(#{lang} = 0, pd.title_zh, pd.title_en) as brand_name ",
            "from ecw_order_item oi ",
            "left join ecw_product_brank pd ",
            "on pd.id = oi.brand ",
            "left join ecw_order o ",
            "on o.order_id = oi.order_id ",
            "where ((oi.warehouse_in_info is null and exists(select 1 from ecw_order_warehouse_in owi where owi.order_item_id = oi.order_item_id and owi.deleted = 1)) or (oi.deleted = 1)) ", // 退仓的数据
            "and oi.order_id = #{orderId}"
    })
    List<OrderItemBackVO> getInfoItemListWhenWarehouseInDeleted(@Param("orderId") Long orderId, @Param("lang") Integer lang);

    @ResultType(OrderItemBackVO.class)
    @Select({
            "<script>",
            "select ",
            "oi.*, ",
            "IF(#{lang} = 0, pd.title_zh, pd.title_en) as brand_name ",
            "from ecw_order_item oi",
            "left join ecw_product_brank pd",
            "on pd.id = oi.brand",
            "where  oi.deleted = 0 and ",
            "oi.warehouse_in_info is not null and warehouse_in_info ->> '$.cartonsNum' > 0 ",
            "and oi.order_id in ",
            "<foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>#{item}</foreach>",
            "</script>"
    })
    List<OrderItemBackVO> orderItemList(@Param("lang") Integer lang, @Param("list") List<Long> list);

}
