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


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.cf.imes.framework.common.enums.OrderPackageStatusEnum;
import com.cf.imes.framework.common.enums.OrderPackageTypeEnum;
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.PackRespVO;
import com.cf.imes.module.executor.controller.admin.manage.query.vo.pack.OrderPackageVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.LabelOrderPackData;
import com.cf.imes.module.executor.dal.dataobject.pack.OrderPackageDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface OrderPackageMapper extends BaseMapperX<OrderPackageDO> {


    void updatePackList(@Param("orderId") Long orderId, @Param("packIds")  List<Long> packIds, @Param("organId")  Long organId);

    IPage<OrderPackageVO> selectByOrderNo(@Param("page") IPage page, @Param(Constants.WRAPPER) Wrapper<OrderPackageDO> wrapper);

    default OrderPackageDO selectByPackNo(String packNo, Long organId) {
        return selectOne(new LambdaQueryWrapperX<OrderPackageDO>()
                .eq(OrderPackageDO::getOrganId, organId)
                .eqIfPresent(OrderPackageDO::getPackageNo, packNo));
    }



    List<PackRespVO> selectPackPage(@Param("orderIds") List<Long> orderIds, @Param("organId") Long organId);



    default OrderPackageDO selectByPackId(Long packId, Long orderId, Long organId) {
        return selectOne(new LambdaQueryWrapperX<OrderPackageDO>()
                .eq(OrderPackageDO::getOrganId, organId)
                .eq(OrderPackageDO::getDeleted,false)
                .eqIfPresent(OrderPackageDO::getOrderId,orderId)
                .eqIfPresent(OrderPackageDO::getId, packId));
    }



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



    default List<OrderPackageDO> selectPackStatus(Long orderId, Long organId) {
        return selectList(new LambdaQueryWrapperX<OrderPackageDO>()
                .eq(OrderPackageDO::getOrganId,organId)
                .eq(OrderPackageDO::getDeleted,false)
                .eqIfPresent(OrderPackageDO::getOrderId, orderId));
    }


    default List<OrderPackageDO> selectPackList(Long orderId, Long organId) {
        return selectList(new LambdaQueryWrapperX<OrderPackageDO>()
                .eq(OrderPackageDO::getOrganId,organId)
                .eq(OrderPackageDO::getDeleted,false)
                .eqIfPresent(OrderPackageDO::getOrderId, orderId)
                .eqIfPresent(OrderPackageDO::getStatus, OrderPackageStatusEnum.UNPACKED.getStatus()));
    }


    List<PackRespVO> selectPackPieceCount(@Param("orderIds") List<Long> orderIds, @Param("organId") Long organId);


    // 删除当前生产单下所有的包裹
    default void updateDelete(Long orderId,Long organId) {
        LambdaUpdateWrapper<OrderPackageDO> wrapper = new LambdaUpdateWrapper<OrderPackageDO>()
                .set(OrderPackageDO::getDeleted, true)
                .eq(OrderPackageDO::getOrganId,organId)
                .eq(OrderPackageDO::getOrderId, orderId)
                .eq(OrderPackageDO::getType, OrderPackageTypeEnum.PLATE.getType());
        update(wrapper);
    }


    List<OrderPackageVO> selectPackListByOrderId(@Param("orderId") Long orderId,@Param("organId") Long organId);


    default List<OrderPackageDO> selectPackStatusList(Long orderId, Long organId) {
        return selectList(new LambdaQueryWrapperX<OrderPackageDO>()
                .eq(OrderPackageDO::getOrganId,organId)
                .eq(OrderPackageDO::getDeleted,false)
                .eqIfPresent(OrderPackageDO::getOrderId, orderId)
                .eq(OrderPackageDO::getStatus,OrderPackageStatusEnum.PACKAGED.getStatus()));
    }


    List<LabelOrderPackData> selectLabelOrderPackData(@Param("packIds") List<Long> packIds,@Param("organId") Long organId);



    List<LabelOrderPackData> selectPartPack(@Param("orderIds") List<Long> orderIds,@Param("organId") Long organId,@Param("type") Integer type ,@Param("status") Integer status);



    default List<OrderPackageDO> selectPackListByIds(List<Long> packIds, Long organId) {
        return selectList(new LambdaQueryWrapperX<OrderPackageDO>()
                .eq(OrderPackageDO::getOrganId,organId)
                .eq(OrderPackageDO::getDeleted,false)
                .inIfPresent(OrderPackageDO::getId, packIds)
                .eqIfPresent(OrderPackageDO::getStatus,OrderPackageStatusEnum.PACKAGED.getStatus())
                .select(OrderPackageDO::getId,OrderPackageDO::getOrderId,OrderPackageDO::getPackageNo));

    }

}
