package cn.tsy.idsse.dsds.service.dict;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hme.framework.web.exception.BusinessException;
import cn.hme.framework.web.helper.FrameAssertHelper;
import cn.tsy.data.idsse.dsds.mapper.dict.DictFundingMapper;
import cn.tsy.data.idsse.dsds.model.dict.DictFunding;

@Service
public class DictFundingService {

	@Autowired
	private DictFundingMapper mFunding;

	/***********************
	 * 增加或者修改
	 */
	public void editFunding(DictFunding funding) {
		FrameAssertHelper.isNotEmpty(funding.getFunding_name(), "资助机构名称不允许为空！");
		// 判断类型是否存在
		List<DictFunding> list = this.mFunding.selectListByColumn("funding_name", funding.getFunding_name());
		if (funding.getTsy_id() != null) {
			if (list.size() > 0 && list.get(0).getTsy_id() - funding.getTsy_id() != 0)
				throw new BusinessException("资助机构名称【" + funding.getFunding_name() + "】已经存在，请检查！");
			this.mFunding.update(funding);
		} else {
			FrameAssertHelper.isEmpty(list, "资助机构名称【" + funding.getFunding_name() + "】已经存在，请检查！");
			funding.setCreate_time(LocalDateTime.now());
			this.mFunding.insert(funding);
		}
	}

	/*****************************
	 * 删除，
	 */
	public void deleteFunding(int funding_id) {
		/**************
		 * @todo 需要去判断是否真的可以删除
		 */
		this.mFunding.deleteByKey(funding_id);
	}
}
