package com.yem.wm.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yem.em.task.AssequipmentTaskEdit;
import com.yem.wm.utils.YunzhijiaUtils;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.exception.KDException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhouc
 * @date 2024/3/11 11:14
 * @className YunZhiJaGetOpenidTask
 * @description 获取云之家人员Openid
 */
public class YunZhiJaGetOpenidTask extends AbstractTask {
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        Log log = LogFactory.getLog(YunZhiJaGetOpenidTask.class);
        QFilter filter = new QFilter("enable", QCP.equals, "1");
        filter.and(new QFilter("phone", "!=", ""));
        DynamicObjectCollection bos_user = QueryServiceHelper.query("bos_user",
                "id,name,number,phone,useropenid,eid,tid,fuid", new QFilter[]{filter}, null);
        if (bos_user.size() > 0) {
            for (int i = 0; i < bos_user.size(); i++) {
                DynamicObject user = bos_user.get(i);
                if (user.getString("phone").length() > 0) {
                    Map<String, String> phone = getOpenId(user.getString("phone"));
                    if (phone.size() > 0) {
                        DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(user.getLong("id"), "bos_user");
                        dynamicObject.set("eid", "54991");
                        dynamicObject.set("tid", "54991");
                        dynamicObject.set("useropenid", phone.get("openId"));
                        dynamicObject.set("fuid", phone.get("uid"));
                        SaveServiceHelper.update(dynamicObject);
                    }
                }
            }
        }
    }

    public static Map<String, String> getOpenId(String phonNum) {
        Map<String, String> map = new HashMap<>();
        try {
            String req = "{\"type\":0,\"array\":[\"" + phonNum + "\"]}";
            String retstr = YunzhijiaUtils.doPost(YunzhijiaUtils.yzjurl + "/cmmpapi/cmmp/person.do?tokenId=D4EB05B9FE9C41D0868D33EC92AAEDB4", req);
            JSONObject retJson = JSONObject.parseObject(retstr);
            JSONArray datas = retJson.getJSONArray("data");
            for (int i = 0; i < datas.size(); i++) {
                JSONObject data = datas.getJSONObject(i);
                map.put("openId", data.getString("openId"));
                map.put("uid", data.getString("uid"));
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } catch (NoSuchAlgorithmException ex) {
            throw new RuntimeException(ex);
        } catch (KeyManagementException ex) {
            throw new RuntimeException(ex);
        }
        return map;
    }
}
