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.TblCountryBo;
import org.dromara.system.domain.vo.TblCountryVo;
import org.dromara.system.domain.TblCountry;
import org.dromara.system.mapper.TblCountryMapper;
import org.dromara.system.service.ITblCountryService;

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

/**
 * 乡镇管理Service业务层处理
 *
 * @author Lion Li
 * @date 2024-09-27
 */
@RequiredArgsConstructor
@Service
public class TblCountryServiceImpl implements ITblCountryService {

    private final TblCountryMapper baseMapper;

    /**
     * 查询乡镇管理
     */
    @Override
    public TblCountryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询乡镇管理列表
     */
    @Override
    public TableDataInfo<TblCountryVo> queryPageList(TblCountryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblCountry> lqw = buildQueryWrapper(bo);
        Page<TblCountryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询乡镇管理列表
     */
    @Override
    public List<TblCountryVo> queryList(TblCountryBo bo) {
        LambdaQueryWrapper<TblCountry> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblCountry> buildQueryWrapper(TblCountryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblCountry> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TblCountry::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSites()), TblCountry::getSites, bo.getSites());
        return lqw;
    }

    /**
     * 新增乡镇管理
     */
    @Override
    public Boolean insertByBo(TblCountryBo bo) {
        TblCountry add = MapstructUtils.convert(bo, TblCountry.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改乡镇管理
     */
    @Override
    public Boolean updateByBo(TblCountryBo bo) {
        TblCountry update = MapstructUtils.convert(bo, TblCountry.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除乡镇管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
