package com.ruoyi.bst.locationLog.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.locationLog.domain.LocationLog;
import com.ruoyi.bst.locationLog.domain.LocationLogQuery;
import com.ruoyi.bst.locationLog.domain.LocationLogVO;
import com.ruoyi.bst.locationLog.domain.vo.LocationLogPositionVO;
import com.ruoyi.bst.locationLog.mapper.LocationLogMapper;
import com.ruoyi.bst.locationLog.service.LocationLogService;
import com.ruoyi.common.utils.collection.CollectionUtils;

/**
 * 定位日志Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-24
 */
@Service
public class LocationLogServiceImpl implements LocationLogService
{
    @Autowired
    private LocationLogMapper locationLogMapper;

    /**
     * 查询定位日志
     *
     * @param id 定位日志主键
     * @return 定位日志
     */
    @Override
    public LocationLogVO selectLocationLogById(Long id, boolean scope)
    {
        if (id == null) {
            return null;
        }
        LocationLogQuery query = new LocationLogQuery();
        query.setId(id);
        query.setScope(scope);
        query.addAreaPermission(AreaJoinPermission.DEVICE_VIEW.getCode());
        query.addAreaPermission(AreaJoinPermission.ORDER_VIEW.getCode());
        return this.selectOne(query);
    }

    private LocationLogVO selectOne(LocationLogQuery query) {
        PageHelper.startPage(1, 1);
        List<LocationLogVO> list = this.selectLocationLogList(query);
        return CollectionUtils.firstElement(list);
    }

    /**
     * 查询定位日志列表
     *
     * @param locationLog 定位日志
     * @return 定位日志
     */
    @Override
    public List<LocationLogVO> selectLocationLogList(LocationLogQuery locationLog)
    {
        return locationLogMapper.selectLocationLogList(locationLog);
    }

    /**
     * 新增定位日志
     *
     * @param locationLog 定位日志
     * @return 结果
     */
    @Override
    public int insertLocationLog(LocationLog locationLog)
    {
        return locationLogMapper.insertLocationLog(locationLog);
    }

    /**
     * 修改定位日志
     *
     * @param locationLog 定位日志
     * @return 结果
     */
    @Override
    public int updateLocationLog(LocationLog locationLog)
    {
        return locationLogMapper.updateLocationLog(locationLog);
    }

    /**
     * 批量删除定位日志
     *
     * @param ids 需要删除的定位日志主键
     * @return 结果
     */
    @Override
    public int deleteLocationLogByIds(List<Long> ids)
    {
        return locationLogMapper.deleteLocationLogByIds(ids);
    }

    @Override
    public int batchInsert(List<? extends LocationLog> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return 0;
        }
        return locationLogMapper.batchInsert(list);
    }

    @Override
    public List<LocationLogPositionVO> selectPositionListByOrderId(Long orderId) {
        if (orderId == null) {
            return null;
        }
        LocationLogQuery query = new LocationLogQuery();
        query.setOrderId(orderId);
        query.setValid(true);
        return locationLogMapper.selectPositionList(query);
    }
}
