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.WfTextbookSubscription;
import com.xjrsoft.module.textbook.entity.WfTextbookSubscriptionItem;
import com.xjrsoft.module.textbook.mapper.WfTextbookSubscriptionItemMapper;
import com.xjrsoft.module.textbook.mapper.WfTextbookSubscriptionMapper;
import com.xjrsoft.module.textbook.service.IWfTextbookSubscriptionService;
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-25
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class WfTextbookSubscriptionServiceImpl extends MPJBaseServiceImpl<WfTextbookSubscriptionMapper, WfTextbookSubscription> implements IWfTextbookSubscriptionService {
    private final WfTextbookSubscriptionMapper wfTextbookSubscriptionWfTextbookSubscriptionMapper;

    private final WfTextbookSubscriptionItemMapper wfTextbookSubscriptionWfTextbookSubscriptionItemMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(WfTextbookSubscription wfTextbookSubscription) {
        wfTextbookSubscriptionWfTextbookSubscriptionMapper.insert(wfTextbookSubscription);
        for (WfTextbookSubscriptionItem wfTextbookSubscriptionItem : wfTextbookSubscription.getWfTextbookSubscriptionItemList()) {
            wfTextbookSubscriptionItem.setWfTextbookSubscriptionId(wfTextbookSubscription.getId());
            wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.insert(wfTextbookSubscriptionItem);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(WfTextbookSubscription wfTextbookSubscription) {
        wfTextbookSubscriptionWfTextbookSubscriptionMapper.updateById(wfTextbookSubscription);
        //********************************* WfTextbookSubscriptionItem  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<WfTextbookSubscriptionItem> wfTextbookSubscriptionItemList = wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.selectList(Wrappers.lambdaQuery(WfTextbookSubscriptionItem.class).eq(WfTextbookSubscriptionItem::getWfTextbookSubscriptionId, wfTextbookSubscription.getId()).select(WfTextbookSubscriptionItem::getId));
            List<Long> wfTextbookSubscriptionItemIds = wfTextbookSubscriptionItemList.stream().map(WfTextbookSubscriptionItem::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> wfTextbookSubscriptionItemOldIds = wfTextbookSubscription.getWfTextbookSubscriptionItemList().stream().map(WfTextbookSubscriptionItem::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> wfTextbookSubscriptionItemRemoveIds = wfTextbookSubscriptionItemIds.stream().filter(item -> !wfTextbookSubscriptionItemOldIds.contains(item)).collect(Collectors.toList());

            for (WfTextbookSubscriptionItem wfTextbookSubscriptionItem : wfTextbookSubscription.getWfTextbookSubscriptionItemList()) {
                //如果不等于空则修改
                if (wfTextbookSubscriptionItem.getId() != null) {
                    wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.updateById(wfTextbookSubscriptionItem);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    wfTextbookSubscriptionItem.setWfTextbookSubscriptionId(wfTextbookSubscription.getId());
                    wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.insert(wfTextbookSubscriptionItem);
                }
            }
            //已经不存在的id 删除
            if(wfTextbookSubscriptionItemRemoveIds.size() > 0){
                wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.deleteBatchIds(wfTextbookSubscriptionItemRemoveIds);
            }
        }
        //********************************* WfTextbookSubscriptionItem  增删改  结束 *******************************************/

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        wfTextbookSubscriptionWfTextbookSubscriptionMapper.deleteBatchIds(ids);
        wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.delete(Wrappers.lambdaQuery(WfTextbookSubscriptionItem.class).in(WfTextbookSubscriptionItem::getWfTextbookSubscriptionId, ids));

        return true;
    }

    @Override
    public WfTextbookSubscription selectById(Long id) {
        WfTextbookSubscription wfTextbookSubscription = this.getById(id);
        LambdaQueryWrapper<WfTextbookSubscriptionItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(WfTextbookSubscriptionItem::getWfTextbookSubscriptionId,id);
        List<WfTextbookSubscriptionItem> wfTextbookSubscriptionItemList = wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.selectList(queryWrapper);
        wfTextbookSubscription.setWfTextbookSubscriptionItemList(wfTextbookSubscriptionItemList);
        return wfTextbookSubscription;
    }
}
