<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;

use app\common\library\Token;
use think\Db;
use app\common\model\UserDeviceInfo;
use onlogin\onlogin;

use addons\epay\library\Service;
use app\common\library\Wechat;

use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Faceid\V20180301\FaceidClient;
use TencentCloud\Faceid\V20180301\Models\IdCardVerificationRequest;
use TencentCloud\Iai\V20200303\IaiClient;
use TencentCloud\Iai\V20200303\Models\CompareFaceRequest;

/**
 * 女性设置自己的打招呼
 */
class Usergreet extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';


    //添加打招呼内容,女性使用
    public function addgreetcontent() {
        $type = input('type', 0, 'intval'); //类型:0=文字,1=语音,2=相册
        $title = input('title', '', 'trim'); //标题(type=1必传)
        $content = input('content', '', 'trim'); //内容
        $duration = input('duration', 0, 'intval'); //时长(type=1必传)

        if (!in_array($type, [0, 1, 2])) {
            $this->error('您的网络开小差了');
        }
        if ($content === '') {
            $this->error('请设置打招呼内容');
        }
        if (iconv_strlen($content, 'utf-8') > 255) {
            $this->error('打招呼内容太长了');
        }
        if ($type == 1) {
            if ($title === '') {
                $this->error('请输入语音名称');
            }
            if (iconv_strlen($title, 'utf-8') > 50) {
                $this->error('语音名称太长了');
            }
            if (!$duration) {
                $this->error('参数缺失');
            }
        }

        $count = Db::name('user_greet_content')->where(['user_id' => $this->auth->id, 'type' => $type])->count('id');
        if ($count > 10) {
            $this->error('同一类型打招呼最多设置10条');
        }

        $data['user_id'] = $this->auth->id;
        $data['type'] = $type;
        $data['content'] = $content;
        if ($type == 1) {
            $data['title'] = $title;
            $data['duration'] = $duration;
        }
        $data['createtime'] = time();
        //查询是否有默认打招呼内容
        $default_greet_count = Db::name('user_greet_content')->where(['user_id' => $this->auth->id, 'is_default' => 1])->count('id');
        if (!$default_greet_count) {
            $data['is_default'] = 1;
        }

        $rs = Db::name('user_greet_content')->insertGetId($data);
        if (!$rs) {
            $this->error('您的网络开小差了');
        }

        $this->success('设置成功');
    }

    //查询打招呼内容,女性使用
    public function getgreetcontent() {
        $type = input('type', 0, 'intval'); //类型:0=文字,1=语音,2=相册,3=所有招呼
        if (!in_array($type, [0, 1, 2, 3])) {
            $this->error('您的网络开小差了');
        }

        if ($type == 3) {
            $list = Db::name('user_greet_content')->where(function ($query) {
                $query->where('user_id', $this->auth->id)->where('type', 'neq', 1);
            })->whereOr(function ($query) {
                $query->where('user_id', $this->auth->id)->where('type', 1)->where('is_default', 1);
            })->select();
        } else {
            $where['user_id'] = $this->auth->id;
            $where['type'] = $type;

            $list = Db::name('user_greet_content')->where($where)->select();
        }
        if ($list) {
            foreach ($list as &$v) {
                if ($v['type'] != 0) {
                    $v['content'] = one_domain_image($v['content']);
                }
            }
        }

        $this->success('打招呼内容', $list);
    }
    
    //设置默认打招呼内容,女性使用
    public function setdefaultgreet() {
        $id = input('id', 0, 'intval'); //打招呼id
        if (!$id) {
            $this->error('您的网络开小差了');
        }

        $count = Db::name('user_greet_content')->where(['user_id' => $this->auth->id, 'id' => $id])->count('id');
        if (!$count) {
            $this->error('您的网络开小差了');
        }

        //开启事务
        Db::startTrans();
        //清除之前默认
        $rs = Db::name('user_greet_content')->where(['user_id' => $this->auth->id])->setField('is_default', 0);
        if ($rs === false) {
            Db::rollback();
            $this->error('您的网络开小差了');
        }
        //设置
        $rt = Db::name('user_greet_content')->where(['user_id' => $this->auth->id, 'id' => $id])->setField('is_default', 1);
        if ($rt === false) {
            Db::rollback();
            $this->error('您的网络开小差了');
        }

        Db::commit();
        $this->success('设置成功');
    }

    //删除打招呼内容,女性使用
    public function deldefaultgreet() {
        $id = input('id', 0, 'intval'); //打招呼id
        if (!$id) {
            $this->error('您的网络开小差了');
        }

        $mt_user_greet_content = Db::name('user_greet_content');
        $info = $mt_user_greet_content->where(['user_id' => $this->auth->id, 'id' => $id])->find();
        if (!$info) {
            $this->error('您的网络开小差了');
        }

        //开启事务
        Db::startTrans();
        //删除
        $rs = $mt_user_greet_content->where(['user_id' => $this->auth->id, 'id' => $id])->delete();
        if ($rs === false) {
            Db::rollback();
            $this->error('您的网络开小差了');
        }
        //若为默认打招呼内容则重新设置一条
        if ($info['is_default'] == 1) {
            $first_greet = $mt_user_greet_content->where(['user_id' => $this->auth->id])->find();
            if ($first_greet) {
                $rt = $mt_user_greet_content->where(['user_id' => $this->auth->id, 'id' => $first_greet['id']])->setField('is_default', 1);
                if ($rt === false) {
                    Db::rollback();
                    $this->error('您的网络开小差了');
                }
            }
        }

        Db::commit();
        $this->success('删除成功');
    }
}