package com.yingpai.miniapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yingpai.miniapp.domain.MiniappUser;
import com.yingpai.miniapp.domain.UserFeedback;
import com.yingpai.miniapp.domain.bo.UserFeedbackBo;
import com.yingpai.miniapp.domain.vo.UserFeedbackVo;
import com.yingpai.miniapp.mapper.MiniappUserMapper;
import com.yingpai.miniapp.mapper.UserFeedbackMapper;
import com.yingpai.miniapp.service.IUserFeedbackService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

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

/**
 * 用户反馈Service实现
 */
@Service
@RequiredArgsConstructor
public class UserFeedbackServiceImpl implements IUserFeedbackService {

    private final UserFeedbackMapper feedbackMapper;
    private final MiniappUserMapper userMapper;

    @Override
    public TableDataInfo<UserFeedbackVo> queryPageList(UserFeedbackBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserFeedback> wrapper = buildQueryWrapper(bo);
        Page<UserFeedback> page = feedbackMapper.selectPage(pageQuery.build(), wrapper);

        List<UserFeedbackVo> voList = page.getRecords().stream()
            .map(this::toVo)
            .collect(Collectors.toList());

        Page<UserFeedbackVo> voPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        voPage.setRecords(voList);
        return TableDataInfo.build(voPage);
    }

    @Override
    public List<UserFeedbackVo> queryList(UserFeedbackBo bo) {
        LambdaQueryWrapper<UserFeedback> wrapper = buildQueryWrapper(bo);
        return feedbackMapper.selectList(wrapper).stream()
            .map(this::toVo)
            .collect(Collectors.toList());
    }

    @Override
    public UserFeedbackVo queryById(Long id) {
        UserFeedback feedback = feedbackMapper.selectById(id);
        return feedback != null ? toVo(feedback) : null;
    }

    @Override
    public Boolean deleteById(Long id) {
        return feedbackMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean deleteByIds(List<Long> ids) {
        return feedbackMapper.deleteBatchIds(ids) > 0;
    }

    private LambdaQueryWrapper<UserFeedback> buildQueryWrapper(UserFeedbackBo bo) {
        LambdaQueryWrapper<UserFeedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(bo.getUserId() != null, UserFeedback::getUserId, bo.getUserId());
        wrapper.like(StringUtils.isNotBlank(bo.getContent()), UserFeedback::getContent, bo.getContent());
        wrapper.orderByDesc(UserFeedback::getCreateTime);

        // 如果有昵称或手机号查询，需要先查用户ID
        if (StringUtils.isNotBlank(bo.getNickname()) || StringUtils.isNotBlank(bo.getPhone())) {
            List<Long> userIds = userMapper.selectList(new LambdaQueryWrapper<MiniappUser>()
                    .like(StringUtils.isNotBlank(bo.getNickname()), MiniappUser::getNickname, bo.getNickname())
                    .like(StringUtils.isNotBlank(bo.getPhone()), MiniappUser::getPhone, bo.getPhone()))
                .stream()
                .map(MiniappUser::getId)
                .collect(Collectors.toList());
            if (userIds.isEmpty()) {
                wrapper.eq(UserFeedback::getUserId, -1L); // 无匹配用户
            } else {
                wrapper.in(UserFeedback::getUserId, userIds);
            }
        }

        return wrapper;
    }

    private UserFeedbackVo toVo(UserFeedback feedback) {
        UserFeedbackVo vo = new UserFeedbackVo();
        vo.setId(feedback.getId());
        vo.setUserId(feedback.getUserId());
        vo.setContent(feedback.getContent());
        vo.setImages(feedback.getImages());
        vo.setCreateTime(feedback.getCreateTime());

        // 查询用户信息
        var user = userMapper.selectById(feedback.getUserId());
        if (user != null) {
            vo.setNickname(user.getNickname());
            vo.setPhone(user.getPhone());
        }

        return vo;
    }
}
