package com.haha.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haha.entity.InventoryLog;
import com.haha.mapper.InventoryLogMapper;
import com.haha.service.InventoryLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 库存变动日志服务实现类
 */
@Slf4j
@Service
public class InventoryLogServiceImpl extends ServiceImpl<InventoryLogMapper, InventoryLog>
        implements InventoryLogService {

    @Autowired
    private InventoryLogMapper inventoryLogMapper;

    @Override
    public IPage<InventoryLog> getPage(int page, int pageSize, Map<String, Object> params) {
        LambdaQueryWrapper<InventoryLog> wrapper = new LambdaQueryWrapper<>();

        // 设备ID筛选
        String deviceId = (String) params.get("deviceId");
        if (StringUtils.hasText(deviceId)) {
            wrapper.eq(InventoryLog::getDeviceId, deviceId);
        }

        // 商品编码筛选
        String productCode = (String) params.get("productCode");
        if (StringUtils.hasText(productCode)) {
            wrapper.eq(InventoryLog::getProductCode, productCode);
        }

        // 变动类型筛选
        Object changeType = params.get("changeType");
        if (changeType != null) {
            wrapper.eq(InventoryLog::getChangeType, changeType);
        }

        // 订单号筛选
        String orderId = (String) params.get("orderId");
        if (StringUtils.hasText(orderId)) {
            wrapper.eq(InventoryLog::getOrderId, orderId);
        }

        // 时间范围
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        if (StringUtils.hasText(startTime)) {
            wrapper.ge(InventoryLog::getCreateTime, LocalDateTime.parse(startTime + "T00:00:00"));
        }
        if (StringUtils.hasText(endTime)) {
            wrapper.le(InventoryLog::getCreateTime, LocalDateTime.parse(endTime + "T23:59:59"));
        }

        // 按创建时间倒序
        wrapper.orderByDesc(InventoryLog::getCreateTime);

        return page(new Page<>(page, pageSize), wrapper);
    }

    @Override
    public InventoryLog logChange(String deviceId, Long productId, String productCode, String productName,
                                  Integer changeType, Integer changeQuantity,
                                  Integer beforeStock, Integer afterStock,
                                  String orderId, String activityId,
                                  Long operatorId, String operatorName, String remark) {
        InventoryLog logRecord = new InventoryLog();
        logRecord.setDeviceId(deviceId);
        logRecord.setProductId(productId);
        logRecord.setProductCode(productCode);
        logRecord.setProductName(productName);
        logRecord.setChangeType(changeType);
        logRecord.setChangeQuantity(changeQuantity);
        logRecord.setBeforeStock(beforeStock);
        logRecord.setAfterStock(afterStock);
        logRecord.setOrderId(orderId);
        logRecord.setActivityId(activityId);
        logRecord.setOperatorId(operatorId);
        logRecord.setOperatorName(operatorName);
        logRecord.setRemark(remark);
        logRecord.setCreateTime(LocalDateTime.now());

        save(logRecord);

        log.info("库存变动日志: deviceId={}, productCode={}, type={}, quantity={}, before={}, after={}",
                deviceId, productCode, changeType, changeQuantity, beforeStock, afterStock);

        return logRecord;
    }

    @Override
    public List<Map<String, Object>> getChangeStatistics(String deviceId, String startTime, String endTime) {
        return inventoryLogMapper.selectChangeStatistics(deviceId, startTime, endTime);
    }

    @Override
    public List<Map<String, Object>> getByProductCode(String productCode, int limit) {
        return inventoryLogMapper.selectByProductCode(productCode, limit);
    }
}
