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.service.CompanyCustomerEvaluationFormFileService;
import com.ecep.contract.service.CompanyCustomerFileService;
import com.ecep.contract.vo.CompanyCustomerEvaluationFormFileVo;
import com.ecep.contract.vo.CustomerFileVo;

import lombok.NoArgsConstructor;

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

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

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

    private CompanyCustomerEvaluationFormFileService getEvaluationFormFileService() {
        if (evaluationFormFileService == null) {
            evaluationFormFileService = SpringApp.getBean(CompanyCustomerEvaluationFormFileService.class);
        }
        return evaluationFormFileService;
    }

    @Override
    protected CompanyCustomerEvaluationFormFileVo initialize() {
        // 直接返回Vo对象
        return getEvaluationFormFileService().findById(getItem());
    }

    @Override
    public String format(CompanyCustomerEvaluationFormFileVo vo) {
        if (vo == null || vo.getCustomerFile() == null) {
            return super.format(vo);
        }
        CustomerFileVo customerFile = getFileService().findById(vo.getCustomerFile());
        String path = customerFile.getFilePath();
        if (StringUtils.hasText(path)) {
            File file = new File(path);
            return file.getName();
        }
        return toString(customerFile);
    }
}
