package com.jwipc.sys.controller.pc;

import com.jwipc.sys.dto.FileResposeDTO;
import com.jwipc.sys.dto.KingdeeInvoiceCloudAssistantEventCodeDTO;
import com.jwipc.sys.dto.KingdeeInvoiceCloudAssistantResultDTO;
import com.jwipc.sys.dto.KingdeeInvoiceCloudRecognitionCheckResultDTO;
import com.jwipc.sys.logic.KingdeeInvoiceCloudLogic;
import com.jwipc.sys.util.FinancialShareUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 *  金蝶发票云接口代理
 * </p>
 *
 * @author Chen Tianshi
 * @since 2025/9/24 14:54
 */
@RestController
@RequestMapping(MODULE_ROOT + "/kingdeeInvoiceCloudAssistant/rest/")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class KingdeeInvoiceCloudRestController {

    private final KingdeeInvoiceCloudLogic kingdeeInvoiceCloudAssistantLogic;
    private final FinancialShareUtil financialShareUtil;

    /**
     * 获取金蝶发票助手页面eventCode
     * @param eventCodeDTO 需要的参数
     * @return accessToken和eventCode
     */
    @RequestMapping("getAssistantInfo")
    public KingdeeInvoiceCloudAssistantResultDTO getAssistantInfo(@RequestBody KingdeeInvoiceCloudAssistantEventCodeDTO eventCodeDTO) {
        return kingdeeInvoiceCloudAssistantLogic.getAssistantInfo(eventCodeDTO);
    }

    /**
     * 上传发票，并且推到发票云返回机构化数据
     * @param file 文件流
     * @return 发票结构化数据
     */
    @PostMapping("recognitionCheck")
    public ResponseEntity<KingdeeInvoiceCloudRecognitionCheckResultDTO> recognitionCheck(@RequestPart("file") MultipartFile file, @RequestParam(value = "billId") String billId, String orgNumber) {
        KingdeeInvoiceCloudRecognitionCheckResultDTO resultDTO = kingdeeInvoiceCloudAssistantLogic.recognitionCheck(file,billId,orgNumber);
        return ResponseEntity.ok(resultDTO);
    }

    /**
     * 删除发票云发票
     * @param serialNos 发票流水号集合
     * @return 发票结构化数据
     */
    @PostMapping("deleteInvoice")
    public ResponseEntity deleteInvoice(String serialNos) {
        kingdeeInvoiceCloudAssistantLogic.deleteInvoice(serialNos);
        return ResponseEntity.ok(true);
    }

    /**
     * 删除发票云发票
     * @param signType  01-签收 02-反签收
     * @param serialNos 发票流水号集合
     * @return 发票结构化数据
     */
    @PostMapping("billSign")
    public ResponseEntity billSign(String signType,String serialNos) {
        kingdeeInvoiceCloudAssistantLogic.billSign(null,signType,serialNos);
        return ResponseEntity.ok(true);
    }

    /**
     * 通过文件url下载发票云文件
     * @param downloadUrl 发票云提供的文件下载地址
     * @param fileName 文件名
     * @return 文件信息
     */
    @PostMapping("downloadAndUploadFile")
    public ResponseEntity<FileResposeDTO> downloadAndUploadFile(String downloadUrl, String fileName) {
        FileResposeDTO result = kingdeeInvoiceCloudAssistantLogic.downloadAndUploadFile(downloadUrl, fileName);
        return ResponseEntity.ok(result);
    }

    /**
     * 附件上传到共享系统文件服务器并与单据的绑定
     * @param fileId SRM文件ID
     * @param billNo 单据编号
     * @param attachmentKey 附件面板标识
     * @param billNumber 单据标识
     */
    @PostMapping("uploadFileForBill")
    public ResponseEntity<Boolean> uploadFileForBill(Long fileId, String billNo, String attachmentKey, String billNumber) {
        financialShareUtil.uploadFileForBill(fileId, billNo, attachmentKey, billNumber);
        return ResponseEntity.ok().build();
    }

    /**
     * 批量上传文件到共享系统
     * @param fileIds 逗号分隔的文件ID
     * @param billNo 单据编号
     * @param attachmentKey 附件面板标识
     * @param billNumber 单据标识
     * @return 上传失败的信息
     */
    @PostMapping("uploadBatchFileForBill")
    public ResponseEntity<List<String>> uploadBatchFileForBill(String fileIds, String billNo, String attachmentKey, String billNumber) {
        List<Long> fileIdList = Arrays.stream(StringUtils.split(fileIds, ",")).map(Long::valueOf).toList();
        return ResponseEntity.ok(financialShareUtil.uploadBatchFileForBill(fileIdList, billNo, attachmentKey, billNumber));
    }
}
