package com.jy.system.dao;

import cn.dev33.satoken.secure.BCrypt;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.framework.model.base.BaseDao;
import com.jy.framework.model.constants.CommonConstant;
import com.jy.system.mapper.SysUserMapper;
import com.jy.system.model.dto.SysUserSelectDto;
import com.jy.system.model.entity.SysUser;
import com.jy.system.model.table.SysDeptTable;
import com.jy.system.model.table.SysUserTable;
import com.jy.system.model.vo.SysUserInfoVo;
import com.jy.system.model.vo.SysUserVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SysUserDao extends BaseDao<SysUserMapper, SysUser> {

    /**
     * 用户列表
     */
    public List<SysUserInfoVo> getList(SysUserSelectDto dto) {
        SysUserTable su = SysUserTable.su;

        return sql(SysUserInfoVo.class)
                .select(
                        su.id,
                        su.username,
                        su.nickname
                )
                .from(su)
                .where(
                        su.id.ne(CommonConstant.SUPER_ADMIN_USER_ID),
                        su.status.eq(dto.getStatus())
                )
                .orderBy(
                        su.builtin.desc(),
                        su.id.desc()
                )
                .list();
    }

    /**
     * 用户分页
     */
    public Page<SysUserVo> getPage(SysUserSelectDto dto) {
        SysUserTable su = SysUserTable.su;
        SysDeptTable sd = SysDeptTable.sd;

        return sql(SysUserVo.class)
                .select(
                        su.id,
                        su.username,
                        su.nickname,
                        su.deptId,
                        su.phoneNumber,
                        su.email,
                        su.builtin,
                        su.status,
                        sd.name.as(SysUserVo::getDeptName)
                )
                .from(su)
                .leftJoin(sd).on(su.deptId.eq(sd.id))
                .where(
                        su.id.ne(CommonConstant.SUPER_ADMIN_USER_ID),
                        su.id.in(dto.getInUserIdList()),
                        su.id.notIn(dto.getNotInUserIdList()),
                        su.deptId.eq(dto.getDeptId()),
                        su.builtin.eq(dto.getBuiltin()),
                        su.status.eq(dto.getStatus()),
                        su.username.like(dto.getUsername()),
                        su.nickname.like(dto.getNickname())
                )
                .orderBy(
                        su.builtin.desc(),
                        su.id.desc()
                )
                .page(dto.getPage());
    }

    /**
     * 用户明细
     */
    public SysUserVo getDetail(Long id) {
        SysUserTable su = SysUserTable.su;

        return sql(SysUserVo.class)
                .select(
                        su.id,
                        su.nickname,
                        su.deptId,
                        su.phoneNumber,
                        su.email,
                        su.sex,
                        su.builtin,
                        su.status,
                        su.remark
                )
                .from(su)
                .where(su.id.eq(id))
                .one();
    }

    /**
     * 用户名是否存在
     */
    public boolean usernameExist(String username) {
        return lambdaQuery().eq(SysUser::getUsername, username).exists();
    }

    /**
     * 修改密码
     */
    public void updatePassword(Long userId, String password) {
        SysUser sysUser = new SysUser();
        sysUser.setId(userId);
        sysUser.setPassword(BCrypt.hashpw(password));
        updateById(sysUser);
    }

    /**
     * 更新登录信息
     */
    public void updateLoginInfo(Long userId, String loginIp, Date loginTime) {
        lambdaUpdate()
                .eq(SysUser::getId, userId)
                .set(SysUser::getLoginIp, loginIp)
                .set(SysUser::getLoginTime, loginTime)
                .update();
    }

    /**
     * 根据用户名查询用户
     */
    public SysUser getByUsername(String username) {
        return lambdaQuery().eq(SysUser::getUsername, username).one();
    }

    /**
     * 根据部门id查询用户id
     */
    public Set<Long> getUserIdSetByDeptId(Collection<Long> deptIds) {
        return lambdaQuery().in(SysUser::getDeptId, deptIds).list()
                .stream().map(SysUser::getId).collect(Collectors.toSet());
    }

    /**
     * 判断部门是否存在
     */
    public boolean deptExists(Long deptId) {
        return lambdaQuery().eq(SysUser::getDeptId, deptId).exists();
    }

}
