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.PositionCommunityBo;
import org.dromara.system.domain.vo.PositionCommunityVo;
import org.dromara.system.domain.PositionCommunity;
import org.dromara.system.mapper.PositionCommunityMapper;
import org.dromara.system.service.IPositionCommunityService;

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

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

    private final PositionCommunityMapper baseMapper;

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

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

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

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

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

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

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

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

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