package com.hys.app.service.erp.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hys.app.converter.erp.CompanyPackageConverter;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.mapper.erp.CompanyPackageMapper;
import com.hys.app.model.erp.entity.*;
import com.hys.app.service.erp.CompanyPackageManager;
import com.hys.app.util.ValidateUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.regex.Pattern;

/**
 * 平台管理员业务类
 *
 * @author zh
 * @version v7.0
 * @since v7.0.0
 * 2018-06-20 20:38:26
 */
@Service
@AllArgsConstructor
public class CompanyPackageManagerImpl extends ServiceImpl<CompanyPackageMapper, CompanyPackage> implements CompanyPackageManager {

    @Override
    public Long createCompanyPackage(CompanyPackageDO packageDo) {
        CompanyPackage convert = CompanyPackageConverter.INSTANCE.convert(packageDo);
        checkParam(convert);
        save(convert);
        return convert.getId();
    }

    private void checkParam(CompanyPackage convert) {
        if (ValidateUtil.checkRepeat(baseMapper, CompanyPackage::getName, convert.getName(), CompanyPackage::getId, convert.getId())) {
            throw new ServiceException("套餐名称重复");
        }
    }
}
