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

namespace app\api\controller\super_maihuo;

use app\api\controller\Base;
use think\Db;
use app\common\model\bbfxshop\User;

class Group extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\SuperMaihuo;
    }
    
    public function detail(){
        $id = $this->param("id/d");
        
        $group = Db::name("bbfx_super_maihuo_group_user")->alias("a")->join("bbfx_super_maihuo_group b","a.group_id=b.id","LEFT")->where(['a.uniacid'=>UNIACID,'a.user_id'=>$this->user['id'],'b.id'=>$id])->group("a.group_id")->order("b.id desc")->field("b.*")->find();
        if(empty($group)){
            __error("数据不存在");
        }
        
        $user_list = Db::name("bbfx_super_maihuo_group_user")->alias("a")->join("bbfx_super_maihuo_user b","a.pid=b.id","LEFT")->where(["a.uniacid"=>UNIACID,"a.group_id"=>$group['id']])->order("a.id asc")->field("a.*,b.info,b.createtime as otime")->select();
        $utime = 0;
        $id = 0;
        foreach ($user_list as $k => &$v){
            $info = iunserializer($v['info']);
            $v['avatar'] = cdnurl($info['avatar']);
            $v['nickname'] = $info['nickname'];
            $v['mobile'] = secret($info['mobile'], true, 3, 4);
            unset($v['info']);
            if($v['createtime'] > strtotime(date("Y-01-01 00:00:00"))){
                $v['createtime'] = date("m/d H:i");
            }
            else{
                $v['createtime'] = date("Y/m/d H:i");
            }
            if($v['contribution_value'] >= 2 && $group['status'] == 1){
                if($v['utime'] < $utime || $utime == 0){
                    $utime = $v['utime'];
                    $id = $v['id'];
                }
            }
            if($v['otime'] > strtotime(date("Y-01-01 00:00:00"))){
                $v['otime'] = date("m/d H:i");
            }
            else{
                $v['otime'] = date("Y/m/d H:i");
            }
        }
        unset($v);
        
        if($id > 0){
            foreach ($user_list as $k => &$v){
                $v['is_abonus_user'] = 0;
                if($v['id'] == $id){
                    $v['is_abonus_user'] = 1;
                }
            }
            unset($v);
        }
        
        $group['stime'] = date('Y-m-d H:i:s',$group['stime']);
        if(!empty($group['etime'])){
            $group['etime'] = date('Y-m-d H:i:s',$group['etime']);
        }
        
        __success("操作成功",['user_list'=>$user_list,'group'=>$group]);
    }
    
    public function getList(){
        $page = max(1,$this->param("page/d"));
        $limit = 20;
        
        
        $list = Db::name("bbfx_super_maihuo_group_user")->alias("a")->join("bbfx_super_maihuo_group b","a.group_id=b.id","LEFT")->where(['a.uniacid'=>UNIACID,'a.user_id'=>$this->user['id']])->group("a.group_id")->order("b.id desc")->field("b.*")->select();
        
        foreach ($list as $key => &$val){
            /*
            $user_list = Db::name("bbfx_super_maihuo_group_user")->alias("a")->join("bbfx_super_maihuo_user b","a.pid=b.id","LEFT")->where(["a.uniacid"=>UNIACID,"a.group_id"=>$val['id']])->order("a.id asc")->field("a.*,b.info")->select();
            
            foreach ($user_list as $k => &$v){
                $info = iunserializer($v['info']);
                $v['avatar'] = cdnurl($info['avatar']);
                $v['nickname'] = $info['nickname'];
                unset($v['info']);
            }
            unset($v);
            
            $val['user_list'] = $user_list;*/
            
            $val['stime'] = date('Y-m-d H:i:s',$val['stime']);
            if(!empty($val['etime'])){
                $val['etime'] = date('Y-m-d H:i:s',$val['etime']);
                $group_user = Db::name("bbfx_super_maihuo_group_user")->alias("a")->join("bbfx_super_maihuo_user b","a.pid=b.id","LEFT")->where(["a.uniacid"=>UNIACID,"a.group_id"=>$val['id'],'a.contribution_value'=>2])->order("a.utime asc")->field("a.*,b.info,b.createtime as otime")->find();
                
                if(!empty($group_user)){
                    $info = iunserializer($group_user['info']);
                    $abonus_user = [];
                    $abonus_user['avatar'] = cdnurl($info['avatar']);
                    $abonus_user['nickname'] = $info['nickname'];
                    $abonus_user['mobile'] = secret($info['mobile'], true, 3, 4);
                    $val['abonus_user'] = $abonus_user;
                }
            }
            else{
                $val['etime'] = "";
            }
            
        }
        unset($val);
        
        __success("操作成功",['list'=>$list]);
    }
}