package com.wms.service.serviceImplements;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wms.entity.app.dto.UserDto;
import com.wms.entity.table.User;
import com.wms.mapper.UserMapper;
import com.wms.service.UserService;
import com.wms.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserServiceImplements extends ServiceImpl<UserMapper, User> implements UserService {
    private final UserMapper userMapper;

    @Override
    public UserDto validateUser(User user, boolean withAuth) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>()
                .eq(User::getLoginAccount, user.getLoginAccount());
        if (withAuth) {
            queryWrapper.and(userLambdaQueryWrapper ->
                    userLambdaQueryWrapper.eq(User::getLoginPassword, user.getLoginPassword())
                            .or().eq(StringUtils.isEmpty(user.getLoginPassword()), User::getLoginPassword, null));
        }
        return BeanUtil.copyProperties(userMapper.selectOne(queryWrapper), UserDto.class);
    }
}
