package com.ms.biz.spi.callback;

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.Ret;
import com.ms.biz.common.SPIBaseService;
import com.ms.api.dto.auth.MemberAuthCallBackRequestDTO;
import com.ms.biz.service.Shop1688DsAuthService;
import com.ms.biz.service.ShopTo1688DsMemberService;
import com.ms.biz.tool.CommonTool;
import com.ms.dal.entity.Shop1688DsAuth;
import com.ms.dal.entity.ShopTo1688DsMember;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Objects;

/**
 * 获取授权1688授权回调地址
 */
@ExtensionService("memberAuthCallBack")
@Slf4j
public class MemberAuthCallbackService extends SPIBaseService implements ExtensionServiceHandler<MemberAuthCallBackRequestDTO, Ret> {
    @Autowired
    private Shop1688DsAuthService shop1688DsAuthService;
    @Autowired
    private ShopTo1688DsMemberService shopTo1688DsMemberService;
    @Override
    public BaseResponse<Ret> handle(BaseRequest<MemberAuthCallBackRequestDTO> req) {
        // ----参数校验----
        MemberAuthCallBackRequestDTO fields =  req.getData();
        log.info("receive callback: " + JSON.toJSONString(fields));
        String body = fields.getBody();
        try {
            JSONObject map = JSONObject.parseObject(body);
            String authCode = map.getString("authCode");
            String cnaliMemberId = map.getString("cnaliMemberId");
            String gmtMemberExpireTime = map.getString("gmtMemberExpireTime");
            String userId = map.getString("userId");
            this.authCode = fields.getAuthCode();

            Shop1688DsAuth shop1688DsAuth = shop1688DsAuthService.getDetailByShopId(Long.valueOf(userId));
            if (Objects.isNull(shop1688DsAuth)) {
                shop1688DsAuth = new Shop1688DsAuth();
                shop1688DsAuth.setGmtCreate(new Date());
                shop1688DsAuthService.insert(shop1688DsAuth);
            }
            shop1688DsAuth.setAuthCode(authCode);
            shop1688DsAuth.setShopId(Long.valueOf(userId));
            shop1688DsAuth.setGmtModified(new Date());
            log.info("receive callback: " + shop1688DsAuth);
            shop1688DsAuthService.updateByPrimaryKey(shop1688DsAuth);

            ShopTo1688DsMember shopTo1688DsMember = shopTo1688DsMemberService.getDetailByShopId(Long.valueOf(userId));
            if (Objects.isNull(shopTo1688DsMember)) {
                shopTo1688DsMember = new ShopTo1688DsMember();
                shopTo1688DsMember.setGmtCreate(new Date());
                shopTo1688DsMemberService.insert(shopTo1688DsMember);
            }
            shopTo1688DsMember.setCnaliMemberId(cnaliMemberId);
            shopTo1688DsMember.setShopId(Long.valueOf(userId));
            shopTo1688DsMember.setGmtExpire(gmtMemberExpireTime);
            shopTo1688DsMember.setGmtModified(new Date());
            shopTo1688DsMemberService.updateByPrimaryKey(shopTo1688DsMember);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

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