package cn.iocoder.yudao.module.order.service.orderWarehousePicture;

import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.order.dal.dataobject.orderItem.OrderItemDO;
import cn.iocoder.yudao.module.order.dal.mysql.orderItem.OrderItemMapper;
import cn.iocoder.yudao.module.order.vo.orderItem.OrderItemQueryVO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.framework.mybatis.core.service.AbstractService;
import cn.iocoder.yudao.module.order.vo.orderWarehousePicture.*;
import cn.iocoder.yudao.module.order.dal.dataobject.orderWarehousePicture.OrderWarehousePictureDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.order.convert.orderWarehousePicture.OrderWarehousePictureConvert;
import cn.iocoder.yudao.module.order.dal.mysql.orderWarehousePicture.OrderWarehousePictureMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.order.enums.ErrorCodeConstants.*;

/**
 * 订单仓库图片 Service 实现类
 *
 * @author yanghao
 */
@Service
@Validated
public class OrderWarehousePictureServiceImpl extends AbstractService<OrderWarehousePictureMapper, OrderWarehousePictureDO> implements OrderWarehousePictureService {

    @Resource
    private OrderWarehousePictureMapper warehousePictureMapper;

    @Resource
    private OrderItemMapper orderItemMapper;

    @Override
    public Long createWarehousePicture(OrderWarehousePictureCreateReqVO createReqVO) {
        // 插入
        OrderWarehousePictureDO warehousePicture = OrderWarehousePictureConvert.INSTANCE.convert(createReqVO);
        warehousePictureMapper.insert(warehousePicture);
        // 返回
        return warehousePicture.getId();
    }

    @Override
    public void createWarehousePictureBatch(List<String> urls, Integer type, Long bizId) {
        if (CollectionUtil.isNotEmpty(urls)) {
            List<OrderWarehousePictureDO> orderWarehousePictureDOList = warehousePictureMapper.selectList(new LambdaQueryWrapperX<OrderWarehousePictureDO>()
                    .eq(OrderWarehousePictureDO::getType, type).eq(OrderWarehousePictureDO::getBizId, bizId));
            if (CollectionUtil.isNotEmpty(orderWarehousePictureDOList)) {
                //待删除
                List<Long> waitToDelete = orderWarehousePictureDOList.stream().filter(t -> !urls.contains(t.getUrl())).map(OrderWarehousePictureDO::getId).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(waitToDelete)) {
                    warehousePictureMapper.deleteBatchIds(waitToDelete);
                }
            }

            //待新增
            List<OrderWarehousePictureDO> waitToAdd = urls.stream().filter(t -> orderWarehousePictureDOList.stream().noneMatch(u -> u.getUrl().equals(t)))
                    .map(url -> new OrderWarehousePictureDO(null, url, type, bizId, null)).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(waitToAdd)) {
                warehousePictureMapper.insertBatch(waitToAdd);
            }
        }
        //若urls为空，则删除原有的影像
        else {
            warehousePictureMapper.deleteBy(type, bizId, null);
        }
    }

    @Override
    public void updateWarehousePicture(OrderWarehousePictureUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateWarehousePictureExists(updateReqVO.getId());
        // 更新
        OrderWarehousePictureDO updateObj = OrderWarehousePictureConvert.INSTANCE.convert(updateReqVO);
        warehousePictureMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehousePicture(Long id) {
        // 校验存在
        this.validateWarehousePictureExists(id);
        // 删除
        warehousePictureMapper.deleteById(id);
    }

    @Override
    public void deleteOrderItemWarehousePicture(Long orderItemId) {
        warehousePictureMapper.deleteBy(5, orderItemId, null);
    }

    private void validateWarehousePictureExists(Long id) {
        if (warehousePictureMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_PICTURE_NOT_EXISTS);
        }
    }

    @Override
    public OrderWarehousePictureDO getWarehousePicture(Long id) {
        return warehousePictureMapper.selectById(id);
    }

    @Override
    public List<OrderWarehousePictureDO> getWarehousePictureList(Collection<Long> ids) {
        return warehousePictureMapper.selectBatchIds(ids);
    }

    @Override
    public List<OrderWarehousePictureDO> getWarehousePictureList(Integer type, Long bizId) {
        return warehousePictureMapper.selectList(new LambdaQueryWrapperX<OrderWarehousePictureDO>()
                .eq(OrderWarehousePictureDO::getType, type)
                .eq(OrderWarehousePictureDO::getBizId, bizId));
    }

    @Override
    public List<String> getWarehousePictureUrlList(Integer type, Long bizId) {
        return this.getWarehousePictureList(type, bizId).stream().map(OrderWarehousePictureDO::getUrl).collect(Collectors.toList());
    }

    @Override
    public List<OrderWarehousePictureBackVO> getWarehousePictureBackVOList(Integer type, Long bizId) {
        List<OrderWarehousePictureDO> warehousePictureList = this.getWarehousePictureList(type, bizId);
        return OrderWarehousePictureConvert.INSTANCE.convertList(warehousePictureList);
    }

    @Override
    public PageResult<OrderWarehousePictureDO> getWarehousePicturePage(OrderWarehousePictureQueryVO query, PageVO page) {
        return warehousePictureMapper.selectPage(page, query);
    }

    @Override
    public List<OrderWarehousePictureDO> getWarehousePictureList(OrderWarehousePictureQueryVO query) {


        //若类型是完成入仓影像，则需要查询此订单的入仓影像和 订单项的入仓影像
        if (query.getType() == 1) {
            Long bizId = query.getBizId(); //orderId

            //通过订单id查询所有的订单项id
            OrderItemQueryVO orderItemQueryVO = new OrderItemQueryVO();
            orderItemQueryVO.setOrderId(bizId);
            List<OrderItemDO> orderItemDOList = orderItemMapper.selectList(orderItemQueryVO);
            if (CollectionUtil.isNotEmpty(orderItemDOList)) {
                List<Long> orderItemIdList = orderItemDOList.stream().map(OrderItemDO::getOrderItemId).collect(Collectors.toList());
                query.setOrderItemIdList(orderItemIdList);
            }

        }

        return warehousePictureMapper.selectList(query);
    }

    @Override
    public List<String> getOrderItemWarehousePictureUrlList(Long orderItemId) {
        return this.getWarehousePictureUrlList(5, orderItemId);
    }
}
