package cn.iocoder.yudao.module.system.controller.admin.biz;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.material.MaterialInfoPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.material.MaterialInfoRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.material.MaterialInfoSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.material.MaterialInfoUpdateStatusReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.material.excel.MaterialInfoImportExcelVO;
import cn.iocoder.yudao.module.system.service.biz.MaterialInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

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

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 物资信息")
@RestController
@RequestMapping("/materialInfo")
@Validated
public class MaterialInfoController {

    @Resource
    private MaterialInfoService materialInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建物资信息")
    public CommonResult<Long> create(@Valid @RequestBody MaterialInfoSaveReqVO createReqVO) {
        return success(materialInfoService.createMaterialInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物资信息")
    @TenantIgnore
    public CommonResult<Boolean> update(@Valid @RequestBody MaterialInfoSaveReqVO updateReqVO) {
        materialInfoService.updateMaterialInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物资信息")
    @Parameter(name = "id", description = "编号", required = true)
    @TenantIgnore
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        materialInfoService.deleteMaterialInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物资信息详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @TenantIgnore
    public CommonResult<MaterialInfoRespVO> get(@RequestParam("id") Long id) {
        return success(materialInfoService.getMaterialInfo(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物资信息分页")
    @TenantIgnore // tenantIds 由前端传入（上级可查多个下级）
    public CommonResult<PageResult<MaterialInfoRespVO>> page(@Valid MaterialInfoPageReqVO pageReqVO) {
        PageResult<MaterialInfoRespVO> pageResult = materialInfoService.getMaterialInfoPage(pageReqVO);
        return success(pageResult);
    }

    @PutMapping("/update-status")
    @Operation(summary = "启用/禁用物资信息")
    @TenantIgnore
    public CommonResult<Boolean> updateStatus(@Valid @RequestBody MaterialInfoUpdateStatusReqVO reqVO) {
        materialInfoService.updateMaterialInfoStatus(reqVO);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获得物资信息列表")
    @Parameter(name = "tenantId", description = "机构id", required = true, example = "1")
    @TenantIgnore
    public CommonResult<List<MaterialInfoRespVO>> list(@RequestParam("tenantId") Long tenantId) {
        return success(materialInfoService.getMaterialList(tenantId));
    }

    @PostMapping("/import")
    @Operation(summary = "导入物资信息")
    public CommonResult<Map<String, Object>> importMaterialInfo(@RequestParam("file") MultipartFile file) throws Exception {
        List<MaterialInfoImportExcelVO> list = ExcelUtils.read(file, MaterialInfoImportExcelVO.class);
        return success(materialInfoService.importMaterialInfoExcel(list));
    }

    @GetMapping("/downloadExcel")
    @Operation(summary = "下载物资信息导入模板")
    public void downloadFile(HttpServletResponse response) throws IOException {
        String filePath = "files/物资信息导入模板.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(filePath);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=物资信息导入模板");
        IOUtils.copy(classPathResource.getInputStream(), response.getOutputStream());
        response.flushBuffer();
    }
}

