package com.seer.teach.mp.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.exception.CommonException;
import com.seer.teach.mp.entity.MpMessageGroupEntity;
import com.seer.teach.mp.mapper.MqMessageGroupMapper;
import com.seer.teach.mp.service.IMqMessageGroupService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 消息群组表 服务实现类
 * </p>
 *
 * @author Wang
 * @since 2025-08-09 20:26:58
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MqMessageGroupServiceImpl extends ServiceImpl<MqMessageGroupMapper, MpMessageGroupEntity> implements IMqMessageGroupService {

    /**
     * 创建消息群组
     * @param params
     */
    @Override
    public boolean crateMesGroup(MpMessageGroupEntity params) {
        MpMessageGroupEntity one = getOne(new LambdaQueryWrapper<MpMessageGroupEntity>()
                .eq(MpMessageGroupEntity::getGroupName, params.getGroupName()));
        if(one != null){
            throw new CommonException(ResultCodeEnum.MESSAGE_GROUP_EXISTS);
        }
        MpMessageGroupEntity entity = new MpMessageGroupEntity();
        entity.setGroupName(params.getGroupName());
        entity.setDescription(params.getDescription());
        return save(entity);
    }

    /**
     * 根据ID查询消息群组
     * @param id
     * @return
     */
    @Override
    public MpMessageGroupEntity getMesGroupById(Integer id) {
        return getById(id);
    }

    /**
     * 分页查询消息群组
     * @param params
     * @return
     */
    @Override
    public IPage<MpMessageGroupEntity> pageList(Page<MpMessageGroupEntity> pageParam ,MpMessageGroupEntity params) {
        LambdaQueryWrapper<MpMessageGroupEntity> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(params.getGroupName())){
            wrapper.like(MpMessageGroupEntity::getGroupName, params.getGroupName());
        }
        IPage<MpMessageGroupEntity> resultPage = this.page(pageParam, wrapper);
        log.info("群组查询成功{}", resultPage);
        return resultPage;
    }

    /**
     * 更新消息群组
     * @param params
     * @param id
     */
    @Override
    public boolean updateMesGroup(MpMessageGroupEntity params, Integer id) {
        MpMessageGroupEntity entity = getById(id);
        if(entity == null){
            throw new CommonException(ResultCodeEnum.MESSAGE_GROUP_NOT_FOUND);
        }
        entity.setGroupName(params.getGroupName());
        entity.setDescription(params.getDescription());
        return updateById(entity);
    }

    /**
     * 删除消息群组
     * @param id
     */
    @Override
    public boolean deleteById(Integer id) {
        MpMessageGroupEntity entity = getById(id);
        if(entity == null){
            throw new CommonException(ResultCodeEnum.MESSAGE_GROUP_NOT_FOUND);
        }
        boolean result = removeById(id);
        log.info("群组删除成功{}", result);
        if(!result){
            throw new CommonException(ResultCodeEnum.MESSAGE_GROUP_DELETE_FAILED);
        }
        return result;
    }

}