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.CheckDsAppAuthExpireRequestDTO;
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;

/**
 * 检查店铺授权1688分销采购账户授权是否过期
 */
@ExtensionService("checkDsAppAuthExpire")
@Slf4j
public class CheckDsAppAuthExpireService extends SPIBaseService implements ExtensionServiceHandler<CheckDsAppAuthExpireRequestDTO, JSONObject>{

    @Autowired
    private DsJsonRequestTemplate dsJsonRequestTemplate;

    @Override
    public BaseResponse<JSONObject> handle(BaseRequest<CheckDsAppAuthExpireRequestDTO> req) {
        initHandle(req);
        CheckDsAppAuthExpireRequestDTO fields =  req.getData();
        HashMap<String, Object> params = new HashMap<>();
        params.put("userIds", SecurityTool.encodeByAES(String.valueOf(shopId)));
        params.put("memberId",fields.getMemberId());
        params.put("authCode",fields.getAuthCode());
        params.put("platform", CommonConst.PLATFORM);
        String res = null;
        try {
            res = dsJsonRequestTemplate.execute("/platform_shop/checkDsAppAuthExpire", params);
            log.info(res);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        // ----结果返回----
        return R.ok(JSON.parseObject(res));
    }
}
