package org.dromara.project.controller;

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.project.domain.req.constructionuser.ConstructionUserCreateReq;
import org.dromara.project.domain.req.constructionuser.ConstructionUserQueryReq;
import org.dromara.project.domain.req.constructionuser.ConstructionUserUpdateReq;
import org.dromara.project.domain.vo.BusConstructionUserVo;
import org.dromara.project.service.IBusConstructionUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 施工人员
 *
 * @author lcj
 * @date 2025-03-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/constructionUser")
public class BusConstructionUserController extends BaseController {

    private final IBusConstructionUserService busConstructionUserService;

    /**
     * 查询施工人员列表
     */
    @SaCheckPermission("project:constructionUser:list")
    @GetMapping("/list")
    public TableDataInfo<BusConstructionUserVo> list(ConstructionUserQueryReq req, PageQuery pageQuery) {
        return busConstructionUserService.queryPageList(req, pageQuery);
    }

    /**
     * 导出施工人员列表
     */
    @SaCheckPermission("project:constructionUser:export")
    @Log(title = "施工人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ConstructionUserQueryReq req, HttpServletResponse response) {
        List<BusConstructionUserVo> list = busConstructionUserService.queryList(req);
        ExcelUtil.exportExcel(list, "施工人员", BusConstructionUserVo.class, response);
    }

    /**
     * 获取施工人员详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("project:constructionUser:query")
    @GetMapping("/{id}")
    public R<BusConstructionUserVo> getInfo(@NotNull(message = "主键不能为空")
                                            @PathVariable Long id) {
        return R.ok(busConstructionUserService.queryById(id));
    }

    /**
     * 新增施工人员
     */
    @SaCheckPermission("project:constructionUser:add")
    @Log(title = "施工人员", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody ConstructionUserCreateReq req) {
        return R.ok(busConstructionUserService.insertByBo(req));
    }

    /**
     * 修改施工人员
     */
    @SaCheckPermission("project:constructionUser:edit")
    @Log(title = "施工人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ConstructionUserUpdateReq req) {
        return toAjax(busConstructionUserService.updateByBo(req));
    }

    /**
     * 删除施工人员
     *
     * @param ids 主键串
     */
    @SaCheckPermission("project:constructionUser:remove")
    @Log(title = "施工人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(busConstructionUserService.deleteWithValidByIds(List.of(ids), true));
    }
}
