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

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.orderWarehouseApproval.OrderWarehouseApprovalDO;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.module.order.vo.orderWarehouseApproval.OrderWarehouseApprovalQueryVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

/**
 * 入仓单审核 Mapper
 * @author yanghao
 */
@Mapper
public interface OrderWarehouseApprovalMapper extends AbstractMapper<OrderWarehouseApprovalDO> {
    @Override
    default PageResult<OrderWarehouseApprovalDO> selectPage(PageVO page, Object object) {
        if (object instanceof OrderWarehouseApprovalQueryVO) {
            OrderWarehouseApprovalQueryVO vo = (OrderWarehouseApprovalQueryVO)object;
            return selectPage(page, new LambdaQuery<OrderWarehouseApprovalDO>()
                .eqIfPresent(OrderWarehouseApprovalDO::getOrderIds, vo.getOrderId())
                .eqIfPresent(OrderWarehouseApprovalDO::getOrderNo, vo.getOrderNo())
                .eqIfPresent(OrderWarehouseApprovalDO::getOrderItemId, vo.getOrderItemId())
                .eqIfPresent(OrderWarehouseApprovalDO::getWarehouseInId, vo.getWarehouseInId())
                .eqIfPresent(OrderWarehouseApprovalDO::getFormId, vo.getFormId())
                .eqIfPresent(OrderWarehouseApprovalDO::getType, vo.getType())
                .eqIfPresent(OrderWarehouseApprovalDO::getFollowUpAdminId, vo.getFollowUpAdminId())
                .eqIfPresent(OrderWarehouseApprovalDO::getStatus, vo.getStatus())
                .eqIfPresent(OrderWarehouseApprovalDO::getProcessingResults, vo.getProcessingResults())
                .eqIfPresent(OrderWarehouseApprovalDO::getFinishBy, vo.getFinishBy())
                .eqIfPresent(OrderWarehouseApprovalDO::getIsBatch, vo.getIsBatch())
                .eqIfPresent(OrderWarehouseApprovalDO::getIsFromUserClient, vo.getIsFromUserClient())
                .betweenIfPresent(OrderWarehouseApprovalDO::getFinishTime, vo.getBeginFinishTime(), vo.getEndFinishTime())
                .betweenIfPresent(OrderWarehouseApprovalDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderWarehouseApprovalDO::getId));
        }

        return null;
    }

    @Override
    default List<OrderWarehouseApprovalDO> selectList(Object object) {
        if (object instanceof OrderWarehouseApprovalQueryVO) {
            OrderWarehouseApprovalQueryVO vo = (OrderWarehouseApprovalQueryVO)object;
            return selectList(new LambdaQuery<OrderWarehouseApprovalDO>()
                .eqIfPresent(OrderWarehouseApprovalDO::getOrderIds, vo.getOrderId())
                .eqIfPresent(OrderWarehouseApprovalDO::getOrderNo, vo.getOrderNo())
                .eqIfPresent(OrderWarehouseApprovalDO::getOrderItemId, vo.getOrderItemId())
                .eqIfPresent(OrderWarehouseApprovalDO::getWarehouseInId, vo.getWarehouseInId())
                .eqIfPresent(OrderWarehouseApprovalDO::getFormId, vo.getFormId())
                .eqIfPresent(OrderWarehouseApprovalDO::getType, vo.getType())
                .eqIfPresent(OrderWarehouseApprovalDO::getFollowUpAdminId, vo.getFollowUpAdminId())
                .eqIfPresent(OrderWarehouseApprovalDO::getStatus, vo.getStatus())
                .eqIfPresent(OrderWarehouseApprovalDO::getProcessingResults, vo.getProcessingResults())
                .eqIfPresent(OrderWarehouseApprovalDO::getFinishBy, vo.getFinishBy())
                .eqIfPresent(OrderWarehouseApprovalDO::getIsBatch, vo.getIsBatch())
                .eqIfPresent(OrderWarehouseApprovalDO::getIsFromUserClient, vo.getIsFromUserClient())
                .betweenIfPresent(OrderWarehouseApprovalDO::getFinishTime, vo.getBeginFinishTime(), vo.getEndFinishTime())
                .betweenIfPresent(OrderWarehouseApprovalDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderWarehouseApprovalDO::getId));
        }

        return null;
    }

    @Select("select * from ecw_order_warehouse_approval where deleted = 0 and type = #{type} and order_ids = #{orderId} order by id desc limit 1")
    OrderWarehouseApprovalDO selectOne(@Param("type") Integer type, @Param("orderId") Long orderId);

    @ResultType(Long.class)
    @Select({
            "<script>",
            "select",
            " count(1)",
            "from ecw_order_warehouse_approval a",
            "where ",
            "a.order_ids = #{orderId} ",
            "<when test = 'typeList != null and typeList.size() > 0'>",
            "and a.type in <foreach item='type' index='index' collection='typeList' open='(' separator=',' close=')'>#{type}</foreach> ",
            "</when>",
            "<when test = 'orderItemIdList != null and orderItemIdList.size() > 0'>",
            "and (a.order_item_id is null or (a.order_item_id is not null and a.order_item_id in <foreach item='orderItemId' index='index' collection='orderItemIdList' open='(' separator=',' close=')'>#{orderItemId}</foreach>)) ",
            "</when>",
            "and a.status = 1 ",
            "</script>"
    })
    long countProcessingApproval(@Param("orderId") Long orderId, @Param("orderItemIdList") List<Long> orderItemIdList, @Param("typeList") List<Integer> typeList);
}
