package com.ecep.contract.controller.table.cell;

import java.io.File;

import org.springframework.util.StringUtils;

import com.ecep.contract.SpringApp;
import com.ecep.contract.model.CompanyCustomerEvaluationFormFile;
import com.ecep.contract.model.CompanyCustomerFile;
import com.ecep.contract.service.CompanyCustomerFileService;

import lombok.NoArgsConstructor;

/**
 * 评估文件 TableCell
 */
@NoArgsConstructor
public class EvaluationFileTableCell<V> extends AsyncUpdateTableCell<V, CompanyCustomerEvaluationFormFile> {
    private CompanyCustomerFileService fileService;

    public EvaluationFileTableCell(CompanyCustomerFileService fileService) {
        this.fileService = fileService;
    }

    public CompanyCustomerFileService getFileService() {
        if (fileService == null) {
            fileService = SpringApp.getBean(CompanyCustomerFileService.class);
        }
        return fileService;
    }


    @Override
    protected CompanyCustomerEvaluationFormFile initialize() {
        return getFileService().findCustomerEvaluationFormFileById(getItem().getId());
    }

    @Override
    public String format(CompanyCustomerEvaluationFormFile entity) {
        CompanyCustomerFile customerFile = entity.getCustomerFile();
        if (customerFile == null) {
            return super.format(entity);
        }
        String path = customerFile.getFilePath();
        if (StringUtils.hasText(path)) {
            File file = new File(path);
            return file.getName();
        }
        return toString(customerFile);
    }
}
