package com.ecep.contract.converter;

import java.util.List;

import com.ecep.contract.service.ContractGroupService;
import javafx.util.StringConverter;
import org.springframework.util.StringUtils;

import com.ecep.contract.vo.ContractGroupVo;

public class ContractGroupStringConverter extends StringConverter<ContractGroupVo> {
    private ContractGroupService service;

    public ContractGroupStringConverter(ContractGroupService contractGroupService) {
        this.service = contractGroupService;
    }

    @Override
    public String toString(ContractGroupVo group) {
        if (group == null) {
            return "All";
        }
        return group.getCode() + " " + group.getName() + " " + group.getTitle();
    }

    @Override
    public ContractGroupVo fromString(String string) {
        if (!StringUtils.hasText(string)) {
            return null;
        }
        return service.findByCode(string);
    }
}
