package com.keyware.shandan.bianmu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keyware.shandan.bianmu.entity.DirectoryTableVo;
import com.keyware.shandan.bianmu.entity.DirectoryVo;
import com.keyware.shandan.bianmu.mapper.DirectoryTableMapper;
import com.keyware.shandan.bianmu.service.DirectoryService;
import com.keyware.shandan.bianmu.service.DirectoryTableService;
import com.keyware.shandan.common.service.BaseServiceImpl;
import com.keyware.shandan.dynacmicform.core.basic.entity.FormInfoPo;
import com.keyware.shandan.dynacmicform.core.basic.services.FormInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

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

@Service
public class DirectoryTableServiceImpl extends BaseServiceImpl<DirectoryTableMapper, DirectoryTableVo, Integer> implements DirectoryTableService {

    @Autowired
    private FormInfoService formInfoService;

    @Autowired
    private DirectoryService directoryService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addFormBatch(String dirId, List<String> formIdList) {
        formIdList.forEach(formId -> {
            FormInfoPo formInfo = formInfoService.getById(formId);
            DirectoryTableVo dtv = DirectoryTableVo.builder()
                    .dirId(dirId)
                    .formId(formInfo.getId())
                    .formName(formInfo.getFormId())
                    .formComment(formInfo.getFormName())
                    .build();
            save(dtv);
        });
        return true;
    }

    @Override
    public Page<DirectoryTableVo> page(Page<DirectoryTableVo> page, QueryWrapper<DirectoryTableVo> wrapper) {
        DirectoryTableVo table = wrapper.getEntity();
        // 先获取所有子节点
        DirectoryVo dir = directoryService.getById(table.getDirId());
        Assert.notNull(dir, "目录节点不存在");
        Set<String> dirIds = directoryService.allChildrenLists(dir).stream().map(DirectoryVo::getId).collect(Collectors.toSet());
        dirIds.add(table.getDirId());
        wrapper.clear();
        wrapper.in("DIRECTORY_ID", dirIds);
        if (StringUtils.hasText(table.getFormComment())) {
            wrapper.and(subWrapper -> subWrapper
                    .like("FORM_COMMENT", table.getFormComment())
                    .or()
                    .like("FORM_NAME", table.getFormComment()));
        }
        return super.page(page, wrapper);
    }

    @Override
    public boolean removeFormBatch(String dirId, List<String> formIdList) {
        QueryWrapper<DirectoryTableVo> wrapper = new QueryWrapper<>();
        wrapper.eq("DIRECTORY_ID", dirId).in("FORM_ID", formIdList);
        return remove(wrapper);
    }
}
