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

@Service
public class DictEquipmentService {

	@Autowired
	private DictEquipmentMapper mEquipment;

	/***********************
	 * 增加或者修改
	 */
	public void editEquipment(DictEquipment equipment) {
		FrameAssertHelper.isNotEmpty(equipment.getEquipment_name(), "设备类型名称不允许为空！");
		// 判断类型是否存在
		List<DictEquipment> list = this.mEquipment.selectListByColumn("equipment_name", equipment.getEquipment_name());
		if (equipment.getTsy_id() != null) {
			if (list.size() > 0 && list.get(0).getTsy_id() - equipment.getTsy_id() != 0)
				throw new BusinessException("设备类型名称【" + equipment.getEquipment_name() + "】已经存在，请检查！");
			this.mEquipment.update(equipment);
		} else {
			FrameAssertHelper.isEmpty(list, "设备类型名称【" + equipment.getEquipment_name() + "】已经存在，请检查！");
			equipment.setCreate_time(LocalDateTime.now());
			this.mEquipment.insert(equipment);
		}
	}

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