package com.hzs.system.base.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.exception.ServiceException;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.system.base.BdRegionVertex;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.mapper.BdRegionVertexMapper;
import com.hzs.system.base.service.IBdRegionVertexService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 区域体系分组配置表 服务实现类
 * </p>
 *
 * @author sangelxiu1
 * @since 2025-06-25
 */
@Service
public class BdRegionVertexServiceImpl extends ServiceImpl<BdRegionVertexMapper, BdRegionVertex> implements IBdRegionVertexService {

    @Override
    public List<BdRegionVertex> getList(BdRegionVertex param) {
        LambdaQueryWrapper<BdRegionVertex> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(param)){
            if(StringUtils.isNotEmpty(param.getName())){
                queryWrapper.like(BdRegionVertex::getName, param.getName());
            }
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void add(BdRegionVertex param) {
        if(ObjectUtil.isNotEmpty(param)){
            if(StringUtils.isEmpty(param.getName())){
                throw new ServiceException("区域体系分组名称不能为空!");
            }
        }
        LambdaQueryWrapper<BdRegionVertex> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdRegionVertex::getName, param.getName());
        queryWrapper.eq(BdRegionVertex::getDelFlag, EDelFlag.UN_DELETE.getValue());
        int sameNameCount = baseMapper.selectCount(queryWrapper);
        if(sameNameCount > 0){
            throw new ServiceException("已存在相同的区域体系分组名称!");
        }
        param.setPkCreator(SecurityUtils.getUserId());
        param.setCreationTime(new Date());
        param.setPkModified(param.getPkCreator());
        param.setModifiedTime(param.getModifiedTime());
        param.setDelFlag(EDelFlag.UN_DELETE.getValue());
        baseMapper.insert(param);
    }

    @Override
    public void delete(BdRegionVertex param) {
        // 验证是否已被引用
        if(ObjectUtil.isNotEmpty(param)){
            if(ObjectUtil.isEmpty(param.getPkId())){
                throw new ServiceException("区域体系分组主键不能为空!");
            }
        }
        int validateMappingCount = baseMapper.validateMappingCount(param.getPkId());
        if(validateMappingCount > 0){
            throw new ServiceException("存在已引用此配置的体系信息, 不允许删除!");
        }
        baseMapper.deleteById(param.getPkId());
    }

    @Override
    public void update(BdRegionVertex param) {
        if(ObjectUtil.isNotEmpty(param)){
            if(ObjectUtil.isEmpty(param.getPkId())){
                throw new ServiceException("区域体系分组主键不能为空!");
            }
            if(StringUtils.isEmpty(param.getName())){
                throw new ServiceException("区域体系分组名称不能为空!");
            }
        }
        LambdaQueryWrapper<BdRegionVertex> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdRegionVertex::getName, param.getName());
        queryWrapper.ne(BdRegionVertex::getPkId, param.getPkId());
        queryWrapper.eq(BdRegionVertex::getDelFlag, EDelFlag.UN_DELETE.getValue());
        int sameNameCount = baseMapper.selectCount(queryWrapper);
        if(sameNameCount > 0){
            throw new ServiceException("已存在相同的区域体系分组名称!");
        }
        param.setPkModified(SecurityUtils.getUserId());
        param.setModifiedTime(new Date());
        param.setDelFlag(EDelFlag.UN_DELETE.getValue());
        baseMapper.updateById(param);
    }

    @Override
    public BdRegionVertex getById(BdRegionVertex param) {
        if(ObjectUtil.isNotEmpty(param)){
            if(ObjectUtil.isEmpty(param.getPkId())){
                throw new ServiceException("区域体系分组主键不能为空!");
            }
        }
        return baseMapper.selectById(param.getPkId());
    }
}
