package com.palmnest.service.org.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.DateHelper;
import com.palmnest.application.dto.output.org.AboutOrgOutDto;
import com.palmnest.model.property.AboutOrg;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.service.OrganizationManager;
import com.palmnest.service.org.AboutOrgManager;
import com.palmnest.service.org.AboutOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("aboutOrgService")
public class AboutOrgServiceImpl implements AboutOrgService {

	@Autowired
	private AboutOrgManager     aboutOrgManager;
	@Autowired
	private OrganizationManager organizationManager;

	@Override
	public List<AboutOrgOutDto> findByOrg(Integer orgId) {
		List<AboutOrg>       list = aboutOrgManager.findByOrg(orgId);
		List<AboutOrgOutDto> dtos = new ArrayList<AboutOrgOutDto>();
		for (AboutOrg aOrg : list) {
			AboutOrgOutDto dto = convertToDto(aOrg);
			dtos.add(dto);
		}
		return dtos;
	}

	private AboutOrgOutDto convertToDto(AboutOrg aOrg) {
		AboutOrgOutDto dto = new AboutOrgOutDto(aOrg.getId(), aOrg.getOrgId(), aOrg.getTitle(), aOrg.getContent(), aOrg.getIsEnabled());
		dto.setCreateTime(DateHelper.formatDateToStr(aOrg.getCreateTime()));
		dto.setModifyTime(DateHelper.formatDateToStr(aOrg.getModifyTime()));
		dto.setCreator(aOrg.getCreator());
		dto.setModifier(aOrg.getModifier());
		dto.setHotLineName(aOrg.getHotLineName());
		dto.setHotLine(aOrg.getHotLine());
		dto.setFileIds(aOrg.getFileIds());
		dto.setType(aOrg.getType());
		TbOrganization org = organizationManager.getOrganizationById(aOrg.getOrgId());
		if (org != null) {
			dto.setOrgId(aOrg.getOrgId());
			dto.setOrgName(org.getName());
		}
		return dto;
	}

	@Override
	public void save(AboutOrg aboutOrg) {
		aboutOrgManager.saveModel(aboutOrg);
	}

	@Override
	public void update(AboutOrg aboutOrg) {
		aboutOrgManager.updateModel(aboutOrg);
	}

	@Override
	public AboutOrg findById(Long id) {
		return aboutOrgManager.get(id);
	}

	@Override
	public void delete(AboutOrg aboutOrg) {
		aboutOrgManager.remove(aboutOrg);
	}

	@Override
	public Page getList(Map<String, String> params) {
		return aboutOrgManager.getList(params);
	}

	@Override
	public AboutOrgOutDto findDtoBy(Long id) {
		AboutOrg aOrg = aboutOrgManager.get(id);
		if (aOrg != null) {
			return convertToDto(aOrg);
		}
		return null;
	}

}
