package com.weaver.seconddev.interfaces.workflow.action;

import com.alibaba.fastjson.JSON;
import com.weaver.authority.api.rest.RemoteAuthorityRoleService;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.ebuilder.form.client.ebform.data.EbFormDataDubboService;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.framework.rpc.annotation.RpcReference;
import com.weaver.seconddev.interfaces.easoa.CallEasBlacklistServices;
import com.weaver.seconddev.interfaces.easoa.ChangeWhiteListState;
import com.weaver.seconddev.interfaces.prop.ConfigProp;
import com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.interfaces.workflow.action.Action;
import com.weaver.verupgrade.soa.workflow.request.RequestInfo;
import com.weaver.verupgrade.workflow.request.RequestManager;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service("workflow_action_CustomJavaCode")
public class CustomJavaCode extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {


    @Autowired
    private RequestInfo requestInfoTemp;

    @Autowired
    private ConfigProp configProp;

    @RpcReference(group = "ebuilderform")
    private EbFormDataDubboService ebFormDataDubboService;

    @RpcReference(group = "ebuilderform")
    private RemoteAuthorityRoleService remoteAuthorityRoleService;


    @Autowired
    private CallEasBlacklistServices callEasBlacklistServices;

    @Autowired
    private ChangeWhiteListState changeWhiteListState;

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CustomJavaCode.class);


    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
        logger.error("start CustomTeAction");
        logger.error("params = " + JSON.toJSONString(params));
        Map<String, Object> weaverResultMap = new HashMap<>();
        // 获取当前日期和时间
        Date currentDate = new Date();

        // 格式化日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = dateFormat.format(currentDate);

        // 格式化时间
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String formattedTime = timeFormat.format(currentDate);



        Long billid = Long.parseLong(String.valueOf(params.getOrDefault("billid", -1)));

        logger.error("billid = " + billid);
        try {

            logger.error("billid : " + billid);
            RecordSet recordSet = I18nContextUtil.getBean(RecordSet.class);
            String str1 = " select * from e10_common.dbo.formtable_main_619 where id =" + billid;
            logger.error(" sql == " + str1);
            recordSet.executeSql(str1);
            ArrayList<Map<String, String>> arrayList = new ArrayList();
            while (recordSet.next()) {
                String k = recordSet.getString("id");
                String str4 = recordSet.getString("bh");
                String m = recordSet.getString("lx");
                String str5 = recordSet.getString("mc");
                String str6 = recordSet.getString("dm");
                String str7 = recordSet.getInt("zt") + "";
                String str8 = recordSet.getString("yxjzq");
                String str9 = recordSet.getString("jyyy");
                if (!"".equals(str9))
                    str9 = str9.replace("<br>", "");
                String str10 =formattedDate;
                String str11 = formattedTime;
                Map<String, String> hashMap = new HashMap<String, String>();
                hashMap.put("id", k  );
                hashMap.put("bh", str4);
                hashMap.put("dm", str6);
                hashMap.put("lx", m  );
                hashMap.put("mc", str5);
                hashMap.put("zt", str7);
                hashMap.put("yxjzq", str8);
                hashMap.put("jyyy", str9);
                hashMap.put("modedatacreatedate", str10);
                hashMap.put("modedatacreatetime", str11);
                arrayList.add(hashMap);
            }

            logger.error(" 入参 == " + arrayList);
            String[] arrayOfString = callEasBlacklistServices.syncBlacklist(arrayList);
            String str2 = arrayOfString[0];
            String str3 = arrayOfString[1];
            logger.error("callEasBlacklistServices  do blacklist webservice: " + str2 + "][message : " + str3 + "]");


            changeWhiteListState.changeState(arrayList);


            Map<String, Object> res = new HashMap<>();
            res.put("status", true);
            res.put("msg", "success");
            res.put("params", params);
            return WeaResult.success(res);
        } catch (Exception e) {
            logger.error("异常信息 ==== " + e.getMessage());
            e.printStackTrace();
            return WeaResult.fail(500, "出错啦:"+e);
        }

//
    }

    Map<String, Object> getResultMapForAction(Map<String, Object> map, String key, Object value, RequestManager requestManager) {

        if (key != null && !key.isEmpty()) {
            map.put(key, value);
        }
        String msgContent = requestManager.getMessagecontent();
        if (msgContent != null && !msgContent.isEmpty()) {
            map.put("msgContent", msgContent);
        }
        String msgId = requestManager.getMessageid();
        if (msgId != null && !msgId.isEmpty()) {
            map.put("msgId", msgId);
        }
        return map;
    }




























}
