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

namespace app\admin\controller\bbfxshop\user;

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

class Recharge extends Base
{
    
    public function _initialize(){
        parent::_initialize();
    }
    
    public function check(){
        $params = $this->param();
        
        $id = intval($params['id']);
        $status = intval($params['status']);
        $reason = $params['reason'];
        $log = Db::name("bbfx_recharge_order")->where(["uniacid"=>UNIACID,"id"=>$id])->find();
        if(empty($log)){
            __error("充值记录不存在"); 
        }
        
        if(!in_array($status,[1,2])){
            __error("审核状态错误");  
        }
        
        if($log['status'] != 0){
            __error("已审核");  
        }
        
        if($log['mode'] == 1){
            __error("充值方式错误");  
        }

        if($status == 2 && empty($reason)){
            __error("请输入驳回原因");  
        }
        
        try {
            Db::startTrans();
            $data = ["status"=>$status];
            if($status == 2){
                $data['reason'] = $reason;
            }
            $res = Db::name("bbfx_recharge_order")->where(['id'=>$log['id']])->update($data);
            if($res){
                if($status == 1){
                    $res = Order::rechargePayResult($log['order_no'],$log['mode']);
                    if(empty($res)){
                        throw new \Exception("充值失败");
                    }
                }
                $data['id'] = $log['id'];
                \app\admin\model\AdminLog::record("","",$data,'bbfx_recharge_order');
            }
            else{
                throw new \Exception("操作失败");
            }
            
            Db::commit();
        } 
        catch (\Exception $e) {
            Db::rollback();
            __error(0,$e->getMessage());
        }
        
        __success("操作成功"); 
        
    }
    
    public function getList(){
        $params = $this->param();
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):10;
        $where = array("a.uniacid"=>UNIACID);
        $status = intval($params['status']);
        $startTime = trim($params['startTime']);
        $endTime = trim($params['endTime']);
        
        $keyword = trim($params['keyword']);
        
        $type = max(1,intval($params['type']));
        if(!empty($keyword)){
            
            if($type == 1){
                $where['b.nickname'] = ['like',"%".$keyword."%"];
            }
            else if($type == 2){
                $where['a.realname'] = ['like',"%".$keyword."%"];
            }
            else if($type == 3){
                $where['b.mobile'] = $keyword;
            }
            else if($type == 4){
                $where['a.user_id'] = intval($keyword);
            }
        }
         
        if($status){
            $where['a.status'] = $status;
        }
        
        $whereTime = [];
        if($startTime){
            $startTime = strtotime($startTime." 00:00:00");
            $where['a.createtime'] = ['>=',$startTime];
        }
        
        if($endTime){
            $whereTime['a.createtime'] = ['<=',$endTime];
        }
        $export = intval($params['export']);
        if($export == 1){
            $pindex = 1;
            $psize = 999999;
        }
        $where1 = function($query){
            $query->whereOr(function($query){
                $query->where(['a.mode'=>1,'a.is_pay'=>1]);
            });
            $query->whereOr(function($query){
                $query->where(['a.mode'=>['>',1]]);
            });
        };
        $list = Db::name("bbfx_recharge_order")->alias("a")->join("bbfx_user b","a.user_id=b.id")->where($where)->where($where1)->where($whereTime)->field('a.*,b.avatar,b.nickname')->order('a.createtime desc')->limit(($pindex - 1) * $psize,$psize)->select();
        
    	foreach($list as &$val){
    	    $val['avatar'] = cdnurl($val['avatar']);
    	    $val['pay_pingzheng'] = cdnurl($val['pay_pingzheng']);
    	    $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
    	    if($val['paytime']){
    	        $val['paytime'] = date("Y-m-d H:i:s",$val['paytime']);
    	    }
    	    else{
    	        $val['paytime'] = "";
    	    }
            $type = "充值余额";
            if($val['type'] == 2){
                $type = "充值积分";
            }
            
            $val['type_text'] = $type;
            $val['mode_text'] = Order::getRechargeModeList($val['mode']);
    	    
    	    if($export == 1 && $val['is_pay'] == 1){
    	        
    	        $export_list[] = array(
    	            $val['id'],
    	            "【".$val['user_id']."】".$val['nickname'],
    	            $val['money'],
    	            $val['actual_money'],
    	            $type,
    	            $val['mode_text'],
    	            $val['createtime']
    	        );
    	    }
    	}
    	unset($val);
    	
    	if($export == 1){
    	    $headArr = ["ID","用户信息","充值金额","实际到账","充值类型","充值方式","充值时间"];
    	    $result = \app\common\model\PhpOffice::excelExport("cz-".date("Y-m-d"),$headArr,$export_list);
    	}
      
    	$total = Db::name("bbfx_recharge_order")->alias("a")->join("bbfx_user b","a.user_id=b.id")->where($where)->where($where1)->where($whereTime)->count();
      	
        __success("操作成功",['total'=>$total,'list'=>$list]);
    }

    
    
    public function post(){
        $params = $this->param();
        
        $user_id = intval($params['user_id']);
        $type = intval($params['type']);
        $money = floatval($params['money']);
        $cz_type = intval($params['cz_type']);
        $notes = $params['notes'];
        $value = $money;
        if($type == 1){
            $value = 0 - $money;
        }
        $info = ['notes'=>$notes,'admin_id'=>$this->auth->id,'admin_username'=>$this->auth->username];
        if($cz_type == 1){
            if($money <= 0){
                __error("请输入充值积分");
            }            
            $result = User::setUserMoney($user_id,$value,2,3,"后台充值",$info);
        }
        else if($cz_type == 3){
            if($money <= 0){
                __error("请输入充值余额");
            } 
            $result = \app\common\model\bbfxshop\CumulativeFanhuan::setMoney(1,$user_id,$value,$info);
            if($result['code'] != 1){
                __result($result);
            }
        }
        else if($cz_type == 4){
            if($money <= 0){
                __error("请输入充值积分");
            }
            $result = \app\common\model\bbfxshop\CumulativeFanhuan::setMoney(2,$user_id,$value,$info);
            if($result['code'] != 1){
                __result($result);
            }
        }
        else{
            if($money <= 0){
                __error("请输入充值余额");
            }
            $result = User::setUserMoney($user_id,$value,1,3,"后台充值",$info);
        }
        
        if($result){
            $title = "余额充值";
            $content = "用户ID：{$user_id}，充值金额：{$money}";
            if($cz_type == 1){
                $title = "积分充值";
                $content = "用户ID：{$user_id}，充值积分：{$money}";
            }
            else if($cz_type == 3){
                $title = "待返还余额充值";
                $content = "用户ID：{$user_id}，充值待返还余额：{$money}";
            }
            else if($cz_type == 4){
                $title = "待返还积分充值";
                $content = "用户ID：{$user_id}，充值待返还积分：{$money}";
            }
            
            \app\admin\model\AdminLog::record($title,$content,['id'=>$user_id,'notes'=>$notes],'bbfx_user');
            
            __success("充值成功");
        }
        else{
            __error("充值失败");
        }
    }

    public function delete(){
        $this->commonDelete('bbfx_recharge_order');
    }

    // 后台余额充值记录
    public function getMoneyLog(){
        $params = $this->param();
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):20;
        
        $where = array("a.uniacid"=>UNIACID,'a.type'=>1,'a.item_type'=>3);
        
        $keyword = trim($params['keyword']);
        $startTime = trim($params['startTime']);
        $endTime = trim($params['endTime']);
        $type = max(1,intval($params['type']));
        if($keyword){
            
            if($type == 1){
                $where['b.nickname'] = ['like',"%".$keyword."%"];
            }
            else if($type == 2){
                $where['b.realname'] = ['like',"%".$keyword."%"];
            }
            else if($type == 3){
                $where['b.mobile'] = $keyword;
            }
            else if($type == 4){
                $where['b.id'] = intval($keyword);
            }
            else if($type == 5){
                $where['a.remarks'] = ['like','%'.$keyword.'%'];
            }
            
        }
        
        if($startTime){
            $startTime = strtotime($startTime." 00:00:00");
            $where['a.createtime'] = ['>=',$startTime];
        }
        
        $whereTime = [];
        if($endTime){
            $endTime = strtotime($endTime." 23:59:59");
            $whereTime['a.createtime'] = ['<=',$endTime];
        }
        $export = intval($params['export']);
        if($export == 1){
            $pindex = 1;
            $psize = 999999;
        }
        
        $list = Db::name("bbfx_money_log")->alias("a")->join("bbfx_user b","a.user_id = b.id")->where($where)->where($whereTime)->field('a.*,b.nickname,b.avatar,b.mobile')->order('a.createtime desc,a.id desc')->limit(($pindex-1)*$psize,$psize)->select();
    	foreach($list as $key=>&$val){
    	    $val['money'] = bd_number($val['money']);
    	    $val['befores'] = bd_number($val['befores']);
    	    $val['afters'] = bd_number($val['afters']);
    	    $info = iunserializer($val['info']);
    	    if($val['item_type'] == 3){
    	        $val['remarks'] .= "-".$info['notes'];
    	    }
    	    $val['avatar'] = cdnurl($val['avatar']);
    	    $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            $val['admin_username'] = "";
            if(!empty($info['admin_username'])){
                $val['admin_username'] = $info['admin_username'];
            }
    	    if($export == 1){
    	        $export_list[] = array(
    	            $val['id'],
                     $val['admin_username'],
    	            "【".$val['user_id']."】".$val['nickname'],
    	            $val['money'],
    	            $val['befores'],
    	            $val['afters'],
    	            $val['remarks'],
    	            $val['createtime']
    	        );
    	    }
    	}
    	unset($val);
    	
    	if($export == 1){
    	    $headArr = ["ID","操作员","用户信息","变动金额","变动前","变动后","备注","操作时间"];
    	    \app\common\model\PhpOffice::excelExport("money-".date("Y-m-d"),$headArr,$export_list);
    	}
    	
    	$total = Db::name("bbfx_money_log")->alias("a")->join("bbfx_user b","a.user_id = b.id")->where($where)->where($whereTime)->count();
    	
    	__success("操作成功",['total'=>$total,'list'=>$list]);
    }

    // 后台积分充值记录
    public function getScoreLog(){
        $params = $this->param();
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):20;
        
        $where = array("a.uniacid"=>UNIACID,'a.type'=>2);
        
        $keyword = trim($params['keyword']);
        $startTime = trim($params['startTime']);
        $endTime = trim($params['endTime']);
        $type = max(1,intval($params['type']));
        if($keyword){
            
            if($type == 1){
                $where['b.nickname'] = ['like',"%".$keyword."%"];
            }
            else if($type == 2){
                $where['b.realname'] = ['like',"%".$keyword."%"];
            }
            else if($type == 3){
                $where['b.mobile'] = $keyword;
            }
            else if($type == 4){
                $where['b.id'] = intval($keyword);
            }
            else if($type == 5){
                $where['a.remarks'] = ['like','%'.$keyword.'%'];
            }
            
        }
        
        if($startTime){
            $startTime = strtotime($startTime." 00:00:00");
            $where['a.createtime'] = ['>=',$startTime];
        }
        
        $whereTime = [];
        if($endTime){
            $endTime = strtotime($endTime." 23:59:59");
            $whereTime['a.createtime'] = ['<=',$endTime];
        }
        
        $export = intval($params['export']);
        if($export == 1){
            $pindex = 1;
            $psize = 999999;
        }
        $list = Db::name("bbfx_money_log")->alias("a")->join("bbfx_user b","a.user_id = b.id")->where($where)->where($whereTime)->field('a.*,b.nickname,b.avatar,b.mobile')->order('a.createtime desc,a.id desc')->limit(($pindex-1)*$psize,$psize)->select();
    	foreach($list as $key=>&$val){
    	    $info = iunserializer($val['info']);
    	    if($val['item_type'] == 3){
    	        $val['remarks'] .= "-".$info['notes'];
    	    }
    	    $val['avatar'] = cdnurl($val['avatar']);
    	    $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            $val['admin_username'] = "";
            if(!empty($info['admin_username'])){
                $val['admin_username'] = $info['admin_username'];
            }
    	    $val['money'] = bd_number($val['money']);
    	    $val['befores'] = bd_number($val['befores']);
    	    $val['afters'] = bd_number($val['afters']);
    	    if($export == 1){
    	        $export_list[] = array(
    	            $val['id'],
                    $val['admin_username'],
    	            "【".$val['user_id']."】".$val['nickname'],
    	            $val['money'],
    	            $val['befores'],
    	            $val['afters'],
    	            $val['remarks'],
    	            $val['createtime']
    	        );
    	    }
    	}
    	unset($val);
    	
    	if($export == 1){
    	    $headArr = ["ID","操作员","用户信息","变动积分","变动前","变动后","备注","操作时间"];
    	    $result = \app\common\model\PhpOffice::excelExport("score-".date("Y-m-d"),$headArr,$export_list);
    	}
    	
    	$total = Db::name("bbfx_money_log")->alias("a")->join("bbfx_user b","a.user_id = b.id")->where($where)->where($whereTime)->count();
    	__success("操作成功",['total'=>$total,'list'=>$list]);
    }

    // 后台佣金充值记录
    public function getCommissionLog(){
        $params = $this->param();
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):20;
        
        $where = array("a.uniacid"=>UNIACID,'a.type'=>0);
        
        $keyword = trim($params['keyword']);
        $startTime = trim($params['startTime']);
        $endTime = trim($params['endTime']);
        $type = max(1,intval($params['type']));
        if($keyword){
            
            if($type == 1){
                $where['b.nickname'] = ['like',"%".$keyword."%"];
            }
            else if($type == 2){
                $where['b.realname'] = ['like',"%".$keyword."%"];
            }
            else if($type == 3){
                $where['b.mobile'] = $keyword;
            }
            else if($type == 4){
                $where['b.id'] = intval($keyword);
            }
            else if($type == 5){
                $where['a.remarks'] = ['like','%'.$keyword.'%'];
            }
            
        }
        
        if($startTime){
            $startTime = strtotime($startTime." 00:00:00");
            $where['a.createtime'] = ['>=',$startTime];
        }
        
        $whereTime = [];
        if($endTime){
            $endTime = strtotime($endTime." 23:59:59");
            $whereTime['a.createtime'] = ['<=',$endTime];
        }
        $export = intval($params['export']);
        if($export == 1){
            $pindex = 1;
            $psize = 999999;
        }
        $list = Db::name("bbfx_fenxiao_commission")->alias("a")->join("bbfx_user b","a.user_id = b.id")->where($where)->where($whereTime)->field('a.*,b.nickname,b.avatar,b.mobile')->order('a.createtime desc,a.id desc')->limit(($pindex-1)*$psize,$psize)->select();
    	foreach($list as $key=>&$val){
    	    $info = iunserializer($val['info']);
    	    if($val['type'] == 0 && !empty($info['notes'])){
    	        $val['remarks'] .= "-".$info['notes'];
    	    }
    	    $val['avatar'] = cdnurl($val['avatar']);
    	    $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            $val['admin_username'] = "";
            if(!empty($info['admin_username'])){
                $val['admin_username'] = $info['admin_username'];
            }

    	    if($export == 1){
    	        $export_list[] = array(
    	            $val['id'],
                    $val['admin_username'],
    	            "【".$val['user_id']."】".$val['nickname'],
    	            $val['money'],
    	            $val['befores'],
    	            $val['afters'],
    	            $val['remarks'],
    	            $val['createtime']
    	        );
    	    }
    	}
    	unset($val);
    	
    	if($export == 1){
    	    $headArr = ["ID","操作员","用户信息","变动金额","变动前","变动后","备注","操作时间"];
    	    $result = \app\common\model\PhpOffice::excelExport("commission-".date("Y-m-d"),$headArr,$export_list);
    	}
    	
    	$total = Db::name("bbfx_fenxiao_commission")->alias("a")->join("bbfx_user b","a.user_id = b.id")->where($where)->where($whereTime)->count();
    	
    	__success("操作成功",['total'=>$total,'list'=>$list]);
    }
}