package com.cf.imes.module.executor.service.plan;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.cf.imes.framework.common.enums.PlanFilterTypeEnum;
import com.cf.imes.framework.common.enums.PlanStatusEnum;
import com.cf.imes.framework.common.util.json.JsonUtils;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.cf.imes.framework.mybatis.core.query.QueryWrapperX;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderRespVO;
import com.cf.imes.module.executor.dal.dataobject.goods.GoodsDO;
import com.cf.imes.module.executor.dal.dataobject.order.OrderDO;
import com.cf.imes.module.executor.dal.dataobject.orderBody.OrderBodyDO;
import com.cf.imes.module.executor.dal.dataobject.orderItem.OrderItemDO;
import com.cf.imes.module.executor.dal.dataobject.planitem.PlanItemDO;
import com.cf.imes.module.executor.dal.dataobject.plate.PlateDO;
import com.cf.imes.module.executor.dal.mysql.goods.GoodsMapper;
import com.cf.imes.module.executor.dal.mysql.module.ModuleMapper;
import com.cf.imes.module.executor.dal.mysql.order.OrderMapper;
import com.cf.imes.module.executor.dal.mysql.orderBody.OrderBodyMapper;
import com.cf.imes.module.executor.dal.mysql.orderItem.OrderItemMapper;
import com.cf.imes.module.executor.dal.mysql.planitem.PlanItemMapper;
import com.cf.imes.module.executor.dal.mysql.plate.PlateMapper;
import com.cf.imes.module.executor.enums.OrderStatusEnum;
import com.cf.imes.module.system.api.machine.MachineApi;
import com.cf.imes.module.system.api.machine.dto.CuttingRespDTO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import com.cf.imes.module.executor.controller.admin.plan.vo.*;
import com.cf.imes.module.executor.dal.dataobject.plan.PlanDO;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;

import com.cf.imes.module.executor.dal.mysql.plan.PlanMapper;

import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getUserOrganId;
import static com.cf.imes.module.executor.enums.ErrorCodeConstants.*;

/**
 * 生产单开料排单 Service 实现类
 *
 * @author 晨丰科技
 */
@Service
@Validated
public class PlanServiceImpl implements PlanService {

    @Resource
    private PlanMapper planMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private PlanItemMapper planItemMapper;

    @Resource
    private OrderItemMapper orderItemMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private PlateMapper plateMapper;

    @Resource
    private MachineApi machineApi;

    @Resource
    private ModuleMapper moduleMapper;

    @Resource
    private OrderBodyMapper orderBodyMapper;

    @Resource
    private IdentifierGenerator identifierGenerator;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createPlan(PlanSaveReqVO createReqVO) {
        List<PlanSaveReqVO.Item> itemList = createReqVO.getItemList();
        // 插入
        long sort = System.currentTimeMillis();
        PlanDO plan = BeanUtils.toBean(createReqVO, PlanDO.class);
        plan.setIsPay(Boolean.FALSE);
        plan.setSort(sort);
        plan.setPlanNo(identifierGenerator.nextId(null).toString());
        String orderNos = itemList.stream().map(i -> i.getOrderId()).distinct().collect(Collectors.toList()).toString();
        plan.setOrderNos(orderNos);
        planMapper.insert(plan);

        // 修改对应的生产单的状态为 生产中
        List<Long> orderIds = JSON.parseArray(orderNos).toJavaList(Long.class);
        List<OrderDO> orderDOS = orderMapper.selectBatchIds(orderIds).stream().map(m -> m.setStatus(OrderStatusEnum.IN_PRODUCTION.getStatus())).collect(Collectors.toList());
        orderMapper.updateBatch(orderDOS);

        Long planId = plan.getId();
       /* List<Long> orderIds = createReqVO.getOrderIds();
        if (CollectionUtil.isNotEmpty(orderIds)) {
            ArrayList<PlanOrderDO> planOrderDOS = new ArrayList<>();
            for (Long orderId : orderIds) {
                planOrderDOS.add(PlanOrderDO.builder()
                        .planId(planId)
                        .orderId(orderId)
                        .build());
            }
            planOrderMapper.insertBatch(planOrderDOS);
        }*/
        //List<Long> plateIds = createReqVO.getPlateIds();


        Set<Long> itemIds = plateMapper.selectBatchOrderIdsAndGoodsIds(itemList,getUserOrganId());

        if (CollectionUtil.isNotEmpty(itemIds)) {
            ArrayList<PlanItemDO> planItemDOS = new ArrayList<>();
            for (Long itemId : itemIds) {
                planItemDOS.add(PlanItemDO.builder()
                        .planId(planId)
                        .itemId(itemId)
                        .build());
            }
            planItemMapper.insertBatch(planItemDOS);
        }
        // 返回
        return plan.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePlan(PlanSaveReqVO updateReqVO) {
        // 校验存在
        validatePlanExists(updateReqVO.getId());
        // 更新
        PlanDO plan = BeanUtils.toBean(updateReqVO, PlanDO.class);
        plan.setIsPay(Boolean.FALSE);
        planMapper.updateById(plan);
        Long planId = plan.getId();

        /*planOrderMapper.delete(new LambdaQueryWrapperX<PlanOrderDO>().eq(PlanOrderDO::getPlanId, planId));*/

       /* List<Long> orderIds = updateReqVO.getOrderIds();
        if (CollectionUtil.isNotEmpty(orderIds)) {
            ArrayList<PlanOrderDO> planOrderDOS = new ArrayList<>();
            for (Long orderId : orderIds) {
                planOrderDOS.add(PlanOrderDO.builder()
                        .planId(planId)
                        .orderId(orderId)
                        .build());
            }
            planOrderMapper.insertBatch(planOrderDOS);
        }*/
       /* planItemMapper.delete(new LambdaQueryWrapperX<PlanItemDO>().eq(PlanItemDO::getPlanId, planId));
        List<Long> plateIds = updateReqVO.getPlateIds();
        if (CollectionUtil.isNotEmpty(plateIds)) {
            ArrayList<PlanItemDO> planItemDOS = new ArrayList<>();
            for (Long plateId : plateIds) {
                planItemDOS.add(PlanItemDO.builder()
                        .planId(planId)
                        .itemId(plateId)
                        .build());
            }
            planItemMapper.insertBatch(planItemDOS);
        }*/

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deletePlan(Long id) {
        // 校验存在
        PlanDO planDO = planMapper.selectById(id);
        if (planDO == null) {
            throw exception(PLAN_NOT_EXISTS);
        }
        if (planDO.getStatus().equals(PlanStatusEnum.DURINGCUTTING.getStatus()) || planDO.getStatus().equals(PlanStatusEnum.OPENEDMATERIAL.getStatus())) {
            throw exception(PLAN_NOT_ALLOW_DELETE);
        }
        // 删除
        planMapper.deleteById(id);
        /*planOrderMapper.delete(new LambdaQueryWrapperX<PlanOrderDO>().eq(PlanOrderDO::getPlanId, id));*/
        planItemMapper.delete(new LambdaQueryWrapperX<PlanItemDO>().eq(PlanItemDO::getPlanId, id));

        // 修改生产单的状态为待排单
        List<Long> orderIds = JSON.parseArray(planDO.getOrderNos()).toJavaList(Long.class);
        List<OrderDO> orderDOS = orderMapper.selectBatchIds(orderIds).stream().map(m -> m.setStatus(OrderStatusEnum.NO_SORT.getStatus())).collect(Collectors.toList());
        orderMapper.updateBatch(orderDOS);

        return Boolean.TRUE;
    }

    private void validatePlanExists(Long id) {
        if (planMapper.selectById(id) == null) {
            throw exception(PLAN_NOT_EXISTS);
        }
    }

    @Override
    public PlanRespVO getPlan(Long id) {
        PlanDO planDO = planMapper.selectById(id);
        MPJLambdaWrapper<OrderItemDO> wrapper = new MPJLambdaWrapperX<OrderItemDO>().select(OrderItemDO::getOrderId)
                .rightJoin(PlanItemDO.class, PlanItemDO::getItemId, OrderItemDO::getId)
                .isNotNull(OrderItemDO::getId);
        Set<Long> orderIds = orderItemMapper.selectJoinList(OrderItemDO.class, wrapper).stream().map(OrderItemDO::getOrderId).collect(Collectors.toSet());

        /*Set<Long> orderIds = planOrderMapper.selectList(new LambdaQueryWrapperX<PlanOrderDO>().eq(PlanOrderDO::getPlanId, planDO.getPlanNo()))
                .stream()
                .map(e -> e.getOrderId())
                .collect(Collectors.toSet());*/
        PlanRespVO respVO = BeanUtils.toBean(planDO, PlanRespVO.class);
        List<GoodsDO> goodsDOS = goodsMapper.selectList(new LambdaQueryWrapperX<GoodsDO>()
                .eq(GoodsDO::getOrganId,getUserOrganId())
                .in(GoodsDO::getOrderId, orderIds));
        List<PlateDO> plateDOS = plateMapper.selectList(new LambdaQueryWrapperX<PlateDO>()
                .eq(PlateDO::getOrganId,getUserOrganId())
                .in(PlateDO::getGoodsId, goodsDOS.stream().map(GoodsDO::getGoodsId).collect(Collectors.toSet())));
        List<PlateInfoVO> plateInfoVOS = goodsDOS.stream().map(e -> PlateInfoVO.builder()
                        .material(e.getMaterial())
                        .width(e.getWidth())
                        .height(e.getHeight())
                        .thickness(e.getThickness())
                        .area(e.getHeight().multiply(e.getWidth()).setScale(2, RoundingMode.HALF_UP))
                        .count(plateDOS.stream().filter(f -> Objects.equals(f.getGoodsId(), e.getGoodsId())).collect(Collectors.toSet()).size())
                        .build())
                .collect(Collectors.toList());
        respVO.setPlateInfoList(plateInfoVOS);
        return respVO;
    }

    @Override
    public PageResult<PlanRespVO> getPlanPage(PlanPageReqVO pageReqVO) {
        PageResult<PlanDO> planDOPageResult = planMapper.selectPage(pageReqVO);
        if (CollectionUtil.isEmpty(planDOPageResult.getList())) {
            return new PageResult<>();
        }
        Set<Long> macheineIds = planDOPageResult.getList().stream().map(PlanDO::getMachineId).collect(Collectors.toSet());
        List<CuttingRespDTO> machines = machineApi.list(macheineIds).getCheckedData();


        List<Long> planIds = planDOPageResult.getList().stream().map(PlanDO::getId).collect(Collectors.toList());
//        // 根据排单ID查询排单明细表对应的明细ID
//        List<PlanItemDO> itemDOS = planItemMapper.selectList(new LambdaQueryWrapperX<PlanItemDO>().in(PlanItemDO::getPlanId, ids));
//
//        List<GoodsDO> goodsDOS = goodsMapper.selectList(new LambdaQueryWrapperX<GoodsDO>().in(GoodsDO::getOrderId, ids));
//
//        List<Long> longs = goodsDOS.stream().map(GoodsDO::getGoodsId).collect(Collectors.toList());
//
//
//        List<PlateDO> plateDOS = plateMapper.selectList(new LambdaQueryWrapperX<PlateDO>().in(PlateDO::getGoodsId,longs ));

        List<PlateInfoVO> plateInfoVOS = planItemMapper.selectGoodsList(planIds,getUserOrganId());


        PageResult<PlanRespVO> planRespVOPageResult = BeanUtils.toBean(planDOPageResult, PlanRespVO.class);

//        planRespVOPageResult.getList().forEach(e -> {
//            e.setMachineName(machines.stream()
//                    .filter(f -> Objects.equals(f.getId(), e.getMachineId()))
//                    .map(CuttingRespDTO::getName)
//                    .findAny()
//                    .orElse(null));
//            e.setPlateInfoList(goodsDOS.stream()
//                    .filter(f -> Objects.equals(f.getOrderId(), e.getId()))
//                    .map(p -> PlateInfoVO.builder()
//                            .material(p.getMaterial())
//                            .width(p.getWidth())
//                            .height(p.getHeight())
//                            .thickness(p.getThickness())
//                            .color(p.getColor())
//                            .area(p.getHeight().multiply(p.getWidth()).setScale(2, RoundingMode.HALF_UP))
//                            .count(plateDOS.stream().filter(f -> Objects.equals(f.getGoodsId(), p.getGoodsId())).collect(Collectors.toSet()).size())
//                            .build())
//                    .collect(Collectors.toList()));
//        });

        planRespVOPageResult.getList().forEach(e -> {
            e.setMachineName(machines.stream()
                    .filter(f -> Objects.equals(f.getId(), e.getMachineId()))
                    .map(CuttingRespDTO::getName)
                    .findAny()
                    .orElse(null));
            e.setPlateInfoList(plateInfoVOS);
        });

        return planRespVOPageResult;
    }

    @Override
    public PageResult<OrderRespVOCopy> getOrderPage(OrderPageReqVOCopy pageReqVO) {
        PageDTO<OrderRespVOCopy> page = new PageDTO<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());

        QueryWrapperX<OrderDO> queryWrapperX = new QueryWrapperX<>();
        queryWrapperX
                .eq("a.organ_id",getUserOrganId())
                .eq("a.status",OrderStatusEnum.NO_SORT.getStatus())
                .eqIfPresent("a.id", pageReqVO.getOrderId())
                .isNull("opi.item_id")
                .eqIfPresent("a.customer", pageReqVO.getConsignee())
                .eqIfPresent("a.custom_order_no", pageReqVO.getDefaultId())
                .eqIfPresent("a.address", pageReqVO.getConsigneeAddress())
                .betweenIfPresent("a.create_time", pageReqVO.getCreateTime())
                .eqIfPresent("op.is_special_shaped", pageReqVO.getSpecialShaped())
                .eqIfPresent("op.is_sculpt", pageReqVO.getSculpt())
                .eqIfPresent("op.is_door", pageReqVO.getIsDoor())
                .betweenIfPresent("op.height", new BigDecimal[]{pageReqVO.getLongMinRang(), pageReqVO.getLongMaxRang()})
                .betweenIfPresent("op.width", new BigDecimal[]{pageReqVO.getWidthMinRang(), pageReqVO.getWidthMaxRang()})
                .groupBy("a.id", " a.order_date", "a.delivery_date", "a.customer", "a.address", "a.custom_order_no")
                .orderByDesc("a.create_time");
        List<Integer> filterTypes = new ArrayList<>();
        if (Objects.nonNull(pageReqVO.getHoleThrough()) && pageReqVO.getHoleThrough()) {
            filterTypes.add(PlanFilterTypeEnum.DIGGINGTHROUGHTHESHAPE.getType());
        }
        if (Objects.nonNull(pageReqVO.getBurrow()) && pageReqVO.getBurrow()) {
            filterTypes.add(PlanFilterTypeEnum.THEREAREPERFORATEDHOLES.getType());
        }
        if (Objects.nonNull(pageReqVO.getTwoDimensionalToolPath()) && pageReqVO.getTwoDimensionalToolPath()) {
            filterTypes.add(PlanFilterTypeEnum.TWODIMENSIONALCUTTINGPATH.getType());
        }

        queryWrapperX.inIfPresent("op.filter_type", filterTypes);

        IPage<OrderRespVOCopy> orderRespVOCopies = orderMapper.selectOrderPage(page, queryWrapperX);

        List<OrderRespVOCopy> records = orderRespVOCopies.getRecords();
        for (OrderRespVOCopy orderRespVOCopy : records) {

            List<PlatePage> platePages = plateMapper.selectPlatePage(orderRespVOCopy.getOrderId(),getUserOrganId()/*, orderRespVOCopy.getGoodsId()*/);
            orderRespVOCopy.setPlatePages(platePages);

        }


        if (CollectionUtil.isEmpty(records)) {
            return new PageResult<>();
        }

        return new PageResult<>(orderRespVOCopies.getRecords(), orderRespVOCopies.getTotal());


//        IPage<OrderRespVOCopy> orderDOPageResult = orderMapper.selectOrderPage(queryWrapperX);

//        if (CollectionUtil.isEmpty(orderDOPageResult.getRecords())) {
//            return new PageResult<>();
//        }
//        return new PageResult<>(orderDOPageResult.getRecords(), orderDOPageResult.getTotal());
    }

    @Override
    public List<PlatePage> getNotPlanPlateListPage(PlateReqPageVO pageVO) {
//        PageDTO<PlatePage> page = new PageDTO<>(pageVO.getPageNo(), pageVO.getPageSize());
        List<PlatePage> pageRes = plateMapper.selectPlatePage(/*page, */pageVO.getOrderId(),getUserOrganId()/*, pageVO.getGoodsId()*/);
        return pageRes;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addPlate(AddPlateReq req) {
        List<OrderItemDO> orderItemDOS = new ArrayList<>();
        List<PlanItemDO> planItemDOS = new ArrayList<>();
        for (AddPlateReq.Obj obj : req.getList()) {
            orderItemDOS.add(OrderItemDO.builder()
                    .planId(req.getPlanId())
                    .orderId(obj.getOrderId())
                    .plateId(obj.getPlateId())
                    .type(1)
                    .build());

            planItemDOS.add(PlanItemDO.builder()
                    .planId(req.getPlanId())
                    .itemId(obj.getPlateId())
                    .build());
        }
        orderItemMapper.insertBatch(orderItemDOS);
        planItemMapper.insertBatch(planItemDOS);
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean cancellation(Long id) {
        PlanDO planDO = planMapper.selectById(id);
        if (planDO == null) {
            throw exception(PLAN_NOT_EXISTS);
        }
        if (planDO.getStatus().equals(PlanStatusEnum.NEWORDER.getStatus()) || planDO.getStatus().equals(PlanStatusEnum.BOARDHASBEENAPPLIEDFOR.getStatus())) {
            throw exception(PLAN_NOT_ALLOW_CANCEL);
        }
        Set<Long> itemIds = planItemMapper.selectList(new LambdaQueryWrapperX<PlanItemDO>()
                        .eq(PlanItemDO::getOrganId,getUserOrganId())
                        .eq(PlanItemDO::getPlanId, id))
                .stream().map(PlanItemDO::getItemId)
                .collect(Collectors.toSet());
        plateMapper.update(new LambdaUpdateWrapper<PlateDO>().in(PlateDO::getId, itemIds).set(PlateDO::getIsCancel, Boolean.TRUE));
        return Boolean.TRUE;
    }

    @Override
    public PageResult<PlateResList> getPlateByPlanId(GetPlateByPlanIdVO vo) {
        validatePlanExists(vo.getPlanId());
        QueryWrapperX<PlateDO> queryWrapperX = new QueryWrapperX<>();
        List<Integer> filterTypes = new ArrayList<>();
        if (Objects.nonNull(vo.getHoleThrough()) && vo.getHoleThrough()) {
            filterTypes.add(PlanFilterTypeEnum.DIGGINGTHROUGHTHESHAPE.getType());
        }
        if (Objects.nonNull(vo.getBurrow()) && vo.getBurrow()) {
            filterTypes.add(PlanFilterTypeEnum.THEREAREPERFORATEDHOLES.getType());
        }
        if (Objects.nonNull(vo.getTwoDimensionalToolPath()) && vo.getTwoDimensionalToolPath()) {
            filterTypes.add(PlanFilterTypeEnum.TWODIMENSIONALCUTTINGPATH.getType());
        }

        queryWrapperX.eq("p.plan_id", vo.getPlanId())
                .eq("a.organ_id",getUserOrganId())
                .eqIfPresent("a.is_door", vo.getIsDoor())
                .eqIfPresent("a.is_special_shaped", vo.getSpecialShaped())
                .eqIfPresent("a.is_sculpt", vo.getSculpt())
                .betweenIfPresent("a.width", new BigDecimal[]{vo.getWidthMinRang(), vo.getWidthMaxRang()})
                .betweenIfPresent("a.height", new BigDecimal[]{vo.getLongMinRang(), vo.getLongMaxRang()})
                .inIfPresent("a.filter_type", filterTypes)
                .betweenIfPresent("f.delivery_date", new Date[]{vo.getBeginDate(), vo.getEndDate()});

        PageDTO<PlateResList> page = new PageDTO<>(vo.getPageNo(), vo.getPageSize());
        IPage<PlateResList> pageResult = plateMapper.selectPlateByPlanId(page, queryWrapperX);
        Set<Long> bodyIds = pageResult.getRecords().stream().map(PlateResList::getBodyId).collect(Collectors.toSet());
        if (CollectionUtil.isNotEmpty(bodyIds)) {
            List<OrderBodyDO> orderBodyDOS = orderBodyMapper.selectBatchIds(bodyIds);
            for (PlateResList resList : pageResult.getRecords()) {
                resList.setBodyName(orderBodyDOS.stream().filter(e -> Objects.equals(e.getId(), resList.getBodyId())).map(OrderBodyDO::getName).findAny().orElse(null));
                resList.setRoomName(orderBodyDOS.stream().filter(e -> Objects.equals(e.getRoomId(), resList.getRoomId())).map(OrderBodyDO::getRoomName).findAny().orElse(null));
            }
        }
        return new PageResult<>(pageResult.getRecords(), pageResult.getTotal());
    }

    @Override
    public PageResult<OrderRespVO> getOrderByPlanId(PlanOrderPageReqVO pageReqVO) {
        MPJLambdaWrapperX<OrderDO> lambdaWrapperX = new MPJLambdaWrapperX<>();
        lambdaWrapperX.leftJoin(OrderItemDO.class, OrderItemDO::getOrderId, OrderDO::getId)
                .leftJoin(PlanItemDO.class, PlanItemDO::getItemId, OrderItemDO::getId)
                .eq(PlanItemDO::getOrganId,getUserOrganId())
                .eq(PlanItemDO::getPlanId, pageReqVO.getPlanId());

        PageResult<OrderDO> pageResult = orderMapper.selectJoinPage(pageReqVO, OrderDO.class, lambdaWrapperX);
        return BeanUtils.toBean(pageResult, OrderRespVO.class);
    }

    @Override
    public PageResult<PlanGoodsVO> getNotPlanGoodsListPageByOrderId(NotPlanGoodsListPageVO pageVO) {
        return goodsMapper.selectJoinPage(pageVO, PlanGoodsVO.class, new MPJLambdaWrapperX<GoodsDO>()
                .leftJoin(PlateDO.class, PlateDO::getGoodsId, GoodsDO::getId)
                .leftJoin(OrderItemDO.class, OrderItemDO::getPlateId, PlateDO::getId)
                .leftJoin(PlanItemDO.class, PlanItemDO::getItemId, OrderItemDO::getId)
                .select(GoodsDO::getGoodsId, GoodsDO::getHeight, GoodsDO::getWidth, GoodsDO::getColor, GoodsDO::getGoodsName, GoodsDO::getThickness)
                .selectSum(PlateDO::getArea, "area")
                .selectCount(PlateDO::getId, "num")
                .isNotNull(PlanItemDO::getId)
                .eq(GoodsDO::getOrganId,getUserOrganId())
                .eq(GoodsDO::getOrderId, pageVO.getOrderId())
                .groupBy(GoodsDO::getId)
        );
    }

    @Override
    public Boolean setSort(List<SortVO> list) {
        ArrayList<PlanDO> planDOS = new ArrayList<>();
        list.forEach(e -> {
            planDOS.add(PlanDO.builder()
                    .id(e.getPlanId())
                    .sort(e.getSort())
                    .build());

        });
        planMapper.updateBatch(planDOS);
        return Boolean.TRUE;
    }

}