package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.LifeUser;
import shop.alien.entity.store.vo.LifeUserVo;
import shop.alien.mapper.*;
import shop.alien.store.service.PlatformLifeUserService;

@Service
@RequiredArgsConstructor
public class PlatformLifeUserServiceImpl implements PlatformLifeUserService {

    private final PlatformLifeUserMapper platformLifeUserMapper;

    private final LifeUserMapper lifeUserMapper;

    private final LifeUserDynamicsMapper lifeUserDynamicsMapper;

    private final LifeLikeRecordMapper lifeLikeRecordMapper;

    private final StoreCommentMapper storeCommentMapper;

    @Override
    public IPage<LifeUserVo> getUserList(Integer page, Integer size, String name, String phone) {
        QueryWrapper<LifeUserVo> wrapper = new QueryWrapper<>();
        wrapper.eq("user.delete_flag", 0);
        if (StringUtils.isNotEmpty(name)) {
            wrapper.like("user.real_name", name);
        }
        if (StringUtils.isNotEmpty(phone)) {
            wrapper.like("user.user_phone", phone);
        }
        IPage<LifeUserVo> userList = platformLifeUserMapper.getUserList(new Page<>(page, size), wrapper);
        //统计点赞数量
        /*for (LifeUserVo user : userList.getRecords()) {
            //获取两表信息
            //社区动态表
            Integer dynamicsCount = 0;
            List<LifeUserDynamics> lifeUserDynamicsList =
                    lifeUserDynamicsMapper.selectList(
                            new LambdaQueryWrapper<LifeUserDynamics>()
                                    .eq(LifeUserDynamics::getPhoneId, "user_" + user.getUserPhone()));
            for (LifeUserDynamics lifeUserDynamics : lifeUserDynamicsList) {
                dynamicsCount += lifeLikeRecordMapper.selectCount(new LambdaQueryWrapper<LifeLikeRecord>().eq(LifeLikeRecord::getHuifuId, lifeUserDynamics.getId()).eq(LifeLikeRecord::getType, 2));
            }
            //评论表
            Integer commentCount = 0;
            List<StoreComment> storeComments =
                    storeCommentMapper.selectList(new LambdaQueryWrapper<StoreComment>().eq(StoreComment::getUserId, user.getId()));
            for (StoreComment storeComment : storeComments) {
                commentCount += lifeLikeRecordMapper.selectCount(new LambdaQueryWrapper<LifeLikeRecord>().eq(LifeLikeRecord::getHuifuId, storeComment.getId()).eq(LifeLikeRecord::getType, 1));
            }
            user.setLikesNumber(commentCount + dynamicsCount);
        }*/
        return userList;
    }

    @Override
    public LifeUser getUserById(Integer id) {
        LifeUser lifeUser = lifeUserMapper.selectById(id);
        return lifeUser;
    }
}
