package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.ZsColumnBo;
import com.ruoyi.system.domain.vo.ZsColumnVo;
import com.ruoyi.system.domain.ZsColumn;
import com.ruoyi.system.mapper.ZsColumnMapper;
import com.ruoyi.system.service.IZsColumnService;

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

/**
 * 站点栏目管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-29
 */
@RequiredArgsConstructor
@Service
public class ZsColumnServiceImpl implements IZsColumnService {

    private final ZsColumnMapper baseMapper;

    /**
     * 查询站点栏目管理
     */
    @Override
    public ZsColumnVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询站点栏目管理列表
     */
    @Override
    public List<ZsColumnVo> queryList(ZsColumnBo bo) {
        LambdaQueryWrapper<ZsColumn> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZsColumn> buildQueryWrapper(ZsColumnBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsColumn> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getWebsiteName()), ZsColumn::getWebsiteName, bo.getWebsiteName());
        lqw.eq(StringUtils.isNotBlank(bo.getLanguageCode()), ZsColumn::getLanguageCode, bo.getLanguageCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ZsColumn::getName, bo.getName());
        lqw.eq(bo.getStatus() != null, ZsColumn::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增站点栏目管理
     */
    @Override
    public Boolean insertByBo(ZsColumnBo bo) {
        ZsColumn add = BeanUtil.toBean(bo, ZsColumn.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改站点栏目管理
     */
    @Override
    public Boolean updateByBo(ZsColumnBo bo) {
        ZsColumn update = BeanUtil.toBean(bo, ZsColumn.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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