package com.seer.teach.mall.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.common.enums.OrderStatusEnum;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.enums.ShippingStatusEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.mall.entity.OrderChildEntity;
import com.seer.teach.mall.mapper.OrderChildMapper;
import com.seer.teach.mall.request.OrderConsigneeUpdateReq;
import com.seer.teach.mall.service.IOrderChildService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * <p>
 * 商城订单子表 服务实现类
 * </p>
 *
 * @author ZS
 * @since 2025-09-18
 */
@Slf4j
@Service
public class OrderChildServiceImpl extends ServiceImpl<OrderChildMapper, OrderChildEntity> implements IOrderChildService {

    @Override
    public List<OrderChildEntity> getListByParentIds(Set<Integer> orderIds) {
        if (CollectionUtil.isEmpty(orderIds)) {
            return Collections.emptyList();
        }
        return super.list(new LambdaQueryWrapper<>(OrderChildEntity.class).in(OrderChildEntity::getParentOrderId, orderIds));
    }

    @Override
    public List<OrderChildEntity> getListByParentId(Integer orderId) {
        return super.list(new LambdaQueryWrapper<>(OrderChildEntity.class).eq(OrderChildEntity::getParentOrderId, orderId));
    }

    @Override
    public List<OrderChildEntity> getListByChildOrderSn(String childOrderSn) {
        if (StringUtils.isEmpty(childOrderSn)) {
            return Collections.emptyList();
        }
        return super.list(new LambdaQueryWrapper<>(OrderChildEntity.class).in(OrderChildEntity::getChildOrderSn, childOrderSn));
    }

    @Override
    public OrderChildEntity checkOrderChild(Integer orderChildId) {
        OrderChildEntity orderChildEntity = super.getById(orderChildId);
        AssertUtils.notNull(orderChildEntity, ResultCodeEnum.ORDER_NOT_FOUND);
        return orderChildEntity;
    }

    @Override
    public OrderChildEntity checkOrderChildByChildOrderSn(String orderChildSn) {
        OrderChildEntity orderChildEntity = super.getOne(new LambdaQueryWrapper<>(OrderChildEntity.class).eq(OrderChildEntity::getChildOrderSn, orderChildSn));
        AssertUtils.notNull(orderChildEntity, ResultCodeEnum.ORDER_NOT_FOUND);
        return orderChildEntity;
    }

    @Override
    public boolean deliveryOrder(Integer orderId, String logisticsCode, String shippingNo, Integer userId) {
        boolean updateOrderStatusResult = super.update(new LambdaUpdateWrapper<OrderChildEntity>()
                .eq(OrderChildEntity::getId, orderId)
                .set(OrderChildEntity::getOrderStatus, OrderStatusEnum.SHIPPED.getCode())
                .set(OrderChildEntity::getShippingStatus, ShippingStatusEnum.SHIPPED.getCode())
                .set(OrderChildEntity::getDeliveryTime, LocalDateTime.now())
                .set(OrderChildEntity::getLogisticsCode, logisticsCode)
                .set(OrderChildEntity::getShippingNo, shippingNo)
                .set(OrderChildEntity::getDeliveryUserId, userId));
        log.info("更新订单状态结果：{}", updateOrderStatusResult);
        return updateOrderStatusResult;
    }

    @Override
    public boolean deliveryOrderByParentOrderId(Integer parentOrderId, String logisticsCode, String shippingNo, Integer userId) {
        boolean updateOrderStatusResult = super.update(new LambdaUpdateWrapper<OrderChildEntity>()
                .eq(OrderChildEntity::getParentOrderId, parentOrderId)
                .set(OrderChildEntity::getOrderStatus, OrderStatusEnum.SHIPPED.getCode())
                .set(OrderChildEntity::getShippingStatus, ShippingStatusEnum.SHIPPED.getCode())
                .set(OrderChildEntity::getDeliveryTime, LocalDateTime.now())
                .set(OrderChildEntity::getLogisticsCode, logisticsCode)
                .set(OrderChildEntity::getShippingNo, shippingNo)
                .set(OrderChildEntity::getDeliveryUserId, userId));
        log.info("deliveryOrderByParentOrderId 更新订单状态结果：{}", updateOrderStatusResult);
        return updateOrderStatusResult;
    }

    @Override
    public boolean updateAddressIdByChildOrderId(Integer childOrderId, Integer addressId) {
        checkOrderChild(childOrderId);
        if (Objects.nonNull(addressId)) {
            return super.update(new LambdaUpdateWrapper<>(OrderChildEntity.class).eq(OrderChildEntity::getId, childOrderId)
                    .set(OrderChildEntity::getAddressId, addressId));
        }
        return false;
    }

    @Override
    public boolean deleteOrderByParentOrderId(Integer parentOrderId) {
        return super.update(new LambdaUpdateWrapper<>(OrderChildEntity.class).eq(OrderChildEntity::getParentOrderId, parentOrderId)
                .set(OrderChildEntity::getOrderStatus, OrderStatusEnum.DELETED.getCode()));
    }

    @Override
    public boolean deleteOrderByIdAndMerchantId(Integer id, Long merchantId) {
        return super.update(new LambdaUpdateWrapper<>(OrderChildEntity.class).eq(OrderChildEntity::getId, id)
                .eq(OrderChildEntity::getMerchantUserId, merchantId)
                .set(OrderChildEntity::getOrderStatus, OrderStatusEnum.DELETED.getCode()));
    }

    @Override
    public boolean cancelOrder(Integer childOrderId, Integer userId) {
        return super.update(new LambdaUpdateWrapper<>(OrderChildEntity.class).eq(OrderChildEntity::getId, childOrderId).set(OrderChildEntity::getOrderStatus, OrderStatusEnum.CANCELED.getCode()));
    }

    @Override
    public boolean cancelOrder(Set<Integer> childrenOrderIds, Integer userId) {
        return super.update(new LambdaUpdateWrapper<>(OrderChildEntity.class).in(OrderChildEntity::getId, childrenOrderIds).set(OrderChildEntity::getOrderStatus, OrderStatusEnum.CANCELED.getCode()));
    }

    @Override
    public boolean updateStatusByParentOrderId(Integer parentOrderId, OrderStatusEnum orderStatusEnum) {
        boolean updated = super.update(new LambdaUpdateWrapper<>(OrderChildEntity.class).eq(OrderChildEntity::getParentOrderId, parentOrderId).set(OrderChildEntity::getOrderStatus, orderStatusEnum.getCode()));
        log.info("updateStatusByParentOrderId 更新订单状态结果：{}", updated);
        AssertUtils.isTrue(updated, ResultCodeEnum.ORDER_CLOSE_FAIL);
        return true;
    }

    @Override
    public boolean updateOrderConsigneeInfo(OrderConsigneeUpdateReq orderReq) {
        // 更新子订单信息
        boolean result = super.update(new LambdaUpdateWrapper<OrderChildEntity>()
                .set(StringUtils.isNotBlank(orderReq.getConsignee()), OrderChildEntity::getConsignee, orderReq.getConsignee())
                .set(StringUtils.isNotBlank(orderReq.getMobile()), OrderChildEntity::getMobile, orderReq.getMobile())
                .set(StringUtils.isNotBlank(orderReq.getAddress()), OrderChildEntity::getAddress, orderReq.getAddress())
                .set(StringUtils.isNotBlank(orderReq.getCity()), OrderChildEntity::getCity, orderReq.getCity())
                .set(StringUtils.isNotBlank(orderReq.getProvince()), OrderChildEntity::getProvince, orderReq.getProvince())
                .set(StringUtils.isNotBlank(orderReq.getDistrict()), OrderChildEntity::getDistrict, orderReq.getDistrict())
                .set(Objects.nonNull(orderReq.getAddressId()), OrderChildEntity::getAddressId, orderReq.getAddressId())
                .set(StringUtils.isNotBlank(orderReq.getRemark()), OrderChildEntity::getRemark, orderReq.getRemark())
                .eq(OrderChildEntity::getId, orderReq.getChildOrderId()));
        log.info("子订单更新结果：{}", result ? "成功" : "失败");
        AssertUtils.isTrue(result, ResultCodeEnum.ORDER_UPDATE_ERROR);
        return true;
    }

    @Override
    public void updateOrderConsigneeInfoByChildOrderIds(OrderConsigneeUpdateReq orderUpdateReq, Set<Integer> childOrderIds) {
        // 更新子订单信息
        boolean result = super.update(new LambdaUpdateWrapper<OrderChildEntity>()
                .set(StringUtils.isNotBlank(orderUpdateReq.getConsignee()), OrderChildEntity::getConsignee, orderUpdateReq.getConsignee())
                .set(StringUtils.isNotBlank(orderUpdateReq.getMobile()), OrderChildEntity::getMobile, orderUpdateReq.getMobile())
                .set(StringUtils.isNotBlank(orderUpdateReq.getAddress()), OrderChildEntity::getAddress, orderUpdateReq.getAddress())
                .set(StringUtils.isNotBlank(orderUpdateReq.getCity()), OrderChildEntity::getCity, orderUpdateReq.getCity())
                .set(StringUtils.isNotBlank(orderUpdateReq.getProvince()), OrderChildEntity::getProvince, orderUpdateReq.getProvince())
                .set(StringUtils.isNotBlank(orderUpdateReq.getDistrict()), OrderChildEntity::getDistrict, orderUpdateReq.getDistrict())
                .set(Objects.nonNull(orderUpdateReq.getAddressId()), OrderChildEntity::getAddressId, orderUpdateReq.getAddressId())
                .set(StringUtils.isNotBlank(orderUpdateReq.getRemark()), OrderChildEntity::getRemark, orderUpdateReq.getRemark())
                .in(OrderChildEntity::getId, childOrderIds));
        log.info("子订单更新结果：{}", result ? "成功" : "失败");
        AssertUtils.isTrue(result, ResultCodeEnum.ORDER_UPDATE_ERROR);
    }
}
