package com.weaver.seconddev.chapanda.esign.controller;

import com.alibaba.fastjson.JSONObject;
import com.weaver.common.authority.annotation.WeaPermission;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.seconddev.chapanda.esign.entity.CallbackUrlBean;
import com.weaver.seconddev.chapanda.esign.entity.FinishDocUrlBean;
import com.weaver.seconddev.chapanda.esign.service.SignFlowsCallBackService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/papi/secondev/cbd/signflow")
public class SignFlowsCallBackController {

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


    @Autowired
    SignFlowsCallBackService signFlowsCallBackService;

    /***
     * 回调接口
     * @return
     * @throws IOException
     */
    @WeaPermission(publicPermission = true)
    @PostMapping("/doCallBack")
    public JSONObject doCallBack(@RequestBody CallbackUrlBean callbackUrlBean) throws IOException {

        String action = callbackUrlBean.getAction();
        log.error("action:{}",action);
        String flowId = String.valueOf(callbackUrlBean.getFlowId());
        log.error("flowId:{}",flowId);
        String flowType = callbackUrlBean.getFlowType();
        log.error("flowType:{}",flowType);
        int status = callbackUrlBean.getStatus();
        log.error("status:{}",status);
        String createTime = callbackUrlBean.getCreateTime();
        log.error("createTime:{}",createTime);
        String endTime = callbackUrlBean.getEndTime();
        log.error("endTime:{}",endTime);

        List<FinishDocUrlBean> finishDocUrlBeans = callbackUrlBean.getFinishDocUrlBeans();
        for (int i = 0; i < finishDocUrlBeans.size(); i++) {
            FinishDocUrlBean finishDocUrlBean = finishDocUrlBeans.get(i);
            String docFileKey = finishDocUrlBean.getDocFileKey();
            log.error("docFileKey:{}",docFileKey);
            String downloadDocUrl = finishDocUrlBean.getDownloadDocUrl();
            log.error("downloadDocUrl:{}",downloadDocUrl);
            String finishFileKey = finishDocUrlBean.getFinishFileKey();
            log.error("finishFileKey:{}",finishFileKey);
        }

        if("SIGN_FLOW_FINISH".equals(action)){
            log.error("action22:{}",action);
            if("Common".equals(flowType)){
                log.error("flowType22:{}",flowType);
                if(status == 2){
                    log.error("status22:{}",status);
                    if(StringUtils.isNotEmpty(flowId)){
                        log.error("flowId22:{}",flowId);
                        if(finishDocUrlBeans.size()>0){
                            FinishDocUrlBean finishDocUrlBean = finishDocUrlBeans.get(0);
                            String docFileKey = finishDocUrlBean.getDocFileKey();
                            String downloadDocUrl = finishDocUrlBean.getDownloadDocUrl();
                            String finishFileKey = finishDocUrlBean.getFinishFileKey();
                            log.error("downloadDocUrl:{}",downloadDocUrl);
                            log.error("finishFileKey:{}",finishFileKey);
                            if(StringUtils.isNotBlank(downloadDocUrl)){
                              signFlowsCallBackService.creatDocFile(flowId,downloadDocUrl,finishFileKey);
                            }
                        }
                    }
                }
            }
        }
        JSONObject dataJson = new JSONObject();
        dataJson.put("errCode","0");
        dataJson.put("msg","成功");

        return dataJson;
    }

    /**
     * 测试接口
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @WeaPermission(publicPermission = true)
    @GetMapping("/test")
    public WeaResult<Object> test(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, Object> recordMap = new HashMap<String,Object>();
        recordMap.put("test","test");
        return WeaResult.success(recordMap);
    }

}
