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.dao.HrmQueryOpenIdDao;
import com.weaver.seconddev.chapanda.feishu.util.CommonUtil;
import com.weaver.seconddev.chapanda.feishu.util.Esb2FeishuOpenIdUtil;
import com.weaver.seconddev.chapanda.feishu.util.Esb2FeishuSendDeleteUtil;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private HrmCommonUtil hrmCommonUtil;

    @Autowired
    Esb2FeishuOpenIdUtil esb2FeishuOpenIdUtil;

    @Autowired
    Esb2FeishuSendDeleteUtil esb2FeishuSendDeleteUtil;

    @Autowired
    HrmQueryOpenIdDao hrmQueryOpenIdDao;

    @Autowired
    CommonUtil commonUtil;


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

        String requestid = String.valueOf(params.get("requestid"));
        String createTime = String.valueOf(params.get("createTime"));
        if(StringUtils.isNotBlank(createTime)){
            if(createTime.contains("T")){
                createTime = createTime.replace("T"," ");
            }
        }

        String requestName = String.valueOf(params.get("requestName"));
        String archivingTime = String.valueOf(params.get("archivingTime"));
        if("null".equals(archivingTime) || archivingTime == null){
            archivingTime = "";
        }
        String nodeId = String.valueOf(params.get("nodeId"));
        String nodeName = String.valueOf(params.get("nodeName"));
        String degree = String.valueOf(params.get("degree"));

        log.error("requestid:{}",requestid);
        log.error("requestName:{}",requestName);
        log.error("nodeName:{}",nodeName);

        String mobileurl = String.valueOf(params.get("mobileurl"));
        String pcurl = String.valueOf(params.get("pcurl"));
        String creatorid = String.valueOf(params.get("creatorid"));

        String approvestatus = String.valueOf(params.get("approvestatus"));
        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 recepttime = String.valueOf(params.get("recepttime"));
        if(StringUtils.isNotBlank(recepttime)){
            if(recepttime.contains("T")){
                recepttime = recepttime.replace("T"," ");
            }
        }

        log.error("msgType:{}",msgType);

        String cid = String.valueOf(params.get("cid"));
        log.error("bot:requestid:{},nodeid:{},operatorid:{},cid{},msgType:{},todo",requestid,nodeId,operatorid,cid,msgType);

        String code = "";

        if("2".equals(msgType)){

            String creatorname = "";
            String create_open_id = "";
            if(StringUtils.isNotBlank(creatorid)){
                Long creator_id = commonUtil.convertStringToLong(creatorid);
                if(creator_id > 0L){
                    SimpleEmployee creatorEmployee = hrmCommonUtil.getSimpleEmployee(creator_id);
                    String creatorMobile = creatorEmployee.getMobile();
                    log.error("creatorMobile:{}",creatorMobile);

                    creatorname = creatorEmployee.getUsername();
                    create_open_id = hrmQueryOpenIdDao.queryHrmOpenId(creatorid);

                    if(StringUtils.isBlank(create_open_id)){
                        if(StringUtils.isNotBlank(creatorMobile)) {
                            Map<String, Object> userMap = esb2FeishuOpenIdUtil.queryFeishuOpenIdByMobiles(creatorMobile);
                            create_open_id = String.valueOf(userMap.get(creatorMobile));
                        }
                    }
                }
            }

            log.error("creatorname:{}",creatorname);
            log.error("create_open_id:{}",create_open_id);

            String operator_open_id = esb2FeishuOpenIdUtil.queryFeishuOpenId(operatorid);
//            if(StringUtils.isNotBlank(operatorid)){
//                Long userid = Long.valueOf(operatorid);
//                SimpleEmployee userEmployee = hrmCommonUtil.getSimpleEmployee(userid);
//
//                String userMobile = userEmployee.getMobile();
//                log.error("userMobile:{}",userMobile);
//                log.error("操作人:{}",userEmployee.getUsername());
//                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);
            if(StringUtils.isNotBlank(create_open_id)){
                if(StringUtils.isNotBlank(operator_open_id)){
                    String status = "DELETED";
                    code = esb2FeishuSendDeleteUtil.senTodoMessage(
                            requestid,requestName,requestmark,
                            creatorname,createTime,
                            nodeId,nodeName,
                            archivingTime,
                            degree,mobileurl,pcurl,create_open_id,operator_open_id,
                            recepttime,status,cid
                    );
                    if(!"0".equals(code)){
                        log.error("todoOpenIdList:{}","创建飞书待办异常");
                    }
                }else{
                    log.error("todoOpenIdList:{}","获取待办人员为空");
                }
            }else{
                log.error("creatorMobile:{}","创建人飞书openid为空");
            }

        }


        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("msg","成功");
        actionMap.put("data",requestid+"_"+operatorid);
        actionMap.put("status",code);
        actionMap.put("requestid",requestid);
        actionMap.put("nodeid",nodeId);
        actionMap.put("operatorid",operatorid);
        return WeaResult.success(actionMap);
    }
}
