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.DictUnitShareMapper;
import cn.tsy.data.idsse.dsds.model.dict.DictUnitShare;

@Service
public class DictUnitShareService {

	@Autowired
	private DictUnitShareMapper mUnitShare;

	/***********************
	 * 增加或者修改
	 */
	public void editUnitShare(DictUnitShare share) {
		FrameAssertHelper.isNotEmpty(share.getUnit_name(), "共享单位名称不允许为空！");
		// 判断类型是否存在
		List<DictUnitShare> list = this.mUnitShare.selectListByColumn("unit_name", share.getUnit_name());
		if (share.getTsy_id() != null) {
			if (list.size() > 0 && list.get(0).getTsy_id() - share.getTsy_id() != 0)
				throw new BusinessException("共享单位名称【" + share.getUnit_name() + "】已经存在，请检查！");
			this.mUnitShare.update(share);
		} else {
			FrameAssertHelper.isEmpty(list, "共享单位名称【" + share.getUnit_name() + "】已经存在，请检查！");
			share.setCreate_time(LocalDateTime.now());
			this.mUnitShare.insert(share);
		}
	}

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