package com.mzl.flower.service.impl.comment; import cn.hutool.core.util.IdUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.mzl.flower.config.exception.ValidationException; import com.mzl.flower.config.security.SecurityUtils; import com.mzl.flower.dto.request.comment.*; import com.mzl.flower.dto.response.comment.FlowerCommentStatisVO; import com.mzl.flower.dto.response.comment.FlowerCommentVO; import com.mzl.flower.entity.FlowerCommentDO; import com.mzl.flower.entity.customer.Customer; import com.mzl.flower.entity.payment.Order; import com.mzl.flower.entity.payment.OrderItem; import com.mzl.flower.entity.supplier.Supplier; import com.mzl.flower.enums.FlowerCommentShowEnum; import com.mzl.flower.enums.TrueOrFalseEnum; import com.mzl.flower.mapper.comment.FlowerCommentMapper; import com.mzl.flower.mapper.comment.FlowerCommentMapperCustom; import com.mzl.flower.mapper.customer.CustomerMapper; import com.mzl.flower.mapper.payment.OrderItemMapper; import com.mzl.flower.mapper.payment.OrderMapper; import com.mzl.flower.mapper.supplier.SupplierMapper; import com.mzl.flower.service.comment.FlowerCommentService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.List; /** *

* 商品评论表 服务实现类 *

* * @author @TaoJie * @since 2024-09-29 */ @Service public class FlowerCommentServiceImpl extends ServiceImpl implements FlowerCommentService { @Autowired private FlowerCommentMapperCustom flowerCommentMapperCustom; @Autowired private OrderItemMapper orderItemMapper; @Autowired private SupplierMapper supplierMapper; @Autowired private CustomerMapper customerMapper; @Autowired private OrderMapper orderMapper; @Transactional @Override public boolean createFlowerComment(CreateFlowerCommentDTO dto) { final FlowerCommentVO byOrderItemId = getByOrderItemId(dto.getOrderItemId()); if(null!=byOrderItemId){ throw new ValidationException("当前商品已经评论过!"); } FlowerCommentDO flowerCommentDO=new FlowerCommentDO(); BeanUtils.copyProperties(dto,flowerCommentDO); flowerCommentDO.setId(IdUtil.simpleUUID()); flowerCommentDO.create(SecurityUtils.getUserId()); final OrderItem orderItem = orderItemMapper.selectById(dto.getOrderItemId()); if(null==orderItem){ throw new ValidationException("当前订单项目不存在!"); } flowerCommentDO.setOrderId(orderItem.getOrderId()); flowerCommentDO.setCustomerUserId(orderItem.getCreateBy()); flowerCommentDO.setSupplierId(orderItem.getSupplierId()); flowerCommentDO.setFlowerId(orderItem.getFlowerId()); flowerCommentDO.setFlowerCover(orderItem.getFlowerCover()); // 补充商户信息 Customer customer = customerMapper.selectOne(new QueryWrapper() .eq("user_id", orderItem.getCreateBy())); if(null!=customer && null!=customer.getId()){ flowerCommentDO.setCustomerId(customer.getId()); } // 补充供应商信息 Supplier supplier = supplierMapper.selectById(orderItem.getSupplierId()); if(null!=supplier && StringUtils.isNotBlank(supplier.getUserId())){ flowerCommentDO.setSupplierUserId(supplier.getUserId()); } // 设置显示状态为显示 flowerCommentDO.setShowFlag(FlowerCommentShowEnum.SHOW.getFlag()); return baseMapper.insert(flowerCommentDO)>0; } @Transactional @Override public boolean updateFlowerComment(UpdateFlowerCommentDTO dto) { FlowerCommentDO flowerCommentDO=baseMapper.selectById(dto.getId()); BeanUtils.copyProperties(dto,flowerCommentDO); flowerCommentDO.update(SecurityUtils.getUserId()); return baseMapper.updateById(flowerCommentDO)>0; } @Override public boolean deleteFlowerComment(String id) { return baseMapper.deleteById(id)>0; } @Override public FlowerCommentVO getDetailById(String id) { QueryFlowerCommentDTO dto=new QueryFlowerCommentDTO(); dto.setId(id); final List list = getList(dto); if(CollectionUtils.isNotEmpty(list)){ return list.get(0); } return null; } @Override public Page getPage(Page page, QueryFlowerCommentDTO dto) { List list=flowerCommentMapperCustom.getPage(page,dto); return page.setRecords(list); } @Override public List getList(QueryFlowerCommentDTO dto) { return flowerCommentMapperCustom.getList(dto); } @Override public FlowerCommentVO getByOrderItemId(String orderItemId) { QueryFlowerCommentDTO dto=new QueryFlowerCommentDTO(); dto.setOrderItemId(orderItemId); final List list = getList(dto); if(CollectionUtils.isNotEmpty(list)){ return list.get(0); } return null; } @Override public BigDecimal getSupplierAvgScore(Long supplierId) { return flowerCommentMapperCustom.getSupplierAvgScore(supplierId); } @Transactional @Override public boolean createFlowerCommentBatch(CreateFlowerCommentBatchDTO dto) { if(CollectionUtils.isNotEmpty(dto.getList())){ dto.getList().forEach(item->{ // 保存评论信息 final FlowerCommentVO byOrderItemId = getByOrderItemId(item.getOrderItemId()); if(null!=byOrderItemId){ // 更新 UpdateFlowerCommentDTO updateFlowerCommentDTO=new UpdateFlowerCommentDTO(); BeanUtils.copyProperties(item,updateFlowerCommentDTO); updateFlowerCommentDTO.setId(byOrderItemId.getId()); updateFlowerComment(updateFlowerCommentDTO); }else{ createFlowerComment(item); } }); // 设置订单的评论状态为评论的下个阶段 Order order=orderMapper.selectById(dto.getOrderId()); return true; } return false; } @Override public FlowerCommentStatisVO getSupplierStatis(Long supplierId) { FlowerCommentStatisVO vo=new FlowerCommentStatisVO(); // 获取平均分 final BigDecimal supplierAvgScore = flowerCommentMapperCustom.getSupplierAvgScore(supplierId); vo.setAvg(supplierAvgScore); //当前商家评论总数 final Integer amount = baseMapper.selectCount(new QueryWrapper().lambda() .eq(FlowerCommentDO::getDeleted, TrueOrFalseEnum.FALSE) .eq(FlowerCommentDO::getShowFlag,FlowerCommentShowEnum.SHOW.getFlag()) .eq(FlowerCommentDO::getSupplierId, supplierId)); vo.setCommentAmount(amount); return vo; } @Override public boolean updateShowFlowerComment(ShowFlowerCommentDTO dto) { FlowerCommentDO flowerCommentDO = baseMapper.selectById(dto.getId()); if (null == flowerCommentDO) { throw new ValidationException("评论不存在"); } flowerCommentDO.setShowFlag(dto.getShowFalg()); flowerCommentDO.setUpdateBy(SecurityUtils.getUserId()); return baseMapper.updateById(flowerCommentDO)>0; } @Override public boolean updateReplayFlowerComment(ReplayFlowerCommentDTO dto) { FlowerCommentDO flowerCommentDO = baseMapper.selectById(dto.getId()); if (null == flowerCommentDO) { throw new ValidationException("评论不存在"); } flowerCommentDO.setReplayBy(dto.getReplayContent()); flowerCommentDO.setReplayTime(LocalDateTime.now()); flowerCommentDO.setUpdateBy(SecurityUtils.getUserId()); return baseMapper.updateById(flowerCommentDO)>0; } }