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


import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MaterialConfigRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.MaterialConfigExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MaterialConfigDO;
import cn.iocoder.yudao.module.system.service.biz.MateriaConfigService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MaterialConfigSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MaterialConfigPageReqVO;
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 java.util.Objects;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @Author: jinyd
 * @CreateTime: 2025/3/13  15:03
 * @Description:
 * @Version: 4.0
 */


@Tag(name = "管理后台 - 物资设置")
@RestController
@RequestMapping("/material/config")
@Validated
public class MaterialConfigController {

    @Resource
    private MateriaConfigService materiaConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建物资设置")
    @TenantIgnore
    public CommonResult<Long> createConfig(@Valid @RequestBody MaterialConfigSaveReqVO createReqVO) {
        createReqVO.setTenantId(createReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(materiaConfigService.createConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物资设置")
    @TenantIgnore
    public CommonResult<Boolean> updateConfig(@Valid @RequestBody MaterialConfigSaveReqVO updateReqVO) {
        updateReqVO.setTenantId(updateReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        materiaConfigService.updateConfig(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得物资设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<MaterialConfigRespVO> getConfig(@RequestParam("id") Long id) {
        MaterialConfigDO config = materiaConfigService.getConfig(id);
        return success(BeanUtils.toBean(config, MaterialConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物资设置分页")
    @TenantIgnore
    public CommonResult<PageResult<MaterialConfigRespVO>> getConfigPage(@Valid MaterialConfigPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<MaterialConfigDO> pageResult = materiaConfigService.getConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物资设置 Excel")
    @ApiAccessLog(operateType = EXPORT)
    @TenantIgnore
    public void exportConfigExcel(@Valid MaterialConfigPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialConfigDO> list = materiaConfigService.getConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物质设置.xls", "数据", MaterialConfigRespVO.class,
                BeanUtils.toBean(list, MaterialConfigRespVO.class));
    }

    @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();
    }

    @PostMapping("/importMaterialExcel")
    @Operation(summary = "导入物资设置")
    public CommonResult<Map<String, Object>> importSupplierExcel(@RequestBody MultipartFile file, Integer orgType, Long tenantId) throws Exception {
        List<MaterialConfigExcelVO> list = ExcelUtils.read(file, MaterialConfigExcelVO.class);
        return success(materiaConfigService.importMaterialExcel(list, orgType, tenantId));
    }



}
