package com.dd.admin.business.author.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.admin.common.model.PageFactory;
import com.dd.admin.business.author.entity.Author;
import com.dd.admin.business.author.mapper.AuthorMapper;
import com.dd.admin.business.author.service.AuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.admin.common.utils.AddressUtils;
import com.dd.admin.common.utils.IPUtils;
import com.dd.admin.common.utils.RandomXiaohongshuAuthorName;
import com.dd.admin.common.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dd.admin.business.author.domain.AuthorVo;
import com.dd.admin.business.author.domain.AuthorDto;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 作者（博主） 服务实现类
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-12-12
 */
@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, Author> implements AuthorService {

    @Autowired
    HttpServletRequest request;

    @Override
    public IPage<AuthorVo> selectAuthorPage(AuthorDto authorDto) {
        Page page = PageFactory.defaultPage();
        return baseMapper.selectAuthorPage(page,authorDto);
    }

    @Override
    public List<AuthorVo> selectAuthorList(AuthorDto authorDto) {
        return baseMapper.selectAuthorList(authorDto);
    }

    @Override
    public Author selectAuthorByPhoneNumber(String phoneNumber) {
        LambdaQueryWrapper<Author> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Author::getPhoneNumber,phoneNumber);
        Author author = baseMapper.selectOne(queryWrapper);
        return author;
    }

    @Override
    public Author createNewAuthor(String phoneNumber) {
        Author author = new Author();
        author.setPhoneNumber(phoneNumber);
        //随机生成昵称
        author.setAuthorName(RandomXiaohongshuAuthorName.generateAuthorName());
        author.setAuthorNo(StringUtil.getNumberForPK() + StringUtil.createCode(2));
        author.setDescription("我是" + author.getAuthorName());
        //生成随机头像因为没有点击上传所有没有头像id
        author.setAvatarUrl("http://8.146.211.120:8080/upload/avatar/avatar ("+ StringUtil.createCode(1) +").jpg");
        author.setBackGroundUrl("http://8.146.211.120:8080/upload/notes/note (6).jpg");
        author.setIpAddress(IPUtils.getIpAddr(request)); // 请求IP
        author.setIpRealAddress(AddressUtils.getRealAddress(author.getIpAddress())); //ip真实地址
        this.save(author);
        return author;
    }

    @Override
    public Long selectAuthorUpAndStarTotalCount(String authorId) {
        return baseMapper.selectAuthorUpAndStarTotalCount(authorId);
    }
}
