package com.ssj.service.weixin.library.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.kmt.kmt.domain.KmtExerciseBookCourse;
import com.ssj.bean.weixin.libmy.domain.LibVipBook;
import com.ssj.dao.weixin.library.dao.LibVipBookDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.security.exception.CommonException;
import com.ssj.service.conch.parents.v1.dto.BookDTO;
import com.ssj.service.kmt.service.KmtExerciseBookCourseService;
import com.ssj.service.weixin.library.service.LibVipBookService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author yrf
 * @date 2019/4/15 16:29
 */
@Service(version="1.0")
public class LibVipBookServiceImpl extends BaseServiceImpl<LibVipBook, String> implements LibVipBookService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LibVipBookDao libVipBookDao;
    @Autowired
    private KmtExerciseBookCourseService kmtExerciseBookCourseService;

    @Override
    public PagingAndSortingRepository<LibVipBook, String> getDao() {
        return libVipBookDao;
    }

    @Override
    public Integer countByVipId(String vipId) {
        return libVipBookDao.countByVipId(vipId);
    }

    @Override
    public void addBook(String vipId, List<BookDTO> bookList) throws CommonException {
        List<LibVipBook> libVipBookList = new ArrayList<>();
        bookList.forEach(bookDTO -> {
            String bookId = bookDTO.getBookId();
            String bookName = bookDTO.getBookName();
            String subject = bookDTO.getSubject();

            //排除传入的重复教辅数据
            boolean isExist = libVipBookList.stream().anyMatch(it ->
                    ((StringUtils.isNotBlank(bookId) && bookId.equals(it.getBookId())) ||
                            (StringUtils.isBlank(bookId) &&
                                    Objects.equals(it.getBookName(), bookName) &&
                                    Objects.equals(it.getSubject(), subject))));
            if (isExist) {
                return;
            }

            KmtExerciseBookCourse kmtExerciseBookCourse = null;
            if (StringUtils.isNotBlank(bookId)) {
                //教辅id不为空表示家长是从系统提供的教辅中选择的
                List<KmtExerciseBookCourse> kmtExerciseBookCourseList = kmtExerciseBookCourseService.findByBookId(bookId);
                if (CollectionUtils.isEmpty(kmtExerciseBookCourseList)) {
                    logger.info("添加孩子绑定教辅失败，bookList={}",  bookList);
                    throw new CommonException("教辅选择错误");
                }
                kmtExerciseBookCourse = kmtExerciseBookCourseList.get(0);
            }

            LibVipBook libVipBook = new LibVipBook();
            libVipBook.setVipId(vipId);
            if (kmtExerciseBookCourse != null) {
                libVipBook.setBookId(bookId);
                libVipBook.setBookName(kmtExerciseBookCourse.getBookName());
                libVipBook.setSubject(kmtExerciseBookCourse.getCourseName());
                libVipBook.setVersion(kmtExerciseBookCourse.getVersion());
            } else {
                //家长手动输入教辅情况下，教辅名称和科目不能为空
                if (StringUtils.isBlank(bookName)) {
                    logger.info("添加孩子绑定教辅失败，bookList={}",  bookList);
                    throw new CommonException("教辅名称不能为空");
                }
                if (StringUtils.isBlank(subject)) {
                    logger.info("添加孩子绑定教辅失败，bookList={}",  bookList);
                    throw new CommonException("教辅科目不能为空");
                }

                libVipBook.setBookName(bookName);
                libVipBook.setSubject(subject);
            }
            libVipBook.setCreateTime(new Date());
            libVipBookList.add(libVipBook);
        });
        this.save(libVipBookList);
    }

    @Override
    public void deleteByVipId(String vipId) {
        libVipBookDao.deleteByVipId(vipId);
    }

    @Override
    public List<LibVipBook> findLibVipBookByVipId(String vipId, String subject) {
        return libVipBookDao.findVipBookByVipIdAndSubject(vipId, subject);
    }
}
