package com.mortals.xhx.module.product.service.impl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.product.model.ProductQuery;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.product.dao.ProductDao;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.service.ProductService;
import org.springframework.util.ObjectUtils;

/**
* ProductService
* 产品 service实现
*
* @author zxfei
* @date 2022-06-27
*/
@Service("productService")
public class ProductServiceImpl extends AbstractCRUDServiceImpl<ProductDao, ProductEntity, Long> implements ProductService {


    @Override
    protected void saveBefore(ProductEntity entity, Context context) throws AppException {
        //新增产品编码不能重复
        ProductEntity productEntity = this.selectOne(new ProductQuery().productCode(entity.getProductCode()));
        if(!ObjectUtils.isEmpty(productEntity))throw new AppException("当前产品编码已存在！");

        super.saveBefore(entity, context);
    }

    @Override
    protected void updateBefore(ProductEntity entity, Context context) throws AppException {
        ProductEntity productEntity = this.get(entity.getId());
        if(!productEntity.getProductCode().equals(entity.getProductCode())){
            ProductEntity productEntity1 = this.selectOne(new ProductQuery().productCode(entity.getProductCode()));
            if(!ObjectUtils.isEmpty(productEntity1))throw new AppException("当前产品编码已存在！");
        }
        super.updateBefore(entity, context);
    }
}