package org.dromara.controller.proprietorCar;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.domain.proprietorCar.bo.ProprietorCarBo;
import org.dromara.domain.proprietorCar.vo.ProprietorCarVo;
import org.dromara.service.IProprietorCarService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 业主车辆管理
 *
 * @author boman
 * @date 2025-04-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wuYe/car")
public class ProprietorCarController extends BaseController {

    private final IProprietorCarService proprietorCarService;

    /**
     * 查询业主车辆管理列表
     */
    @SaCheckPermission("wuYe:car:list")
    @GetMapping("/list")
    public TableDataInfo<ProprietorCarVo> list(ProprietorCarBo bo, PageQuery pageQuery) {
        return proprietorCarService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出业主车辆管理列表
     */
    @SaCheckPermission("wuYe:car:export")
    @Log(title = "业主车辆管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProprietorCarBo bo, HttpServletResponse response) {
        List<ProprietorCarVo> list = proprietorCarService.queryList(bo);
        ExcelUtil.exportExcel(list, "业主车辆管理", ProprietorCarVo.class, response);
    }

    /**
     * 获取业主车辆管理详细信息
     *
     * @param carId 主键
     */
    @SaCheckPermission("wuYe:car:query")
    @GetMapping("/{carId}")
    public R<ProprietorCarVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long carId) {
        return R.ok(proprietorCarService.queryById(carId));
    }

    /**
     * 新增业主车辆管理
     */
    @SaCheckPermission("wuYe:car:add")
    @Log(title = "业主车辆管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProprietorCarBo bo) {
        return toAjax(proprietorCarService.insertByBo(bo));
    }

    /**
     * 修改业主车辆管理
     */
    @SaCheckPermission("wuYe:car:edit")
    @Log(title = "业主车辆管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProprietorCarBo bo) {
        return toAjax(proprietorCarService.updateByBo(bo));
    }

    /**
     * 删除业主车辆管理
     *
     * @param carIds 主键串
     */
    @SaCheckPermission("wuYe:car:remove")
    @Log(title = "业主车辆管理", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{carIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] carIds) {
        return toAjax(proprietorCarService.deleteWithValidByIds(List.of(carIds), true));
    }
}
