<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2017/12/7
 * Time: 13:04
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
use RedisDB;
use Config;

class MessageApiController extends Controller
{
    public function Entrance(Request $request, $id ){
        //统一入口
        $this->$id($request, $id);
    }

    private function Export($errcode=0,$errmsg='成功',$data=''){
        echo json_encode(['errcode'=>$errcode,'errmsg'=>$errmsg,'data'=>$data]);
        exit();
    }

    //删除消息模板
    public function deletetemplate($request){
        $data = $request->input();
        try{
            $tid = isset($data['id']) && $data['id'] ? intval($data['id']) : 0 ;
            $cnt = DB::connection('message');
            $bk = $cnt->table('lie_msg_tpl')->where("tpl_id",$tid)->update(['status'=>-1]);
            if($bk){
                $this->Export(0, '');
            }else{
                throw new \Exception('操作失败');
            }
        }catch(\Exception $e){
            $this->Export(1000, '操作失败');
        }

    }

    //新增/编辑 消息模板
    private function addTemplate($request)
    {
        $data = $request->input();
//        var_dump($data);die;

        $insert_tpl_id = $data['tpl_id'];

        //至少选择一个渠道
        if (empty($data['channels'])) {
            $this->Export(11001, '请勾选至少一个消息渠道');
        }
        if (empty($data['description'])) {
            $this->Export(11002, '请输入消息key');
        }
        if (empty($data['tpl_key'])) {
            $this->Export(11003, '请输入模板描述');
        }

        $is_dumplicated = DB::connection('message')->table('lie_msg_tpl')->where('description',$data['description'])->first();
        if(empty($insert_tpl_id))
        {
            if($is_dumplicated !== NULL)
            {
                $this->Export(11022, '已存在相同模板key,请更换key内容');
            }
        }else
        {
            $is_edit_dumplicated = DB::connection('message')->table('lie_msg_tpl')->where('description',$data['description'])->whereNotIn('tpl_id',[$insert_tpl_id])->first();
            if($is_edit_dumplicated !== NULL)
            {
                $this->Export(11023, '已存在相同模板key,请更换key内容');
            }
        }


        //添加事务 保证 数据库多条修改时 原子操作
        DB::connection('message')->transaction(function() use($data) {
            //消息模板模型
            $tpl_model = [];
            $tpl_model['tpl_id'] = $data['tpl_id'];
            $tpl_model['channels'] = $data['channels'];
            $tpl_model['description'] = $data['description'];
            $tpl_model['ex_str'] = $data['tpl_key'];
            $tpl_model['source_type'] = $data['source_type'];
            $tpl_model['creater'] = $data['creater'];
            $tpl_model['obj_user'] = $data['obj_user'];
            $tpl_model['msg_type'] = $data['msg_type'];

            if (empty($tpl_model['tpl_id'])) {
                $tpl_model['create_time'] = time();
                $tpl_model['update_time'] = time();
                $insert_tpl_id = DB::connection('message')->table('lie_msg_tpl')->insertGetId($tpl_model);
//                var_dump($insert_tpl_id);die;
                if (!$insert_tpl_id) {
                    $this->Export(11020, '新增消息模板失败');
                }
            } else {
                $tpl_model['update_time'] = time();
                $result = DB::connection('message')->table('lie_msg_tpl')->where('tpl_id', $tpl_model['tpl_id'])->update($tpl_model);
                $insert_tpl_id = $tpl_model['tpl_id'];
                if (!$result) {
                    $this->Export(11021, '编辑消息模板失败');
                }
            }

            //每一个渠道模板 的验证与写数据库
            //选了站内信渠道 就要选站内信类型
            if (strpos($data['channels'], '1') !== false) {
                if (empty($data['msg_type'])) {
                    $this->Export(11003, '请选择站内信类型');
                }
                if (empty($data['inner_title'])) {
                    $this->Export(11004, '请填写站内信标题');
                }
                if (empty($data['inner_content'])) {
                    $this->Export(11005, '请填写站内信内容');
                }
                if (empty($data['op_type'])) {
                    $this->Export(11006, '请选择点击消息操作');
                }
                if (empty($data['url']) && $data['op_type'] == 2) {
                    $this->Export(11007, '请填写要打开的url');
                }
                $chn_tpl_model_inner = [];
                $chn_tpl_model_inner['channel_tpl_id'] = $data['inner_channel_tpl_id'];
                $chn_tpl_model_inner['channel_type'] = $data['inner_channel_type'];
                $chn_tpl_model_inner['title'] = $data['inner_title'];
                $chn_tpl_model_inner['content'] = $data['inner_content'];
                $chn_tpl_model_inner['op_type'] = $data['op_type'];
                $chn_tpl_model_inner['tpl_id'] = $insert_tpl_id;
                if ($data['op_type'] == 2) {
                    $chn_tpl_model_inner['url'] = $data['url'];
                }
                if (empty($chn_tpl_model_inner['channel_tpl_id'])) {
                    $chn_tpl_model_inner['create_time'] = time();
                    $chn_tpl_model_inner['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_inner);
                    if (!$result) {
                        $this->Export(11008, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_inner['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_inner['channel_tpl_id'])->update($chn_tpl_model_inner);
                    if (!$result) {
                        $this->Export(11009, '编辑消息模板失败');
                    }
                }
            } else {
                if (!empty($data['inner_channel_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['inner_channel_tpl_id'])->delete();
                    if (!$delete_result) {
                        $this->Export(11009, '编辑消息模板失败');
                    }
                }
            }
            if (strpos($data['channels'], '2') !== false) {
                if (empty($data['sms_content'])) {
                    $this->Export(11010, '请填写短信内容');
                }
                $chn_tpl_model_sms = [];
                $chn_tpl_model_sms['channel_tpl_id'] = $data['sms_channel_tpl_id'];
                $chn_tpl_model_sms['channel_type'] = $data['sms_channel_type'];

                $chn_tpl_model_sms['content'] = $data['sms_content'];
                $chn_tpl_model_sms['tpl_id'] = $insert_tpl_id;
                if (empty($chn_tpl_model_sms['channel_tpl_id'])) {
                    $chn_tpl_model_sms['create_time'] = time();
                    $chn_tpl_model_sms['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_sms);
                    if (!$result) {
                        $this->Export(11011, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_sms['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_sms['channel_tpl_id'])->update($chn_tpl_model_sms);
                    if (!$result) {
                        $this->Export(11012, '编辑消息模板失败');
                    }
                }
            } else {
                if (!empty($data['sms_channel_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['sms_channel_tpl_id'])->delete();
                    if (!$delete_result) {
                        $this->Export(11012, '编辑消息模板失败');
                    }
                }
            }
            if (strpos($data['channels'], '3') !== false) {
                if (empty($data['email_title'])) {
                    $this->Export(11013, '请填写邮件标题');
                }
                if (empty($data['email_content'])) {
                    $this->Export(11014, '请填写邮件内容');
                }
                $chn_tpl_model_email = [];
                $chn_tpl_model_email['channel_tpl_id'] = $data['email_channel_tpl_id'];
                $chn_tpl_model_email['channel_type'] = $data['email_channel_type'];
                $chn_tpl_model_email['title'] = $data['email_title'];
                $chn_tpl_model_email['content'] = $data['email_content'];
                $chn_tpl_model_email['tpl_id'] = $insert_tpl_id;
                if (empty($chn_tpl_model_email['channel_tpl_id'])) {
                    $chn_tpl_model_email['create_time'] = time();
                    $chn_tpl_model_email['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_email);
                    if (!$result) {
                        $this->Export(11015, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_email['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_email['channel_tpl_id'])->update($chn_tpl_model_email);
                    if (!$result) {
                        $this->Export(11016, '编辑消息模板失败', $result);
                    }
                }
            } else {
                if (!empty($data['email_channel_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['email_channel_tpl_id'])->delete();
                    if (!$delete_result) {
                        $this->Export(11016, '编辑消息模板失败');
                    }
                }
            }
            if (strpos($data['channels'], '4') !== false) {
                if (empty($data['wechat_notify_title'])) {
                    $this->Export(11017, '请填写微信模板编号');
                }
                $chn_tpl_model_wechatnotify = [];
                $chn_tpl_model_wechatnotify['channel_tpl_id'] = $data['wechat_notify_tpl_id'];
                $chn_tpl_model_wechatnotify['channel_type'] = $data['wechat_notify_channel_type'];
                $chn_tpl_model_wechatnotify['title'] = $data['wechat_notify_title'];
                $chn_tpl_model_wechatnotify['tpl_id'] = $insert_tpl_id;
                $chn_tpl_model_wechatnotify['url'] = $data['wechat_notify_url'];
                $wechat_head = $data['wechat_notify_head'];
                $wechat_tail = $data['wechat_notify_tail'];
                $wechat_content = json_encode(array('wechat_tpl_head' => $wechat_head, 'wechat_tpl_tail' => $wechat_tail));
                $chn_tpl_model_wechatnotify['content'] = $wechat_content;
                if (empty($chn_tpl_model_wechatnotify['channel_tpl_id'])) {
                    $chn_tpl_model_wechatnotify['create_time'] = time();
                    $chn_tpl_model_wechatnotify['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_wechatnotify);
                    if (!$result) {
                        $this->Export(11018, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_wechatnotify['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_wechatnotify['channel_tpl_id'])->update($chn_tpl_model_wechatnotify);
                    if (!$result) {
                        $this->Export(11019, '编辑消息模板失败');
                    }
                }
            } else {
                if (!empty($data['wechat_notify_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['wechat_notify_tpl_id'])->delete();
                    if (!$delete_result) {
                        $this->Export(11019, '编辑消息模板失败');
                    }
                }
            }
            if (strpos($data['channels'], '5') !== false) {
                if (empty($data['ding_content'])) {
                    $this->Export(11020, '请填写钉钉消息内容');
                }
                $chn_tpl_model_ding = [];
                $chn_tpl_model_ding['channel_tpl_id'] = $data['ding_channel_tpl_id'];
                $chn_tpl_model_ding['channel_type'] = $data['ding_channel_type'];

                $chn_tpl_model_ding['content'] = $data['ding_content'];
                $chn_tpl_model_ding['tpl_id'] = $insert_tpl_id;
                if (empty($chn_tpl_model_ding['channel_tpl_id'])) {
                    $chn_tpl_model_ding['create_time'] = time();
                    $chn_tpl_model_ding['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_ding);
                    if (!$result) {
                        $this->Export(11021, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_ding['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_ding['channel_tpl_id'])->update($chn_tpl_model_ding);
                    if (!$result) {
                        $this->Export(11022, '编辑消息模板失败');
                    }
                }
            } else {
                if (!empty($data['ding_channel_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['ding_channel_tpl_id'])->delete();
                    if (!$delete_result) {
                        $this->Export(11023, '编辑消息模板失败');
                    }
                }
            }
        });
        $this->Export(0, empty($tpl_model['tpl_id']) ? '新增成功' : '修改成功');
    }

    //编辑手动消息
    //发送手动消息
    private function sendManualMessage($request)
    {
        $data = $request->input();
        $is_dumplicated = DB::connection('message')->table('lie_msg_tpl')->where('description',$data['description'])->first();
        if($is_dumplicated !== NULL)
        {
            $this->Export(11024, '已存在相同模板key,请更换key内容');
        }

        $url = '';
        $current_domain = $_SERVER['HTTP_HOST'];
        if($current_domain === Config('msgconfig.domain_local'))
        {
            $url = Config('msgconfig.api_domain_local');
        }elseif ($current_domain === Config('msgconfig.domain_sz'))
        {
            $url = Config('msgconfig.api_domain_sz');
        }elseif ($current_domain === Config('msgconfig.domain_release'))
        {
            $url = Config('msgconfig.api_domain_release');
        }

        $params = [];
        $params['channels'] = $data['channels'];
        $params['msg_type'] = $data['msg_type'];
//        $params['creater'] = $data['creater'];
        $params['description'] = $data['description'];
        $params['ex_str'] = $data['true_description'];
        $params['title_1'] = $data['title_1'];
        $params['title_3'] = $data['title_3'];
        $params['content_1'] = $data['content_1'];
        $params['content_2'] = $data['content_2'];
        $params['content_3'] = $data['content_3'];
        $params['op_type'] = $data['op_type'];
        $params['url_1'] = $data['url_1'];
        $params['obj_user'] = $data['obj_user'];
        $params['title_4'] = $data['title_4'];
        $params['url_4'] = $data['url_4'];

        $wechat_head = $data['wechat_notify_head'];
        $wechat_tail = $data['wechat_notify_tail'];
        $wechat_content = json_encode(array('wechat_tpl_head'=>$wechat_head,'wechat_tpl_tail'=>$wechat_tail));
        $params['content_4'] = $wechat_content;
        $params['expect_send_time'] = $data['expect_send_time'];
        //csrf
        $check['k1'] = time();
        $check['k2'] = pwdhash($check['k1'],'fh6y5t4rr351d2c3bryi');
        $params['pf'] = 1;
        $params['k1'] = $check['k1'];
        $params['k2'] = $check['k2'];
        $response = json_decode(curl($url,$params,1),true);

        $this->Export($response['err_code'], $response['err_msg']);

    }
}