package com.ms.biz.spi.auth;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinritemai.cloud.base.api.BaseRequest;
import com.jinritemai.cloud.base.api.BaseResponse;
import com.jinritemai.cloud.base.api.ExtensionService;
import com.jinritemai.cloud.base.api.ExtensionServiceHandler;
import com.ms.biz.common.R;
import com.ms.biz.common.SPIBaseService;
import com.ms.biz.consts.CommonConst;
import com.ms.api.dto.auth.GetAuthInfosRequestDTO;
import com.ms.biz.tool.DsJsonRequestTemplate;
import com.ms.biz.tool.SecurityTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取抖店店铺绑定的1688账户
 */
@ExtensionService("getAuthInfos")
@Slf4j
public class GetAuthInfosService extends SPIBaseService implements ExtensionServiceHandler<GetAuthInfosRequestDTO, JSONObject> {

    @Autowired
    private DsJsonRequestTemplate dsJsonRequestTemplate;

    @Override
    public BaseResponse<JSONObject> handle(BaseRequest<GetAuthInfosRequestDTO> req) {
        initHandle(req);
        // ----参数校验----
        GetAuthInfosRequestDTO fields =  req.getData();

        // ----逻辑校验----

        // ----业务处理----
        Map<String, Object> params = new HashMap<>();
        params.put("userId", SecurityTool.encodeByAES(String.valueOf(shopId)));
        params.put("platform", CommonConst.PLATFORM);
        String res = null;
        JSONObject resObj = null;
        try {
            res = dsJsonRequestTemplate.execute("/micro_move/get_auth_info", params);
            log.info(res);
            resObj = JSON.parseObject(res);
            resObj.remove("authCode");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        // ----结果返回----
        return R.ok(resObj);
    }
}
