package com.zg.project.wisdom.controller;

import com.zg.framework.web.domain.AjaxResult;
import com.zg.project.wisdom.domain.dto.PhotoDeleteDTO;
import com.zg.project.wisdom.service.PhotoService;
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 java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 【照片上传接口】
 * - 负责接收前端图片（单图/多图），并调用 Service 完成：落盘 + URL入库
 * - 依赖静态资源映射：/photo/** -> D:\photo\
 */
@RestController
@RequestMapping("/photo")
public class PhotoController {

    @Resource
    private PhotoService photoService;

    /**
     * 【多图上传】（推荐）
     * 提交方式：multipart/form-data
     * 参数：
     *  - files：多文件（同名字段，可传多张）
     *  - photoType：0(入库相关) / 1(出库相关)
     *  - billNo：单据号
     *  - xmMs：项目描述
     *  - documentType：单据类型（建文件夹用，不入库）
     *  - gysMc：供应商名称（入库时命名用）
     * 返回：每张图片的可访问 URL 列表
     */
    @PostMapping(value = "/upload/batch", consumes = "multipart/form-data")
    public AjaxResult uploadBatch(@RequestPart("files") List<MultipartFile> files,
                                  @RequestParam("photoType") String photoType,
                                  @RequestParam("billNo") String billNo,
                                  @RequestParam(value = "xmMs", required = false, defaultValue = "") String xmMs,
                                  @RequestParam(value = "documentType", required = false, defaultValue = "default") String documentType,
                                  @RequestParam(value = "gysMc", required = false, defaultValue = "") String gysMc) {
        // 有新参数就走新方法；没有则兼容旧逻辑
        List<String> urls = photoService.uploadAndSave(files, photoType, billNo, xmMs, documentType, gysMc);
        return AjaxResult.success("上传成功", urls);
    }

    /**
     * 【单图上传】（可选）
     * 与多图参数一致，只是文件字段为 file
     * 返回：单张图片的可访问 URL
     */
//    @PostMapping(value = "/upload", consumes = "multipart/form-data")
//    public AjaxResult upload(@RequestPart("file") MultipartFile file,
//                             @RequestParam("photoType") String photoType,
//                             @RequestParam("billNo") String billNo,
//                             @RequestParam("xmMs") String xmMs) {
//        List<String> urls = photoService.uploadAndSave(Collections.singletonList(file), photoType, billNo, xmMs);
//        return AjaxResult.success("上传成功", urls.isEmpty() ? null : urls.get(0));
//    }

    /**
     * 【查询：按 billNo（可选photoType）取照片URL列表】
     * GET /photo/list?billNo=...&photoType=0|1
     * - billNo 必填
     * - photoType 可不传：不传则返回该单据下所有类型的照片
     * 返回：纯 URL 列表
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam("billNo") String billNo,
                           @RequestParam(value = "photoType", required = false) String photoType) {
        return AjaxResult.success(photoService.listUrlsByBill(billNo, photoType));
    }

    /**
     * 【删除：按 URL 列表】
     * @param dto
     * @return
     */
    @PostMapping(value = "/delete", consumes = "application/json")
//    @PreAuthorize("@ss.hasPermi('wisdom:photo:remove')")
    public AjaxResult delete(@Validated @RequestBody PhotoDeleteDTO dto) {
        Map<String, Object> result = photoService.deleteByUrls(dto.getUrls());
        return AjaxResult.success("删除完成", result);
    }
}
