/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.service.sysmgr.service.impl;

import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.core.cache.TsmPropertyCache;
import com.crunii.micro.domain.entity.main.TfmProperty;
import com.crunii.micro.domain.entity.main.TfmPropertyValue;
import com.crunii.micro.domain.repo.main.TfmPropertyRepo;
import com.crunii.micro.domain.repo.main.TfmPropertyValueRepo;
import com.crunii.micro.service.sysmgr.dto.fm.*;
import com.crunii.micro.service.sysmgr.jdbc.FmPropertyJdbcDao;
import com.crunii.micro.service.sysmgr.service.FmPropertyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 田平 create 2019年2月20日下午4:03:35
 */
@Service
public class FmPropertyServiceImpl implements FmPropertyService {
	@Autowired
	private TfmPropertyRepo tfmPropertyRepo;
	@Autowired
	private TfmPropertyValueRepo tfmPropertyValueRepo;
	@Autowired
	private FmPropertyJdbcDao fmPropertyJdbcDao;
	@Autowired
	private TsmPropertyCache tsmPropertyCache;

	public List<TfmPropertyValue> getValuesByCode(String propCode) {
		TfmProperty prop = tfmPropertyRepo.getUniqueByProp("propCode", propCode);
		return tfmPropertyValueRepo.findByProp("propId", prop.getPropId());
	}

	public PageRsp<TfmPropertyValue> pageValue(PageReq<FmValueReq> req) {

		return fmPropertyJdbcDao.pageValue(req);
	}

	public PageRsp<TfmProperty> pageProperty(PageReq<FmPropertyReq> req) {

		return fmPropertyJdbcDao.pageProperty(req);
	}

	public void createProperty(FmPropertyC propertyC) {
		TfmProperty property = new TfmProperty();
		BeanUtils.copyProperties(propertyC, property);
		property.setCreateTime(new Date());
		property.setState(1);
		tfmPropertyRepo.saveAndFlush(property);
		tsmPropertyCache.reload();
	}

	public void updateProperty(FmPropertyU propertyU) {
		TfmProperty property = tfmPropertyRepo.getOne(propertyU.getPropId());
		BeanUtils.copyProperties(propertyU, property);
		tfmPropertyRepo.saveAndFlush(property);
		tsmPropertyCache.reload();
	}

	public void createValue(FmValueC valueC) {
		TfmPropertyValue value = new TfmPropertyValue();
		BeanUtils.copyProperties(valueC, value);
		value.setCreateTime(new Date());
		value.setState(1);
		tfmPropertyValueRepo.saveAndFlush(value);
		tsmPropertyCache.reload();
	}

	public void updateValue(FmValueU valueU) {
		TfmPropertyValue value = tfmPropertyValueRepo.getOne(valueU.getValueId());
		BeanUtils.copyProperties(valueU, value);
		tfmPropertyValueRepo.saveAndFlush(value);
		tsmPropertyCache.reload();
	}

}
