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.SupplierInfoPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.SupplierInfoRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.SupplierInfoSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.common.TenantIdsReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.BuildImportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.SupplierImportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.materialcategory.MaterialCategoryRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MaterialCategoryDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.SupplierInfoDO;
import cn.iocoder.yudao.module.system.service.biz.SupplierInfoService;
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.multipart.MultipartFile;

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

import java.io.IOException;
import java.util.Arrays;
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: 2024/11/21  9:51
 * @Description:
 * @Version: 4.0
 */


@Tag(name = "管理后台 - 供应商信息")
@RestController
@RequestMapping("/supplier/info")
@Validated
public class SupplierInfoController {

    @Resource
    private SupplierInfoService infoService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商信息")
    public CommonResult<Long> createInfo(@Valid @RequestBody SupplierInfoSaveReqVO createReqVO) {
        return success(infoService.createInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商信息")
    @TenantIgnore
    public CommonResult<Boolean> updateInfo(@Valid @RequestBody SupplierInfoSaveReqVO updateReqVO) {
        infoService.updateInfo(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得供应商信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<SupplierInfoRespVO> getInfo(@RequestParam("id") Long id) {
        SupplierInfoDO info = infoService.getInfo(id);
        return success(BeanUtils.toBean(info, SupplierInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商信息分页")
//    @PreAuthorize("@ss.hasPermission('supplier:info:query')")
    @TenantIgnore
    public CommonResult<PageResult<SupplierInfoRespVO>> getInfoPage(@Valid SupplierInfoPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(pageReqVO.getTenantIds() == null || pageReqVO.getTenantIds().length == 0 ?
                new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<SupplierInfoRespVO> pageResult = infoService.getInfoPage(pageReqVO);
        return success(pageResult);
    }

    @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("/importSupplierExcel")
    @Operation(summary = "导入供应商")
    public CommonResult<Map<String, Object>> importSupplierExcel(@RequestBody MultipartFile file) throws Exception {
        List<SupplierImportExcelVO> list = ExcelUtils.read(file, SupplierImportExcelVO.class);
        return success(infoService.importSupplierExcel(list));
    }

    @GetMapping("/list")
    @Operation(summary = "获得供应商列表")
    @TenantIgnore
    public CommonResult<List<SupplierInfoDO>> list(@Valid TenantIdsReqVO reqVO) {
        Long[] tenantIds = reqVO.getTenantIds();
        if (tenantIds == null || tenantIds.length == 0) {
            tenantIds = Arrays.asList(TenantContextHolder.getTenantId()).toArray(new Long[0]);
        }
        List<SupplierInfoDO> list = infoService.list(tenantIds);
        return success(BeanUtils.toBean(list, SupplierInfoDO.class));
    }
}
