package com.citygis.web.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.citygis.common.core.domain.AjaxResult;
import com.citygis.web.domain.TabUser;
import com.citygis.web.mapper.TabUserMapper;
import com.citygis.web.service.ITabUserService;
import com.citygis.web.utils.AesUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 登录用户 服务实现类
 * </p>
 *
 * @author citygis
 * @since 2025-01-13
 */
@Service
public class TabUserServiceImpl extends ServiceImpl<TabUserMapper, TabUser> implements ITabUserService {

    @Override
    @Transactional
    public AjaxResult insertTabUser(TabUser tabUser) {
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        long id = snowflake.nextId();
        tabUser.setId(id);

        LambdaQueryWrapper<TabUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TabUser::getUserId, tabUser.getUserId());
        TabUser one = this.getOne(queryWrapper);

        if (one != null) {
            String s = AesUtil.aesEncrypt(tabUser.getPassword());

            tabUser.setPassword(s);
            this.update(tabUser, queryWrapper);
        } else {
            String s = AesUtil.aesEncrypt(tabUser.getPassword());

            tabUser.setPassword(s);
            this.save(tabUser);
        }

        return AjaxResult.success("添加成功");
    }
}
