<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\admin\controller\bbfxshop\message;

use app\admin\controller\bbfxshop\Base;
use think\Db;


class Site extends Base
{
    
    public $model = null;
    public function _initialize(){
        $this->initConfigFlag = false;
        parent::_initialize();
    }
    
    public function getList(){
        $params = $this->param();
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):10;
        
        $where = array("uniacid"=>UNIACID);
        
        $list1 = Db::name("bbfx_site_message")->where($where)->order('id desc')->select();
        if(empty($list1)) $list1 = [];
        $list2 = [];
        $result = ihttp_get("http://www.baibianfenxiao.com/api/message/getList");
        $result = json_decode($result['content'],true);
        if($result['code'] == 1){
            $list2 = $result['data']['list'];
        }
        foreach ($list1 as $key => &$val){
            $val['type'] = 1;
        }
        unset($val);
        foreach ($list2 as $key => &$val){
            $val['type'] = 2;
        }
        unset($val);
        
        $list = array_merge($list1,$list2);
        $total = count($list);

        $sorts = array_column($list,'createtime');
        array_multisort($sorts,SORT_DESC,$list);

        $list = array_slice($list, ($pindex-1)*$psize, $psize);


        foreach ($list as $key => &$val){
            $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            $val['read'] = 0;
            $log = Db::name("bbfx_site_message_log")->where(["uniacid"=>UNIACID,'type'=>$val['type'],"admin_id"=>$this->auth->id,'pid'=>$val['id']])->count();
            if(!empty($log)){
                $val['read'] = 1;
            }
        }
        unset($val);
        
        __success("操作成功",['total'=>$total,'list'=>$list]);
    }

    public function read(){
        $id = $this->param('id/d');
        $type = $this->param('type/d');
        if($id <= 0){
            __error("消息ID不能为空");
        }
        if(!in_array($type,[1,2])){
            __error("消息类型不能为空");
        }

        $data = [
            'uniacid'=>UNIACID,
            'admin_id'=>$this->auth->id,
            'pid'=>$id,
            'type'=>$type,
            'createtime'=>time()
        ];
        
        try{
            Db::name("bbfx_site_message_log")->insert($data);
        }
        catch(\Exception $e){
            __error($e->getMessage());
        }

        __success("操作成功");
    }
    
    public function detail(){
        $params = $this->param();
        
        $info = Db::name("bbfx_site_message")->where("id",$params['id'])->find();
        
        if(empty($info)){
            __error("消息不存在");
        }
        $info['createtime'] = date("Y-m-d H:i:s",$info['createtime']);
        __success('操作成功',$info);
    }
    
    public function post(){
        $params = $this->param();

        if($this->auth->id != 1){
            __error("您无权限操作");
        }

        $data = array(
            "uniacid"=>UNIACID,
            "title"=>$params['title'],
            "content"=>htmlspecialchars_decode($this->param('content/h')),
        );
        if(!empty($params['id'])){
            $res = Db::name("bbfx_site_message")->where("id",$params['id'])->update($data);
            $data['id'] = $params['id'];
        }
        else{
            $data['createtime'] = time();
            $res = Db::name("bbfx_site_message")->insertGetId($data);
            $data['id'] = $res;
        }
        
        if($res){
            \app\admin\model\AdminLog::record("","",$data,"bbfx_site_message");
            __success("编辑成功");
        }
        else{
            __success("操作成功");
        }
    }
    
    public function delete(){
        if($this->auth->id != 1){
            __error("您无权限操作");
        }
        $this->commonDelete("bbfx_site_message");
    }
}