package com.backendsys.service.Ai.Aiivh;

import com.backendsys.entity.Ai.Aiivh.AiPersonDTO;
import com.backendsys.entity.Ai.Aiivh.AiPersonTimbreCustomDTO;
import com.backendsys.entity.Ai.Aiivh.AiivhAnchorDTO;
import com.backendsys.exception.CustException;
import com.backendsys.mapper.Ai.Aiivh.AiivhMakeVirtualmanTaskMapper;
import com.backendsys.utils.response.PageInfoResult;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AiivhMakeVirtualmanTaskServiceImpl implements AiivhMakeVirtualmanTaskService {

    @Autowired
    private AiivhMakeVirtualmanTaskMapper aiivhMakeVirtualmanTaskMapper;

    @Override
    public Map<String, Object> queryAiivhMakeVirtualmanTaskList(Integer pageNum, Integer pageSize, AiPersonDTO aiPersonDTO) {
        // 分页查询
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
            PageHelper.getLocalPage().setPageSizeZero(true);
        }
        // 分页输出 (自定义)
        List<Map<String, Object>> list = aiivhMakeVirtualmanTaskMapper.queryAiivhMakeVirtualmanTaskList(aiPersonDTO);

        PageInfoResult pageInfoResult = new PageInfoResult(list);
        return pageInfoResult.toMap();
    }

    @Override
    public Map<String, Object> queryAiivhMakeVirtualmanTaskDetail(AiPersonDTO aiPersonDTO) {

        Map<String, Object> response = aiivhMakeVirtualmanTaskMapper.queryAiivhMakeVirtualmanTaskDetail(aiPersonDTO);
        if (response == null) throw new CustException("找不到任务");

        String anchor_name_timestamp = (String) response.get("anchor_name");
        String[] parts = anchor_name_timestamp.split("__");
        response.put("anchor_name", parts[0]);

        return response;
    }

    @Override
    public Map<String, Object> insertAiivhMakeVirtualmanTask(AiPersonDTO aiPersonDTO) {
        aiivhMakeVirtualmanTaskMapper.insertAiivhMakeVirtualmanTask(aiPersonDTO);
        return Map.of("quota_code", aiPersonDTO.getQuota_code());
    }
    @Override
    public Map<String, Object> updateAiivhMakeVirtualmanTask(AiPersonDTO aiPersonDTO) {
        aiivhMakeVirtualmanTaskMapper.updateAiivhMakeVirtualmanTask(aiPersonDTO);
        return Map.of("task_id", aiPersonDTO.getTask_id());
    }

    /**
     * 创建自定义形象
     */
    @Override
    public Map<String, Object> insertAiivhAnchorCustom(AiivhAnchorDTO aiivhAnchorDTO) {
        aiivhMakeVirtualmanTaskMapper.insertAiPersonVirtualmanCustom(aiivhAnchorDTO);
        return Map.of("anchor_name", aiivhAnchorDTO.getAnchor_name());
    }
    /**
     * 更新自定义形象
     */
    @Override
    public Map<String, Object> updateAiPersoneVirtualmanCustom(AiivhAnchorDTO aiivhAnchorDTO) {
        aiivhMakeVirtualmanTaskMapper.updateAiPersonVirtualmanCustom(aiivhAnchorDTO);
        return Map.of("anchor_name", aiivhAnchorDTO.getAnchor_name());
    }

    /**
     * 创建自定义形象
     */
    @Override
    public Map<String, Object> insertAiPersoneTimbreCustom(AiPersonTimbreCustomDTO aiPersonTimbreCustomDTO) {
        aiivhMakeVirtualmanTaskMapper.insertAiPersonTimbreCustom(aiPersonTimbreCustomDTO);
        return Map.of("timbre_name", aiPersonTimbreCustomDTO.getTimbre_name());
    }
    /**
     * 更新自定义形象
     */
    @Override
    public Map<String, Object> updateAiPersoneTimbreCustom(AiPersonTimbreCustomDTO aiPersonTimbreCustomDTO) {
        aiivhMakeVirtualmanTaskMapper.updateAiPersonTimbreCustom(aiPersonTimbreCustomDTO);
        return Map.of("timbre_name", aiPersonTimbreCustomDTO.getTimbre_name());
    }


}
