package cn.axzo.workflow.core.service.impl;

import cn.axzo.framework.data.mybatisplus.model.BaseEntity;
import cn.axzo.workflow.common.enums.FileTypeEnum;
import cn.axzo.workflow.common.exception.WorkflowEngineException;
import cn.axzo.workflow.core.repository.entity.ExtAxDocContent;
import cn.axzo.workflow.core.repository.mapper.ExtAxDocContentMapper;
import cn.axzo.workflow.core.service.ExtAxDocContentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.axzo.workflow.common.code.BpmnModelRespCode.MODEL_FILE_CONTENT_DATA_ERROR;

/**
 * 文档模板内容
 *
 * @author wangli
 * @since 2025-03-28 18:47
 */
@Service
@Slf4j
public class ExtAxDocContentServiceImpl implements ExtAxDocContentService {
    @Resource
    private ExtAxDocContentMapper extAxDocContentMapper;

    @Override
    public List<ExtAxDocContent> getByIds(List<Long> ids) {
        return extAxDocContentMapper.selectBatchIds(ids);
    }

    @Override
    public ExtAxDocContent createContent(String content, FileTypeEnum fileType, Long docId) {
        ExtAxDocContent entity = new ExtAxDocContent();
        entity.setContent(StringUtils.hasText(content) ? content : "");
        entity.setFileType(fileType.name());
        entity.setFileId(docId);
        extAxDocContentMapper.insert(entity);
        return entity;
    }

    @Override
    public ExtAxDocContent updateContent(String content, FileTypeEnum fileType, String docId) {
        ExtAxDocContent entity = extAxDocContentMapper.selectOne(new LambdaQueryWrapper<ExtAxDocContent>()
                .eq(ExtAxDocContent::getId, Long.valueOf(docId)));
        if (Objects.nonNull(content)) {
            entity.setContent(content);
        }
        entity.setFileType(fileType.name());
        extAxDocContentMapper.updateById(entity);
        return entity;
    }

    @Override
    public ExtAxDocContent deleteContent(String docId) {
        ExtAxDocContent entity = extAxDocContentMapper.selectOne(new LambdaQueryWrapper<ExtAxDocContent>()
                .eq(ExtAxDocContent::getId, docId));
        extAxDocContentMapper.deleteById(entity);
        return entity;
    }

    @Override
    public List<ExtAxDocContent> batchDeleteContent(List<String> docIds) {
        List<ExtAxDocContent> entities = extAxDocContentMapper.selectList(new LambdaQueryWrapper<ExtAxDocContent>()
                .in(ExtAxDocContent::getId, docIds));
        extAxDocContentMapper.deleteBatchIds(entities.stream().map(BaseEntity::getId).collect(Collectors.toList()));
        return entities;
    }

    @Override
    public String getContent(Long docId) {
        ExtAxDocContent content = extAxDocContentMapper.selectOne(ExtAxDocContent::getId, docId);
        if (Objects.isNull(content)) {
            throw new WorkflowEngineException(MODEL_FILE_CONTENT_DATA_ERROR);
        }
        return content.getContent();
    }
}
