package cn.ey.biz.module.system.controller.admin.contract.vo;

import cn.ey.biz.common.util.json.JsonUtils;
import io.swagger.annotations.ApiModel;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Data
@ApiModel(value = "合同保存VO", description = "通用合同保存结构")
public class ContractSaveVO<T> {

    private String serviceLine;

    @NotBlank(message = "合同类型不能为空")
    private String type;

    private String projectType;

    private String slType;

    @Valid
    private T content;

    public <V> ContractSaveVO<V> convert(Class<V> type) {
        ContractSaveVO<V> target = new ContractSaveVO<>();
        target.setType(this.getType());
        target.setServiceLine(this.getServiceLine());
        target.setProjectType(this.getProjectType());
        target.setSlType(this.getSlType());
        target.setContent(JsonUtils.parseObject(
                JsonUtils.toJsonPrettyString(this.getContent()), type));
        return target;
    }

    public <V> ContractSaveVO<List<V>> convertArray(Class<V> type) {
        ContractSaveVO<List<V>> target = new ContractSaveVO<>();
        target.setType(this.getType());
        target.setServiceLine(this.getServiceLine());
        target.setContent(JsonUtils.parseArray((String) this.getContent(), type));
        return target;
    }

}
