package com.ylx.massage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.common.core.domain.model.WxLoginUser;
import com.ylx.common.exception.ServiceException;
import com.ylx.common.utils.StringUtils;
import com.ylx.massage.domain.TComment;
import com.ylx.massage.domain.TOrder;
import com.ylx.massage.enums.OrderStatusEnum;
import com.ylx.massage.mapper.TCommentMapper;
import com.ylx.massage.service.TCommentService;
import com.ylx.massage.service.TOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;


/**
 * 评论表 服务实现类
 */
@Service
public class TCommentServiceImpl extends ServiceImpl<TCommentMapper, TComment> implements TCommentService {

    @Resource
    private TOrderService orderService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveComment(TComment comment, WxLoginUser wxLoginUser) {

        if(StringUtils.isBlank(comment.getOrderNo())){
            throw new ServiceException("订单号不能为空");
        }
        if(StringUtils.isBlank(comment.getOrderId())){
            throw new ServiceException("订单id不能为空");
        }
        if(StringUtils.isBlank(comment.getcJsid())){
            throw new ServiceException("技师id不能空");
        }
        if(StringUtils.isBlank(comment.getcText())){
            throw new ServiceException("评论内容不能空");
        }
        if(StringUtils.isBlank(comment.getName())){
            throw new ServiceException("技师姓名不能空");
        }
        comment.setcIcon(wxLoginUser.getCIcon());
        comment.setcNickName(wxLoginUser.getCNickName());
        comment.setcOpenId(wxLoginUser.getCOpenid());
        comment.setdTime(LocalDateTime.now());
        this.save(comment);
        TOrder order = new TOrder();
        //修改订单状态（已完成以评论）
        order.setnStatus(OrderStatusEnum.COMPLETE.getCode());
        order.setcId(comment.getOrderId());
        return orderService.updateById(order);
    }
}
