package com.tofly.gpsboot.controller.es;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.Date8Util;
import com.tofly.es.GpsUpload;
import com.tofly.gpsboot.common.Constant;
import com.tofly.es.dto.GpsSectionSearchDto;
import com.tofly.es.dto.UserGpsSearchDto;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.service.es.IGpsUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 巡检人员监控信息信息表
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/userGps")
@Api(tags = "巡检人员监控信息接口")
public class UserGpsController {
    @Autowired
    private IGpsUploadService iGpsUploadService;


    @GetMapping("/getSectionGps")
    @ApiOperation(value = "获取巡检人员分段轨迹)")
    public ResultRespone<Page<GpsUpload>> getSectionGps(Page page, @ModelAttribute UserGpsSearchDto userGpsSearch) {
        ResultRespone<Page<GpsUpload>> rest = new ResultRespone<>();
        try {
            if (null == userGpsSearch.getUserId()) {
                throw new BusinessException(Constant.IS_USER_ID_NULL_CODE,Constant.IS_USER_ID_NULL);
            }
            if (null == userGpsSearch.getStartDate()) {
                userGpsSearch.setStartDate(Date8Util.getStartTime());
            }
            if (null == userGpsSearch.getEndDate()) {
                userGpsSearch.setEndDate(Date8Util.getEndTime());
            }
            Page<GpsUpload> userSectionGps = iGpsUploadService.getUserSectionGps(page, userGpsSearch);
            if(null != userSectionGps){
                rest.setResult(userSectionGps);
            }
        }catch (BusinessException e){
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
       return rest;
    }

    @GetMapping("/section")
    @ApiOperation(value = "获取巡检人员分段")
    public ResultRespone<List<Map<String,Object>>> getSection(@ModelAttribute GpsSectionSearchDto gpsSectionSearch) {
        ResultRespone<List<Map<String,Object>>> rest = new ResultRespone<>();
        try {
            if (null == gpsSectionSearch.getUserId() ) {
                throw new BusinessException(Constant.IS_USER_ID_NULL_CODE,Constant.IS_USER_ID_NULL);
            }
            if (null == gpsSectionSearch.getStartDate()) {
                gpsSectionSearch.setStartDate(Date8Util.getStartTime());
            }
            if (null == gpsSectionSearch.getEndDate()) {
                gpsSectionSearch.setEndDate(Date8Util.getEndTime());
            }
            List<Map<String, Object>> userSection = iGpsUploadService.getUserSection(gpsSectionSearch);
            if(null != userSection){
                rest.setResult(userSection);
            }
        }catch (BusinessException e){
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
       return rest;
    }
}
