package com.unis.base.server.domain;

import com.unis.base.common.constant.FilingStatusConstant;
import com.unis.base.common.vo.EquipmentRecordsQueryVO;
import com.unis.base.common.vo.EquipmentRecordsVO;
import com.unis.base.server.dao.EquipmentRecordsDao;
import com.unis.base.server.entity.EquipmentRecordsDO;
import com.unis.common.utils.LsBeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @author WangZL
 * @version 1.0
 * @Title EquipmentRecordsDomain
 * @ProjectName 持久化
 * @Description
 * @date 2019/07/22 17点26分
 */
@Component
public class EquipmentRecordsDomain extends BaseDomain {

	@Autowired
	private EquipmentRecordsDao equipmentRecordsDao;

	/**
	 * 根据参数获取设备记录列表
	 * @param equipmentRecordsQueryVO
	 * @return
	 */
	public List<EquipmentRecordsVO> getEquipmentRecordsList(EquipmentRecordsQueryVO equipmentRecordsQueryVO) {
		return equipmentRecordsDao.getEquipmentRecordsList(equipmentRecordsQueryVO);
	}

	/**
	 * 根据设备编号获取设备记录
	 * @param sbbajlId
	 * @return
	 */
	public EquipmentRecordsVO getEquipmentRecordsInfo(Integer sbbajlId) {
		EquipmentRecordsDO equipmentRecordsDO = equipmentRecordsDao.getEquipmentRecordsInfo(sbbajlId);
		if (equipmentRecordsDO == null){
			return  null;
		}
		EquipmentRecordsVO equipmentRecordsVO = new EquipmentRecordsVO();
		LsBeanUtils.copyAllProperties(equipmentRecordsDO, equipmentRecordsVO);
		return equipmentRecordsVO;
	}

	/**
	 * 新增设备记录
	 * @param equipmentRecordsVO
	 * @return
	 */
	public int insert(EquipmentRecordsVO equipmentRecordsVO) {
		String sfba = equipmentRecordsVO.getSfba().toString();
        //只有已备案和已驳回状态，新增历史表
		if (FilingStatusConstant.FILING.getValue().equals(sfba)
				|| FilingStatusConstant.REJECTED.getValue().equals(sfba)) {
            //驳回的审批意见必填
			if (FilingStatusConstant.REJECTED.getValue().equals(sfba)) {
				if (StringUtils.isEmpty(equipmentRecordsVO.getSpyj()))
					return -2;
			}
			//进行信息copy
			EquipmentRecordsDO equipmentRecordsDO = new EquipmentRecordsDO();
			//获取最大记录id
			Integer sbbajlId = equipmentRecordsDao.selectMaxId();
			if (sbbajlId == null) {
				sbbajlId = 0;
			}
			equipmentRecordsVO.setSbbajlId(sbbajlId + 1);
			equipmentRecordsVO.setStatus(1);
			equipmentRecordsVO.setCreateDate(new Date());
			LsBeanUtils.copyAllProperties(equipmentRecordsVO, equipmentRecordsDO);
			return equipmentRecordsDao.insert(equipmentRecordsDO);
		} else {
			return -1;
		}
	}


}
