package com.aircraft.modules.order.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.aircraft.modules.order.domain.OrderMain;
import com.aircraft.modules.order.domain.dto.OrderMainPageQueryDTO;
import com.aircraft.modules.order.domain.dto.UpdateOrderDTO;
import com.aircraft.modules.order.domain.enums.ConfirmStatusEnum;
import com.aircraft.modules.order.domain.enums.MainOrderStatusEnum;
import com.aircraft.modules.order.mapper.OrderMainMapper;
import com.aircraft.modules.order.service.IOrderMainService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单主表 服务实现类
 * </p>
 *
 * @author cli
 * @since 2025-07-10
 */
@Service
public class OrderMainServiceImpl extends ServiceImpl<OrderMainMapper, OrderMain> implements IOrderMainService {

    @Resource
    private OrderMainMapper orderMainMapper;
    @Override
    public List<OrderMain> queryAll(OrderMainPageQueryDTO pageQueryDTO) {
        LambdaQueryWrapper<OrderMain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryDTO.getAttractionId()), OrderMain::getAttractionId, pageQueryDTO.getAttractionId());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryDTO.getCustomerId()), OrderMain::getCustomerId, pageQueryDTO.getCustomerId());
        queryWrapper.like(StrUtil.isNotBlank(pageQueryDTO.getPhone()), OrderMain::getPhone, pageQueryDTO.getPhone());
        // 添加时间查询条件
        queryWrapper.ge(ObjectUtil.isNotNull(pageQueryDTO.getQueryStartTime()), OrderMain::getOrderCreateTime, pageQueryDTO.getQueryStartTime());
        queryWrapper.le(ObjectUtil.isNotNull(pageQueryDTO.getQueryEndTime()), OrderMain::getOrderCreateTime, pageQueryDTO.getQueryEndTime());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryDTO.getMainOrderStatus()), OrderMain::getMainOrderStatus, pageQueryDTO.getMainOrderStatus());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryDTO.getSettlementStatus()), OrderMain::getSettlementStatus, pageQueryDTO.getSettlementStatus());
        queryWrapper.eq(ObjectUtil.isNotNull(pageQueryDTO.getAreaId()), OrderMain::getAreaId, pageQueryDTO.getAreaId());
        queryWrapper.in(ObjectUtil.isNotNull(pageQueryDTO.getMainOrderStatusList()), OrderMain::getMainOrderStatus, pageQueryDTO.getMainOrderStatusList());
        queryWrapper.like(ObjectUtil.isNotNull(pageQueryDTO.getOrderNo()), OrderMain::getOrderNo, pageQueryDTO.getOrderNo());
        queryWrapper.orderByDesc(OrderMain::getId);
        return list(queryWrapper);
    }

    @Override
    public List<OrderMain> queryAllOrders() {
        LambdaQueryWrapper<OrderMain> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(OrderMain::getOrderCreateTime);

        // 直接调用 MyBatis-Plus 提供的 baseMapper.selectList 方法
        return baseMapper.selectList(wrapper);
    }

    @Override
    public OrderMain addOrder(OrderMain orderMain) {
        this.save(orderMain);
        return orderMain;
    }

    @Override
    public void editOrder(UpdateOrderDTO updateOrderDTO) {
        // 路线多个用逗号隔开
        String routeIdsStr = updateOrderDTO.getRouteIds().stream().map(Object::toString).collect(Collectors.joining(StrUtil.COMMA));
        // 按需更新字段
        LambdaUpdateWrapper<OrderMain> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OrderMain::getId, updateOrderDTO.getOrderId())
                .set(OrderMain::getCustomerId, updateOrderDTO.getCustomerId())
                .set(OrderMain::getPhone, updateOrderDTO.getPhone())
                .set(OrderMain::getAttractionId, updateOrderDTO.getAttractionId())
                .set(OrderMain::getRouteIds, routeIdsStr)
                .set(OrderMain::getCargoWeight, updateOrderDTO.getCargoWeight())
                .set(OrderMain::getSurchargeAmount, updateOrderDTO.getSurchargeAmount())
                .set(OrderMain::getAmount, updateOrderDTO.getAmount())
                .set(OrderMain::getTotalAmount, updateOrderDTO.getTotalAmount());
        this.update(updateWrapper);
    }

    @Override
    public OrderMain obtainOrderMainById(Long orderId) {
        return getById(orderId);
    }

    @Override
    public void deleteOrderMainById(Long orderId) {
        this.removeById(orderId);
    }

    @Override
    public void updateOrderStatus(Long orderId, Integer orderStatus) {
        this.update(Wrappers.<OrderMain>lambdaUpdate()
                .eq(OrderMain::getId, orderId)
                .set(OrderMain::getMainOrderStatus, orderStatus));
    }

    @Override
    public List<OrderMain> obtainOrderList(OrderMainPageQueryDTO pageQueryDTO) {
        return orderMainMapper.obtainOrderList(pageQueryDTO);
    }

    @Override
    public List<OrderMain> obtainOrderListByOrderIds(List<Long> orderIds) {
        LambdaQueryWrapper<OrderMain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(OrderMain::getId, orderIds);
        return list(queryWrapper);
    }

    @Override
    public void updateSettlementStatus(List<Long> orderIds, Integer settlementStatus) {
        if (CollectionUtil.isEmpty(orderIds)) {
            return;
        }
        this.update(Wrappers.<OrderMain>lambdaUpdate()
                .in(OrderMain::getId, orderIds)
                .set(OrderMain::getSettlementStatus, settlementStatus));
    }

    @Override
    public void finishMainOrder(OrderMain updateOrderMain) {
        // 根据 ID 进行更新
        this.update(updateOrderMain, Wrappers.<OrderMain>lambdaUpdate()
                .eq(OrderMain::getId, updateOrderMain.getId())
                .set(OrderMain::getOrderFinishTime, updateOrderMain.getOrderFinishTime())
                .set(ObjectUtil.isNotNull(updateOrderMain.getMainOrderStatus()),
                        OrderMain::getMainOrderStatus, updateOrderMain.getMainOrderStatus())
                .set(ObjectUtil.isNotNull(updateOrderMain.getConfirmTime()),
                        OrderMain::getConfirmTime, updateOrderMain.getConfirmTime())
        );
    }

    @Override
    public List<OrderMain> obtainOrderNotFinish(Date finishTime) {
        return list(Wrappers.lambdaQuery(OrderMain.class)
                .eq(OrderMain::getMainOrderStatus, MainOrderStatusEnum.WAIT_CONFIRM.getCode())
                .le(OrderMain::getOrderFinishTime, finishTime)
        );
    }

    @Override
    public void updateAutoFinishOrder(List<Long> orderIds, Integer mainOrderStatus) {
        if (CollectionUtil.isEmpty(orderIds)) {
            return;
        }
        this.update(Wrappers.<OrderMain>lambdaUpdate()
                .in(OrderMain::getId, orderIds)
                .set(OrderMain::getMainOrderStatus, mainOrderStatus)
                .set(OrderMain::getOrderFinishTime, new Date())
        );
    }
}
