package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppBaseUserMapper;
import org.wcs.mapper.intf.AppBaseUserService;
import org.wcs.model.dto.serve.user.QueryUserReq;
import org.wcs.model.po.app.AppBaseUser;
import org.wcs.model.po.app.AppBaseUserGroup;
import org.wcs.model.po.app.JoinUserInfoWithGroupInfo;
import org.wcs.utils.AppBeanUtils;
import org.wcs.utils.AppStringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户表操作类
 */
@Service
@Slf4j
public class AppBaseUserDao extends ServiceImpl<AppBaseUserMapper, AppBaseUser> implements AppBaseUserService {

    /**
     * 查询用户表
     * @param appBaseUser 查询条件
     * @return 结果
     */
    @Override
    public List<AppBaseUser> query(AppBaseUser appBaseUser) {
        if(appBaseUser == null || AppBeanUtils.isAllPropertyNull(appBaseUser)) {
            return null;
        }
        try {
            LambdaQueryWrapper<AppBaseUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(appBaseUser.getUserId() != null, AppBaseUser::getUserId, appBaseUser.getUserId())
                    .eq(appBaseUser.getUserName() != null, AppBaseUser::getUserName, appBaseUser.getUserName())
                    .eq(appBaseUser.getUserPwd() != null, AppBaseUser::getUserPwd, appBaseUser.getUserPwd())
                    .eq(appBaseUser.getUserStatus() != null, AppBaseUser::getUserStatus, appBaseUser.getUserStatus())
                    .eq(appBaseUser.getUserGroup() != null, AppBaseUser::getUserGroup, appBaseUser.getUserGroup())
                    .eq(appBaseUser.getSex() != null, AppBaseUser::getSex, appBaseUser.getSex());
            return super.list(queryWrapper);
        } catch (Exception ex) {
            log.error("查询用户表异常", ex);
            return null;
        }
    }

    /**
     * 根据用户ID查询用户表
     * @param userId 用户ID
     * @return 列表
     */
    @Override
    public List<AppBaseUser> queryByUserId(String userId) {
        if(AppStringUtils.isEmpty(userId)) {
            return null;
        }
        try {
            LambdaQueryWrapper<AppBaseUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AppBaseUser::getUserId, userId);
            return super.list(queryWrapper);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 查询用户带用户组的信息
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public List<JoinUserInfoWithGroupInfo> query(String userId) {
        if(AppStringUtils.isEmpty(userId)) {
            return null;
        }
        try {
            return baseMapper.queryUserInfoAndGroupInfo(userId);
        } catch (Exception ex) {
            log.error("联表用户组表查询用户表异常", ex);
            return null;
        }
    }

    /**
     * 条件查询不超过某一级别的用户
     * @param user 查询条件
     * @param maxUserLevel 最高级别
     * @return 查询结果
     */
    @Override
    public List<AppBaseUser> query(AppBaseUser user, Integer maxUserLevel) {
        if(user == null) {
            return null;
        }
        try {
            LambdaQueryWrapper<AppBaseUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(AppStringUtils.isNotEmpty(user.getUserId()), AppBaseUser::getUserId, user.getUserId())
                    .like(AppStringUtils.isNotEmpty(user.getUserName()), AppBaseUser::getUserName, user.getUserName())
                    .eq(user.getUserStatus() != null, AppBaseUser::getUserStatus, user.getUserStatus())
                    .like(AppStringUtils.isNotEmpty(user.getUserGroup()), AppBaseUser::getUserGroup, user.getUserGroup())
                    .eq(user.getSex() != null, AppBaseUser::getSex, user.getSex())
                    .le(AppBaseUser::getUserLevel, maxUserLevel);
            return super.list(queryWrapper);
        } catch (Exception ex) {
            log.error("查询用户表异常", ex);
            return null;
        }
    }

    /**
     * 更新用户表
     * @param user 更新条件
     * @return 更新结果
     */
    @Override
    public int update(AppBaseUser user) {
        if(user == null) {
            return 0;
        }
        try {
            LambdaUpdateWrapper<AppBaseUser> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(user.getUserId() != null, AppBaseUser::getUserId, user.getUserId())
                    .set(AppStringUtils.isNotEmpty(user.getUserName()), AppBaseUser::getUserName, user.getUserName())
                    .set(AppStringUtils.isNotEmpty(user.getUserPwd()), AppBaseUser::getUserPwd, user.getUserPwd())
                    .set(user.getUserStatus() != null, AppBaseUser::getUserStatus, user.getUserStatus())
                    .set(user.getUserGroup() != null, AppBaseUser::getUserGroup, user.getUserGroup())
                    .set(user.getSex() != null, AppBaseUser::getSex, user.getSex())
                    .set(user.getUserLevel() != null, AppBaseUser::getUserLevel, user.getUserLevel())
                    .set(AppStringUtils.isNotEmpty(user.getPhoto()), AppBaseUser::getPhoto, user.getPhoto())
                    .set(user.getLastLoginTime() != null, AppBaseUser::getLastLoginTime, user.getLastLoginTime())
                    .set(AppBaseUser::getUpdateTime, LocalDateTime.now());
            return super.update(user, updateWrapper) ? 1 : 0;
        } catch (Exception e) {
            log.error("更新用户表异常", e);
            return 0;
        }
    }

    /**
     * 根据用户ID删除用户表
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int deleteWithUserId(String userId) {
        try {
            return super.removeById(userId) ? 1 : 0;
        } catch (Exception e) {
            log.error("删除用户表失败", e);
            return 0;
        }
    }

    @Override
    public int insert(AppBaseUser appBaseUser) {
        if(appBaseUser == null) {
            return 0;
        }
        try {
            appBaseUser.setCreateTime(LocalDateTime.now());
            appBaseUser.setUpdateTime(LocalDateTime.now());
            return super.save(appBaseUser) ? 1 : 0;
        } catch (Exception e) {
            log.error("插入用户表异常", e);
            return 0;
        }
    }
}
