package com.njzscloud.supervisory.voicebox.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.voicebox.mapper.CloudVoiceboxMapper;
import com.njzscloud.supervisory.voicebox.pojo.entity.CloudVoiceboxEntity;
import com.njzscloud.supervisory.voicebox.pojo.param.NotifyParam;
import com.njzscloud.supervisory.voicebox.pojo.param.SearchCloudVoiceboxParam;
import com.njzscloud.supervisory.voicebox.pojo.result.NotifyResult;
import com.njzscloud.supervisory.voicebox.pojo.result.SearchCloudVoiceboxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 云音箱
 */
@Slf4j
@Service
public class CloudVoiceboxService extends ServiceImpl<CloudVoiceboxMapper, CloudVoiceboxEntity> implements IService<CloudVoiceboxEntity> {

    @Autowired(required = false)
    private VoiceboxApi voiceboxApi;

    /**
     * 新增
     */
    public void add(CloudVoiceboxEntity cloudVoiceboxEntity) {
        this.save(cloudVoiceboxEntity);
    }

    /**
     * 修改
     */
    public void modify(CloudVoiceboxEntity cloudVoiceboxEntity) {
        this.updateById(cloudVoiceboxEntity);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    public CloudVoiceboxEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<SearchCloudVoiceboxResult> paging(PageParam pageParam, SearchCloudVoiceboxParam searchCloudVoiceboxParam) {
        String sn = searchCloudVoiceboxParam.getSn();
        String nickname = searchCloudVoiceboxParam.getNickname();
        return PageResult.of(baseMapper.paging(pageParam.toPage(), Wrappers.<CloudVoiceboxEntity>query()
                .eq(StrUtil.isNotBlank(sn), "a.sn", sn)
                .and(StrUtil.isNotBlank(nickname), it -> it.like("c.username", sn)
                        .or().like("b.nickname", nickname))
        ));
    }

    public void play(Long userId, String message, Object... param) {
        if (voiceboxApi == null) {
            return;
        }
        CloudVoiceboxEntity cloudVoiceboxEntity = this.getOne(Wrappers.<CloudVoiceboxEntity>lambdaQuery().eq(CloudVoiceboxEntity::getUserId, userId));
        if (cloudVoiceboxEntity == null) {
            log.warn("当前用户未配置云音箱：{}", userId);
            return;
        }
        try {
            NotifyResult notify = voiceboxApi.notify(new NotifyParam()
                    .setId(cloudVoiceboxEntity.getSn())
                    .setMessage(StrUtil.format(message, param))
            );
            log.info("播放结果:{}", JSON.toJSONString(notify));
        } catch (Exception e) {
            log.error("云音箱播放失败", e);
        }
    }
}
