package com.jwipc.sys.controller.pc;

import com.jwipc.api.dto.ESignFileResposeDTO;
import com.jwipc.sys.dto.DigitalSealBusinessDetailDTO;
import com.jwipc.sys.logic.ESignLogic;
import com.jwipc.sys.dto.DigitalSealBusinessDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

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

/**
 * @author shenyang.ou
 * @date 2024/5/9 11:14
 * @desc e签宝前端
 */
@RestController
@RequestMapping(MODULE_ROOT + "/esign/rest")
@RequiredArgsConstructor
public class ESignRestController {

    private final ESignLogic eSignLogic;
    /**
     * 上传签章文件
     * @param files 文件
     */
    @RequestMapping("/saveSignFile")
    public ResponseEntity<List<ESignFileResposeDTO>> saveSignFile(@RequestPart("files") MultipartFile[] files,String fileIds) {
        List<ESignFileResposeDTO> eSignFileResposeDTOS =  eSignLogic.saveSignFile(files,fileIds);
        return ResponseEntity.ok(eSignFileResposeDTOS);
    }

    /**
     * 发起签署
     * @param digitalSealBusinessDTO 参数
     */
    @RequestMapping("/sendSign")
    public ResponseEntity sendSign(@RequestBody DigitalSealBusinessDTO digitalSealBusinessDTO) {
        eSignLogic.sendSign(digitalSealBusinessDTO);
        return ResponseEntity.ok(true);
    }

    /**
     * 附件列表
     * @param businessId 业务id
     */
    @RequestMapping("/file/list")
    public ResponseEntity<List<DigitalSealBusinessDetailDTO>> fileList(Long businessId,String businessTypeCode,String signOrder,Integer notShowDelete) {
        List<DigitalSealBusinessDetailDTO> digitalSealBusinessDetailDTOS = eSignLogic.fileList(businessId,businessTypeCode,signOrder,notShowDelete);
        return ResponseEntity.ok(digitalSealBusinessDetailDTOS);
    }

    /**
     * 附件列表, 主要是交付管理->采购订单，签署文件查看，勾选多个订单，查询多个订单的签署附件。
     * @param businessId 业务id集合
     */
    @RequestMapping("/file/lists")
    public ResponseEntity<List<DigitalSealBusinessDetailDTO>> fileList(String businessId,String businessTypeCode) {
        List<DigitalSealBusinessDetailDTO> digitalSealBusinessDetailDTOS = new ArrayList<>();
        List<String> idList = Arrays.asList(businessId.split(","));
        for (String id :idList) {
            List<DigitalSealBusinessDetailDTO> tempDigitalSealBusinessDetailDTOS = eSignLogic.fileList(Long.parseLong(id), businessTypeCode,null,null);
            digitalSealBusinessDetailDTOS.addAll(tempDigitalSealBusinessDetailDTOS);
        }
        return ResponseEntity.ok(digitalSealBusinessDetailDTOS);
    }

    /**
     * 附件列表
     * @param businessIds 业务ids
     */
    @RequestMapping("/file/status/list")
    public ResponseEntity<List<DigitalSealBusinessDetailDTO>> fileList(String businessIds,String businessTypeCode,Integer signStatus) {
        List<DigitalSealBusinessDetailDTO> digitalSealBusinessDetailDTOS = eSignLogic.fileListByStatus(businessIds,businessTypeCode,signStatus);
        return ResponseEntity.ok(digitalSealBusinessDetailDTOS);
    }

    @PostMapping("/remove")
    public ResponseEntity remove(Long id,String deleteRemark) {
        eSignLogic.remove(id,deleteRemark);
        return ResponseEntity.ok(true);
    }
}
