package com.malk.ruisi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.malk.ruisi.service.QysApiService;
import com.malk.ruisi.service.QysService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.malk.ruisi.constant.QysConstant.*;

@Slf4j
@Service
public class QysServiceImpl implements QysService {
   @Autowired
   private QysApiService qysApiService;
    @Override
    public JSONObject createDraft(Map param) throws Exception {
        return qysApiService.sendPost(param,QYS_API_CREATEBYCATEGORY);
    }

    @Override
    public JSONObject createbyfile(Map da) throws Exception {
        return qysApiService.sendPostFile(da,QYS_API_CREATEBYFILE);
    }

    @Override
    public JSONObject appointurl(String contractId) throws Exception {

            Map<String,Object> data=new HashMap();
            data.put("contractId",contractId);
            return qysApiService.sendGet(data,QYS_API_PRESIGNURL);
//            return qysApiService.sendPost(data,QYS_API_PRESIGNURL);

    }

    @Override
    public JSONObject createbycategory(Map data) throws Exception {
        return qysApiService.sendPost(data,QYS_API_CREATEBYCATEGORY);
    }

    @Override
    public JSONObject getDetail(String bizId, String tenantName) throws Exception {
        Map<String ,Object> data = new HashMap();
        data.put("bizId",bizId);
        data.put("tenantName",tenantName);
        return qysApiService.sendGet(data,QYS_API_CONTRACT_DETAIL);
    }

    @Override
    public JSONObject send(String contractId, String tenantName) throws Exception {
        Map<String,Object> data=new HashMap();
        data.put("contractId",contractId);
        data.put("tenantName",tenantName);
        return qysApiService.sendPost(data,QYS_API_SEND);
    }
    public JSONObject getYWFLid(Map data) throws Exception {
        return qysApiService.sendGet(data,QYS_API_CATEGORIES,false);
    }
    /***
     * 下载签署后文件
     * @param contractId
     * @param file
     * @throws Exception
     */
    @Override
    public void download(String contractId, File file) throws Exception {
        log.info("下载文件：{}",file.getPath());
        qysApiService.getFile(new JSONObject().fluentPut("contractId",contractId),QYS_API_DOWNLOAD,file);
        log.info("下载完成：文件大小：{}",file.length());
    }

    @Override
    public JSONObject createseal(Map map) throws Exception {
        return qysApiService.sendPost(map,QYS_API_SEALBYCATEGORY);
    }

    @Override
    public JSONObject applyseals(Map param) throws Exception {
        return qysApiService.sendPost(param,QYS_API_MULTIPLE);
    }

    @Override
    public JSONObject signurla(Map signatory) throws Exception {
        return qysApiService.sendPost(signatory,QYS_API_SIGNURL);
    }
}
