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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keyware.shandan.bianmu.entity.DirectoryTableVo;
import com.keyware.shandan.bianmu.mapper.DirectoryTableMapper;
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 java.util.List;

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

    @Autowired
    private FormInfoService formInfoService;

    @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 boolean removeFormBatch(String dirId, List<String> formIdList) {
        QueryWrapper<DirectoryTableVo> wrapper = new QueryWrapper<>();
        wrapper.eq("DIRECTORY_ID", dirId).in("FORM_ID", formIdList);
        return remove(wrapper);
    }
}
