package com.happy.service.impl;

import com.happy.Model.BookingComment;
import com.happy.Model.FileInfo;
import com.happy.dao.BookingCommentDao;
import com.happy.service.BookingCommentService;
import com.happy.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("BookingCommentService")

public class BookingCommentImplService implements BookingCommentService {

    @Resource
    BookingCommentDao bookingCommentDao;

    @Resource
    FileService fileService;



    /**
     * 添加评论
     *
     * @param pictureList
     * @param bookingComment
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int orderRate( BookingComment bookingComment,List<String> pictureList) {

//        添加评论表并返回主键

        String id = bookingCommentDao.insertBookingComment(bookingComment);
        int i = 1;
        if (pictureList.size() > 0) {
            List<FileInfo> fileInfoList = new ArrayList<>();
            for (String s : pictureList) {
                String[] split = s.split("/");
                String s1 = split[split.length-1];
                FileInfo fileInfo = new FileInfo();
                fileInfo.setLinkId(id);
                fileInfo.setName(s1);
                fileInfo.setUrl(s);

                fileInfoList.add(fileInfo);
            }

            i = fileService.batchInsertFile(fileInfoList);
        }
        if (Long.valueOf(id) > 0 && i > 0) {
            return i;
        }else {
            return 0;
        }

    }


}
