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

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.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.executor.api.plan.vo.OrderGroupRespVO;
import com.cf.imes.module.executor.controller.admin.plan.bo.OrderIds;
import com.cf.imes.module.executor.controller.admin.plan.vo.OrderGoodsResp;
import com.cf.imes.module.executor.controller.admin.plan.vo.OrderRespVOCopy;
import com.cf.imes.module.executor.dal.dataobject.orderGroup.OrderGroupDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 生产单加工组 Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface OrderGroupMapper extends BaseMapperX<OrderGroupDO> {

    int updatePlateNumById(@Param("orderId") Long orderId, @Param("plateNum") Double plateNum);

    // 生产单中包含的加工组
    List<OrderGroupDO> getOrderGroupByOrderId(@Param("orderId") Long orderId, @Param("organId") Long organId);

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

    // 单个修改删除状态 by bodyId
    default int updateDeletedById(Collection<Long> bodyIds, Integer deleted, Long organId, Long orderId) {
        if (bodyIds != null && !bodyIds.isEmpty()) {
            return update(new LambdaUpdateWrapper<OrderGroupDO>()
                    .set(OrderGroupDO::getDeleted, deleted)
                    .in(OrderGroupDO::getBodyId, bodyIds)
                    .eq(OrderGroupDO::getOrganId, organId)
                    .eq(OrderGroupDO::getOrderId, orderId));
        }
        return 0;
    }

    default int updateDeletedGroup(Collection<Long> bodyIds , Long organId, Long orderId) {
            return update(new LambdaUpdateWrapper<OrderGroupDO>()
                    .set(OrderGroupDO::getDeleted, true)
                    .eq(OrderGroupDO::getOrganId, organId)
                    .eq(OrderGroupDO::getOrderId, orderId)
                    .in(OrderGroupDO::getBodyId, bodyIds));

    }


    default int updateNoDeletedGroup(Collection<Long> bodyIds , Long organId, Long orderId) {
        return update(new LambdaUpdateWrapper<OrderGroupDO>()
                .set(OrderGroupDO::getDeleted, false)
                .eq(OrderGroupDO::getOrganId, organId)
                .eq(OrderGroupDO::getOrderId, orderId)
                .in(OrderGroupDO::getBodyId, bodyIds));

    }


    default int deletedById(Long id, Long organId) {
        return delete(new LambdaUpdateWrapper<OrderGroupDO>()
                .eq(OrderGroupDO::getBodyId, id)
                .eq(OrderGroupDO::getOrganId, organId));
    }

    //    通过柜体id和加工组类型名称查询加工组
    default List<OrderGroupDO> selectByRoomIdAndGroupTypeName(Long orderId, Long bodyId,
                                                              String groupTypeName, Long organId, Integer deleted) {
        return selectList(new LambdaUpdateWrapper<OrderGroupDO>()
                .eq(OrderGroupDO::getOrderId, orderId)
                .eq(OrderGroupDO::getBodyId, bodyId)
                .eq(OrderGroupDO::getGroupTypeName, groupTypeName)
                .eq(OrderGroupDO::getOrganId, organId)
                .eq(OrderGroupDO::getDeleted, deleted));
    }

    //    通过柜体id和加工组类型id加工组名称名称查询加工组
    default OrderGroupDO selectByRoomIdAndGroupTypeIdAndGroupName(Long orderId, Long bodyId,
                                                                  Long groupTypeId, String groupName, Long organId, Integer deleted) {
        return selectOne(new LambdaUpdateWrapper<OrderGroupDO>()
                .eq(OrderGroupDO::getOrderId, orderId)
                .eq(OrderGroupDO::getBodyId, bodyId)
                .eq(OrderGroupDO::getGroupTypeId, groupTypeId)
                .eq(OrderGroupDO::getName, groupName)
                .eq(OrderGroupDO::getOrganId, organId)
                .eq(OrderGroupDO::getDeleted, deleted));
    }

    //    根据柜体id修改删除状态
    default int updateDeletedByBodyId(Long orderId, Long bodyId, Integer deleted, Long organId) {
        return update(new LambdaUpdateWrapper<OrderGroupDO>()
                .set(OrderGroupDO::getDeleted, deleted)
                .eq(OrderGroupDO::getOrderId, orderId)
                .eq(OrderGroupDO::getBodyId, bodyId)
                .eq(OrderGroupDO::getOrganId, organId));
    }

    //    根据生产单id删除
    default int deleteByOrderId(Long orderId, Long organId) {
        return delete(new LambdaQueryWrapperX<OrderGroupDO>()
                .eq(OrderGroupDO::getOrganId, organId)
                .eqIfPresent(OrderGroupDO::getOrderId, orderId));
    }

    // 批量删除
    default int deleteBatch(Collection<?> orderId) {
        return delete(new LambdaQueryWrapperX<OrderGroupDO>()
                .in(OrderGroupDO::getOrderId, orderId));
    }

    // 批量查询
    default List<Long> selectListByBodyId(Collection<Long> bodyIds, Long orderId, Long organId, Integer deleted) {
        return selectList(new LambdaQueryWrapperX<OrderGroupDO>()
                .eq(OrderGroupDO::getOrderId, orderId)
                .eq(OrderGroupDO::getOrganId, organId)
                .in(OrderGroupDO::getBodyId, bodyIds)).stream().map(OrderGroupDO::getId).collect(Collectors.toList());
    }


    List<OrderGroupRespVO> selectPlanGroupDataSource(@Param("filed") String filed, @Param("field2") String field2 ,@Param("groupIds")  List<Long> groupIds, @Param("organId") Long organId);

    // 控制是否删除，进行查询
    default List<OrderGroupDO> selectList(Long orderId, Integer deleted, Long organId) {
        return selectList(new LambdaUpdateWrapper<OrderGroupDO>()
                .eq(OrderGroupDO::getOrderId, orderId)
                .eq(OrderGroupDO::getDeleted, deleted)
                .eq(OrderGroupDO::getOrganId, organId));
    }

    IPage<OrderRespVOCopy> selectFilterOrderPagePlate(@Param("page") IPage page, @Param(Constants.WRAPPER) Wrapper<OrderGroupDO> wrapper);

    List<Long> selectFilterOrderPlateList( @Param(Constants.WRAPPER) Wrapper<OrderGroupDO> wrapper);

    IPage<OrderGoodsResp> selectFilterGoodsPagePlate(@Param("page") IPage page, @Param(Constants.WRAPPER) Wrapper<OrderGroupDO> wrapper);

    List<OrderIds> selectFilterGoodsPlateList(@Param(Constants.WRAPPER) Wrapper<OrderGroupDO> wrapper);

}