package cn.sikey.mcdisk.dal.mysql;

import cn.sikey.mcdisk.dal.dataobject.AuthorizeDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 设备授权
 *
 * @author nelson
 */
@Mapper
public interface AuthorizeMapper extends BaseMapper<AuthorizeDO> {

    default boolean queryIsAuthorize(String deviceId) {
        QueryWrapper<AuthorizeDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id", deviceId);
        AuthorizeDO authorizeDO = selectOne(queryWrapper);
        return Objects.nonNull(authorizeDO);
    }

    default AuthorizeDO queryAuthorize(String deviceId) {
        QueryWrapper<AuthorizeDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id", deviceId);
        return selectOne(queryWrapper);
    }

    default void updateAuthorizeByDeviceId(AuthorizeDO authorizeDO) {
        UpdateWrapper<AuthorizeDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("device_id", authorizeDO.getDeviceId());
        updateWrapper.set("authorize_status", authorizeDO.getAuthorizeStatus());
        updateWrapper.set("cloud_id", authorizeDO.getCloudId());
        updateWrapper.set("photo_id", authorizeDO.getPhotoId());
        updateWrapper.set("access_token", authorizeDO.getAccessToken());
        updateWrapper.set("update_time", authorizeDO.getUpdateTime());
        updateWrapper.set("updater", authorizeDO.getDeviceId());
        update(updateWrapper);
    }

    default void updateAuthorizeTokenByDeviceId(AuthorizeDO authorizeDO) {
        UpdateWrapper<AuthorizeDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("device_id", authorizeDO.getDeviceId());
        updateWrapper.set("access_token", authorizeDO.getAccessToken());
        updateWrapper.set("expire_time", authorizeDO.getExpireTime());
        updateWrapper.set("authorize_status", authorizeDO.getAuthorizeStatus());
        updateWrapper.set("update_time", authorizeDO.getUpdateTime());
        updateWrapper.set("updater", authorizeDO.getDeviceId());
        update(updateWrapper);
    }

    default void updateAuthorizeStatusByDeviceId(AuthorizeDO authorizeDO) {
        UpdateWrapper<AuthorizeDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("device_id", authorizeDO.getDeviceId());
        updateWrapper.set("authorize_status", authorizeDO.getAuthorizeStatus());
        updateWrapper.set("update_time", authorizeDO.getUpdateTime());
        updateWrapper.set("updater", authorizeDO.getDeviceId());
        update(updateWrapper);
    }

    default void updateAuthorizeAccessTokenByDeviceId(String deviceId) {
        UpdateWrapper<AuthorizeDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("device_id", deviceId);
        updateWrapper.set("access_token", null);
        updateWrapper.set("update_time", LocalDateTime.now());
        updateWrapper.set("updater", deviceId);
        update(updateWrapper);
    }

}


