package com.ruoyi.export.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.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 com.ruoyi.export.domain.bo.ExportItemBo;
import com.ruoyi.export.domain.vo.ExportItemVo;
import com.ruoyi.export.domain.ExportItem;
import com.ruoyi.export.mapper.ExportItemMapper;
import com.ruoyi.export.service.IExportItemService;

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

/**
 * 展示品Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-17
 */
@RequiredArgsConstructor
@Service
public class ExportItemServiceImpl implements IExportItemService {

    private final ExportItemMapper baseMapper;

    /**
     * 查询展示品
     */
    @Override
    public ExportItemVo queryById(Long itemId){
        return baseMapper.selectVoById(itemId);
    }

    /**
     * 查询展示品列表
     */
    @Override
    public TableDataInfo<ExportItemVo> queryPageList(ExportItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExportItem> lqw = buildQueryWrapper(bo);
        Page<ExportItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询展示品列表
     */
    @Override
    public List<ExportItemVo> queryList(ExportItemBo bo) {
        LambdaQueryWrapper<ExportItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ExportItem> buildQueryWrapper(ExportItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExportItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCategoryId() != null, ExportItem::getCategoryId, bo.getCategoryId());
        lqw.eq(StringUtils.isNotBlank(bo.getLangCode()), ExportItem::getLangCode, bo.getLangCode());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), ExportItem::getItemName, bo.getItemName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), ExportItem::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), ExportItem::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getAccessory()), ExportItem::getAccessory, bo.getAccessory());
        lqw.eq(StringUtils.isNotBlank(bo.getLinkUrl()), ExportItem::getLinkUrl, bo.getLinkUrl());
        lqw.eq(bo.getItemType() != null, ExportItem::getItemType, bo.getItemType());
        lqw.eq(bo.getSortOrder() != null, ExportItem::getSortOrder, bo.getSortOrder());
        lqw.eq(bo.getIsEnabled() != null, ExportItem::getIsEnabled, bo.getIsEnabled());
        return lqw;
    }

    /**
     * 新增展示品
     */
    @Override
    public Boolean insertByBo(ExportItemBo bo) {
        ExportItem add = BeanUtil.toBean(bo, ExportItem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setItemId(add.getItemId());
        }
        return flag;
    }

    /**
     * 修改展示品
     */
    @Override
    public Boolean updateByBo(ExportItemBo bo) {
        ExportItem update = BeanUtil.toBean(bo, ExportItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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