package net.northking.cctp.se.attach;


import net.northking.cctp.se.repository.AttachmentInfoRepository;
import net.northking.cctp.se.util.AttachUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AttachInfoServiceImpl implements AttachInfoService {

    private static final Logger logger = LoggerFactory.getLogger(AttachInfoServiceImpl.class);

    @Autowired
    private AttachmentInfoRepository attachRepo;

    @Override
    public AttachmentDB save(String filename, String filePath, String tenantId, String objectId, String bizCode) {
        return save(filename, filePath, tenantId, -1, -1, objectId, bizCode);
    }

    @Override
    public AttachmentDB save(String filename, String filePath, String tenantId, int x, int y, String objectId, String bizCode) {
        AttachmentDB attachmentDB = new AttachmentDB();
        attachmentDB.setId(AttachUtils.createId(filename));
        attachmentDB.setUrl(AttachUtils.exchangeToUrl(attachmentDB.getId(), tenantId));
        attachmentDB.setFilename(filename);
        attachmentDB.setFilePath(filePath);
        attachmentDB.setCreatedTime(new Date());
        attachmentDB.setTenantId(tenantId);
        attachmentDB.setFailureTimes(0);
        attachmentDB.setX(x);
        attachmentDB.setY(y);
        attachmentDB.setObjectId(objectId);
        attachmentDB.setBizCode(bizCode);
        attachRepo.saveAndFlush(attachmentDB);
        return attachmentDB;
    }
}
