package com.cpxt.biz.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpxt.biz.domain.*;
import com.cpxt.biz.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cpxt.biz.service.IBizOrderService;

/**
 * 订单Service业务层处理
 * 
 * @author YIN
 * @date 2024-12-16
 */
@Service
public class BizOrderServiceImpl implements IBizOrderService 
{
    @Autowired
    private BizOrderMapper bizOrderMapper;

    @Autowired
    private BizCustomerMapper bizCustomerMapper;

    @Autowired
    private BizCustomerRouteMapper bizCustomerRouteMapper;

    @Autowired
    private BizCustomerWarehouseMapper bizCustomerWarehouseMapper;

    @Autowired
    private BizCustomerShopMapper bizCustomerShopMapper;

    @Autowired
    private BizCarMapper bizCarMapper;

    @Autowired
    private BizDriverMapper bizDriverMapper;

    /**
     * 查询订单
     * 
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public BizOrder selectBizOrderById(Long id)
    {
        return bizOrderMapper.selectById(id);
    }

    /**
     * 查询订单列表（分页）
     *
     * @param bizOrder 订单
     * @return 订单
     */
    @Override
    public Page<BizOrder> selectBizOrderPage(BizOrder bizOrder, int pageNum, int pageSize) {
        LambdaQueryWrapper<BizOrder> queryWrapper = buildQueryWrapper(bizOrder);
        return bizOrderMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper);
    }

    /**
     * 查询订单列表
     * 
     * @param bizOrder 订单
     * @return 订单
     */
    @Override
    public List<BizOrder> selectBizOrderList(BizOrder bizOrder)
    {
        LambdaQueryWrapper<BizOrder> queryWrapper = buildQueryWrapper(bizOrder);
        return bizOrderMapper.selectList(queryWrapper);
    }

    private LambdaQueryWrapper<BizOrder> buildQueryWrapper(BizOrder bizOrder) {
        LambdaQueryWrapper<BizOrder> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bizOrder.getOrderSn())){
            queryWrapper.like(BizOrder::getOrderSn, bizOrder.getOrderSn());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getOrderStatus())){
            queryWrapper.eq(BizOrder::getOrderStatus, bizOrder.getOrderStatus());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getOrderType())){
            queryWrapper.eq(BizOrder::getOrderType, bizOrder.getOrderType());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getCustomerId())){
            queryWrapper.eq(BizOrder::getCustomerId, bizOrder.getCustomerId());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getCustomerName())){
            queryWrapper.like(BizOrder::getCustomerName, bizOrder.getCustomerName());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getRouteId())){
            queryWrapper.eq(BizOrder::getRouteId, bizOrder.getRouteId());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getRouteName())){
            queryWrapper.like(BizOrder::getRouteName, bizOrder.getRouteName());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getWarehouseId())){
            queryWrapper.eq(BizOrder::getWarehouseId, bizOrder.getWarehouseId());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getWarehouseName())){
            queryWrapper.like(BizOrder::getWarehouseName, bizOrder.getWarehouseName());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getShopId())){
            queryWrapper.eq(BizOrder::getShopId, bizOrder.getShopId());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getShopName())){
            queryWrapper.like(BizOrder::getShopName, bizOrder.getShopName());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getCarId())){
            queryWrapper.eq(BizOrder::getCarId, bizOrder.getCarId());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getCarNo())){
            queryWrapper.like(BizOrder::getCarNo, bizOrder.getCarNo());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getDriverId())){
            queryWrapper.eq(BizOrder::getDriverId, bizOrder.getDriverId());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getDriverName())){
            queryWrapper.like(BizOrder::getDriverName, bizOrder.getDriverName());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getCopilotId())){
            queryWrapper.eq(BizOrder::getCopilotId, bizOrder.getCopilotId());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getCopilotName())){
            queryWrapper.like(BizOrder::getCopilotName, bizOrder.getCopilotName());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getSenderName())){
            queryWrapper.like(BizOrder::getSenderName, bizOrder.getSenderName());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getSenderLinkman())){
            queryWrapper.like(BizOrder::getSenderLinkman, bizOrder.getSenderLinkman());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getSenderPhone())){
            queryWrapper.like(BizOrder::getSenderPhone, bizOrder.getSenderPhone());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getSenderAddress())){
            queryWrapper.like(BizOrder::getSenderAddress, bizOrder.getSenderAddress());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getSenderLng())){
            queryWrapper.eq(BizOrder::getSenderLng, bizOrder.getSenderLng());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getSenderLat())){
            queryWrapper.eq(BizOrder::getSenderLat, bizOrder.getSenderLat());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getSenderCompany())){
            queryWrapper.like(BizOrder::getSenderCompany, bizOrder.getSenderCompany());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getReceiverName())){
            queryWrapper.like(BizOrder::getReceiverName, bizOrder.getReceiverName());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getReceiverLinkman())){
            queryWrapper.like(BizOrder::getReceiverLinkman, bizOrder.getReceiverLinkman());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getReceiverPhone())){
            queryWrapper.like(BizOrder::getReceiverPhone, bizOrder.getReceiverPhone());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getReceiverAddress())){
            queryWrapper.like(BizOrder::getReceiverAddress, bizOrder.getReceiverAddress());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getReceiverLng())){
            queryWrapper.eq(BizOrder::getReceiverLng, bizOrder.getReceiverLng());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getReceiverLat())){
            queryWrapper.eq(BizOrder::getReceiverLat, bizOrder.getReceiverLat());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getReceiverCompany())){
            queryWrapper.like(BizOrder::getReceiverCompany, bizOrder.getReceiverCompany());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getGoodsType())){
            queryWrapper.eq(BizOrder::getGoodsType, bizOrder.getGoodsType());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getGoodsName())){
            queryWrapper.like(BizOrder::getGoodsName, bizOrder.getGoodsName());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getWeight())){
            queryWrapper.eq(BizOrder::getWeight, bizOrder.getWeight());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getVolume())){
            queryWrapper.eq(BizOrder::getVolume, bizOrder.getVolume());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getLength())){
            queryWrapper.eq(BizOrder::getLength, bizOrder.getLength());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getWidth())){
            queryWrapper.eq(BizOrder::getWidth, bizOrder.getWidth());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getHeight())){
            queryWrapper.eq(BizOrder::getHeight, bizOrder.getHeight());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getTotalQuantity())){
            queryWrapper.eq(BizOrder::getTotalQuantity, bizOrder.getTotalQuantity());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getInsured())){
            queryWrapper.eq(BizOrder::getInsured, bizOrder.getInsured());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getInsuredMoney())){
            queryWrapper.eq(BizOrder::getInsuredMoney, bizOrder.getInsuredMoney());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getInsuredFee())){
            queryWrapper.eq(BizOrder::getInsuredFee, bizOrder.getInsuredFee());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getOrderFee())){
            queryWrapper.eq(BizOrder::getOrderFee, bizOrder.getOrderFee());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getIsCancel())){
            queryWrapper.eq(BizOrder::getIsCancel, bizOrder.getIsCancel());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getCancelReason())){
            queryWrapper.like(BizOrder::getCancelReason, bizOrder.getCancelReason());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getCancelTime())){
            queryWrapper.like(BizOrder::getCancelTime, bizOrder.getCancelTime());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getPayType())){
            queryWrapper.eq(BizOrder::getPayType, bizOrder.getPayType());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getPayMode())){
            queryWrapper.eq(BizOrder::getPayMode, bizOrder.getPayMode());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getPayId())){
            queryWrapper.eq(BizOrder::getPayId, bizOrder.getPayId());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getPayStatus())){
            queryWrapper.eq(BizOrder::getPayStatus, bizOrder.getPayStatus());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getPayTime())){
            queryWrapper.like(BizOrder::getPayTime, bizOrder.getPayTime());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getStatus())){
            queryWrapper.eq(BizOrder::getStatus, bizOrder.getStatus());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getStartTime())){
            queryWrapper.like(BizOrder::getStartTime, bizOrder.getStartTime());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getArriveTime())){
            queryWrapper.like(BizOrder::getArriveTime, bizOrder.getArriveTime());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getArriveRemark())){
            queryWrapper.like(BizOrder::getArriveRemark, bizOrder.getArriveRemark());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getArriveLng())){
            queryWrapper.eq(BizOrder::getArriveLng, bizOrder.getArriveLng());
        }
        if (ObjectUtil.isNotEmpty(bizOrder.getArriveLat())){
            queryWrapper.eq(BizOrder::getArriveLat, bizOrder.getArriveLat());
        }
        return queryWrapper;
    }

    /**
     * 新增订单
     * 
     * @param bizOrder 订单
     * @return 结果
     */
    @Override
    public int insertBizOrder(BizOrder bizOrder)
    {
        Long customerId = bizOrder.getCustomerId(); // 客户id
        Long routeId = bizOrder.getRouteId(); // 路线id
        Long warehouseId = bizOrder.getWarehouseId(); // 仓库id
        Long shopId = bizOrder.getShopId(); // 店铺id
        Long carId = bizOrder.getCarId(); // 配送车辆id
        Long driverId = bizOrder.getDriverId(); // 配送司机id
        Long copilotId = bizOrder.getCopilotId(); // 副驾司机id

        bizOrder.setCustomerName(bizCustomerMapper.selectById(customerId).getName());
        bizOrder.setRouteName(bizCustomerRouteMapper.selectById(routeId).getName());
        bizOrder.setWarehouseName(bizCustomerWarehouseMapper.selectById(warehouseId).getName());
        bizOrder.setShopName(bizCustomerShopMapper.selectById(shopId).getName());
        bizOrder.setCarNo(bizCarMapper.selectById(carId).getCarNo());
        bizOrder.setDriverName(bizDriverMapper.selectById(driverId).getName());
        bizOrder.setCopilotName(bizDriverMapper.selectById(copilotId).getName());
        return bizOrderMapper.insert(bizOrder);
    }

    /**
     * 修改订单
     * 
     * @param bizOrder 订单
     * @return 结果
     */
    @Override
    public int updateBizOrder(BizOrder bizOrder)
    {
        return bizOrderMapper.updateById(bizOrder);
    }

    /**
     * 批量删除订单
     * 
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteBizOrderByIds(Long[] ids)
    {
        UpdateWrapper<BizOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", (Object) ids);
        updateWrapper.set("status","2"); //状态改为删除
        return bizOrderMapper.update(null,updateWrapper);
    }

    /**
     * 删除订单信息
     * 
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteBizOrderById(Long id)
    {
        UpdateWrapper<BizOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("status","2"); //状态改为删除
        return bizOrderMapper.update(null,updateWrapper);
    }

}
