package com.xjrsoft.module.textbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.module.textbook.entity.WfExerciseBook;
import com.xjrsoft.module.textbook.entity.WfExerciseBookItem;
import com.xjrsoft.module.textbook.mapper.WfExerciseBookItemMapper;
import com.xjrsoft.module.textbook.mapper.WfExerciseBookMapper;
import com.xjrsoft.module.textbook.service.IWfExerciseBookService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @title: 作业本征订
* @Author szs
* @Date: 2023-12-27
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class WfExerciseBookServiceImpl extends MPJBaseServiceImpl<WfExerciseBookMapper, WfExerciseBook> implements IWfExerciseBookService {
    private final WfExerciseBookMapper wfExerciseBookWfExerciseBookMapper;

    private final WfExerciseBookItemMapper wfExerciseBookWfExerciseBookItemMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(WfExerciseBook wfExerciseBook) {
        wfExerciseBookWfExerciseBookMapper.insert(wfExerciseBook);
        for (WfExerciseBookItem wfExerciseBookItem : wfExerciseBook.getWfExerciseBookItemList()) {
            wfExerciseBookItem.setWfExerciseBookId(wfExerciseBook.getId());
            wfExerciseBookWfExerciseBookItemMapper.insert(wfExerciseBookItem);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(WfExerciseBook wfExerciseBook) {
        wfExerciseBookWfExerciseBookMapper.updateById(wfExerciseBook);
        //********************************* WfExerciseBookItem  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<WfExerciseBookItem> wfExerciseBookItemList = wfExerciseBookWfExerciseBookItemMapper.selectList(Wrappers.lambdaQuery(WfExerciseBookItem.class).eq(WfExerciseBookItem::getWfExerciseBookId, wfExerciseBook.getId()).select(WfExerciseBookItem::getId));
            List<Long> wfExerciseBookItemIds = wfExerciseBookItemList.stream().map(WfExerciseBookItem::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> wfExerciseBookItemOldIds = wfExerciseBook.getWfExerciseBookItemList().stream().map(WfExerciseBookItem::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> wfExerciseBookItemRemoveIds = wfExerciseBookItemIds.stream().filter(item -> !wfExerciseBookItemOldIds.contains(item)).collect(Collectors.toList());

            for (WfExerciseBookItem wfExerciseBookItem : wfExerciseBook.getWfExerciseBookItemList()) {
                //如果不等于空则修改
                if (wfExerciseBookItem.getId() != null) {
                    wfExerciseBookWfExerciseBookItemMapper.updateById(wfExerciseBookItem);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    wfExerciseBookItem.setWfExerciseBookId(wfExerciseBook.getId());
                    wfExerciseBookWfExerciseBookItemMapper.insert(wfExerciseBookItem);
                }
            }
            //已经不存在的id 删除
            if(wfExerciseBookItemRemoveIds.size() > 0){
                wfExerciseBookWfExerciseBookItemMapper.deleteBatchIds(wfExerciseBookItemRemoveIds);
            }
        }
        //********************************* WfExerciseBookItem  增删改  结束 *******************************************/

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        wfExerciseBookWfExerciseBookMapper.deleteBatchIds(ids);
        wfExerciseBookWfExerciseBookItemMapper.delete(Wrappers.lambdaQuery(WfExerciseBookItem.class).in(WfExerciseBookItem::getWfExerciseBookId, ids));

        return true;
    }

    @Override
    public WfExerciseBook selectById(Long id) {
        WfExerciseBook wfExerciseBook = this.getById(id);
        LambdaQueryWrapper<WfExerciseBookItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(WfExerciseBookItem::getWfExerciseBookId,id);
        List<WfExerciseBookItem> wfExerciseBookItemList = wfExerciseBookWfExerciseBookItemMapper.selectList(queryWrapper);
        wfExerciseBook.setWfExerciseBookItemList(wfExerciseBookItemList);
        return wfExerciseBook;
    }
}
