package com.cf.imes.module.executor.dal.mysql.orderItem;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.executor.controller.admin.manage.pack.vo.PlateAndPartIdList;
import com.cf.imes.module.executor.controller.admin.manage.pack.vo.PlateDetailsRespVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderGoodsDetailRespVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderGoodsRespVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPlatesDetailRespVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPrintDetailRespVO;
import com.cf.imes.module.executor.controller.admin.orderParts.vo.OrderPartsRespVO;
import com.cf.imes.module.executor.controller.admin.plate.vo.PlateRespVO;
import com.cf.imes.module.executor.dal.dataobject.orderItem.AssemblyComponentChildPrintItemDO;
import com.cf.imes.module.executor.dal.dataobject.orderItem.OrderItemDO;
import com.cf.imes.module.executor.dal.dataobject.orderItem.OrdersViewPartsPageDO;
import com.cf.imes.module.executor.dal.dataobject.orderItem.OrdersViewPlatesPageDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 订单明细表 order_item Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface OrderItemMapper extends BaseMapperX<OrderItemDO> {

    List<PlateRespVO> selectPlatesDetailByOrderId(@Param("orderId") Long orderId, @Param("roomId") Long roomId,
                                                  @Param("bodyId") Long bodyId, @Param("groupId") Long groupId,
                                                  @Param("groupName") String groupName, @Param("organId") Long organId,
                                                  @Param("deleted") Integer deleted);

    IPage<PlateRespVO> selectPlatesDetailByOrderId(@Param("page") IPage<PlateRespVO> page, @Param("orderId") Long orderId, @Param("roomId") Set<Long> roomId,
                                                   @Param("bodyId") Set<Long> bodyId, @Param("groupId") Set<Long> groupId,
                                                   @Param("groupName") String groupName, @Param("organId") Long organId,
                                                   @Param("deleted") Integer deleted);

    List<OrderPartsRespVO> selectPartsDetailByOrderId(@Param("orderId") Long orderId, @Param("roomId") Long roomId,
                                                      @Param("bodyId") Long bodyId, @Param("name") String name,
                                                      @Param("organId") Long organId, @Param("deleted") Integer deleted);

    IPage<OrderPartsRespVO> selectPartsDetailByOrderId(@Param("page") IPage<OrderPartsRespVO> page, @Param("orderId") Long orderId, @Param("roomId") Set<Long> roomId,
                                                       @Param("bodyId") Set<Long> bodyId, @Param("groupId") Set<Long> groupId, @Param("name") String name,
                                                       @Param("organId") Long organId, @Param("deleted") Integer deleted, @Param("types") Set<String> types);


    List<OrderPlatesDetailRespVO> selectPartsByOrderId(@Param("orderId") Long orderId,
                                                       @Param("organId") Long organId,
                                                       @Param("deleted") Integer deleted,
                                                       @Param("types") Set<String> types);

    void deleteByPlateId(@Param("deletePlateIds") List<Long> deletePlateIds);



    default int deleteByBodyId(Long bodyId, Long organId) {
        return delete(new LambdaQueryWrapperX<OrderItemDO>()
                .eq(OrderItemDO::getBodyId, bodyId)
                .eq(OrderItemDO::getOrganId, organId));
    }

    default int deleteByOrderId(Long orderId, Long organId) {
        return delete(new LambdaQueryWrapperX<OrderItemDO>()
                .eq(OrderItemDO::getOrganId, organId)
                .eqIfPresent(OrderItemDO::getOrderId, orderId));
    }
    // 批量删除
    default int deleteBatch(Collection<?> orderId) {
        return delete(new LambdaQueryWrapperX<OrderItemDO>()
                .in(OrderItemDO::getOrderId, orderId));
    }


    default int deleteByPlateId(List<Long> plateId,List<Long> orderId, Long organId) {
        return delete(new LambdaQueryWrapperX<OrderItemDO>()
                .eq(OrderItemDO::getOrganId, organId)
                .in(OrderItemDO::getOrderId, orderId)
                .in(OrderItemDO::getPlateId, plateId));

    }



    default List<OrderItemDO> selectPartsList(List<Long> partIds,List<Long> orderIds, Long organId) {
        return selectList(new LambdaQueryWrapperX<OrderItemDO>()
                .eq(OrderItemDO::getOrganId,organId)
                .inIfPresent(OrderItemDO::getOrderId,orderIds)
                .inIfPresent(OrderItemDO::getPartsId, partIds)
                .select(OrderItemDO::getNum));

    }


    default List<OrderItemDO> selectPlateList(List<Long> plateId,List<Long> orderId, Long organId) {
        return selectList(new LambdaQueryWrapperX<OrderItemDO>()
                .eq(OrderItemDO::getOrganId,organId)
                .inIfPresent(OrderItemDO::getOrderId,orderId)
                .inIfPresent(OrderItemDO::getPlateId, plateId)
                .select(OrderItemDO::getBodyId,OrderItemDO::getPlateId));

    }


    /**
     * 查询订单条件下的包裹id
     *
     * @param orderId
     * @param bodyIds
     * @param organId
     * @return
     */
    default List<OrderItemDO> selectPackId(Long orderId, List<Long> bodyIds, Long organId) {
        return selectList(new LambdaQueryWrapperX<OrderItemDO>()
                .eq(OrderItemDO::getOrganId, organId)
                .eq(OrderItemDO::getOrderId, orderId)
                .inIfPresent(OrderItemDO::getBodyId, bodyIds)
                .select(OrderItemDO::getPackageId));

    }

    /**
     * 重置订单包裹编号下的包裹号、分堆打包状态和数量
     *
     * @param orderId
     * @param packIds
     * @param organId
     */
    void deletePackList(@Param("orderId") Long orderId, @Param("packIds") Set<String> packIds, @Param("organId") Long organId);

    List<OrderGoodsRespVO> selectGoodsByOrderId(@Param("orderId") Long orderId,
                                                @Param("organId") Long organId,
                                                @Param("deleted") Integer deleted);

    IPage<OrderGoodsDetailRespVO> selectGoodsDetailByOrderId(@Param("page") IPage<OrderGoodsDetailRespVO> page, @Param("orderId") Long orderId, @Param("roomId") Set<Long> roomId,
                                                             @Param("bodyId") Set<Long> bodyId, @Param("groupId") Set<Long> groupId, @Param("goodsId") Set<Long> goodsId,
                                                             @Param("organId") Long organId, @Param("deleted") Integer deleted);

    Integer countPartsNotId(@Param("orderId") Long orderId, @Param("bodyId") Set<Long> bodyIds, @Param("goodsId") Long partsId,
                            @Param("organId") Long organId, @Param("deleted") Integer deleted);

    /**
     * 查询订单条件下是否有已排单的板件
     *
     * @param orderId
     * @param roomIds
     * @param bodyId
     * @param organId
     * @return
     */
    List<Integer> selectExistOrderPlan(@Param("orderId") Long orderId, @Param("roomIds") Set<Long> roomIds, @Param("bodyId") Long bodyId, @Param("organId") Long organId);

    List<PlateDetailsRespVO> selectPrintPlateList(@Param("orderNo")Long orderNo, @Param("packageNo") Long packageNo, @Param("organId") Long organId);



    void deleteOrderPack(@Param("packId") Long packId,@Param("orderId") Long orderId,@Param("organId") Long organId);



    void deleteOtherParts(@Param("partsId") Long partsId,@Param("orderId") Long orderId,@Param("organId") Long organId);


    void insertPartsId(@Param("packId") Long packId, @Param("addPartsIdList") List<Long> addPartsIdList,@Param("orderId") Long orderId,@Param("organId") Long organId);


    default List<OrderItemDO> selectPackList(Long packId, Long orderId , Long organId) {
        return selectList(new LambdaQueryWrapperX<OrderItemDO>()
                .eq(OrderItemDO::getOrganId, organId)
                .eqIfPresent(OrderItemDO::getOrderId,orderId)
                .eqIfPresent(OrderItemDO::getPackageId, packId));
    }


    void deleteParts(@Param("packageId") Long packageId,@Param("orderId") Long orderId ,@Param("partsIds")  List<Long> partsIds,@Param("organId") Long organId);

    void updateParts(@Param("partsId")  Long partsId,@Param("packageId")  Long packageId,@Param("orderId") Long orderId,@Param("organId") Long organId);


    default List<OrderItemDO> selectPackPlateList(Long orderId, Long organId) {
        return selectList(new LambdaQueryWrapperX<OrderItemDO>()
                .eq(OrderItemDO::getOrganId, organId)
                .eqIfPresent(OrderItemDO::getOrderId, orderId));
    }


    default List<OrderItemDO> selectPackByPlateId(List<Long> plateId, Long orderId , Long organId) {
        return selectList(new LambdaQueryWrapperX<OrderItemDO>()
                .eq(OrderItemDO::getOrganId, organId)
                .eqIfPresent(OrderItemDO::getOrderId,orderId)
                .inIfPresent(OrderItemDO::getPlateId, plateId)
                .select(OrderItemDO::getPackageId));
    }


    List<PlateAndPartIdList> selectOrderPackPartsIds(@Param("orderId") Long orderId, @Param("organId") Long organId);

    List<PlateAndPartIdList> selectOrderPackPlateIds(@Param("orderId") Long orderId,@Param("organId") Long organId);

    /**
     * 分页查询订单板件视图数据
     *
     * @param page         分页参数
     * @param orderIds     订单ID
     * @param bodyIds      柜体ID列表
     * @param groupId      加工组ID列表
     * @param componentIds 部件ID列表
     * @param cadView      是否需要cad图纸内信息：cad_view_id、seal_left、seal_down、seal_right、seal_up
     * @return
     */
    IPage<OrdersViewPlatesPageDO> selectOrdersViewPlatesPage(@Param("page") IPage<OrdersViewPlatesPageDO> page,
                                                             @Param("orderIds") List<Long> orderIds,
                                                             @Param("bodyIds") Set<Long> bodyIds,
                                                             @Param("groupIds") Set<Long> groupId,
                                                             @Param("componentIds") Set<Long> componentIds,
                                                             @Param("cadView") boolean cadView,
                                                             @Param("deleted") Boolean deleted);

    /**
     * 分页查询订单板件id
     *
     * @param page
     * @param orderIds
     * @param bodyIds
     * @param groupId
     * @param componentIds
     * @return
     */
    IPage<Long> selectOrdersViewPlateIdsPage(@Param("page") IPage<Long> page,
                                             @Param("orderIds") List<Long> orderIds,
                                             @Param("bodyIds") Set<Long> bodyIds,
                                             @Param("groupIds") Set<Long> groupId,
                                             @Param("componentIds") Set<Long> componentIds,
                                             @Param("deleted") Boolean deleted);

    /**
     * 分页查询订单配件视图数据
     *
     * @param page         分页参数
     * @param orderId      订单ID
     * @param bodyIds      柜体ID列表
     * @param groupIds     加工组ID列表
     * @param componentIds 部件ID列表
     * @param cadView      是否需要cad图纸内信息：cad_view_id、seal_left、seal_down、seal_right、seal_up
     * @return
     */
    IPage<OrdersViewPartsPageDO> selectOrdersViewPartsPage(@Param("page") IPage<OrdersViewPartsPageDO> page,
                                                           @Param("orderIds") List<Long> orderId,
                                                           @Param("bodyIds") Set<Long> bodyIds,
                                                           @Param("groupIds") Set<Long> groupIds,
                                                           @Param("componentIds") Set<Long> componentIds,
                                                           @Param("name") String name,
                                                           @Param("cadView") boolean cadView,
                                                           @Param("deleted") Boolean deleted);

    /**
     * 分页查询订单板件id
     *
     * @param page
     * @param orderIds
     * @param bodyIds
     * @param groupId
     * @param componentIds
     * @return
     */
    IPage<OrdersViewPartsPageDO> selectOrdersViewPartsIdsPage(@Param("page") IPage<OrdersViewPartsPageDO> page,
                                                              @Param("orderIds") List<Long> orderIds,
                                                              @Param("bodyIds") Set<Long> bodyIds,
                                                              @Param("groupIds") Set<Long> groupId,
                                                              @Param("componentIds") Set<Long> componentIds,
                                                              @Param("name") String name,
                                                              @Param("deleted") Boolean deleted);

    List<OrdersViewPartsPageDO> selectOrdersViewPartsList(@Param("orderIds") List<Long> orderIds,
                                                          @Param("bodyIds") Set<Long> bodyIds,
                                                          @Param("groupIds") Set<Long> groupIds,
                                                          @Param("componentIds") Set<Long> componentIds,
                                                          @Param("name") String name,
                                                          @Param("deleted") Boolean deleted);

    /**
     * 查询部件关联的板件ID列表
     *
     * @param componentIds
     * @param orderIds
     * @param groupIds
     * @return
     */
    List<String> selectComponentCorrelationPlateIds(@Param("orderIds") List<Long> orderIds, @Param("componentIds") List<Long> componentIds, @Param("groupIds") List<Long> groupIds);

    /**
     * 查询部件关联的部件ID列表
     *
     * @param componentIds
     * @param orderIds
     * @param groupIds
     * @return
     */
    List<String> selectComponentCorrelationPartsIds(@Param("orderIds") List<Long> orderIds, @Param("componentIds") List<Long> componentIds, @Param("groupIds") List<Long> groupIds);


    /**
     * 查询订单下板件视图信息列表
     *
     * @param orderId
     * @param bodyIds
     * @param componentIds
     * @return
     */
    List<OrdersViewPlatesPageDO> selectOrderViewPlates(@Param("orderId") Long orderId,
                                                      @Param("bodyIds") Set<Long> bodyIds,
                                                      @Param("componentIds") Set<Long> componentIds);

    /**
     * 查询订单下板件视图信息列表
     *
     * @param orderId
     * @param bodyIds
     * @param componentIds
     * @return
     */
    List<OrdersViewPartsPageDO> selectOrderViewParts(@Param("orderId") Long orderId,
                                                      @Param("bodyIds") Set<Long> bodyIds,
                                                      @Param("componentIds") Set<Long> componentIds);

    /**
     * 查询订单板件打印信息列表
     *
     * @param orderIds
     * @param componentIds
     * @return
     */
    List<OrderPrintDetailRespVO> selectOrdersPrintPlatesList(@Param("orderIds") List<Long> orderIds, @Param("componentIds") Set<Long> componentIds);

    /**
     * 查询订单板件打印信息列表
     *
     * @param orderIds
     * @param componentIds
     * @return
     */
    List<OrderPrintDetailRespVO> selectOrdersPrintPartsList(@Param("orderIds") List<Long> orderIds, @Param("componentIds") Set<Long> componentIds);

    /**
     * 查询临时表所需数据
     *
     * @param orderId
     * @param organId
     * @param bodyIds
     * @param componentIds
     * @return
     */
    List<OrderItemDO> selectOrderItemForTemp(@Param("orderId") Long orderId,
                                             @Param("organId") Long organId,
                                             @Param("lastId") Long lastId,
                                             @Param("pageSize") int pageSize,
                                             @Param("bodyIds") Set<Long> bodyIds,
                                             @Param("compIds") Set<Long> componentIds);

    /**
     * 物理删除订单材质
     *
     * @return
     */
    int physicalDeleteByOrderIds(@Param("ids") List<Long> ids);

    /**
     * 查询部件板件列表
     *
     * @param orderIds
     * @param componentIds
     * @return
     */
    List<AssemblyComponentChildPrintItemDO> selectAssemblyComponentPrintPlates(
            @Param("orderIds") List<Long> orderIds,
            @Param("componentIds") Set<Long> componentIds);

    /**
     * 查询部件配件列表
     *
     * @param orderIds
     * @param componentIds
     * @return
     */
    List<AssemblyComponentChildPrintItemDO> selectAssemblyComponentPrintParts(
            @Param("orderIds") List<Long> orderIds,
            @Param("componentIds") Set<Long> componentIds);
}