package com.zehong.web.controller.system;

import java.util.List;

import com.zehong.common.core.domain.model.LoginUser;
import com.zehong.common.utils.ServletUtils;
import com.zehong.framework.web.service.TokenService;
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.TVehicleUseRecord;
import com.zehong.system.service.ITVehicleUseRecordService;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.common.core.page.TableDataInfo;

/**
 * 车辆使用记录Controller
 *
 * @author zehong
 * @date 2023-08-19
 */
@RestController
@RequestMapping("/system/record")
public class TVehicleUseRecordController extends BaseController
{
    @Autowired
    private ITVehicleUseRecordService tVehicleUseRecordService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询车辆使用记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(TVehicleUseRecord tVehicleUseRecord)
    {
        startPage();
        List<TVehicleUseRecord> list = tVehicleUseRecordService.selectTVehicleUseRecordList(tVehicleUseRecord);
        return getDataTable(list);
    }

    /**
     * 导出车辆使用记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "车辆使用记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TVehicleUseRecord tVehicleUseRecord)
    {
        List<TVehicleUseRecord> list = tVehicleUseRecordService.selectTVehicleUseRecordList(tVehicleUseRecord);
        ExcelUtil<TVehicleUseRecord> util = new ExcelUtil<TVehicleUseRecord>(TVehicleUseRecord.class);
        return util.exportExcel(list, "车辆使用记录数据");
    }

    /**
     * 获取车辆使用记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{vehicleUseRecordId}")
    public AjaxResult getInfo(@PathVariable("vehicleUseRecordId") Long vehicleUseRecordId)
    {
        return AjaxResult.success(tVehicleUseRecordService.selectTVehicleUseRecordById(vehicleUseRecordId));
    }

    /**
     * 新增车辆使用记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "车辆使用记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TVehicleUseRecord tVehicleUseRecord)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        tVehicleUseRecord.setVehicleUserId(userId);
        return toAjax(tVehicleUseRecordService.insertTVehicleUseRecord(tVehicleUseRecord));
    }

    /**
     * 修改车辆使用记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "车辆使用记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TVehicleUseRecord tVehicleUseRecord)
    {
        return toAjax(tVehicleUseRecordService.updateTVehicleUseRecord(tVehicleUseRecord));
    }

    /**
     * 删除车辆使用记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "车辆使用记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{vehicleUseRecordIds}")
    public AjaxResult remove(@PathVariable Long[] vehicleUseRecordIds)
    {
        return toAjax(tVehicleUseRecordService.deleteTVehicleUseRecordByIds(vehicleUseRecordIds));
    }
}
