package org.dromara.materials.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.constant.HttpStatus;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
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.materials.domain.req.company.CompanyCreateReq;
import org.dromara.materials.domain.req.company.CompanyQueryReq;
import org.dromara.materials.domain.req.company.CompanyUpdateReq;
import org.dromara.materials.domain.vo.BusCompanyVo;
import org.dromara.materials.service.IBusCompanyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公司
 *
 * @author lcj
 * @date 2025-03-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/materials/company")
public class BusCompanyController extends BaseController {

    private final IBusCompanyService busCompanyService;

    /**
     * 查询公司列表
     */
    @SaCheckPermission("materials:company:list")
    @GetMapping("/list")
    public TableDataInfo<BusCompanyVo> list(CompanyQueryReq req, PageQuery pageQuery) {
        return busCompanyService.queryPageList(req, pageQuery);
    }

    /**
     * 导出公司列表
     */
    @SaCheckPermission("materials:company:export")
    @Log(title = "公司", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CompanyQueryReq req, HttpServletResponse response) {
        List<BusCompanyVo> list = busCompanyService.queryList(req);
        ExcelUtil.exportExcel(list, "公司", BusCompanyVo.class, response);
    }

    /**
     * 获取公司详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("materials:company:query")
    @GetMapping("/{id}")
    public R<BusCompanyVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return R.ok(busCompanyService.queryById(id));
    }

    /**
     * 新增公司
     */
    @SaCheckPermission("materials:company:add")
    @Log(title = "公司", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody CompanyCreateReq req) {
        if (req == null) {
            throw new ServiceException("参数不能为空", HttpStatus.BAD_REQUEST);
        }
        return R.ok(busCompanyService.insertByBo(req));
    }

    /**
     * 修改公司
     */
    @SaCheckPermission("materials:company:edit")
    @Log(title = "公司", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CompanyUpdateReq req) {
        return toAjax(busCompanyService.updateByBo(req));
    }

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