package com.persagy.adm.diagram.entity;

import com.persagy.adm.diagram.core.model.template.DiagramTemplate;
import com.persagy.adm.diagram.core.ContentParser;
import com.persagy.adm.diagram.core.model.Diagram;
import com.persagy.adm.diagram.core.model.legend.Legend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 运行时模型-实体 类型转换适配器
 * @author zhaoyk
 */
@Service
public class ModelAdapter {

	@Autowired
	private ContentParser parser;

	public DiagramTemplate toTemplate(TemplateEntity templateEntity){
		DiagramTemplate template = parser.parseContent(templateEntity.getTemplateContent(), DiagramTemplate.class);
		template.setId(templateEntity.getId());
		template.setName(templateEntity.getName());
		template.setCode(templateEntity.getCode());
		template.setRemark(templateEntity.getRemark());
		template.setDiagramType(templateEntity.getDiagramType());
		return template;
	}

	public TemplateEntity toTemplateEntity(DiagramTemplate template){
		TemplateEntity entity = new TemplateEntity();
		entity.setId(template.getId());
		entity.setName(template.getName());
		entity.setCode(template.getCode());
		entity.setRemark(template.getRemark());
		entity.setDiagramType(template.getDiagramType());
		entity.setTemplateContent(parser.toJson(template));
		return entity;
	}

	public Legend toLegend(LegendEntity legendEntity){
		Legend legend = parser.parseContent(legendEntity.getLegendContent(), Legend.class);
		legend.setId(legendEntity.getId());
		legend.setName(legendEntity.getName());
		legend.setCode(legendEntity.getCode());
		legend.setRemark(legendEntity.getRemark());
		legend.setSystem(legendEntity.getBtSystem());
		return legend;
	}

	public LegendEntity toLegendEntity(Legend legend) {
		LegendEntity entity = new LegendEntity();
		entity.setId(legend.getId());
		entity.setName(legend.getName());
		entity.setCode(legend.getCode());
		entity.setRemark(legend.getRemark());
		entity.setBtSystem(legend.getSystem());
		entity.setLegendContent(parser.toJson(legend));
		return entity;
	}

	public Diagram toDiagram(DiagramEntity diagramEntity) {
		Diagram diagram = parser.parseContent(diagramEntity.getDiagramContent(), Diagram.class);
		diagram.setId(diagramEntity.getId());
		diagram.setName(diagramEntity.getName());
		diagram.setRemark(diagramEntity.getRemark());
		diagram.setType(diagramEntity.getType());
		diagram.setTemplateId(diagramEntity.getTemplateId());
		diagram.setSystem(diagramEntity.getBtSystem());
		diagram.setProjectId(diagramEntity.getProjectId());
		diagram.setSystemId(diagramEntity.getSystemId());
		diagram.setGroupCode(diagramEntity.getGroupCode());
		diagram.setExtraProp("state", diagramEntity.getState());
		return diagram;
	}

	public DiagramEntity toDiagramEntity(Diagram diagram) {
		DiagramEntity entity = new DiagramEntity();
		entity.setId(diagram.getId());
		entity.setName(diagram.getName());
		entity.setRemark(diagram.getRemark());
		entity.setType(diagram.getType());
		entity.setTemplateId(diagram.getTemplateId());
		entity.setBtSystem(diagram.getSystem());
		entity.setProjectId(diagram.getProjectId());
		entity.setSystemId(diagram.getSystemId());
		entity.setGroupCode(diagram.getGroupCode());
		entity.setState((String) diagram.getExtraProp("state"));
		entity.setDiagramContent(parser.toJson(diagram));
		return entity;
	}

}
