package com.sptg.iot.biz.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.iot.api.beans.ApiResultBean;
import com.sptg.iot.api.entity.IotAuthentication;
import com.sptg.iot.api.entity.IotSysConf;
import com.sptg.iot.api.util.IotHttpUtil;
import com.sptg.iot.biz.mapper.IotAuthenticationMapper;
import com.sptg.iot.biz.service.IotApiLogServiceInterface;
import com.sptg.iot.biz.service.IotAuthServiceInterface;
import com.sptg.iot.biz.service.IotSysConfServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * @author zwx
 * @create 2020/10/27 17:06
 */
@Service
@RequiredArgsConstructor
public class IotAuthServiceImpl extends ServiceImpl<IotAuthenticationMapper, IotAuthentication> implements IotAuthServiceInterface {

    private final IotSysConfServiceInterface iotSysConfServiceInterface;
    private final IotApiLogServiceInterface iotApiLogServiceInterface;

    @Override
    public ResultBean<IotAuthentication> auth(IotSysConf iotSysConf) {
        String appId = iotSysConf.getAppId();
        String appSecret = iotSysConf.getAppSecret();
        Map<String, Object> bizParamsMap = new HashMap<>();
        bizParamsMap.put("appId", appId);
        bizParamsMap.put("appSecret", appSecret);
        IotHttpUtil httpUtil = new IotHttpUtil(null);
        ApiResultBean<IotAuthentication> apiResultBean = httpUtil.auth(bizParamsMap);
        Map<String, Object> status = apiResultBean.getStatus();
        if(status.get("code").equals(OperationConstant.ZERO)){
            String reqData = JsonUtils.objectToJson(bizParamsMap);
            this.iotApiLogServiceInterface.recordIotApiLog("/auth", null, appId, reqData, apiResultBean);
            IotAuthentication target = this.findByAppId(appId);
            IotAuthentication source = apiResultBean.getData();
            if(target == null){
                target = source;
                target.setExpireDate(DateUtil.parseDate(source.getExpireTime()));
                target.setAppId(appId);
            }else{
                target.setAccessToken(source.getAccessToken());
                target.setRefreshToken(source.getRefreshToken());
            }
            this.updateById(source);
            return new ResultBean<>(ResultEnum.SUCCESS, source);
        }
        return new ResultBean<>(ResultEnum.FAILURE, null);
    }

    @Override
    public IotAuthentication findByAppId(String appId) {
        Wrapper<IotAuthentication> wrapper = new EntityWrapper<>();
        wrapper.eq("s_app_id", appId);
        return this.selectOne(wrapper);
    }
}
