package com.zsElectric.boot.business.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
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.zsElectric.boot.business.mapper.BannerInfoMapper;
import com.zsElectric.boot.business.service.BannerInfoService;
import com.zsElectric.boot.business.model.entity.BannerInfo;
import com.zsElectric.boot.business.model.form.BannerInfoForm;
import com.zsElectric.boot.business.model.query.BannerInfoQuery;
import com.zsElectric.boot.business.model.vo.BannerInfoVO;
import com.zsElectric.boot.business.converter.BannerInfoConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 小程序banner图服务实现类
 *
 * @author wzq
 * @since 2025-11-26 15:59
 */
@Service
@RequiredArgsConstructor
public class BannerInfoServiceImpl extends ServiceImpl<BannerInfoMapper, BannerInfo> implements BannerInfoService {

    private final BannerInfoConverter bannerInfoConverter;

    /**
    * 获取小程序banner图分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<BannerInfoVO>} 小程序banner图分页列表
    */
    @Override
    public IPage<BannerInfoVO> getBannerInfoPage(BannerInfoQuery queryParams) {
        Page<BannerInfoVO> pageVO = this.baseMapper.getBannerInfoPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取小程序banner图表单数据
     *
     * @param id 小程序banner图ID
     * @return 小程序banner图表单数据
     */
    @Override
    public BannerInfoForm getBannerInfoFormData(Long id) {
        BannerInfo entity = this.getById(id);
        return bannerInfoConverter.toForm(entity);
    }
    
    /**
     * 新增小程序banner图
     *
     * @param formData 小程序banner图表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveBannerInfo(BannerInfoForm formData) {
        BannerInfo entity = bannerInfoConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新小程序banner图
     *
     * @param id   小程序banner图ID
     * @param formData 小程序banner图表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateBannerInfo(Long id,BannerInfoForm formData) {
        BannerInfo entity = bannerInfoConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除小程序banner图
     *
     * @param ids 小程序banner图ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteBannerInfos(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的小程序banner图数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
