package com.weaver.seconddev.chapanda.qunjie.esb;

import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.chapanda.qunjie.util.Esb2QunjieEconomicCompensationUtil;
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.HashMap;
import java.util.Map;


/**
 * 经济补偿
 */

@Service("Esb2QunjieEconomicCompensationAction")
public class Esb2QunjieEconomicCompensationAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2QunjieEconomicCompensationAction.class);

    @Autowired
    Esb2QunjieEconomicCompensationUtil esb2QunjieEconomicCompensationUtil;
    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {

        String requestName = (String) params.get("requestname");
        String requestid = (String) params.get("requestid");
        log.error("requestid:{}",requestid);
        log.error("requestName:{}",requestName);

//        String sealId = (String) params.get("sealId");
//        String applyCount = (String) params.get("applyCount");
//        String applyCrossPageSealCount = (String) params.get("applyCrossPageSealCount");
//        log.error("requestName:{}",requestName);
//        log.error("sealId:{}",sealId);
//        log.error("applyCount:{}",applyCount);
//        log.error("applyCrossPageSealCount:{}",applyCrossPageSealCount);

        String fileids = (String) params.get("fileid");
        log.error("fileids:{}",fileids);

//        if(StringUtils.isBlank(sealId)){
//            return WeaResult.fail(500,"获取印章信息为空");
//        }

//        if(StringUtils.isBlank(fileid)){
//            return WeaResult.fail(500,"用印文件为空");
//        }
//        Long longFileId = 0L;
//        try{
//            longFileId = Long.valueOf(fileid);
//        }catch (Exception e){
//            log.error("e:{}",e);
//        }
//
//        if(longFileId <=0){
//            return WeaResult.fail(500,"用印文件ID转换失败");
//        }

//        if(StringUtils.isBlank(applyCount) && StringUtils.isBlank(applyCrossPageSealCount)){
//            return WeaResult.fail(500,"用印次数和骑缝章次数不能同时为空");
//        }

//        int applyCountInt = 0;
//        if(StringUtils.isNotBlank(applyCount)){
//            applyCountInt = Integer.parseInt(applyCount);
//        }
//        int applyCrossPageSealCountInt = 0;
//        if(StringUtils.isNotBlank(applyCrossPageSealCount)){
//            applyCrossPageSealCountInt = Integer.parseInt(applyCrossPageSealCount);
//        }
//        if(applyCountInt <=0 && applyCrossPageSealCountInt <=0){
//            return WeaResult.fail(500,"用印次数和骑缝章次数不能同时小等于0");
//        }

        Map<String, Object> dataMap = esb2QunjieEconomicCompensationUtil.applySealRequest(requestid,requestName,fileids);

        String status = String.valueOf(dataMap.get("status"));
        if(!"0".equals(status)){
            String data = String.valueOf(dataMap.get("data"));
            return WeaResult.fail(500,data);
        }
        String applyId = String.valueOf(dataMap.get("applyId"));
        String workflowBaseId = String.valueOf(dataMap.get("workflowBaseId"));
        String formId = String.valueOf(dataMap.get("formId"));
        String applyNo = String.valueOf(dataMap.get("applyNo"));
        String safeCode = String.valueOf(dataMap.get("safeCode"));
        String vrCode = String.valueOf(dataMap.get("vrCode"));

        if(StringUtils.isBlank(applyId)){
            return WeaResult.fail(500,"用印申请流程创建失败");
        }

        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("msg","申请成功");
        actionMap.put("applyid",applyId);
        actionMap.put("workflowbaseid",workflowBaseId);
        actionMap.put("formid",formId);
        actionMap.put("applyno",applyNo);
        actionMap.put("safecode",safeCode);
        actionMap.put("vrcode",vrCode);
        actionMap.put("status",status);
        actionMap.put("requestid",requestid);
        return WeaResult.success(actionMap);
    }

}
