package com.usky.vpp.controller.web;

import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.vpp.service.VppArchiveService;
import com.usky.vpp.service.vo.VppFileArchiveRequestVO;
import com.usky.vpp.service.vo.VppFileArchiveResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 虚拟电厂 - Archive 接口
 * 网关前缀: /prod-api/service-vpp
 */
@RestController
@RequestMapping("/archive")
public class ArchiveController {

    @Autowired
    private VppArchiveService vppArchiveService;

    /**
     * 创建电子档案
     */
    @PostMapping
    public ApiResult<Boolean> create(@RequestBody VppFileArchiveRequestVO vo) {
        return vppArchiveService.create(vo) ? ApiResult.success(true) : ApiResult.error("创建电子档案失败！请重试！");
    }

    /**
     * 查询电子档案
     * id          主键ID 精确匹配
     * archiveName 档案名称 模糊匹配
     * archiveType 档案类型 1图纸 2文档 3设计稿 4验收报告 5其他
     * siteId 站点id
     * bizType 业务类型
     * bizId 业务主键
     * current 页码
     * size 页大小
     */
    @GetMapping
    public ApiResult<CommonPage<VppFileArchiveResponseVO>> page(@RequestParam(value = "id", required = false) Long id,
                                                                @RequestParam(value = "archiveName", required = false) String archiveName,
                                                                @RequestParam(value = "archiveType", required = false) Integer archiveType,
                                                                @RequestParam(value = "siteId", required = false) Long siteId,
                                                                @RequestParam(value = "bizType", required = false) String bizType,
                                                                @RequestParam(value = "bizId", required = false) Long bizId,
                                                                @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                                                @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        return ApiResult.success(vppArchiveService.page(id, archiveName, archiveType, siteId, bizType, bizId, current, size));
    }

    /**
     * 更新电子档案
     * version 字段自增，每次更新+0.1
     */
    @PutMapping
    public ApiResult<Boolean> upload(@RequestBody VppFileArchiveRequestVO vo) {
        return vppArchiveService.upload(vo) ? ApiResult.success(true) : ApiResult.error("更新电子档案失败！请重试！");
    }

    /**
     * 删除电子档案
     */
    @DeleteMapping(value = "/{id}")
    public ApiResult<Boolean> delete(@PathVariable("id") Long id) {
        return vppArchiveService.delete(id) ? ApiResult.success(true) : ApiResult.error("删除电子档案失败！请重试！");
    }

    /**
     * 批量导出电子档案
     * 根据主键ID数组查询档案，下载文件并打包为ZIP返回
     * 请求示例: GET /archive/export?ids=1&ids=2&ids=3
     *
     * @param ids 主键ID列表
     */
    @GetMapping("/export")
    public void export(@RequestParam("ids") List<Long> ids, HttpServletResponse response) {
        vppArchiveService.export(ids, response);
    }
}
