package com.happy.service.impl;

import com.happy.Model.*;
import com.happy.dao.BookingCommentDao;
import com.happy.dao.FileInfoDao;
import com.happy.dto.BookCommentDto;
import com.happy.dto.IPage;
import com.happy.service.*;
import com.happy.vo.*;
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
    BookService bookService;

    @Resource
    HouseService houseService;

    @Resource
    public FileInfoDao fileDao;
    @Resource
    FileService fileService;

    @Resource
    WorkflowService workflowImplService;

    /**
     * 添加评论
     *
     * @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 != null && 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);
        }
        Workflow workflow = new Workflow();
        workflow.setLinkId(id);
        workflow.setType(2);
        workflow.setRemark(bookingComment.getContent());
        workflow.setStatus(1);
        workflow.setCreateId(bookingComment.getCreateId() + "");
        workflow.setCreateDate(bookingComment.getCreateDate());
        int insert = workflowImplService.insert(workflow);

        if (Long.valueOf(id) > 0 && i > 0 && insert > 0) {
            return i;
        } else {
            return 0;
        }

    }

    @Override
    public IPage evaluatePage(int status, String hotelId, int page, int rows) {
        IPage<BookingCommentPageVo> iPage = new IPage();
        List<BookingCommentPageVo> vos = null;
        int total = 0;
//      全部
        if (status == 0) {
            vos = bookingCommentDao.evaluatePage(hotelId, page, rows);
            total = bookingCommentDao.evaluateTotal(hotelId);
        } else if (status == 1) {
//            带图
            vos = bookingCommentDao.evaluatePagepicture(hotelId, page, rows);
            total = bookingCommentDao.evaluateTotalpicture(hotelId);
        } else if (status == 2) {
            vos = bookingCommentDao.evaluatePageComment(hotelId, page, rows);
            total = bookingCommentDao.evaluateTotalComment(hotelId);
        }

        if (vos!=null&&vos.size()>0) {
            for (BookingCommentPageVo vo : vos) {
                if (vo.getUrl()!=null) {
                    List<String> dateTimeList = vo.getUrl();
                    String s = dateTimeList.get(0);
                    String[] split = s.split(",");
                    ArrayList<String> strings = new ArrayList<>();
                    for (int i = 0; i < split.length; i++) {
                        strings.add(split[i]);
                    }
                    vo.setUrl(strings);
                }
            }
        }

        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        iPage.setPageList(vos);
        return iPage;

    }


    @Override
    public EvaluatePageVo evaluateScore(String hotelId) {
        EvaluatePageVo vo=bookingCommentDao.evaluateScore(hotelId);
        int totalCount = bookingCommentDao.evaluateTotal(hotelId);
        int pictureTotalCount = bookingCommentDao.evaluateTotalpicture(hotelId);
        int commentTotalCount = bookingCommentDao.evaluateTotalComment(hotelId);
        vo.setTotalCount(totalCount);
        vo.setPictureCount(pictureTotalCount);
        vo.setCommentCount(commentTotalCount);

        return vo;
    }

    @Override
    public List<CommentVo> commentVoList(String bookingCommentId) {
        return bookingCommentDao.commentVoList(bookingCommentId);
    }

    @Override
    public int replyComment(BookingComment bookingComment) {
//
        String id = bookingCommentDao.insertBookingComment(bookingComment);

        Workflow workflow = new Workflow();
        workflow.setLinkId(id);
        workflow.setType(2);
        workflow.setRemark(bookingComment.getContent());
        workflow.setStatus(1);
        workflow.setCreateId(bookingComment.getCreateId() + "");
        workflow.setCreateDate(bookingComment.getCreateDate());
        int insert = workflowImplService.insert(workflow);

        if (Long.valueOf(id) > 0 && insert > 0) {
            return 1;
        } else {
            return 0;
        }

    }

    @Override
    public IPage<BookingComment> queryPage(String sqlx, String sql, int page, int rows) {
        IPage<BookingComment> iPage = new IPage();
        List<BookingComment> hotelCouponList = bookingCommentDao.queryPage(sqlx, page, rows);
        int total = bookingCommentDao.queryTotal(sql);
        iPage.setPageList(hotelCouponList);
        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public BookCommentDto getByBookingId(Integer id) {
        Booking book = bookService.getById(id);
        House house = houseService.getById(book.getHouseId());
        BookCommentDto bookCommentDto = new BookCommentDto();
        bookCommentDto.setBook(book);
        bookCommentDto.setHouse(house);
        return bookCommentDto;
    }

    @Override
    public int insterCommpent(BookingComment bookingComment) {
        BookingComment comment = new BookingComment();
        comment.setId(bookingComment.getCommentId());
        comment.setCommentStatus("2");
        int m = bookingCommentDao.updateCommpentStatus(comment);
        int i = bookingCommentDao.insterCommpent(bookingComment);
        if (i > 0 && m > 0) {
            return i;
        } else {
            return 0;
        }
    }

    @Override
    public BookingComment queryById(String id) {
        return bookingCommentDao.queryById(id);
    }

    @Override
    public int update(BookingComment bookingComment) {
        return bookingCommentDao.update(bookingComment);
    }

    @Override
    public CommentDetailsVo commentDetails(String bookingId, Integer status) {
        CommentDetailsVo vo = bookingCommentDao.commentDetails(bookingId, status);
        if (vo != null && vo.getUrl() != null) {
            List<String> dateTimeList = vo.getUrl();
            String s = dateTimeList.get(0);
            String[] split = s.split(",");
            ArrayList<String> strings = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                strings.add(split[i]);
            }
            vo.setUrl(strings);
        }

        return vo;
    }

    @Override
    public IPage<PersonageCommentVo> personageComment(int status, String usersId, int page, int rows) {
        IPage<PersonageCommentVo> iPage = new IPage();
        List<PersonageCommentVo> vos = null;
        int total = 0;
//      待评价
        if (status == 0) {
            vos = bookingCommentDao.personageNotCommentPage(usersId, page, rows);
            total = bookingCommentDao.personageNotCommentTotal(usersId);
        } else if (status == 1) {
//            已评价
            vos = bookingCommentDao.personageCommentPage(usersId, page, rows);
            total = bookingCommentDao.personageCommentTotal(usersId);
        } else if (status == 2) {
//            待审核
            vos = bookingCommentDao.auditPageComment(usersId, page, rows);
            total = bookingCommentDao.auditTotalComment(usersId);
        }else if (status == 3) {
//            审批拒绝
            vos = bookingCommentDao.refuseAuditPageComment(usersId, page, rows);
            total = bookingCommentDao.refuseAuditTotalComment(usersId);
        }
        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        iPage.setPageList(vos);
        return iPage;
    }


    @Override
    public PersonageDetailsVo personageDetails(String bookingCommentId) {
        PersonageDetailsVo vo = bookingCommentDao.personageDetails(bookingCommentId);
        if (vo!=null) {
            List<String> url = vo.getUrl();
            List<String> houseUrl = vo.getHouseUrl();
            if (url != null && url.size() > 0) {
                String s = url.get(0);
                String[] split = s.split(",");
                ArrayList<String> strings = new ArrayList<>();
                for (int i = 0; i < split.length; i++) {
                    strings.add(split[i]);
                }
                vo.setUrl(strings);
            }

            if (url != houseUrl && houseUrl.size() > 0) {
                String s = houseUrl.get(0);
                String[] split = s.split(",");
                ArrayList<String> strings = new ArrayList<>();
                for (int i = 0; i < split.length; i++) {
                    strings.add(split[i]);
                }
                vo.setHouseUrl(strings);
            }
        }

        return vo;
    }


    @Override
    public PersonageCommentCountVo personageCommentCount(String usersId) {
        PersonageCommentCountVo vo = new PersonageCommentCountVo();
        //带评价数量
        Integer waiting = bookingCommentDao.personageNotCommentTotal(usersId);
        vo.setWaitingCount(waiting);
//        已评价
        Integer rated = bookingCommentDao.personageCommentTotal(usersId);
        vo.setRatedCount(rated);
//      待审核
       Integer audit = bookingCommentDao.auditTotalComment(usersId);
        vo.setAuditCount(audit);
//            审批拒绝
        Integer refuseAudit = bookingCommentDao.refuseAuditTotalComment(usersId);
        vo.setRefuseAuditCount(refuseAudit);
        return vo;
    }

    @Override
    public BookingComment getBycommentId(String commentId) {
        return bookingCommentDao.getBycommentId(commentId);
    }


    @Override
    public BookCommentDto getById(String bookId) {
        List<BookingComment> list = bookingCommentDao.getByBookId(bookId);
        if (list == null) return null;
        BookCommentDto bookCommentDto = new BookCommentDto();
        bookCommentDto.setBook(bookService.getById(Integer.parseInt(bookId)));
        BookingComment comment = list.stream().findFirst().orElse(null);
        if (comment != null) {
            List<FileInfo> fileInfos = fileDao.queryList("and link_id ='" + comment.getId() + "'");
            bookCommentDto.setFileInfos(fileInfos);
        }
        for (BookingComment bookingComment : list) {
            List<BookingComment> listByParentId = bookingCommentDao.getByParentId(bookingComment.getId());
            List<BookingComment> bookingCommentList = new ArrayList<>();
            if (listByParentId!=null&&listByParentId.size()>0) {
                for (BookingComment parentComment : listByParentId) {
                    bookingCommentList.add(getComment(parentComment));
                }
            }
            bookingComment.setLowCommentList(bookingCommentList);
        }
        bookCommentDto.setBookingCommentList(list);
        return bookCommentDto;
    }

    public BookingComment getComment(BookingComment parentComment) {
        if (parentComment != null) {
            List<BookingComment> parentCommentList = bookingCommentDao.getByCommentId(parentComment.getId());
            List<BookingComment> list = new ArrayList<>();
            if (parentComment != null && parentCommentList != null) {
                for (BookingComment low : parentCommentList) {//一级评论
                    list.add(low);
                    getLowcomment(low, list);
                }
                parentComment.setLowCommentList(list);
            }
        }
        return parentComment;
    }

//    private void getLowcomment(BookingComment comment){
//        List<BookingComment> list = new ArrayList<>();
//        if (comment.getLowCommentList()!=null) {
//            list = comment.getLowCommentList();
//        }
//        List<BookingComment> low = bookingCommentDao.getByCommentId(comment.getId());
//        if(low != null){
//            list.addAll(low);
//            for(BookingComment low1 : list){
//                getLowcomment(low1,comment);
//            }
//        }
//    }

    private void getLowcomment(BookingComment comment, List<BookingComment> parentCommentList) {
        List<BookingComment> list = new ArrayList<>();
        List<BookingComment> low = bookingCommentDao.getByCommentId(comment.getId());
        if (low != null) {
            for (BookingComment low1 : low) {
                parentCommentList.add(low1);
                getLowcomment(low1, parentCommentList);
            }
        }
    }
}
