package com.muyu.enterpise.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.domain.SysCar;
import com.muyu.domain.req.SysCarReq;
import com.muyu.domain.resp.SysCarResp;
import com.muyu.enterpise.service.SysCarService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 车辆基础信息Controller
 * @author LiHD
 * @date 2024-09-18
 */
@RestController
@RequestMapping("/car")
@Tag(name = "查询车辆基础信息模块",description = "查询车辆基础信息模块")
public class SysCarController extends BaseController
{
    @Resource
    private SysCarService sysCarService;

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

    /**
     * 获取车辆基础信息详细信息
     */
    @RequiresPermissions("saas: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)
    {
        if (sysCarService.checkIdUnique(sysCar)) {
            return error("新增 车辆基础信息 '" + sysCar + "'失败，车辆基础信息已存在");
        }
        sysCar.setCreateBy(SecurityUtils.getUsername());
        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 + "'失败，车辆基础信息不存在");
        }
        sysCar.setUpdateBy(SecurityUtils.getUsername());
        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();
    }
}
