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

import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.common.hrm.util.HrmCommonUtil;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.chapanda.feishu.util.Esb2FeishuBotCreateTodoUtil;
import com.weaver.seconddev.chapanda.feishu.util.Esb2FeishuOpenIdUtil;
import com.weaver.teams.domain.user.SimpleEmployee;
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("Esb2FeishuBotCreateTodoAction")
public class Esb2FeishuBotCreateTodoAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2FeishuBotCreateTodoAction.class);

    @Autowired
    private HrmCommonUtil hrmCommonUtil;

    @Autowired
    Esb2FeishuOpenIdUtil esb2FeishuOpenIdUtil;

    @Autowired
    Esb2FeishuBotCreateTodoUtil esb2FeishuBotCreateTodoUtil;


    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {

        String requestid = String.valueOf(params.get("requestid"));
        String requestName = String.valueOf(params.get("requestName"));
        String mobileurl = String.valueOf(params.get("mobileurl"));
        String pcurl = String.valueOf(params.get("pcurl"));
        String creatorid = String.valueOf(params.get("creatorid"));
        String senderid = String.valueOf(params.get("senderid"));
        String operatorid = String.valueOf(params.get("operatorid"));
        String requestmark = String.valueOf(params.get("requestmark"));
        if("null".equals(requestmark) || requestmark == null){
            requestmark = "";
        }
        String msgType = String.valueOf(params.get("msgType"));
        String nodeid = String.valueOf(params.get("nodeid"));

        String message_ids = "";
        if("0".equals(msgType)){
//            String senderMobile ="";
//            if(StringUtils.isNotBlank(senderid)){
//                Long sender_id = Long.valueOf(senderid);
//                SimpleEmployee senderEmployee = hrmCommonUtil.getSimpleEmployee(sender_id);
//                senderMobile = senderEmployee.getMobile();
//            }

//            String creatorMobile = "";
//            if(StringUtils.isNotBlank(creatorid)){
//                Long creator_id = Long.valueOf(creatorid);
//                SimpleEmployee creatorEmployee = hrmCommonUtil.getSimpleEmployee(creator_id);
//                creatorMobile = creatorEmployee.getMobile();
//            }
//
//            log.error("creatorMobile:{}",creatorMobile);
            String create_open_id = esb2FeishuOpenIdUtil.queryFeishuOpenId(creatorid);
//            if(StringUtils.isNotBlank(creatorMobile)) {
//                Map<String, Object> creatorMap = esb2FeishuOpenIdUtil.queryFeishuOpenIdByMobiles(creatorMobile);
//                create_open_id = String.valueOf(creatorMap.get(creatorMobile));
//            }

            String send_open_id = esb2FeishuOpenIdUtil.queryFeishuOpenId(senderid);
//            if(StringUtils.isNotBlank(senderMobile)){
//                Map<String, Object> senderMap = esb2FeishuOpenIdUtil.queryFeishuOpenIdByMobiles(senderMobile);
//                send_open_id = String.valueOf(senderMap.get(creatorMobile));
//            }

            String operator_open_id = esb2FeishuOpenIdUtil.queryFeishuOpenId(operatorid);
//            if(StringUtils.isNotBlank(operatorid)){
//                Long userid = Long.valueOf(operatorid);
//                log.error("userid:{}",userid);
//                SimpleEmployee userEmployee = hrmCommonUtil.getSimpleEmployee(userid);
//                String userMobile = userEmployee.getMobile();
//                if(StringUtils.isNotBlank(userMobile)){
//                    Map<String, Object> userMap = esb2FeishuOpenIdUtil.queryFeishuOpenIdByMobiles(userMobile);
//                    operator_open_id = String.valueOf(userMap.get(userMobile));
//                }
//            }

            log.error("operator_open_id:{}",operator_open_id);
            log.error("create_open_id:{}",create_open_id);
            if(StringUtils.isNotBlank(create_open_id)){
                if(StringUtils.isNotBlank(send_open_id)){

                    message_ids = esb2FeishuBotCreateTodoUtil.senBotTodoMessage(
                            requestid,requestName,requestmark,
                            create_open_id,send_open_id,operator_open_id,
                            mobileurl,pcurl
                    );

                }else{
                    log.error("send_open_id:{}","获取待办人员为空");
                }
            }else{
                log.error("creatorMobile:{}","创建人飞书openid为空");
            }
        }

        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("message","成功");
        actionMap.put("data",message_ids);

        actionMap.put("messageid",message_ids);
        actionMap.put("requestid",requestid);
        actionMap.put("requestname",requestName);
        actionMap.put("nodeid",nodeid);
        actionMap.put("operatorid",operatorid);
        return WeaResult.success(actionMap);
    }
}
