package com.keyware.shandan.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keyware.shandan.common.entity.Result;
import com.keyware.shandan.common.service.BaseServiceImpl;
import com.keyware.shandan.common.util.MD5Util;
import com.keyware.shandan.common.util.StringUtils;
import com.keyware.shandan.common.util.UUIDUtil;
import com.keyware.shandan.system.entity.OauthClientDetails;
import com.keyware.shandan.system.mapper.OauthClientDetailsMapper;
import com.keyware.shandan.system.service.OauthClientDetailsService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * OAUTH_CLIENT_DETAILS
 * </p>
 *
 * @author fengzhi
 * @since 2021-12-10
 */
@Service
public class OauthClientDetailsImpl extends BaseServiceImpl<OauthClientDetailsMapper, OauthClientDetails, Object>
        implements OauthClientDetailsService {

    @Override
    public Result<OauthClientDetails> updateOrSave(OauthClientDetails entity) throws Exception {
        if(!StringUtils.hasText(entity.getId())){
            entity.setId(UUIDUtil.getUUID());
        }
        OauthClientDetails old = getById(entity.getId());
        if (old == null || !old.getClientSecret().equals(entity.getClientSecret())) {
            if (StringUtils.isNotBlank(entity.getClientSecret())) {
                entity.setClientSecret(MD5Util.getMD5(entity.getClientSecret()));
            }
        }
        return super.updateOrSave(entity);
    }

    @Override
    public List<OauthClientDetails> listByUserSync() {
        QueryWrapper<OauthClientDetails> query = new QueryWrapper<>();
        query.select("CLIENT_ID", "TITLE", "USER_SYNC", "INSERT_URI", "UPDATE_URI", "DELETE_URI", "VERIFY_URI");
        query.eq("USER_SYNC", true);
        return list(query);
    }
}
