package com.muyu.car.controller;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import javax.annotation.Resource;

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.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.car.domain.SysCar;
import com.muyu.car.service.ISysCarService;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.security.utils.SecurityUtils;
import org.springframework.validation.annotation.Validated;
import com.muyu.common.core.web.page.TableDataInfo;

/**
 * 车辆基础信息Controller
 *
 * @author muyu
 * @date 2024-09-17
 */
@RestController
@RequestMapping("/car")
public class SysCarController extends BaseController
{
    @Autowired
    private ISysCarService sysCarService;


    /**
     * 查询车辆基础信息列表
     */
    @RequiresPermissions("car:car:list")
    @GetMapping("/list")
    public Result<TableDataInfo<SysCar>> list(SysCar sysCar)
    {
        startPage();
        List<SysCar> list = sysCarService.selectSysCarList(sysCar);
        return getDataTable(list);
    }

    /**
     * 导出车辆基础信息列表
     */
    @RequiresPermissions("car:car:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCar sysCar)
    {
        List<SysCar> list = sysCarService.selectSysCarList(sysCar);
        ExcelUtil<SysCar> util = new ExcelUtil<SysCar>(SysCar.class);
        util.exportExcel(response, list, "车辆基础信息数据");
    }

    /**
     * 获取车辆基础信息详细信息
     */
    @RequiresPermissions("car:car:query")
    @GetMapping(value = "/{id}")
    public Result<List<SysCar>> getInfo(@PathVariable("id") Long id)
    {
        return success(sysCarService.selectSysCarById(id));
    }

    /**
     * 新增车辆基础信息
     */
    @RequiresPermissions("car:car:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody SysCar sysCar)
    {
        // 获取当前时间（没有时区）
        LocalDateTime now = LocalDateTime.now();
        // 假设我们使用系统默认时区
        ZoneId zoneId = ZoneId.systemDefault();
        // 将LocalDateTime转换为ZonedDateTime（添加了时区信息）
        ZonedDateTime zdt = now.atZone(zoneId);
        // 将ZonedDateTime转换为Instant（UTC时间线上的点）
        Instant instant = zdt.toInstant();
        // 将Instant转换为Date
        Date date = Date.from(instant);
        sysCar.setCarLastJoinTime(date);
        if (sysCarService.checkIdUnique(sysCar)) {
            return error("新增 车辆基础信息 '" + sysCar + "'失败，车辆基础信息已存在");
        }

        return toAjax(sysCarService.save(sysCar));
    }

    /**
     * 修改车辆基础信息
     */
    @RequiresPermissions("car:car:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody SysCar sysCar)
    {
        if (!sysCarService.checkIdUnique(sysCar)) {
            return error("修改 车辆基础信息 '" + sysCar + "'失败，车辆基础信息不存在");
        }

        return toAjax(sysCarService.updateById(sysCar));
    }

    /**
     * 删除车辆基础信息
     */
    @RequiresPermissions("car:car:remove")
	@DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable("ids") Long[] ids)
    {
        sysCarService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }
}
