<?php namespace Source\Lib;
use Dever;
class Content
{
    public function update($data)
    {
        Dever::db('info_data', 'source')->update(array('id' => $data['info_data_id']), array('info_data_content_id' => $data['id']));
        if (isset($data['ai_platform'])) {
            $info = '更新智能生成';
            if ($data['ai_platform']) {
                if (is_string($data['ai_platform'])) {
                    $data['ai_platform'] = explode(',', $data['ai_platform']);
                }
                $platform = Dever::db('platform', 'ai')->find($data['ai_platform'][0]);
                if ($data['ai_platform'][1] == 2) {
                    $platform_type = '智能体';
                    $platform_child = Dever::db('agents', 'ai')->find($data['ai_platform'][2]);
                } else {
                    $platform_type = '模型';
                    $platform_child = Dever::db('model', 'ai')->find($data['ai_platform'][2]);
                }
                $info .= '，智能平台：' . $platform['name'] . '-' . $platform_type . '-' . $platform_child['name'];
            }
            $info .= ' / 提示词类型：' . ($data['ai_prompt_type'] == 1 ? '通用提示词' : '自定义');
            if ($data['ai_prompt_type'] == 1) {
                if (is_string($data['ai_prompt'])) {
                    $data['ai_prompt'] = explode(',', $data['ai_prompt']);
                }
                $prompt = Dever::db('prompt', 'ai')->find($data['ai_prompt'][1]);
                $info .= ' / 提示词：' . $prompt['name'];
            } else {
                $info .= ' / 提示词：' . $data['prompt'];
            }
            
            Dever::load('log', 'source')->add($data['info_data_id'], $info);
            # 更改当前素材状态
            Dever::db('info_data_content', 'source')->update(array('id' => $data['id']), array('status' => 2));
            # 提交生成任务
            \Dever\Helper\Cmd::run('task.create', array('id' => $data['id'], 'authorization' => \Dever\Helper\Str::encode(\Dever\Helper\Env::header('authorization'))), 'source');
            //$this->create($data['id']);
        }
        if (isset($data['file'])) {
            $info = '手动修改文案';
            if ($data['file']) {
                $info = '，更新文件：' . $data['file'];
            }
            if ($data['content']) {
                $info = '，更新文案';
            }
            Dever::load('log', 'source')->add($data['info_data_id'], $info);
            # 更改当前素材状态
            Dever::db('info_data_content', 'source')->update(array('id' => $data['id']), array('status' => 3));
        }
        
        return $data;
    }

    public function create($id)
    {
        $data = Dever::db('info_data_content', 'source')->find(array('id' => $id));
        if ($data && $data['ai_platform']) {
            $data['ai_platform'] = explode(',', $data['ai_platform']);
            if ($data['ai_platform'][1] == 2) {
                $method = 'agents';
                $platform_child = Dever::db('agents', 'ai')->find($data['ai_platform'][2]);
            } else {
                $method = 'model';
                $platform_child = Dever::db('model', 'ai')->find($data['ai_platform'][2]);
            }

            if ($data['ai_prompt_type'] == 1) {
                $data['ai_prompt'] = explode(',', $data['ai_prompt']);
                $prompt = Dever::db('prompt', 'ai')->find($data['ai_prompt'][1]);
                $content = $prompt['positive'];
            } else {
                $content = $data['prompt'];
            }

            $content .= '。请直接回复生成的内容，文章内容如下：';
            $info_data = Dever::db('info_data', 'source')->find(array('id' => $data['info_data_id']));

            # 文章
            if ($info_data['relation'] == 1) {
                $info = Dever::db('info', 'source')->find(array('id' => $data['info_id']));
                $content .= $info['content'];
            } else {
                $value = Dever::db('info_value', 'source')->find(array('id' => $info_data['relation_id']));
                $content .= $value['content'];
            }

            $msg = array();
            $msg[] = array
            (
                'role' => 'user',
                'content' => $content,
            );

            $content = Dever::load($method . '/chat', 'ai')->init($platform_child['id'])->completions($msg);

            if ($content) {
                $content = str_replace("\n", '<br />', $content);
                Dever::db('info_data_content', 'source')->update(array('id' => $data['id']), array('status' => 3, 'content' => $content));
            }
        }
        return 'ok';
    }
}