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.SiteSiteBo;
import org.dromara.system.domain.vo.SiteSiteVo;
import org.dromara.system.domain.SiteSite;
import org.dromara.system.mapper.SiteSiteMapper;
import org.dromara.system.service.ISiteSiteService;

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

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2024-10-31
 */
@RequiredArgsConstructor
@Service
public class SiteSiteServiceImpl implements ISiteSiteService {

    private final SiteSiteMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<SiteSite> buildQueryWrapper(SiteSiteBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SiteSite> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getSiteName()), SiteSite::getSiteName, bo.getSiteName());
        lqw.eq(StringUtils.isNotBlank(bo.getSiteTelnum()), SiteSite::getSiteTelnum, bo.getSiteTelnum());
        lqw.eq(StringUtils.isNotBlank(bo.getSiteAddress()), SiteSite::getSiteAddress, bo.getSiteAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getSiteGps()), SiteSite::getSiteGps, bo.getSiteGps());
        lqw.eq(StringUtils.isNotBlank(bo.getSiteManager()), SiteSite::getSiteManager, bo.getSiteManager());
        lqw.eq(StringUtils.isNotBlank(bo.getSiteLogo()), SiteSite::getSiteLogo, bo.getSiteLogo());
        lqw.eq(bo.getCreatedAt() != null, SiteSite::getCreatedAt, bo.getCreatedAt());
        lqw.eq(bo.getUpdatedAt() != null, SiteSite::getUpdatedAt, bo.getUpdatedAt());
        return lqw;
    }

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

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

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

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