package com.ruoyi.brewery.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.brewery.domain.WyBreweryRole;
import com.ruoyi.brewery.mapper.WyBreweryRoleMapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.brewery.mapper.WyBreweryUserMapper;
import com.ruoyi.common.core.domain.entity.WyBreweryUser;
import com.ruoyi.brewery.service.IWyBreweryUserService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 品牌方用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-22
 */
@Service
public class WyBreweryUserServiceImpl implements IWyBreweryUserService 
{
    @Resource
    private WyBreweryUserMapper wyBreweryUserMapper;

    @Resource
    private WyBreweryRoleMapper roleMapper;

    /**
     * 查询品牌方用户信息
     * 
     * @param userId 品牌方用户信息主键
     * @return 品牌方用户信息
     */
    @Override
    public WyBreweryUser selectWyBreweryUserByUserId(Long userId)
    {
        return wyBreweryUserMapper.selectWyBreweryUserByUserId(userId);
    }

    @Override
    public WyBreweryUser selectUserByUserName(String userName){
        return wyBreweryUserMapper.selectUserByUserName(userName);
    }

    /**
     * 查询品牌方用户信息列表
     * 
     * @param wyBreweryUser 品牌方用户信息
     * @return 品牌方用户信息
     */
    @Override
    public List<WyBreweryUser> selectWyBreweryUserList(WyBreweryUser wyBreweryUser)
    {
        return wyBreweryUserMapper.selectWyBreweryUserList(wyBreweryUser);
    }

    /**
     * 新增品牌方用户信息
     * 
     * @param wyBreweryUser 品牌方用户信息
     * @return 结果
     */
    @Override
    public int insertWyBreweryUser(WyBreweryUser wyBreweryUser)
    {
        wyBreweryUser.setCreateTime(DateUtils.getNowDate());
        return wyBreweryUserMapper.insertWyBreweryUser(wyBreweryUser);
    }

    /**
     * 修改品牌方用户信息
     * 
     * @param wyBreweryUser 品牌方用户信息
     * @return 结果
     */
    @Override
    public int updateWyBreweryUser(WyBreweryUser wyBreweryUser)
    {
        wyBreweryUser.setUpdateTime(DateUtils.getNowDate());
        return wyBreweryUserMapper.updateWyBreweryUser(wyBreweryUser);
    }

    /**
     * 批量删除品牌方用户信息
     * 
     * @param userIds 需要删除的品牌方用户信息主键
     * @return 结果
     */
    @Override
    public int deleteWyBreweryUserByUserIds(Long[] userIds)
    {
        return wyBreweryUserMapper.deleteWyBreweryUserByUserIds(userIds);
    }

    /**
     * 删除品牌方用户信息信息
     * 
     * @param userId 品牌方用户信息主键
     * @return 结果
     */
    @Override
    public int deleteWyBreweryUserByUserId(Long userId)
    {
        return wyBreweryUserMapper.deleteWyBreweryUserByUserId(userId);
    }

    /**
     * 查询用户所属角色组
     *
     * @param userName 用户名
     * @return 结果
     */
    @Override
    public String selectUserRoleGroup(String userName)
    {
        final List<WyBreweryRole> list = roleMapper.selectRolesByUserName(userName);
        if (CollectionUtils.isEmpty(list))
        {
            return StringUtils.EMPTY;
        }
        return list.stream().map(WyBreweryRole::getRoleName).collect(Collectors.joining(","));
    }

    @Override
    public boolean checkPhoneUnique(WyBreweryUser currentUser) {
        Long userId = StringUtils.isNull(currentUser.getUserId()) ? -1L : currentUser.getUserId();
        WyBreweryUser info = wyBreweryUserMapper.checkPhoneUnique(currentUser.getPhonenumber());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public boolean checkEmailUnique(WyBreweryUser currentUser) {
        Long userId = StringUtils.isNull(currentUser.getUserId()) ? -1L : currentUser.getUserId();
        WyBreweryUser info = wyBreweryUserMapper.checkEmailUnique(currentUser.getEmail());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验用户名称是否唯一
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public boolean checkUserNameUnique(WyBreweryUser user)
    {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        WyBreweryUser info = wyBreweryUserMapper.checkUserNameUnique(user.getUserName());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int updateUserProfile(WyBreweryUser currentUser) {
       return wyBreweryUserMapper.updateUserProfile(currentUser);
    }

    @Override
    public int resetUserPwd(String userName, String newPassword) {
        return wyBreweryUserMapper.resetUserPwd(userName, newPassword);
    }

    @Override
    public boolean updateUserAvatar(String username, String avatar) {
        return wyBreweryUserMapper.updateUserAvatar(username, avatar)>0;
    }
}
