package org.qinan.safetyeval.infrastructure.gatewayimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.qinan.safetyeval.domain.entity.FileStorage;
import org.qinan.safetyeval.domain.gateway.ResourceGateway;
import org.qinan.safetyeval.infrastructure.dataobject.ResourceDO;
import org.qinan.safetyeval.infrastructure.mapper.ResourceMapper;
import org.springframework.stereotype.Component;

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

/**
 * 文件资源Gateway实现（Infrastructure层）
 *
 * @author safety-eval
 */
@Component
public class ResourceGatewayImpl implements ResourceGateway {

    @Resource
    private ResourceMapper resourceMapper;

    @Override
    public FileStorage save(FileStorage fileStorage) {
        ResourceDO dataObject = toDO(fileStorage);
        resourceMapper.insert(dataObject);
        fileStorage.setId(dataObject.getId());
        return fileStorage;
    }

    @Override
    public List<FileStorage> saveBatch(List<FileStorage> fileStorages) {
        List<ResourceDO> dataObjects = fileStorages.stream()
                .map(this::toDO)
                .collect(Collectors.toList());
        for (ResourceDO dataObject : dataObjects) {
            resourceMapper.insert(dataObject);
        }
        for (int i = 0; i < fileStorages.size(); i++) {
            fileStorages.get(i).setId(dataObjects.get(i).getId());
        }
        return fileStorages;
    }

    @Override
    public FileStorage get(Integer id) {
        ResourceDO dataObject = resourceMapper.selectById(id);
        return toEntity(dataObject);
    }

    @Override
    public FileStorage getByFilename(String filename) {
        LambdaQueryWrapper<ResourceDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ResourceDO::getFilename, filename);
        ResourceDO dataObject = resourceMapper.selectOne(wrapper);
        return toEntity(dataObject);
    }

    @Override
    public FileStorage getByUrl(String url) {
        LambdaQueryWrapper<ResourceDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ResourceDO::getUrl, url);
        ResourceDO dataObject = resourceMapper.selectOne(wrapper);
        return toEntity(dataObject);
    }

    @Override
    public void delete(Integer id) {
        resourceMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        resourceMapper.deleteBatchIds(ids);
    }

    @Override
    public FileStorage update(FileStorage fileStorage) {
        ResourceDO dataObject = toDO(fileStorage);
        dataObject.setId(fileStorage.getId());
        resourceMapper.updateById(dataObject);
        return get(fileStorage.getId());
    }

    @Override
    public List<FileStorage> listAll() {
        List<ResourceDO> dataObjects = resourceMapper.selectList(null);
        return dataObjects.stream()
                .map(this::toEntity)
                .collect(Collectors.toList());
    }

    @Override
    public List<FileStorage> listByCreator(String creatorUid) {
        LambdaQueryWrapper<ResourceDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ResourceDO::getCreatorUid, creatorUid);
        List<ResourceDO> dataObjects = resourceMapper.selectList(wrapper);
        return dataObjects.stream()
                .map(this::toEntity)
                .collect(Collectors.toList());
    }

    // ---------- 实体转换 ----------

    private ResourceDO toDO(FileStorage entity) {
        if (entity == null) return null;
        ResourceDO dataObject = new ResourceDO();
        dataObject.setUrl(entity.getUrl());
        dataObject.setSize(entity.getSize());
        dataObject.setFilename(entity.getFilename());
        dataObject.setOriginalFilename(entity.getOriginalFilename());
        dataObject.setBasePath(entity.getBasePath());
        dataObject.setPath(entity.getPath());
        dataObject.setExt(entity.getExt());
        dataObject.setContentType(entity.getContentType());
        dataObject.setPlatform(entity.getPlatform());
        dataObject.setMd5(entity.getMd5());
        dataObject.setCreatorName(entity.getCreatorName());
        dataObject.setCreatorUid(entity.getCreatorUid());
        dataObject.setCreatedTime(entity.getCreatedTime());
        return dataObject;
    }

    private FileStorage toEntity(ResourceDO dataObject) {
        if (dataObject == null) return null;
        FileStorage entity = new FileStorage();
        entity.setId(dataObject.getId());
        entity.setUrl(dataObject.getUrl());
        entity.setSize(dataObject.getSize());
        entity.setFilename(dataObject.getFilename());
        entity.setOriginalFilename(dataObject.getOriginalFilename());
        entity.setBasePath(dataObject.getBasePath());
        entity.setPath(dataObject.getPath());
        entity.setExt(dataObject.getExt());
        entity.setContentType(dataObject.getContentType());
        entity.setPlatform(dataObject.getPlatform());
        entity.setMd5(dataObject.getMd5());
        entity.setCreatorName(dataObject.getCreatorName());
        entity.setCreatorUid(dataObject.getCreatorUid());
        entity.setCreatedTime(dataObject.getCreatedTime());
        return entity;
    }
}
