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

namespace app\admin\controller\bbfxshop\sreturn;

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


class User extends Base
{
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Sreturn;
    }

    public function getList(){
        $params = $this->param();
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):20;
        $where = ['b.uniacid'=>UNIACID];
        
        $list = Db::name("bbfx_sreturn_user")->alias("a")->join("bbfx_user b","a.user_id=b.id","LEFT")->where($where)->order("id desc")->field("a.*,b.nickname,b.avatar,b.level,b.is_distributor,b.distributor_status,b.createtime")->limit(($pindex - 1) * $psize,$psize)->select();
        foreach ($list as $key => &$val) {
            $val['avatar'] = cdnurl($val['avatar']);
            $val['fanhuan_total'] = round(Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>$val['type'],'item_type'=>['in',[65,66,67,68]],'user_id'=>$val['user_id']])->sum("money"),2);
        }
        unset($val);
        
        $total = Db::name("bbfx_sreturn_user")->alias("a")->join("bbfx_user b","a.user_id=b.id","LEFT")->where($where)->count();
        __success("操作成功",['total'=>$total,'list'=>$list]);
    }

    public function getRecordList(){
        $params = $this->param();
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):20;
        $where = ['a.uniacid'=>UNIACID,'a.item_type'=>['in',[65,66,67,68]],'a.money'=>['>',0]];
        $type = $this->param("type/d");
        $user_id = $this->param("user_id/d");
        if($type > 0){
            $where['a.type'] = $type;
        }
        if($user_id > 0){
            $where['a.user_id'] = $user_id;
        }
        
        $list = Db::name("bbfx_money_log")->alias("a")->join("bbfx_user b","a.user_id=b.id","LEFT")->where($where)->field("a.*,b.avatar,b.nickname")->order(['a.createtime desc','a.id desc'])->limit(($pindex-1)*$psize,$psize)->select();
        
        foreach ($list as $key => &$val){
            $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            $val['avatar'] = cdnurl($val['avatar']);
            $val['money'] = bd_number($val['money']);
            $val['befores'] = bd_number($val['befores']);
            $val['afters'] = bd_number($val['afters']);

        }
        unset($val);

        $total = Db::name("bbfx_money_log")->alias("a")->join("bbfx_user b","a.user_id=b.id","LEFT")->where($where)->count();

         __success("操作成功",['total'=>$total,'list'=>$list]);
    }

    public function delete(){
        $this->commonDelete("bbfx_sreturn_user");
    }
}