package com.seer.teach.iot.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seer.teach.iot.admin.convert.AdminAdvertisementConvert;
import com.seer.teach.iot.admin.req.SaveAdvertisementReq;
import com.seer.teach.iot.admin.service.AdminAdvertisementService;
import com.seer.teach.common.service.CommonFileService;
import com.seer.teach.iot.module.entity.AdvertisingEntity;
import com.seer.teach.iot.service.AdvertisingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

/**
 *
 * <p>
 *
 * </p >
 *
 * @author Captain
 * @since 2025-09-18 11:51
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminAdvertisementServiceImpl implements AdminAdvertisementService {

    private final AdvertisingService advertisingService;

    private final CommonFileService commonFileService;

    /**
     * 获取空的字符串列表
     *
     * @return 返回一个空的不可变字符串列表
     */
    @Override
    public List<String> list() {
        List<AdvertisingEntity> list = advertisingService.list(new LambdaQueryWrapper<AdvertisingEntity>()
                .orderByAsc(AdvertisingEntity::getSort));
        return list.stream().map(AdvertisingEntity::getUrl).collect(Collectors.toList());
    }

    /**
     * 上传广告图片文件
     *
     * @param file 要上传的图片文件，类型为MultipartFile
     * @return 返回上传后的文件访问路径或标识符
     */
    @Override
    public String upload(MultipartFile file) {
        return commonFileService.uploadAdvertisingPicture(file);
    }

    /**
     * 保存广告信息
     *
     * @param params 广告保存请求参数列表，包含广告的相关信息
     */
    @Override
    public void save(List<SaveAdvertisementReq> params) {
        List<AdvertisingEntity> list = AdminAdvertisementConvert.INSTANCE.toAdvertisementList(params);
        List<AdvertisingEntity> oldData = advertisingService.list();
        advertisingService.removeBatchByIds(oldData);
        advertisingService.saveOrUpdateBatch(list);
    }



}
