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.orderParts.vo.OrderPartsRespVO;
import com.cf.imes.module.executor.controller.admin.plate.vo.PlateRespVO;
import com.cf.imes.module.executor.dal.dataobject.orderItem.OrderItemDO;
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 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 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));

    }


    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));

    }


    void deletePackList(@Param("orderId") Long orderId,@Param("packIds") List<Long> 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 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);

    List<Long> selectOrderPlanList(@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 insertPackId(@Param("plateIdList") List<Long> plateIdList, @Param("packId") Long packId,@Param("orderId") Long orderId,@Param("organId") Long organId);


    void deletePackId(@Param("deletePlateIdList") List<Long> deletePlateIdList,@Param("packId") Long packId,@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);


    void insertParts(@Param("addPartsList") List<Long> addPartsList, @Param("packId")Long packId, @Param("orderId") Long orderId,@Param("organId") Long organId);

    void deletePartsList(@Param("deletePartsList")List<Long> deletePartsList,@Param("packId") Long packId,@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);

}