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

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.orderWarehouseIn.OrderWarehouseInDO;
import cn.iocoder.yudao.module.order.vo.orderItem.OrderItemBackVO;
import cn.iocoder.yudao.module.order.vo.orderWarehouseIn.OrderWarehouseInBackItemVO;
import cn.iocoder.yudao.module.order.vo.orderWarehouseIn.OrderWarehouseInQueryVO;
import cn.iocoder.yudao.module.order.vo.orderWarehouseIn.OrderWarehouseInSumBackVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 入仓货物详情 Mapper
 * @author yanghao
 */
@Mapper
public interface OrderWarehouseInMapper extends AbstractMapper<OrderWarehouseInDO> {
    @Override
    default PageResult<OrderWarehouseInDO> selectPage(PageVO page, Object object) {
        if (object instanceof OrderWarehouseInQueryVO) {
            OrderWarehouseInQueryVO vo = (OrderWarehouseInQueryVO)object;
            return selectPage(page, new LambdaQuery<OrderWarehouseInDO>()
                .eqIfPresent(OrderWarehouseInDO::getOrderId, vo.getOrderId())
                .eqIfPresent(OrderWarehouseInDO::getOrderNo, vo.getOrderNo())
                .eqIfPresent(OrderWarehouseInDO::getOrderItemId, vo.getOrderItemId())
                .eqIfPresent(OrderWarehouseInDO::getExpressNo, vo.getExpressNo())
                .betweenIfPresent(OrderWarehouseInDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderWarehouseInDO::getId));
        }

        return null;
    }

    @Override
    default List<OrderWarehouseInDO> selectList(Object object) {
        if (object instanceof OrderWarehouseInQueryVO) {
            OrderWarehouseInQueryVO vo = (OrderWarehouseInQueryVO)object;
            return selectList(new LambdaQuery<OrderWarehouseInDO>()
                .eqIfPresent(OrderWarehouseInDO::getOrderId, vo.getOrderId())
                .eqIfPresent(OrderWarehouseInDO::getOrderNo, vo.getOrderNo())
                .eqIfPresent(OrderWarehouseInDO::getOrderItemId, vo.getOrderItemId())
                .eqIfPresent(OrderWarehouseInDO::getExpressNo, vo.getExpressNo())
                .betweenIfPresent(OrderWarehouseInDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderWarehouseInDO::getId));
        }

        return null;
    }


    @ResultType(OrderWarehouseInDO.class)
    @Select("select * from ecw_order_warehouse_in wi where wi.deleted = 0 and " +
            "exists( select 1 from ecw_order_item item where item.deleted = 0 and wi.order_item_id = item.order_item_id and item.prod_id = #{prodId} and item.order_id = #{orderId}) " +
            "order by wi.in_time asc limit 1")
    OrderWarehouseInDO getFirstOneOrderWarehouseInByProdId(@Param("orderId") Long orderId, @Param("prodId") Long prodId);

    @ResultType(OrderWarehouseInBackItemVO.class)
    @Select("select t.*,t1.prod_title_en,t1.prod_title_zh,t1.brand,t1.warehouse_in_prod_attr_ids as prodAttrIds,t1.material,t1.usage_ids from ecw_order_warehouse_in t LEFT JOIN ecw_order_item t1 on t.order_item_id=t1.order_item_id where t.order_id=#{orderId} and t.deleted=0")
    List<OrderWarehouseInBackItemVO> getOrderWarehouseInListOrderByInTime(@Param("orderId") Long orderId);

    @ResultType(OrderWarehouseInSumBackVO.class)
    @Select("select sum(volume) as sum_volume_finished_warehouse_in, sum(weight) as sum_weight_finished_warehouse_in," +
            " sum(cartons_num) as sum_cartons_num_finished_warehouse_in, sum(quantity_all) as sum_quantity_all_finished_warehouse_in " +
            " from ecw_order_warehouse_in where deleted = 0 and order_id = #{orderId}")
    OrderWarehouseInSumBackVO getOrderWarehouseInSum(@Param("orderId") Long orderId);

    @ResultType(OrderWarehouseInDO.class)
    @Select("select * from ecw_order_warehouse_in wi where wi.deleted = 1 and wi.order_id = #{orderId} and wi.order_item_id = #{orderItemId} order by id asc")
    List<OrderWarehouseInDO> getDeletedList(@Param("orderId") Long orderId, @Param("orderItemId") Long orderItemId);


    @ResultType(OrderItemBackVO.class)
    @Select("SELECT wi.* ," +
            " CASE #{lang} WHEN 1 THEN GROUP_CONCAT(DISTINCT dict.label_en) WHEN 2 THEN GROUP_CONCAT(DISTINCT dict.label_fr) ELSE GROUP_CONCAT(DISTINCT dict.label) END as specificationType," +
            " CASE #{lang} WHEN 1 THEN GROUP_CONCAT(DISTINCT spe.label_en) WHEN 2 THEN GROUP_CONCAT(DISTINCT spe.label_fr) ELSE GROUP_CONCAT(DISTINCT spe.label) END as warehouseType," +
            "GROUP_CONCAT(wi.remark)  as  warehouseRecordRemark " +
            "FROM ecw_order_warehouse_in wi LEFT JOIN system_dict_data dict ON dict.dict_type = 'packaging_type'  AND dict.`value` = wi.unit " +
            "LEFT JOIN system_dict_data spe ON spe.dict_type = 'warehousing_specification_type'  AND spe.`value` = wi.specification_type  " +
            "where 1 = 1 AND wi.deleted = 0 and wi.order_id = #{orderId} and wi.order_item_id = #{orderItemId} order by id asc")
    OrderItemBackVO getWarehouseInByItemIdAndOrderId(@Param("orderId") Long orderId, @Param("orderItemId") Long orderItemId, @Param("lang") Integer lang);


}
