package com.starry.admin.modules.shop.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.shop.mapper.PlayShopArticleInfoMapper;
import com.starry.admin.modules.shop.module.entity.PlayShopArticleInfoEntity;
import com.starry.admin.modules.shop.module.vo.PlayShopArticleInfoQueryVo;
import com.starry.admin.modules.shop.service.IPlayShopArticleInfoService;
import com.starry.common.utils.IdUtils;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * 店铺首页轮播Service业务层处理
 *
 * @author admin
 * @since 2024-06-05
 */
@Service
public class PlayShopArticleInfoServiceImpl extends ServiceImpl<PlayShopArticleInfoMapper, PlayShopArticleInfoEntity>
        implements
            IPlayShopArticleInfoService {

    @Override
    public PlayShopArticleInfoEntity selectByType(String type) {
        LambdaQueryWrapper<PlayShopArticleInfoEntity> lambdaWrapper = new LambdaQueryWrapper<>();
        lambdaWrapper.eq(PlayShopArticleInfoEntity::getArticleType, type);
        return this.baseMapper.selectOne(lambdaWrapper);
    }

    /**
     * 查询店铺首页轮播
     *
     * @param id
     *            店铺首页轮播主键
     * @return 店铺首页轮播
     */
    @Override
    public PlayShopArticleInfoEntity selectById(String id) {
        PlayShopArticleInfoEntity entity = this.baseMapper.selectById(id);
        if (entity == null) {
            throw new CustomException("对象不存在");
        }
        return entity;
    }

    @Override
    public List<PlayShopArticleInfoEntity> selectByList() {
        return this.baseMapper.selectList(new LambdaQueryWrapper<>());

    }

    @Override
    public IPage<PlayShopArticleInfoEntity> selectByPage(PlayShopArticleInfoQueryVo vo) {
        LambdaQueryWrapper<PlayShopArticleInfoEntity> lambdaWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(vo.getId())) {
            lambdaWrapper.eq(PlayShopArticleInfoEntity::getId, vo.getId());
        }
        if (StrUtil.isNotBlank(vo.getArticleType())) {
            lambdaWrapper.eq(PlayShopArticleInfoEntity::getArticleType, vo.getArticleType());
        }
        if (StrUtil.isNotBlank(vo.getArticleTitle())) {
            lambdaWrapper.like(PlayShopArticleInfoEntity::getArticleTitle, vo.getArticleTitle());
        }
        return this.baseMapper.selectPage(new Page<>(vo.getPageNum(), vo.getPageSize()), lambdaWrapper);
    }

    /**
     * 新增店铺首页轮播
     *
     * @param articleInfoEntity
     *            店铺首页轮播
     * @return 结果
     */
    @Override
    public boolean create(PlayShopArticleInfoEntity articleInfoEntity) {
        if (StrUtil.isBlankIfStr(articleInfoEntity.getId())) {
            articleInfoEntity.setId(IdUtils.getUuid());
        }
        return save(articleInfoEntity);
    }

    /**
     * 修改店铺首页轮播
     *
     * @param articleInfoEntity
     *            店铺首页轮播
     * @return 结果
     */
    @Override
    public boolean update(PlayShopArticleInfoEntity articleInfoEntity) {
        return updateById(articleInfoEntity);
    }

    /**
     * 批量删除店铺首页轮播
     *
     * @param ids
     *            需要删除的店铺首页轮播主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除店铺首页轮播信息
     *
     * @param id
     *            店铺首页轮播主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return this.baseMapper.deleteById(id);
    }
}
