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.HotBo;
import org.dromara.system.domain.vo.HotVo;
import org.dromara.system.domain.Hot;
import org.dromara.system.mapper.HotMapper;
import org.dromara.system.service.IHotService;

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

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

    private final HotMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<Hot> buildQueryWrapper(HotBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Hot> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getHotWide()), Hot::getHotWide, bo.getHotWide());
        lqw.eq(StringUtils.isNotBlank(bo.getHotHigh()), Hot::getHotHigh, bo.getHotHigh());
        lqw.eq(StringUtils.isNotBlank(bo.getHotStar()), Hot::getHotStar, bo.getHotStar());
        lqw.eq(StringUtils.isNotBlank(bo.getHotEnd()), Hot::getHotEnd, bo.getHotEnd());
        lqw.like(StringUtils.isNotBlank(bo.getHotName()), Hot::getHotName, bo.getHotName());
        lqw.eq(StringUtils.isNotBlank(bo.getHotTitle()), Hot::getHotTitle, bo.getHotTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getHotDesc()), Hot::getHotDesc, bo.getHotDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getHotUrl()), Hot::getHotUrl, bo.getHotUrl());
        return lqw;
    }

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

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

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

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