package com.tuoheng.admin.service.flightdata;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuoheng.admin.entity.domain.FlightData;
import com.tuoheng.admin.entity.domain.Inspection;
import com.tuoheng.admin.mapper.FlightDataMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.entity.query.FlightDataQuery;
import com.tuoheng.common.core.common.BaseServiceImpl;
import com.tuoheng.common.core.enums.ServiceExceptionEnum;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.RedisUtils;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author ChengWang
 * @Date 2022/12/1
 */
@Service
@Slf4j
public class FlightDataServiceImpl extends BaseServiceImpl<FlightDataMapper, FlightData> implements IFlightDataService {

    @Autowired
    private FlightDataMapper flightDataMapper;

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取巡检任务飞行轨迹坐标列表
     *
     * @param flightDataQuery
     * @return
     */
    @Override
    public IPage<FlightData> getFlightDataList(FlightDataQuery flightDataQuery) {
        if (StringUtils.isEmpty(flightDataQuery.getInspectionId())) {
            throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
        }
        QueryWrapper<FlightData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("inspection_id", flightDataQuery.getInspectionId());
        queryWrapper.eq("mark", 1);
        //飞机坐标不关联租户id
        //queryWrapper.eq("tenant_id", ShiroUtils.getTenantId())
        queryWrapper.orderByDesc("id");
        //查询分页数据
        IPage<FlightData> page = new Page<>(flightDataQuery.getPage(), flightDataQuery.getLimit());
        IPage<FlightData> pageData = flightDataMapper.selectPage(page, queryWrapper);

        return pageData;
    }

    @Override
    public void addCallback(FlightData entity) {
        log.info("云盒遥测回调,入参：{}", entity);
        if (ObjectUtil.isNull(entity)) {
            log.info("传入参数entity为空");
            return;
        }
        if (StringUtils.isEmpty(entity.getBoxSn())) {
            log.info("云盒SN号为空");
            return;
        }
        //查询正在执行的云盒所对应的任务
        List<Inspection> inspectionList = inspectionMapper.getListByBox(entity.getBoxSn());
        if (StringUtils.isNotEmpty(inspectionList)) {
            entity.setTenantId(inspectionList.get(0).getTenantId());
            entity.setInspectionId(inspectionList.get(0).getId());
            //将最新一条数据存放入缓存，并设置过期时间
            redisUtils.set(inspectionList.get(0).getId(), entity, 10);
        }
        entity.setCreateTime(DateUtils.now());
        flightDataMapper.insert(entity);
        log.info("addCallback接口入库成功：" + entity.getId());
    }
}
