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

//import com.cloudstore.dev.api.bean.MessageBean;
//import com.cloudstore.dev.api.bean.MessageType;
//import com.cloudstore.dev.api.util.Util_Message;

import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.mc.api.async.AsyncSystemMessageRest;
import com.weaver.mc.api.entity.MessageEvent;
import com.weaver.mc.api.entity.MessageModule;
import com.weaver.mc.api.entity.SendMessageEntity;
import com.weaver.mc.api.entity.UserEntity;
import com.weaver.seconddev.interfaces.prop.ConfigProp;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.interfaces.workflow.action.Action;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.soa.workflow.request.RequestInfo;
import com.weaver.verupgrade.workflow.request.RequestManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@org.springframework.stereotype.Service("workflow_action_RemindForCarAction")
public class RemindForCarAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

    // 对应E9的请求信息对象
    @Autowired
    private RequestInfo requestInfoTemp;
    @Autowired
    private ConfigProp configProp;

    @Autowired
    private AsyncSystemMessageRest systemMessageRest;

    private static final Logger logger_9618d585 = LoggerFactory.getLogger(RemindForCarAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        Map<String, Object> weaverResultMap = new HashMap<>();
        Long requestIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("requestid", -1)));
        Long userIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("userid", -1)));
        RequestInfo request = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            try {
                BaseBean basebean = new BaseBean();
                logger_9618d585.error(String.valueOf("do RemindForCarAction start "));
//                String content = Util.null2String(I18nContextUtilcus.getBean(Environment.class).getProperty("remindforweixin.carmessage")).trim();
                String content =   configProp.getCarmessage();
                String requestId = request.getRequestid();
                List<String> list = new ArrayList<String>();
                // 获取主表数据
                Property[] aproperty = request.getMainTableInfo().getProperty();
                for (int i = 0; i < aproperty.length; i++) {
                    String name = aproperty[i].getName().toUpperCase();
                    String value = Util.null2String(aproperty[i].getValue());
                    if (name.equals("SQR")) {
                        // 申请人
                        if (!"".equals(value)) {
                            list.add(value);
                        }
                    }
                    /*if(name.equals("CCRY")){//乘车人员
                	if(!"".equals(value)){
                		String[] array = value.split(",");
                		for(int k=0;k<array.length;k++){
                			list.add(array[k]);
                		}
                	}
                }*/
                }
                // 去除重复的数据
                /*if(list!= null && list.size() > 0){
            	HashSet<String> h = new HashSet<String>(list);
                list.clear();
                list.addAll(h);
            }*/

                // InterfaceUtil.sendMsg(list, ",28ffc89713a64541891276edc32131cc,", requestId, content, 1);

                //TODO custom 消息
//                MessageType messageType = MessageType.newInstance(506);
//                MessageBean messageBean = Util_Message.createMessage(messageType, new HashSet(list), content, content, "", "");
//                messageBean.setCreater(1);
//                Util_Message.store(messageBean);

                list.forEach(item->{

                });

                List<UserEntity> receivers = new ArrayList<>();
//                receivers.add()

                SendMessageEntity smg = new SendMessageEntity();
                smg.setModule(MessageModule.TASK);
                smg.setEvent(MessageEvent.NOTIFY);
                smg.setSender(UserEntity.SYSTEM_USER);
//                smg.setReceivers(Collections.singletonList(DEFAULT_RECIVER));
                smg.setText("任务提醒");
                smg.setPcUrl("");//可以为空
                smg.setAppUrl("");//可以为空
                smg.setTitle("任务提醒");
                smg.setContent(content);
                WeaResult<Long> longWeaResult = systemMessageRest.sendMsg(smg);



                logger_9618d585.error(String.valueOf("do RemindForCarAction success "));
            } catch (Exception e) {
                System.err.println(e.toString());
                return  WeaResult.fail(500, "RemindForCarAction err"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), request.getRequestManager()));

            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_9618d585.error("RemindForCarAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "RemindForCarAction e: {}"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), request.getRequestManager()));
        }
    }

    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;
    }
}
