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.AdvertisingMapper;
import com.zsElectric.boot.business.service.AdvertisingService;
import com.zsElectric.boot.business.model.entity.Advertising;
import com.zsElectric.boot.business.model.form.AdvertisingForm;
import com.zsElectric.boot.business.model.query.AdvertisingQuery;
import com.zsElectric.boot.business.model.vo.AdvertisingVO;
import com.zsElectric.boot.business.converter.AdvertisingConverter;

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;

/**
 * 广告管理服务实现类
 *
 * @author wzq
 * @since 2025-11-26 15:57
 */
@Service
@RequiredArgsConstructor
public class AdvertisingServiceImpl extends ServiceImpl<AdvertisingMapper, Advertising> implements AdvertisingService {

    private final AdvertisingConverter advertisingConverter;

    /**
    * 获取广告管理分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<AdvertisingVO>} 广告管理分页列表
    */
    @Override
    public IPage<AdvertisingVO> getAdvertisingPage(AdvertisingQuery queryParams) {
        Page<AdvertisingVO> pageVO = this.baseMapper.getAdvertisingPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取广告管理表单数据
     *
     * @param id 广告管理ID
     * @return 广告管理表单数据
     */
    @Override
    public AdvertisingForm getAdvertisingFormData(Long id) {
        Advertising entity = this.getById(id);
        return advertisingConverter.toForm(entity);
    }
    
    /**
     * 新增广告管理
     *
     * @param formData 广告管理表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveAdvertising(AdvertisingForm formData) {
        Advertising entity = advertisingConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新广告管理
     *
     * @param id   广告管理ID
     * @param formData 广告管理表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateAdvertising(Long id,AdvertisingForm formData) {
        Advertising entity = advertisingConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除广告管理
     *
     * @param ids 广告管理ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteAdvertisings(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的广告管理数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
