package com.xhkjedu.sstudy.service.subjectbook;

import com.xhkjedu.sstudy.mapper.subjectbook.SubjectBookMapper;
import com.xhkjedu.sstudy.model.subjectbook.TSubjectBook;
import com.xhkjedu.sstudy.utils.StudyUtil;
import com.xhkjedu.sstudy.vo.subjectbook.LsbBookVo;
import com.xhkjedu.sstudy.vo.subjectbook.LsbSubjectVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SubjectBookService
 * Description 教材表
 * Author WN
 * Date 2020/6/2 17:59
 **/
@Service
public class SubjectBookService {
    @Autowired
    private SubjectBookMapper subjectBookMapper;

    /**
     *功能描述 添加教材
     * @author WN
     * @date 2020/6/2
     * @param  * @param subjectBook
     * @return java.lang.Integer
     */
    public Integer addSubjectBook(TSubjectBook subjectBook){
        String id = StudyUtil.getId();
        subjectBook.setLsbid(id);
        subjectBook.setCreatetime(N_Utils.getSecondTimestamp());
        return subjectBookMapper.insertSelective(subjectBook);
    }

    /**
     *功能描述 修改教材
     * @author WN
     * @date 2020/6/2
     * @param  * @param subjectBook
     * @return java.lang.Integer
     */
    public ResultVo updateSubjectBook(TSubjectBook subjectBook){
        TSubjectBook book = subjectBookMapper.selectByPrimaryKey(subjectBook.getLsbid());
        String lsbname = subjectBook.getLsbname();
        if (N_Utils.isNotEmpty(lsbname) && !lsbname.equals(book.getLsbname())) {
            book.setLsbname(lsbname);
            Integer num = subjectBookMapper.checkExisted(book);
            if (num != 0) return new ResultVo(1,"教材不能重复");
        }
        subjectBook.setCreatetime(N_Utils.getSecondTimestamp());
        subjectBookMapper.updateByPrimaryKeySelective(subjectBook);
        return new ResultVo(0,"修改成功");
    }

    /**
     *功能描述 删除教材
     * @author WN
     * @date 2020/6/17
     * @param  * @param subjectBook
     * @return java.lang.Integer
     */
    public Integer delSubjectBook(TSubjectBook subjectBook){
        return subjectBookMapper.deleteByPrimaryKey(subjectBook.getLsbid());
    }

    /**
     *功能描述  获取教材集合
     * @author WN
     * @date 2020/6/7
     * @param  * @param subjectBook
     * @return java.util.List<com.xhkjedu.model.subjectbook.TSubjectBook>
     */
    public List<TSubjectBook> listSubjectBooks(TSubjectBook subjectBook) {
        return subjectBookMapper.listSubjectBooks(subjectBook);
    }

    /**
     *功能描述 所有教材--树形结构
     * @author WN
     * @date 2020/6/3
     * @param  * @param subjectBook
     * @return java.util.List<com.xhkjedu.vo.subjectbook.LsbLevelVo>
     */
    public List<LsbSubjectVo> listSubjectBooksForTree(TSubjectBook subjectBook){
        List<TSubjectBook> sbooks = subjectBookMapper.listSubjectBooks(subjectBook);

        List<LsbSubjectVo> lst = new ArrayList<>();
        for(TSubjectBook sb : sbooks){
            if(!isExistSubject(lst,sb.getSubjectid())){
                LsbSubjectVo s = new LsbSubjectVo();
                s.setLsbid(sb.getLsbid());
                s.setSubjectid(sb.getSubjectid());
                s.setSubjectname(sb.getSubjectname());
                lst.add(s);

                LsbBookVo b = new LsbBookVo();
                b.setLsbid(sb.getLsbid());
                b.setLsbname(sb.getLsbname());
                b.setVersionid(sb.getVersionid());
                b.setVersionname(sb.getVersionname());

                s.getBooks().add(b);
            }else{
                LsbSubjectVo s = lst.get(lst.size()-1);
                if(!isExistBook(s.getBooks(),sb.getLsbid())){
                    LsbBookVo b = new LsbBookVo();
                    b.setLsbid(sb.getLsbid());
                    b.setLsbname(sb.getLsbname());
                    b.setVersionid(sb.getVersionid());
                    b.setVersionname(sb.getVersionname());

                    s.getBooks().add(b);
                }
            }
        }

        return lst;

    }
    //判断是否存在科目
    private boolean isExistSubject(List<LsbSubjectVo> subjects, String subjectid){
        for(int i=0;i<subjects.size();i++){
            if(subjects.get(i).getSubjectid().equals(subjectid)){
                return true;
            }
        }
        return false;
    }

    //判断结合中是否存在册别
    private boolean isExistBook(List<LsbBookVo> books, String lsbid){
        for(int i=0;i<books.size();i++){
            if(books.get(i).getLsbid().equals(lsbid)){
                return true;
            }
        }
        return false;
    }

    //查找使用教材的模块
    public String findUseModule(String lsbid) {
        String useModule = subjectBookMapper.findUseModule(lsbid);
        if (useModule.startsWith(",")) {
            useModule = useModule.substring(1);
        }
        return useModule;
    }

    //教材选择列表
    public List<Map> listForSel() {
        return subjectBookMapper.listForSel();
    }
}
