package org.dromara.personnel.controller;

import java.io.IOException;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.personnel.domain.vo.OpsTenderSupplierInputVo;
import org.dromara.personnel.domain.bo.OpsTenderSupplierInputBo;
import org.dromara.personnel.service.IOpsTenderSupplierInputService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 供应商入库
 * 前端访问路由地址为:/personnel/tenderSupplierInput
 *
 * @author LionLi
 * @date 2025-09-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/tenderSupplierInput")
public class OpsTenderSupplierInputController extends BaseController {

    private final IOpsTenderSupplierInputService tenderSupplierInputService;

    /**
     * 查询供应商入库列表
     */
    @SaCheckPermission("supplierInput:supplierInput:list")
    @GetMapping("/list")
    public TableDataInfo<OpsTenderSupplierInputVo> list(OpsTenderSupplierInputBo bo, PageQuery pageQuery) {
        return tenderSupplierInputService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询供应商入库列表
     */
    @SaCheckPermission("supplierInput:supplierInput:getList")
    @GetMapping("/getList")
    public R<List<OpsTenderSupplierInputVo>> list(OpsTenderSupplierInputBo bo) {
        return R.ok(tenderSupplierInputService.getList(bo));
    }

    /**
     * 导出供应商入库列表
     */
    @SaCheckPermission("supplierInput:supplierInput:export")
    @Log(title = "供应商入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OpsTenderSupplierInputBo bo, HttpServletResponse response) {
        List<OpsTenderSupplierInputVo> list = tenderSupplierInputService.queryList(bo);
        ExcelUtil.exportExcel(list, "供应商入库", OpsTenderSupplierInputVo.class, response);
    }

    /**
     * 导入供应商入库 入库资料文件只有导入后修改进行上传
     */
    @SaCheckPermission("supplierInput:supplierInput:import")
    @Log(title = "供应商入库", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public R<Void> importData(Long projectId,@RequestParam("file") MultipartFile file) throws IOException {
        List<OpsTenderSupplierInputVo> tenderSupplierInputVos = ExcelUtil.importExcel(file.getInputStream(), OpsTenderSupplierInputVo.class);

        return toAjax(tenderSupplierInputService.importData(tenderSupplierInputVos,projectId));
    }

    /**
     * 获取供应商入库详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("supplierInput:supplierInput:query")
    @GetMapping("/{id}")
    public R<OpsTenderSupplierInputVo> getInfo(@NotNull(message = "主键不能为空")
                                            @PathVariable Long id) {
        return R.ok(tenderSupplierInputService.queryById(id));
    }

    /**
     * 新增供应商入库
     */
    @SaCheckPermission("supplierInput:supplierInput:add")
    @Log(title = "供应商入库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(OpsTenderSupplierInputBo bo) {
        return toAjax(tenderSupplierInputService.insertByBo(bo));
    }

    /**
     * 修改供应商入库
     */
    @Transactional
    @SaCheckPermission("supplierInput:supplierInput:edit")
    @Log(title = "供应商入库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(OpsTenderSupplierInputBo bo) {
        return toAjax(tenderSupplierInputService.updateByBo(bo));
    }

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