package com.fuyuanshen.app.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyuanshen.app.domain.AppDeviceVoice;
import com.fuyuanshen.app.domain.bo.AppDeviceVoiceBo;
import com.fuyuanshen.app.mapper.AppDeviceVoiceMapper;
import com.fuyuanshen.app.service.IAppDeviceVoiceService;
import com.fuyuanshen.common.core.utils.StringUtils;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
import com.fuyuanshen.common.oss.core.OssClient;
import com.fuyuanshen.common.oss.entity.UploadResult;
import com.fuyuanshen.common.oss.factory.OssFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 设备语音Service业务层处理
 *
 * @author Lion Li
 * @date 2025-07-02
 */
@RequiredArgsConstructor
@Service
public class AppDeviceVoiceServiceImpl implements IAppDeviceVoiceService {

    private final AppDeviceVoiceMapper baseMapper;

    /**
     * 查询设备语音
     */
    @Override
    public AppDeviceVoice queryById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 查询设备语音列表
     */
    @Override
    public TableDataInfo<AppDeviceVoice> queryPageList(AppDeviceVoiceBo bo, PageQuery pageQuery) {
        Page<AppDeviceVoice> page = baseMapper.selectPage(bo, pageQuery);
        return TableDataInfo.build(page);
    }

    /**
     * 查询设备语音列表
     */
    @Override
    public List<AppDeviceVoice> queryList(AppDeviceVoiceBo bo) {
        return baseMapper.selectList(bo);
    }

    /**
     * 根据新增业务对象插入设备语音
     */
    @Override
    public Boolean insertByBo(AppDeviceVoiceBo bo, MultipartFile file) {
        // 上传文件到MinIO
        String videoUrl = "";
        String coverUrl = "";
        Long size = 0L;
        Integer duration = 0;
        String type = "";
        
        if (file != null && !file.isEmpty()) {
            try {
                OssClient storage = OssFactory.instance();
                String suffix = "";
                String originalFilename = file.getOriginalFilename();
                if (originalFilename != null && originalFilename.lastIndexOf(".") > 0) {
                    suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
                }
                UploadResult result = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
                videoUrl = result.getUrl();
                size = file.getSize();
                type = file.getContentType();
                // TODO: 可以通过其他方式获取音频时长，这里暂时设置为默认值
                duration = 0;
                // TODO: 可以生成封面图，这里暂时设置为空
                coverUrl = "";
            } catch (Exception e) {
                // 文件上传失败处理
                throw new RuntimeException("文件上传失败: " + e.getMessage());
            }
        }

        AppDeviceVoice add = new AppDeviceVoice();
        add.setVideoName(bo.getVideoName());
        add.setVideoUrl(videoUrl);
        add.setDeviceId(bo.getDeviceId());
        add.setRemark(bo.getRemark());
        add.setType(type);
        add.setDuration(duration);
        add.setSize(size);
        add.setCover(coverUrl);
        return baseMapper.insert(add) > 0;
    }

    /**
     * 根据编辑业务对象更新设备语音
     */
    @Override
    public Boolean updateByBo(AppDeviceVoiceBo bo) {
        AppDeviceVoice update = new AppDeviceVoice();
        update.setId(bo.getId());
        update.setVideoName(bo.getVideoName());
        update.setDeviceId(bo.getDeviceId());
        update.setRemark(bo.getRemark());
        return baseMapper.updateById(update) > 0;
    }


    /**
     * 校验并批量删除设备语音信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        // TODO: 可以在这里添加删除MinIO文件的逻辑
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}