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

import java.util.*;

import cn.hutool.core.collection.CollectionUtil;
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.orderWarehousePicture.OrderWarehousePictureDO;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.order.vo.orderWarehousePicture.*;
import org.apache.ibatis.annotations.Param;

/**
 * 订单仓库图片 Mapper
 *
 * @author yanghao
 */
@Mapper
public interface OrderWarehousePictureMapper extends AbstractMapper<OrderWarehousePictureDO> {
    @Override
    default PageResult<OrderWarehousePictureDO> selectPage(PageVO page, Object object) {
        if (object instanceof OrderWarehousePictureQueryVO) {
            OrderWarehousePictureQueryVO vo = (OrderWarehousePictureQueryVO) object;
            return selectPage(page, new LambdaQuery<OrderWarehousePictureDO>()
                    .betweenIfPresent(OrderWarehousePictureDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .eqIfPresent(OrderWarehousePictureDO::getUrl, vo.getUrl())
                    .eqIfPresent(OrderWarehousePictureDO::getType, vo.getType())
                    .eqIfPresent(OrderWarehousePictureDO::getBizId, vo.getBizId())
                    .orderByDesc(OrderWarehousePictureDO::getId));
        }

        return null;
    }

    @Override
    default List<OrderWarehousePictureDO> selectList(Object object) {
        if (object instanceof OrderWarehousePictureQueryVO) {
            OrderWarehousePictureQueryVO vo = (OrderWarehousePictureQueryVO) object;
            return selectList(new LambdaQuery<OrderWarehousePictureDO>()
                    // and (type = ? AND biz_id = ?) OR (type = ? AND biz_id IN (?,?))
                    .and(t ->
                            t.between(vo.getBeginCreateTime() != null && vo.getEndCreateTime() != null, OrderWarehousePictureDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                                    .eq(vo.getType() != null, OrderWarehousePictureDO::getType, vo.getType())
                                    .eq(vo.getBizId() != null, OrderWarehousePictureDO::getBizId, vo.getBizId())
                    )
                    .or(CollectionUtil.isNotEmpty(vo.getOrderItemIdList()),
                            t ->
                                    t.between(vo.getBeginCreateTime() != null && vo.getEndCreateTime() != null, OrderWarehousePictureDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                                            .eq(OrderWarehousePictureDO::getType, 5)
                                            .in(CollectionUtil.isNotEmpty(vo.getOrderItemIdList()), OrderWarehousePictureDO::getBizId, vo.getOrderItemIdList())
                    )
                    .orderByDesc(OrderWarehousePictureDO::getId));
        }

        return null;
    }

    @Delete({
            "<script>",
            "delete from ecw_order_warehouse_picture where 1 = 1 ",
            "<when test = 'type != null'>",
            "AND `type` = #{type}",
            "</when>",
            "<when test = 'bizId != null'>",
            "AND `biz_id` = #{bizId}",
            "</when>",
            "<when test = 'urls != null and urls.size()>0'>",
            "AND url in ",
            "<foreach item='item' index='index' collection='urls' open='(' separator=',' close=')'>#{item}</foreach> ",
            "</when>",
            "</script>"
    })
    void deleteBy(@Param("type") Integer type, @Param("bizId") Long bizId, @Param("urls") List<String> urls);
}
