package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.CommunityRoomBo;
import org.dromara.system.domain.vo.CommunityRoomVo;
import org.dromara.system.domain.CommunityRoom;
import org.dromara.system.mapper.CommunityRoomMapper;
import org.dromara.system.service.ICommunityRoomService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2025-03-24
 */
@RequiredArgsConstructor
@Service
public class CommunityRoomServiceImpl implements ICommunityRoomService {

    private final CommunityRoomMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public CommunityRoomVo queryById(String communityId){
        return baseMapper.selectVoById(communityId);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<CommunityRoomVo> queryPageList(CommunityRoomBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CommunityRoom> lqw = buildQueryWrapper(bo);
        Page<CommunityRoomVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<CommunityRoomVo> queryList(CommunityRoomBo bo) {
        LambdaQueryWrapper<CommunityRoom> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CommunityRoom> buildQueryWrapper(CommunityRoomBo bo) {
//        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CommunityRoom> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCommunityId()), CommunityRoom::getCommunityId, bo.getCommunityId());
        lqw.eq(StringUtils.isNotBlank(bo.getRoomId()), CommunityRoom::getRoomId, bo.getRoomId());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(CommunityRoomBo bo) {
        CommunityRoom add = MapstructUtils.convert(bo, CommunityRoom.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCommunityId(add.getCommunityId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(CommunityRoomBo bo) {
        CommunityRoom update = MapstructUtils.convert(bo, CommunityRoom.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CommunityRoom entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean batchDelete(List<String> list) {
        return baseMapper.batchDelete(list);
    }
}
