package com.prolog.rdc.tms.service.impl;

import java.util.Date;
import java.util.List;

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

import com.google.common.collect.Lists;
import com.prolog.common.util.ToolKits;
import com.prolog.rdc.tms.dao.mapper.TmsGpsDataMapper;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.entity.TmsGpsDataModel;
import com.prolog.rdc.tms.model.vo.GpsDataVO;
import com.prolog.rdc.tms.service.GpsService;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 *
 * GPS业务实现层
 *
 * @className :GpsServiceImpl
 * @author :fanzw
 * @creationTime :2023/02/03
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2023/02/03
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Slf4j
@Service
public class GpsServiceImpl implements GpsService {

	@Autowired
	private TmsGpsDataMapper tmsGpsDataMapper;

	/**
	 * GPS数据保存
	 */
	@Override
	public List<IntfErrorLogDTO> saveGpsData(List<GpsDataVO> gpsDataVOs) {
		ToolKits.isParamNull(gpsDataVOs, "请求参数不为空");

		// 当前时间
		Date date = DateUtil.date();

		// 响应集合
		List<IntfErrorLogDTO> intfErrorLogDTOs = Lists.newArrayList();

		for (GpsDataVO gpsDataVO : gpsDataVOs) {
			// 响应对象
			IntfErrorLogDTO intfErrorLogDTO = new IntfErrorLogDTO();

			try {
				TmsGpsDataModel tmsGpsDataModel = new TmsGpsDataModel();
				BeanUtils.copyProperties(gpsDataVO, tmsGpsDataModel);
				tmsGpsDataModel.setId(ToolKits.getUUID());
				tmsGpsDataModel.setCreatetime(date);
				tmsGpsDataModel.setUpdatetime(date);
				tmsGpsDataMapper.save(tmsGpsDataModel);

				// 成功信息
				intfErrorLogDTO.setErrorInfo(null);
				intfErrorLogDTO.setDataId(gpsDataVO.getId());
				intfErrorLogDTO.setStatus(true);
				intfErrorLogDTOs.add(intfErrorLogDTO);
			} catch (Exception e) {
				e.printStackTrace();

				log.info("GPS数据接口处理失败:{}", e);

				// 错误信息
				intfErrorLogDTO.setErrorInfo(e.getMessage());
				intfErrorLogDTO.setDataId(gpsDataVO.getId());
				intfErrorLogDTO.setStatus(false);
				intfErrorLogDTOs.add(intfErrorLogDTO);
			}
		}

		return intfErrorLogDTOs;
	}

}
