package org.springblade.openapi.wps.convert;

import org.mapstruct.*;
import org.springblade.openapi.wps.pojo.vo.Api4WpsFileVO;
import org.springblade.file.pojo.vo.AttachmentDetailVO;

import java.util.Date;

/**
 * @author bfhuange
 * @date 2024/10/30
 */
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING, unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface WpsFileConvert {

    @Mapping(source = "attachmentName", target = "name")
    @Mapping(source = "url", target = "downloadUrl")
    @Mapping(source = "size", target = "size", qualifiedByName = "long2int")
    @Mapping(source = "createUser", target = "creator")
    @Mapping(source = "createTime", target = "createTime", qualifiedByName = "date2int")
    @Mapping(source = "updateUser", target = "modifier")
    @Mapping(source = "updateTime", target = "modifyTime", qualifiedByName = "date2int")
    Api4WpsFileVO attachment2file(AttachmentDetailVO attachment);

    @Named("date2int")
    default Integer date2int(Date date) {
        if (date == null) {
            return null;
        }
        return Math.toIntExact(date.getTime() / 1000);
    }

    @Named("long2int")
    default Integer long2int(long size) {
        return Math.toIntExact(size);
    }
}
