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

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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.supplier.dao.SupplierDao;
import com.mortals.xhx.module.supplier.model.SupplierEntity;
import com.mortals.xhx.module.supplier.model.SupplierQuery;
import com.mortals.xhx.module.supplier.service.SupplierService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * Title: 供应商表
 * </p>
 * <p>
 * Description: SupplierServiceImpl service接口
 * </p>
 * <p>
 * Copyright: Copyright &reg;
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author
 * @version 1.0.0
 */
@Service("supplierService")
public class SupplierServiceImpl extends AbstractCRUDServiceImpl<SupplierDao, SupplierEntity, Long>
		implements SupplierService {

	@Override
	protected void validData(SupplierEntity entity, Context context) throws AppException {
		// 校验供应商编码是否已被使用
		SupplierQuery assetQuery = new SupplierQuery();
		assetQuery.setCode(entity.getCode());
		List<SupplierEntity> 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);
	}

	@Override
	public Result<SupplierEntity> getListByCondition(Map<String, Object> param, PageInfo pageInfo) {

		Result<SupplierEntity> result = this.dao.getListByCondition(param, pageInfo);
		return result;
	}

}