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.contractor.ContractorCreateReq;
import org.dromara.project.domain.req.contractor.ContractorQueryReq;
import org.dromara.project.domain.req.contractor.ContractorUpdateReq;
import org.dromara.project.domain.vo.BusContractorVo;
import org.dromara.project.service.IBusContractorService;
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/contractor")
public class BusContractorController extends BaseController {

    private final IBusContractorService busContractorService;

    /**
     * 查询分包单位列表
     */
    @SaCheckPermission("project:contractor:list")
    @GetMapping("/list")
    public TableDataInfo<BusContractorVo> list(ContractorQueryReq req, PageQuery pageQuery) {
        return busContractorService.queryPageList(req, pageQuery);
    }

    /**
     * 导出分包单位列表
     */
    @SaCheckPermission("project:contractor:export")
    @Log(title = "分包单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ContractorQueryReq req, HttpServletResponse response) {
        List<BusContractorVo> list = busContractorService.queryList(req);
        ExcelUtil.exportExcel(list, "分包单位", BusContractorVo.class, response);
    }

    /**
     * 获取分包单位详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("project:contractor:query")
    @GetMapping("/{id}")
    public R<BusContractorVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long id) {
        return R.ok(busContractorService.queryById(id));
    }

    /**
     * 新增分包单位
     */
    @SaCheckPermission("project:contractor:add")
    @Log(title = "分包单位", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody ContractorCreateReq req) {
        return R.ok(busContractorService.insertByBo(req));
    }

    /**
     * 修改分包单位
     */
    @SaCheckPermission("project:contractor:edit")
    @Log(title = "分包单位", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ContractorUpdateReq req) {
        return toAjax(busContractorService.updateByBo(req));
    }

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