package com.skr.mdm.api.track.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.skr.mdm.api.track.service.TrackService;
import com.skr.mdm.bean.vo.TerminalVO;
import com.skr.mdm.dao.DeviceEquipmentDao;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.util.GaodeMapAPIUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author nfq
 * @date 2020/9/22 16:35
 */
@Service
public class TrackServiceImpl implements TrackService {

    @Autowired
    private DeviceEquipmentDao equipmentDao;

    @Override
    public ResponseResult listEquipmentByDepartmentId(Integer departmentId, String userName, LoginAdminDTO loginAdminDTO) {
        List<TerminalVO> terminalVOList = equipmentDao.listTerminalByDepartmentId(departmentId, userName, loginAdminDTO.getCompanyId());
        return ResponseResult.success(terminalVOList);
    }

    @Override
    public ResponseResult getLocation(Integer sid, Integer tid, Integer trid) {
        String lastPoint = GaodeMapAPIUtil.getLastPoint(sid, tid, trid);
        JSONObject jsonObject = JSONUtil.parseObj(lastPoint);
        Integer errcode = (Integer) jsonObject.get("errcode");
        Object data = jsonObject.get("data");
        int ok = 10000;
        if (errcode.equals(ok) && !JSONUtil.isNull(data)) {
            return ResponseResult.success(lastPoint);
        }
        String location = equipmentDao.getLocationByTid(tid);
        return ResponseResult.success(location);
    }

    @Override
    public ResponseResult getTrack(Integer sid, Integer tid, Integer trid, Long startTime, Long endTime) {
        return ResponseResult.success(GaodeMapAPIUtil.partitionTrack(sid, tid, trid, startTime, endTime));
    }
}
