package com.weaver.seconddev.chapanda.esign.service.impl;

import com.weaver.eteams.file.client.file.FileObj;
import com.weaver.seconddev.chapanda.esign.dao.SignFlowCallBackDao;
import com.weaver.seconddev.chapanda.esign.service.SignFlowsCallBackService;
import com.weaver.seconddev.chapanda.esign.util.Esb2EsignFileUploadUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import com.weaver.seconddev.chapanda.esign.constant.Constants;

/***
 * author:shil
 */
@Service
public class SignFlowsCallBackServiceImpl implements SignFlowsCallBackService {

    private final static Logger log = LoggerFactory.getLogger(SignFlowsCallBackServiceImpl.class);

    @Autowired
    SignFlowCallBackDao signFlowCallBackDao;

    @Autowired
    Esb2EsignFileUploadUtil esb2EsignFileUploadUtil;

    /**
     *
     * @param flowId
     * @param downloadDocUrl
     * @param finishFileKey
     * @return
     */
    @Override
    public void creatDocFile(String flowId,String downloadDocUrl,String finishFileKey) {
        log.error("flowId33:{}",flowId);
        Long longSignFlowId = Constants.callbacFolderId;
        log.error("longSignFlowId:{}",longSignFlowId);
        Map<String,Object> flowInfoMap = signFlowCallBackDao.queryFlowInfo(flowId);

        String form_data_id = String.valueOf(flowInfoMap.get("form_data_id"));
        String glhtlylc = String.valueOf(flowInfoMap.get("glhtlylc"));
        log.error("form_data_id:{}",form_data_id);
        log.error("glhtlylc:{}",glhtlylc);

        String requestname = "";
        if(StringUtils.isNotBlank(glhtlylc)){
            requestname = signFlowCallBackDao.queryRequestName(glhtlylc);
        }
        log.error("requestname:{}",requestname);

        if(StringUtils.isNotBlank(requestname)){
            Long docId = 0L;
            Long fileid =  0L;
            if(StringUtils.isNotBlank(downloadDocUrl)){
                FileObj fileObj = esb2EsignFileUploadUtil.uploadFile(downloadDocUrl,requestname,longSignFlowId);
                log.error("fileObj:{}",fileObj.toString());
                docId = fileObj.getDocId();
                fileid = fileObj.getId();
                if(fileid > 0){
                    signFlowCallBackDao.updateFormTableInfo(form_data_id,fileid,finishFileKey);
                }
            }
        }
    }
}
