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.dromara.system.domain.Room;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.RoomRegionBo;
import org.dromara.system.domain.vo.RoomRegionVo;
import org.dromara.system.domain.RoomRegion;
import org.dromara.system.mapper.RoomRegionMapper;
import org.dromara.system.service.IRoomRegionService;

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

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-16
 */
@RequiredArgsConstructor
@Service
public class RoomRegionServiceImpl implements IRoomRegionService {

    private final RoomRegionMapper baseMapper;

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

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

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

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

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

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

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RoomRegion 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);
    }
}
