package com.njzscloud.supervisory.user.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.security.support.UserDetail;
import com.njzscloud.common.security.util.EncryptUtil;
import com.njzscloud.common.security.util.SecurityUtil;
import com.njzscloud.supervisory.user.mapper.SysUserAccountMapper;
import com.njzscloud.supervisory.user.pojo.AddUserAccountParam;
import com.njzscloud.supervisory.user.pojo.SysUserAccountEntity;
import com.njzscloud.supervisory.user.pojo.UserAccountModifyParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>sys_user_account</p>
 * <p>用户账号信息表</p>
 */
@Slf4j
@Service
public class SysUserAccountService extends ServiceImpl<SysUserAccountMapper, SysUserAccountEntity> implements IService<SysUserAccountEntity> {

    /**
     * 新增
     *
     * @param addUserAccountParam 数据
     * @return boolean 结果; true-->成功、false-->失败
     */
    public void add(AddUserAccountParam addUserAccountParam) {
        SysUserAccountEntity userAccountEntity = BeanUtil.copyProperties(addUserAccountParam, SysUserAccountEntity.class)
                .setSecret(EncryptUtil.encrypt(addUserAccountParam.getSecret()))
                .setRegdate(LocalDateTime.now());
        this.save(userAccountEntity);
    }

    /**
     * 修改
     *
     * @param userAccountModifyParam 数据
     * @return boolean 结果; true-->成功、false-->失败
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean modify(UserAccountModifyParam userAccountModifyParam) {
        SysUserAccountEntity oldData = this.getOne(Wrappers.<SysUserAccountEntity>lambdaQuery().eq(SysUserAccountEntity::getUserId, userAccountModifyParam.getUserId()));
        SysUserAccountEntity entity = BeanUtil.copyProperties(userAccountModifyParam, SysUserAccountEntity.class)
                .setId(oldData.getId())
                .setSecret(EncryptUtil.encrypt(userAccountModifyParam.getSecret()));
        if (userAccountModifyParam.getDisabled() == Boolean.TRUE) {
            Long userId = oldData.getUserId();
            SecurityUtil.removeToken(userId);
        }
        return this.updateById(entity);
    }

    /**
     * 删除
     *
     * @param id Id
     * @return boolean 结果; true-->成功、false-->失败
     */

    public boolean del(Long id) {
        return this.removeById(id);
    }

    /**
     * 详情
     *
     * @param id Id
     * @return SysUserAccountEntity 结果
     */

    public SysUserAccountEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * @param sysUserAccountEntity 筛选条件
     * @param page                 分页参数
     * @return IPage&lt;SysUserAccountEntity&gt; 分页结果
     */

    public IPage<SysUserAccountEntity> paging(IPage<SysUserAccountEntity> page, SysUserAccountEntity sysUserAccountEntity) {
        Wrapper<SysUserAccountEntity> ew = Wrappers.<SysUserAccountEntity>query(sysUserAccountEntity);
        return baseMapper.paging(page, ew);
    }

    public UserDetail getUserInfo(String account) {
        return baseMapper.selectUserByAccount(account);
    }

}
