package com.zcxk.track.controller;

import com.zcxk.model.AjaxMessage;
import com.zcxk.model.LoginUser;
import com.zcxk.model.ResultStatus;
import com.zcxk.track.entity.GpsData;
import com.zcxk.track.entity.GpsDataHis;
import com.zcxk.track.service.GpsDataHisService;
import com.zcxk.track.service.GpsDataService;
import com.zcxk.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/track/GpsData")
@Api(tags = "GPS数据")
public class GpsDataController {

    @Autowired
    GpsDataService gpsDataService;
    @Autowired
    GpsDataHisService gpsDataHisService;
    
    /**
     * 新增一条数据
     *
     * @param GpsData 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ApiOperation(value = "插入")
    public AjaxMessage<Boolean> insert(@ApiParam(value = "GPS数据", required = true) @RequestBody GpsData GpsData) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        GpsData.setUserId(currentUser.getId());
        GpsData.setUploadDate(new Date());
        List<GpsData> gpsDataList=gpsDataService.selectByUserId(currentUser.getId());
        //插入历史表
        List<GpsDataHis> gpsDataHisList=new ArrayList<>();
        for(GpsData gpsData:gpsDataList){
            GpsDataHis gpsDataHis=new GpsDataHis();
            gpsDataHis.setUserId(gpsData.getUserId());
            gpsDataHis.setUploadDate(gpsData.getUploadDate());
            gpsDataHis.setLongtitude(gpsData.getLongtitude());
            gpsDataHis.setLatitude(gpsData.getLatitude());
            gpsDataHisList.add(gpsDataHis);
        }
        gpsDataHisService.insert(gpsDataHisList);
        //删除原来的实时数据
        gpsDataService.delete(currentUser.getId());
        return new AjaxMessage<>(ResultStatus.OK, gpsDataService.insert(GpsData));
    }
}