package com.mortals.xhx.module.dingtalk;//package com.mortals.xhx.module.dingtalk;
//
//import com.alibaba.fastjson.JSONObject;
//import com.dingtalk.api.DefaultDingTalkClient;
//import com.dingtalk.api.DingTalkClient;
//import com.dingtalk.api.request.OapiGettokenRequest;
//import com.dingtalk.api.request.OapiProcessWorkrecordCreateRequest;
//import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
//import com.dingtalk.api.response.OapiGettokenResponse;
//import com.dingtalk.api.response.OapiProcessWorkrecordCreateResponse;
//import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
//import com.mortals.framework.exception.AppException;
//import com.mortals.xhx.common.code.YesNoEnum;
//import com.mortals.xhx.common.config.TalkConfiguration;
//import com.taobao.api.ApiException;
//import lombok.extern.apachecommons.CommonsLog;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Service;
//
//import java.time.Year;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
///**
// * @ClassName DingTalkService
// * @Description TODO
// * @Author finegirl
// * @Date 2021/2/1 14:23
// **/
//@Service
//@CommonsLog
//public class DingTalkService {
//
//    @Autowired
//    private TalkConfiguration talkConfiguration;
//
//    /**
//     * 获取token
//     * @return
//     */
//    public String getToken() {
//        try {
//            DingTalkClient client = new DefaultDingTalkClient(talkConfiguration.getGetTokneUrl());
//            OapiGettokenRequest req = new OapiGettokenRequest();
//            req.setAppkey(talkConfiguration.getApiKey());
//            req.setAppsecret(talkConfiguration.getAppsecret());
//            req.setHttpMethod("GET");
//            OapiGettokenResponse rsp = client.execute(req);
//            log.info("gettoken rsp:" + rsp.getBody());
//            if (rsp.getErrcode() == 0) {
//                return rsp.getAccessToken();
//            } else {
//                throw new AppException(String.format("errorCode:%s,errorMsg:%s", rsp.getErrcode(), rsp.getErrmsg()));
//            }
//
//        } catch (Exception e) {
//            log.error("gettoken error:", e);
//            throw new AppException(e.getMessage());
//        }
//    }
//
//    /**
//     * 根据手机号码获取userid
//     * @param mobile
//     * @param accessToken
//     * @return
//     */
//    public String getUserIdbyMobile(String mobile, String accessToken) {
//        try {
//            DingTalkClient client = new DefaultDingTalkClient(talkConfiguration.getGetbymobile());
//            OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
//            req.setMobile(mobile);
//            OapiV2UserGetbymobileResponse rsp = client.execute(req, accessToken);
//            log.info("getuser rsp:" + rsp.getBody());
//            if (rsp.getErrcode() == 0) {
//                return rsp.getResult().getUserid();
//            } else {
//                throw new AppException(String.format("errorCode:%s,errorMsg:%s", rsp.getErrcode(), rsp.getErrmsg()));
//            }
//
//        } catch (ApiException e) {
//            log.error("getuserbymobile error:", e);
//            throw new AppException(e.getMessage());
//        }
//    }
//
//    /**
//     * 创建审批实例
//     * @param processCode
//     * @param userId
//     * @param accessToken
//     * @param contentMap
//     * @param title
//     * @return
//     */
//    public String workCreate(String processCode, String userId, String accessToken, Map<String, String> contentMap, String title) {
//        try {
//            DingTalkClient client = new DefaultDingTalkClient(talkConfiguration.getWorkCreate());
//            OapiProcessWorkrecordCreateRequest req = new OapiProcessWorkrecordCreateRequest();
//            OapiProcessWorkrecordCreateRequest.SaveFakeProcessInstanceRequest obj = new OapiProcessWorkrecordCreateRequest.SaveFakeProcessInstanceRequest();
//            obj.setProcessCode(processCode);
//            obj.setOriginatorUserId(userId);
//
//            List<OapiProcessWorkrecordCreateRequest.FormComponentValueVo> list = contentMap.entrySet().stream().map(item -> {
//                OapiProcessWorkrecordCreateRequest.FormComponentValueVo temp = new OapiProcessWorkrecordCreateRequest.FormComponentValueVo();
//                temp.setName(item.getKey());
//                temp.setValue(item.getValue());
//                return temp;
//            }).collect(Collectors.toList());
//
//            obj.setFormComponentValues(list);
//            obj.setTitle(title);
//            req.setRequest(obj);
//            OapiProcessWorkrecordCreateResponse rsp = client.execute(req, accessToken);
//
//            if (rsp.getErrcode() == 0) {
//                return rsp.getResult().getProcessInstanceId();
//            } else {
//                throw new AppException(String.format("errorCode:%s,errorMsg:%s", rsp.getErrcode(), rsp.getErrmsg()));
//            }
//        } catch (ApiException e) {
//            log.error("workCreate error:", e);
//            throw new AppException(e.getMessage());
//        }
//
//    }
//
//}
