package com.zehong.web.controller.system;

import java.util.List;

import com.zehong.system.domain.TVehicleLocationInfo;
import com.zehong.system.service.ITVehicleLocationInfoService;
import io.jsonwebtoken.lang.Collections;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zehong.common.annotation.Log;
import com.zehong.common.core.controller.BaseController;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.enums.BusinessType;
import com.zehong.system.domain.TVehicleInfo;
import com.zehong.system.service.ITVehicleInfoService;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.common.core.page.TableDataInfo;

/**
 * 燃气车辆信息Controller
 *
 * @author zehong
 * @date 2023-08-16
 */
@RestController
@RequestMapping("/system/infos")
public class TVehicleInfosController extends BaseController
{
    @Autowired
    private ITVehicleInfoService tVehicleInfoService;
    @Autowired
    private ITVehicleLocationInfoService itVehicleLocationInfoService;
    /**
     * 查询燃气车辆信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(TVehicleInfo tVehicleInfo)
    {
        startPage();
        List<TVehicleInfo> list = tVehicleInfoService.selectTVehicleInfoList(tVehicleInfo);
        for (int i=0;i<list.size();i++){
            //获取最后位置信息
            TVehicleLocationInfo tVehicleLocationInfo = new TVehicleLocationInfo();
            tVehicleLocationInfo.setCarNum(list.get(i).getCarNum());
            tVehicleLocationInfo.setLast(true);
            List<TVehicleLocationInfo> tVehicleLocationInfoList=itVehicleLocationInfoService.selectTVehicleLocationInfoList(tVehicleLocationInfo);
            //车辆最后位置
            if(!Collections.isEmpty(tVehicleLocationInfoList) && tVehicleLocationInfoList.size() > 0){
                list.get(i).setLongitude(tVehicleLocationInfoList.get(0).getLongitude());
                list.get(i).setLatitude(tVehicleLocationInfoList.get(0).getLatitude());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出燃气车辆信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "燃气车辆信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TVehicleInfo tVehicleInfo)
    {
        List<TVehicleInfo> list = tVehicleInfoService.selectTVehicleInfoList(tVehicleInfo);
        ExcelUtil<TVehicleInfo> util = new ExcelUtil<TVehicleInfo>(TVehicleInfo.class);
        return util.exportExcel(list, "燃气车辆信息数据");
    }

    /**
     * 获取燃气车辆信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{vehicleId}")
    public AjaxResult getInfo(@PathVariable("vehicleId") Long vehicleId)
    {
        return AjaxResult.success(tVehicleInfoService.selectTVehicleInfoById(vehicleId));
    }

    /**
     * 新增燃气车辆信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "燃气车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TVehicleInfo tVehicleInfo)
    {
        return toAjax(tVehicleInfoService.insertTVehicleInfo(tVehicleInfo));
    }

    /**
     * 修改燃气车辆信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "燃气车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TVehicleInfo tVehicleInfo)
    {
        return toAjax(tVehicleInfoService.updateTVehicleInfo(tVehicleInfo));
    }

    /**
     * 删除燃气车辆信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "燃气车辆信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{vehicleIds}")
    public AjaxResult remove(@PathVariable Long[] vehicleIds)
    {
        return toAjax(tVehicleInfoService.deleteTVehicleInfoByIds(vehicleIds));
    }
}
