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

namespace app\api\controller;

use app\api\controller\Base;
use think\Db;
use think\Cache;


class Task extends Base
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function index(){
        $task = $this->paramGet("task/d");
        $i = $this->paramGet("i/d");
        if(!in_array($task,[1,2])){
            __error("操作失败");
        }
        
        $task_time = Cache::get("bbfx_task_time");
        if($task == 1 && $task_time > (time() - 120)){
            addFileLog("taskkk111",date('Y-m-d H:i:s'));
            __success("任务重复");
        }
        

        if($i <= 0){
            $list = Db::name("platform")->where([])->field("id")->order("id asc")->select();
            foreach ($list as $k => $v){
                ihttp_get(request()->domain()."/api/task/index?task=1&i=". $v["id"]);
            }
            __success("操作成功");
        }
        else{
            
            echo json_encode(['code'=>1,'message'=>'操作成功']);
        
            bbfx_finish_request();
            
            \app\common\model\bbfxshop\TaskExecute::run1();
        }
    }

    public function user(){
        $user = $this->user;


        $access_permissions_levels = [];
        $pages = Db::name("bbfx_diypage")->where(["uniacid"=>UNIACID,'type'=>1,'default_status'=>0])->field("id,access_permissions_levels")->select();
        if(!empty($pages)){
            foreach ($pages as $val) {
                $levels = iunserializer($val['access_permissions_levels']);
                if(!empty($levels)){
                    foreach ($levels as $k => &$v){
                        $v = intval($v);
                    }
                    unset($v);
                }
                $access_permissions_levels[$val['id']] = $levels;
            }
        }

        $contract_record = null;
        $Contract_set = \app\common\model\bbfxshop\Contract::getSet();
        if($Contract_set['status'] == 1 && !empty($user)){
            $contract_record = Db::name("bbfx_contract_record")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id'],'status'=>0,'signing_type'=>0])->order("id asc")->find();
            if(!empty($contract_record)){
                $contract_record['content'] = cdnurl($contract_record['content']);
            }
            else{
                $contract_record = null;
            }
        }
        
        
        $apopup_set = Db::name("bbfx_apopup_set")->where(['uniacid'=>UNIACID])->find();
        $apopup = ['set'=>$apopup_set,'list'=>[]];
        if($apopup_set['status'] == 1){
            $apopup_all = Db::name("bbfx_apopup")->where(['uniacid'=>UNIACID,'status'=>1])->order('sort desc')->select();
            $apopup_list = [];
            foreach ($apopup_all as $key => &$val){
                $val['thumb'] = cdnurl($val['thumb']);
                $val['link'] = iunserializer($val['link']);
                
                if($val['tanchu'] > 0 && !empty($user)){
                    $count = Db::name("bbfx_apopup_log")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'pid'=>$val['id']])->count();
                    if($count < $val['tanchu']){
                        $apopup_list[] = $val;
                        $data = array(
                            "uniacid"=>UNIACID,
                            "user_id"=>$user['id'],
                            "pid"=>$val['id'],
                            "createtime"=>time()
                        );
                        Db::name("bbfx_apopup_log")->insert($data);
                    }
                }
                else{
                    $apopup_list[] = $val;
                }
            }
            unset($val);
            $apopup['list'] = $apopup_list;
        }

        $tixian_info = null;
        if(!empty($user)){
            $ptime = time() - 24*60*60;
            $tixian = Db::name("bbfx_withdrawal_log")->where(["uniacid"=>UNIACID,"user_id"=>$user['id'],'type'=>2,'status'=>1,'paytime'=>['>',$ptime],'is_confirm'=>0])->find();
            $tx_success = false;
            if(!empty($tixian)){
                $result = \app\common\model\bbfxshop\Wxpay::queryNewTransfer($tixian['out_batch_no'],$user['platform']);
                if($result['code'] == 1){
                    $state = $result['data']['state'];
        
                    if($state == 'SUCCESS'){
                        Db::name("bbfx_withdrawal_log")->where(['id'=>$tixian['id']])->update(['is_confirm'=>1,'confirmtime'=>time()]);
                    }
                    else{
                        if($state != 'WAIT_USER_CONFIRM' && $state != 'TRANSFERING'){
                            $out_batch_no = date("YmdHis").random(8,true);
                            Db::name("bbfx_withdrawal_log")->where(['id'=>$tixian['id']])->update(['out_batch_no'=>$out_batch_no]);
                            $tixian['out_batch_no'] = $out_batch_no;
                            $transfer_remark = "余额提现";
                            $remarks = ['1'=>'余额提现','2'=>'佣金提现','3'=>'积分提现','5'=>'红包提现','6'=>'预存款提现','7'=>'商户提现'];
                            $transfer_remark = $remarks[$tixian['withdrawal_type']];

                            $res = \app\common\model\bbfxshop\Wxpay::newTransfer($user['openid'],$tixian['realmoney'],$out_batch_no,$tixian['realname'],$transfer_remark,$user['platform']);
                            if($res['code'] == 1){
                                $data = [
                                    'batch_id'=>$res['data']['transfer_bill_no'],
                                    'package_info'=>''
                                ];
                                if(!empty($res['data']['package_info'])){
                                    $data['package_info'] = $res['data']['package_info'];
                                    $tixian['package_info'] = $res['data']['package_info'];
                                }
                                
                                Db::name("bbfx_withdrawal_log")->where(['id'=>$tixian['id']])->update($data);
                                $tx_success = true;
                            }
                        }
                        else{
                            $tx_success = true;
                        }
                    }
                }
            }

            if($tx_success){
                $set = \app\common\model\bbfxshop\Wxpay::getSet($this->user['platform']);
                
                $tixian_info = [
                    "mchId"=>$set['mchid'],
                    "appId"=>$set['appid'],
                    "package"=>$tixian['package_info'],
                    'withdrawal_no'=>$tixian['withdrawal_no'],
                    'money'=>$tixian['realmoney'],
                ];
            }
        }

        $cashlottery = ['num'=>0,'everyday_lottery'=>0];
        if(!empty($user)){
            $Cashlottery_set = \app\common\model\bbfxshop\Cashlottery::getSet();
            if($Cashlottery_set['status'] == 1){
                if(in_array(1,$Cashlottery_set['lottery_method'])){
                    $cashlottery['num'] += $user['cashlottery_lottery_num'];
                }
                if(in_array(0,$Cashlottery_set['lottery_method'])){
                    $time = strtotime(date('Y-m-d 00:00:00'));
                    $log = Db::name("bbfx_cashlottery_reward_record")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'lottery_method'=>0,'createtime'=>['>',$time]])->find();
                    if(empty($log)){
                        $cashlottery['num'] += 1;
                        $cashlottery['everyday_lottery'] = 0;
                    }
                    else{
                        $cashlottery['everyday_lottery'] = 1;
                    }
                }
            }
        }
        
        $result = [
            'access_permissions_levels'=>$access_permissions_levels,// 页面访问权限等级
            'contract_record'=>$contract_record,// 待签合同
            'apopup'=>$apopup,// 弹窗
            'tixian_info'=>$tixian_info,// 提现信息
            'cashlottery'=>$cashlottery
        ];

        __success("操作成功",$result);
    }
}