package com.fshop.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fshop.common.PageHelper;
import com.fshop.common.R;
import com.fshop.dto.LoginUserDto;
import com.fshop.entity.Comment;
import com.fshop.entity.Evaluate;
import com.fshop.mapper.EvaluateMapper;
import com.fshop.service.CommentRepository;
import com.fshop.service.IEvaluateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fshop.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* <p>
* 订单评价表 服务实现类
* </p>
*
* @author dev
* @since 2024-04-23
*/
@Service
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate> implements IEvaluateService {
@Autowired
private EvaluateMapper evaluateMapper;
@Autowired
private CommentRepository commentRepository;
@Override
public R<Page<Evaluate>> getAll(String token, Integer pageNum) {
// 解析token
LoginUserDto loginUser = JwtUtil.parseToken(token);
// 查询所有,user_id等于loginUser.getUserId,且status等于1的评论
QueryWrapper<Evaluate> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", loginUser.getUserId()).eq("status", 1);
// 分页,每页显示10条评论
Page<Evaluate> page = new Page<>(pageNum, PageHelper.EVALUATE_PAGE_SIZE);
page = baseMapper.selectPage(page, wrapper);
if (page != null) {
return R.ok("查询成功", page);
}
return R.error("查询失败");
}
@Override
public R<String> removeEvaluate(String token, String evaluateId) {
// 先查询
LoginUserDto loginUser = JwtUtil.parseToken(token);
QueryWrapper<Evaluate> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", loginUser.getUserId()).eq("evaluate_id", evaluateId).eq("status", 1);
Evaluate evaluate = baseMapper.selectOne(wrapper);
// System.out.println(evaluate);
if (evaluate != null) {
evaluate.setStatus(0);
int update = baseMapper.update(evaluate, wrapper);
if (update > 0) {
return R.ok("删除成功");
}
}
return R.error("查询失败");
}
@Override
public R<Evaluate> getById(String token, String evaluateId) {
// 解析token
LoginUserDto loginUser = JwtUtil.parseToken(token);
QueryWrapper<Evaluate> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", loginUser.getUserId()).eq("evaluate_id", evaluateId).eq("status", 1);
Evaluate evaluate = baseMapper.selectOne(wrapper);
if (evaluate != null) {
return R.ok("查询成功", evaluate);
}
return R.error("查询失败");
}
//查询所有评论并返回用户ID、用户名称、用户头像以及用户评论
@Override
public R getEvaluateInfo(Integer fruitId,Integer currentPage, Integer queryCount) {
Integer preCurrentPage = (currentPage - 1) * queryCount;
return R.ok(evaluateMapper.getEvaluateInfo(fruitId,preCurrentPage , queryCount));
}
@Override
public R<String> saveEvaluate(String token, Evaluate evaluate){
LoginUserDto loginUser = JwtUtil.parseToken(token);
Integer tokenUserId = loginUser.getUserId();
Integer userId = evaluate.getUserId();
Comment comment = new Comment();
comment.setId(UUID.randomUUID().toString()); // 生成随机的 `_id`
evaluate.setEvaluateId(comment.getId());
comment.setFruitId(43);//假数据
comment.setScore(evaluate.getEvaluateScore());
comment.setStatus(1);
// 创建一个OriginalPoster对象并设置其字段值
Comment.OriginalPoster originalPoster = new Comment.OriginalPoster();
originalPoster.setUserId(evaluate.getUserId()); // 假设用户ID是123
originalPoster.setContent( new String(evaluate.getEvaluateInfo()));
LocalDateTime now = LocalDateTime.now();
// 对于LocalDateTime,应该直接使用ISO_LOCAL_DATE_TIME
DateTimeFormatter isoLocalDateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
String iso8601String = now.format(isoLocalDateTimeFormatter);
originalPoster.setPostedAt(iso8601String); // 使用ISO 8601格式的日期时间字符串
comment.setOriginalPoster(originalPoster);
comment.setReplies(new ArrayList<>()); // 确保 `replies` 是一个空列表
commentRepository.save(comment);
// var evaluationData = {
// //userId: userId,
// myorderId: myorderId,
// evaluateInfo: evaluateInfo,
// evaluateScore: evaluateScore
// };
if (tokenUserId == null || userId == null) {
// 至少有一个ID是null,因此它们不相等
return R.error("添加失败");
} else if (tokenUserId != null && tokenUserId.equals(userId)) {
// 两个ID相等
evaluate.setStatus(1);
evaluate.setEvaluateCreateTime(LocalDateTime.now());
evaluate.setUpdateTime(LocalDateTime.now());
evaluate.setFruitId(43);
int insert = evaluateMapper.insert(evaluate);
if(insert > 0){
return R.ok("添加成功");
}else{
return R.error("添加失败");
}
} else {
// 两个ID都不为null,但不相等
return R.error("添加失败");
}
}
}