package com.fs.his.service.impl;

import java.util.Collections;
import java.util.List;

import com.fs.aiSoundReplication.param.TtsRequest;
import com.fs.aiSoundReplication.service.TtsService;
import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.fastgptApi.vo.AudioVO;
import com.fs.huifuPay.sdk.opps.core.utils.StringUtil;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsProjectMapper;
import com.fs.his.domain.FsProject;
import com.fs.his.service.IFsProjectService;

/**
 * 理疗配置Service业务层处理
 * 
 * @author fs
 * @date 2026-04-19
 */
@Service
public class FsProjectServiceImpl extends ServiceImpl<FsProjectMapper, FsProject> implements IFsProjectService {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FsProjectMapper fsProjectMapper;

    @Autowired
    private TtsService ttsService;
    /**
     * 查询理疗配置
     * 
     * @param id 理疗配置主键
     * @return 理疗配置
     */
    @Override
    public FsProject selectFsProjectById(Long id)
    {
        return baseMapper.selectFsProjectById(id);
    }

    /**
     * 查询理疗配置列表
     * 
     * @param fsProject 理疗配置
     * @return 理疗配置
     */
    @Override
    public List<FsProject> selectFsProjectList(FsProject fsProject)
    {
        return baseMapper.selectFsProjectList(fsProject);
    }

    /**
     * 新增理疗配置
     * 
     * @param fsProject 理疗配置
     * @return 结果
     */
    @Override
    public int insertFsProject(FsProject fsProject)
    {
        fsProject.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFsProject(fsProject);
    }

    /**
     * 修改理疗配置
     * 
     * @param fsProject 理疗配置
     * @return 结果
     */
    @Override
    public int updateFsProject(FsProject fsProject)
    {
        fsProject.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateFsProject(fsProject);
    }

    /**
     * 批量删除理疗配置
     * 
     * @param ids 需要删除的理疗配置主键
     * @return 结果
     */
    @Override
    public int deleteFsProjectByIds(Long[] ids)
    {
        return baseMapper.deleteFsProjectByIds(ids);
    }

    /**
     * 删除理疗配置信息
     * 
     * @param id 理疗配置主键
     * @return 结果
     */
    @Override
    public int deleteFsProjectById(Long id)
    {
        return baseMapper.deleteFsProjectById(id);
    }

    @Override
    public List<FsProject> selectFsProjectListOptions() {
        return fsProjectMapper.selectFsProjectListOptions();
    }

    @Override
    public List<FsProject> selectFsProjectListOptionsById(Long cateId) {
        return fsProjectMapper.selectFsProjectListOptionsByCateId(cateId);
    }

    @Override
    public void generateSound(FsProject fsProject) {
        try {
            StringBuilder buffer = new StringBuilder();
            if (!StringUtil.isEmpty(fsProject.getFirstField())) {
                buffer.append(fsProject.getFirstField()).append(": ").append(Jsoup.parse(fsProject.getFirstContent()).text());
            }
            if (!StringUtil.isEmpty(fsProject.getSecondField())) {
                buffer.append(fsProject.getSecondField()).append(": ").append(Jsoup.parse(fsProject.getSecondContent()).text());
            }
            if (!StringUtil.isEmpty(fsProject.getThirdField())) {
                buffer.append(fsProject.getThirdField()).append(": ").append(Jsoup.parse(fsProject.getThirdContent()).text());
            }
            if (!StringUtil.isEmpty(fsProject.getFourthField())) {
                buffer.append(fsProject.getFourthField()).append(": ").append(Jsoup.parse(fsProject.getFourthContent()).text());
            }
            String text = buffer.toString();
            logger.info("文字数据: {}",text);
            TtsRequest ttsRequest = new TtsRequest(null,null,null,text);
            AudioVO audioVO = ttsService.textToSound(ttsRequest);
            FsProject map = new FsProject();
            map.setId(fsProject.getId());
            map.setSoundUrl(audioVO.getWavUrl());
            fsProjectMapper.updateFsProject(map);
        } catch (Exception e) {
            logger.info("理疗生成音频失败，理疗id：{}，失败原因：{}",fsProject.getId(),e.getMessage());
        }
    }
}
