package com.xjrsoft.module.dev.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.pictc.enums.BusinessCode;
import com.pictc.enums.ExceptionCommonCode;
import com.pictc.utils.StringUtils;
import com.xjrsoft.common.enums.ResponseCode;
import com.xjrsoft.common.exception.BusinessException;
import com.xjrsoft.module.dev.entity.Testfrom3;
import com.xjrsoft.module.dev.mapper.Testfrom3Mapper;
import com.xjrsoft.module.dev.service.ITestfrom3Service;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

/**
* @title: service
* @Author 管理员
* @Date: 2025-09-25
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class Testfrom3ServiceImpl extends ServiceImpl<Testfrom3Mapper, Testfrom3> implements ITestfrom3Service {
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void addTestfrom3(Testfrom3 data) {
		if (!StringUtils.isEmpty(data.getDanXingWenBen5097())) {
            throw new BusinessException(ResponseCode.PARAM_MISS);
        }
		if(StringUtils.isNotEmpty(data.getDanXingWenBen5097())){
	        Long nameCount = findNameCount(data.getDanXingWenBen5097());
	        if(nameCount!=null && nameCount.intValue() > 0) {
	          throw new BusinessException(BusinessCode.ofArgs(ExceptionCommonCode.DATA_FIELD_DUPLICATION,"规则名称"));
	        }
	    }
		baseMapper.insert(data);
	}
	
    private Long findNameCount(String name) {
    	LambdaQueryWrapper<Testfrom3> queryWrapper = Wrappers.lambdaQuery(Testfrom3.class);
    	queryWrapper.eq(Testfrom3::getDanXingWenBen5097,name);
		return baseMapper.selectCount(queryWrapper);
    }

	
}
