package com.tmerclub.cloud.warehouse.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.cuerecycle.mapper.CueOrderMapper;
import com.tmerclub.cloud.cuerecycle.model.CueOrder;
import com.tmerclub.cloud.cuerecycle.model.vo.CueOrderVO;
import com.tmerclub.cloud.warehouse.mapper.WarehouseMapper;
import com.tmerclub.cloud.warehouse.model.Warehouse;
import com.tmerclub.cloud.warehouse.model.dto.WarehouseDTO;
import com.tmerclub.cloud.warehouse.model.vo.WarehouseVO;
import com.tmerclub.cloud.warehouse.service.WarehouseService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 仓库Service实现类
 *
 * @author : frank
 * @create : 2025-04-14
 */
@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    WarehouseMapper warehouseMapper;
    @Resource
    CueOrderMapper cueOrderMapper;

    @Override
    public PageVO<WarehouseVO> page(PageDTO pageDTO, WarehouseDTO warehouseDTO) {
        warehouseDTO.setShopId(AuthUserContext.get().getTenantId());
        return PageUtil.doPage(pageDTO, () -> warehouseMapper.list(warehouseDTO));
    }

    @Override
    public WarehouseVO getById(Long id) {
        return warehouseMapper.getById(id);
    }

    @Override
    public int save(WarehouseDTO warehouseDTO) {
        Warehouse warehouse = BeanUtil.toBean(warehouseDTO, Warehouse.class);
        warehouse.setCreateTime(DateUtil.date());
        warehouse.setShopId(AuthUserContext.get().getTenantId());
        return warehouseMapper.save(warehouse);
    }

    @Override
    public int update(WarehouseDTO warehouseDTO) {
        Warehouse warehouse = BeanUtil.toBean(warehouseDTO, Warehouse.class);
        warehouse.setUpdateTime(DateUtil.date());
        return warehouseMapper.update(warehouse);
    }

    @Override
    public int delete(Long id) {
        WarehouseVO warehouseVO = warehouseMapper.getById(id);
        Warehouse warehouse = BeanUtil.toBean(warehouseVO, Warehouse.class);
        warehouse.setUpdateTime(DateUtil.date());
        warehouse.setDeleted(1);
        return warehouseMapper.update(warehouse);
    }

    @Override
    public WarehouseVO getNearbyWarehouse(Long orderId, BigDecimal lat, BigDecimal lng) {
        CueOrderVO orderVO = cueOrderMapper.getById(orderId);
        if (ObjectUtil.isNull(orderVO)) {
            throw new LuckException("暂无订单信息");
        }
        if (ObjectUtil.isNull(orderVO.getWarehouseId())) {
            WarehouseVO nearbyWarehouse = warehouseMapper.getNearbyWarehouse(lat, lng);
            if (ObjectUtil.isNull(nearbyWarehouse)) {
                throw new LuckException("暂无仓库信息");
            }
            // 将仓库id与订单绑定
            CueOrder cueOrder = new CueOrder();
            cueOrder.setOrderId(orderId);
            cueOrder.setWarehouseId(nearbyWarehouse.getWarehouseId());
            cueOrder.setShopId(nearbyWarehouse.getShopId());
            cueOrderMapper.update(cueOrder);
            return nearbyWarehouse;
        } else {
            return warehouseMapper.getById(orderVO.getWarehouseId());
        }

    }
}