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

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cf.imes.framework.common.pojo.PageResult;
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.OrderPartsRespVO;
import com.cf.imes.module.executor.controller.admin.manage.query.vo.pack.PackageDetailsRespVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.LabelOrderPartsData;
import com.cf.imes.module.executor.controller.admin.orderParts.vo.OrderPartsPageReqVO;
import com.cf.imes.module.executor.controller.admin.orderParts.vo.OrderPartsPrintRespVO;
import com.cf.imes.module.executor.controller.admin.orderParts.vo.PartGoodsRespVO;
import com.cf.imes.module.executor.controller.admin.plan.saveOptimize.PrintOrderPartsRespVO;
import com.cf.imes.module.executor.dal.dataobject.orderParts.OrderPartsDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getUserOrganId;

/**
 * 订单配件表 order_parts_{N} Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface OrderPartsMapper extends BaseMapperX<OrderPartsDO> {

    default PageResult<OrderPartsDO> selectPage(OrderPartsPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrderPartsDO>()
                .eq(OrderPartsDO::getOrganId,getUserOrganId())
                .eqIfPresent(OrderPartsDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(OrderPartsDO::getGoodsId, reqVO.getGoodsId())
                .likeIfPresent(OrderPartsDO::getName, reqVO.getName())
                .eqIfPresent(OrderPartsDO::getMaterial, reqVO.getMaterial())
                .eqIfPresent(OrderPartsDO::getType, reqVO.getType())
                .eqIfPresent(OrderPartsDO::getModel, reqVO.getModel())
                .eqIfPresent(OrderPartsDO::getSpec, reqVO.getSpec())
                .eqIfPresent(OrderPartsDO::getBrand, reqVO.getBrand())
                .eqIfPresent(OrderPartsDO::getFactory, reqVO.getFactory())
                .eqIfPresent(OrderPartsDO::getUnit, reqVO.getUnit())
                .eqIfPresent(OrderPartsDO::getPrice, reqVO.getPrice())
                .eqIfPresent(OrderPartsDO::getIsComposite, reqVO.getIsComposite())
                .eqIfPresent(OrderPartsDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(OrderPartsDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OrderPartsDO::getId));
    }


    IPage<PartGoodsRespVO> selectProductList(@Param("page") IPage<PartGoodsRespVO> page , @Param("orderId")Long orderId, @Param("roomId")Long roomId , @Param("bodyId")Long bodyId,@Param("organId")Long organId);

    List<PartGoodsRespVO> selectPartDetail(@Param("orderId") Long orderId, @Param("organId") Long organId, @Param("deleted") Integer deleted);// 配件明细

    List<PartGoodsRespVO> selectPartAll(@Param("orderId") Long orderId, @Param("organId") Long organId, @Param("deleted") Integer deleted);// 配件汇总


    // 单个修改删除状态
    default int updateOrderDeleted(Long id, Integer deleted, Long organId) {
        LambdaUpdateWrapper<OrderPartsDO> wrapper = new LambdaUpdateWrapper<OrderPartsDO>()
                .set(OrderPartsDO::getDeleted, deleted)
                .eq(OrderPartsDO::getOrderId, id)
                .eq(OrderPartsDO::getOrganId, organId);
        if (deleted == 0)
            wrapper.eq(OrderPartsDO::getDeleted, 1);
        return update(wrapper);
    }

    void updateDeletedById(@Param("partsIds") List<Long> partsIds, @Param("deleted") Integer deleted, @Param("organId") Long organId,@Param("orderId") Long orderId);

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

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


    void deletedById(@Param("partsIds") List<Long> partsIds, @Param("organId") Long organId);

    List<PrintOrderPartsRespVO> selectPartList( @Param("orderIds") List<Long> orderIds,@Param("organId") Long organId);

    List<Long> selectPartsIdListByBodyId(@Param("bodyIds") Collection<Long> bodyIds, @Param("organId") Long organId, @Param("orderId") Long orderId);

    int countPartsByOrderId(@Param("orderId") Long orderId, @Param("organId") Long organId, @Param("deleted") Integer deleted);

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


    List<OrderPartsDO> selectPlanPartsDataSource(@Param("filed") String filed,@Param("partIds") List<Long> partIds,@Param("organId") Long organId);



    default List<OrderPartsDO> selectPartIds(List<Long> orderIds, Long organId) {
        return selectList(new LambdaQueryWrapperX<OrderPartsDO>()
                .eq(OrderPartsDO::getOrganId,organId)
                .eq(OrderPartsDO::getDeleted,false)
                .in(OrderPartsDO::getOrderId,orderIds)
                .select(OrderPartsDO::getId));
    }

    // 柜体查找配件
    List<OrderPartsPrintRespVO> selectOrderPartsListByBody(@Param("orderId")Long orderId,
                                                           @Param("organId")Long organId,
                                                           @Param("deleted")Integer deleted,
                                                           @Param("roomId")Long roomId,
                                                           @Param("bodyId")Long bodyId);

    /**
     * 查询包裹配件列表
     *
     * @param orderIds
     * @param packageId
     * @return
     */
    List<PackageDetailsRespVO> selectPackedParts(@Param("orderIds") Set<Long> orderIds, @Param("packageNo") String packageId);

    /**
     * 查询包裹配件加工组、部件关联关系
     *
     * @param orderIds
     * @param partsIds
     * @return
     */
    List<PackageDetailsRespVO> selectPackedPartsGroupAndComponent(@Param("orderIds") Set<Long> orderIds, @Param("partsIds") Set<Long> partsIds);

    /**
     * 查询订单列表未打包配件
     *
     * @param orderIds
     * @return
     */
    List<OrderPartsRespVO> selectOrdersPartsList(@Param("orderIds") List<Long> orderIds);

    /**
     * 查询订单列表分堆配件
     *
     * @param orderIds
     * @return
     */
    List<OrderPartsRespVO> selectOrdersHeapSplitPartsList(@Param("orderIds") List<Long> orderIds);



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


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


    List<LabelOrderPartsData> selectLabelOrderPartsData(@Param("orderIds") List<Long> orderIds, @Param("organId") Long organId);

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