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

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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.partner.dao.PartnerDao;
import com.mortals.xhx.module.partner.model.PartnerEntity;
import com.mortals.xhx.module.partner.model.PartnerQuery;
import com.mortals.xhx.module.partner.service.PartnerService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * Title: 客户表
 * </p>
 * <p>
 * Description: PartnerServiceImpl service接口
 * </p>
 * <p>
 * Copyright: Copyright &reg;
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author
 * @version 1.0.0
 */
@Service("partnerService")
public class PartnerServiceImpl extends AbstractCRUDServiceImpl<PartnerDao, PartnerEntity, Long>
		implements PartnerService {

	@Override
	protected void validData(PartnerEntity entity, Context context) throws AppException {
		// 校验资产码是否已被使用
		PartnerQuery assetQuery = new PartnerQuery();
		assetQuery.setCode(entity.getCode());
		List<PartnerEntity> existList = this.dao.getList(assetQuery);
		if (!existList.isEmpty()) {
			if (entity.getId() == null) { // 新增
				throw new AppException("代码已被使用");
			}
			if (entity.getId() != null && entity.getId().longValue() != existList.get(0).getId().longValue()) { // 修改
				throw new AppException("代码已被使用");
			}
		}
		super.validData(entity, context);
	}

}