/**
* 文件：AssetFlowEventServiceImpl.java
* 版本：1.0.0
* 日期：
* Copyright &reg; 
* All right reserved.
*/

package com.mortals.xhx.module.asset.service.impl;

import com.google.common.collect.Lists;
import com.mortals.framework.common.code.YesNo;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.common.code.AssetFlowStatusEnum;
import com.mortals.xhx.module.asset.dao.AssetFlowEventDao;
import com.mortals.xhx.module.asset.model.AssetFlowEventEntity;
import com.mortals.xhx.module.asset.model.AssetFlowEventQuery;
import com.mortals.xhx.module.asset.service.AssetFlowEventService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * Title: 资产事件表
 * </p>
 * <p>
 * Description: AssetFlowEventServiceImpl service接口
 * </p>
 * <p>
 * Copyright: Copyright &reg;
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author
 * @version 1.0.0
 */
@Service("assetFlowEventService")
public class AssetFlowEventServiceImpl extends AbstractCRUDServiceImpl<AssetFlowEventDao, AssetFlowEventEntity, Long>
		implements AssetFlowEventService {

	@Override
	protected void validData(AssetFlowEventEntity entity, Context context) throws AppException {
		// 校验事件码是否重复
		AssetFlowEventQuery query = new AssetFlowEventQuery();
		query.setCode(entity.getCode());
		List<AssetFlowEventEntity> exists = this.find(query, context);
		if(!exists.isEmpty()){
			if (entity.getId() == null || exists.get(0).getId().longValue() != entity.getId().longValue()) {
				throw new AppException("该事件码已被使用，请重新定义");
			}
		}
		//校验事件名称是否重复
		AssetFlowEventQuery queryByName = new AssetFlowEventQuery();
		queryByName.setName(entity.getName());
		List<AssetFlowEventEntity> existsByName = this.find(queryByName, context);
		if(!existsByName.isEmpty()){
			if (entity.getId() == null || existsByName.get(0).getId().longValue() != entity.getId().longValue()) {
				throw new AppException("该事件名已被使用，请重新定义");
			}
		}
		super.validData(entity, context);
	}

	@Override
	public AssetFlowEventEntity getByEventCode(String eventCode) {
		AssetFlowEventQuery eventQuery = new AssetFlowEventQuery();
		eventQuery.setCode(eventCode);
		// eventQuery.setEnabled(YesNo.YES.getValue());
		List<AssetFlowEventEntity> assetFlowEventEntities = this.find(eventQuery, null);

		return assetFlowEventEntities.isEmpty() ? null : assetFlowEventEntities.get(0);

	}

	@Override
	public List<AssetFlowEventEntity> getFollowEventList(String beforeFlowStatus) {
		AssetFlowEventQuery query1 = new AssetFlowEventQuery();
		query1.setBeforeFlowStatus("%" + beforeFlowStatus + "%");
		query1.setEnabled(YesNo.YES.getValue());
		//查询特殊状态：任务状态
		AssetFlowEventQuery query2 = new AssetFlowEventQuery();
		query2.setBeforeFlowStatus("%" + AssetFlowStatusEnum.Special_597.getValue() + "%");
		query2.setEnabled(YesNo.YES.getValue());

		AssetFlowEventQuery query = new AssetFlowEventQuery();
		query.setOrConditionList(Lists.newArrayList(query1, query2));
		return this.getDao().getList(query);
	}
}