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

namespace app\api\controller\cumulative_fanhuan;

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


class Index extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\CumulativeFanhuan;
    }
    
    public function index(){
        $user = $this->user;
        $fanhuan_user = Db::name("bbfx_cumulative_fanhuan_user")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id'],'type'=>1])->find();
        $money_total = 0;
        $money_ok = 0;
        $money_wait = 0;
        $mode = 0;
        $where = ['uniacid'=>UNIACID,'user_id'=>$user['id'],'item_type'=>['in',[26,52,53,54]],'money'=>['>',0]];
        if(!empty($fanhuan_user)){
            $mode = $fanhuan_user['mode'];
            $money_total = $fanhuan_user['total'];
            $money_wait = $fanhuan_user['surplus'];
            $money_ok = round($fanhuan_user['total'] - $fanhuan_user['surplus'],2);
            // $money_ok = Db::name("bbfx_money_log")->where($where)->where(['type'=>1])->sum("money");
            // $money_ok = round($money_ok,2);
            // $money_wait = round($fanhuan_user['total'] - $money_ok,2);
        }

        $fanhuan_score_user = Db::name("bbfx_cumulative_fanhuan_user")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id'],'type'=>2])->find();
        $money_score_total = 0;
        $money_score_ok = 0;
        $money_score_wait = 0;
        if(!empty($fanhuan_score_user)){
            if(empty($fanhuan_user)){
                $mode = $fanhuan_score_user['mode'];
            }
            $money_score_total = $fanhuan_score_user['total'];
            // $money_score_wait = $fanhuan_score_user['surplus'];
            // $money_score_ok = round($fanhuan_score_user['total'] - $fanhuan_score_user['surplus'],2);
            $money_score_ok = Db::name("bbfx_money_log")->where($where)->where(['type'=>2])->sum("money");
            $money_score_ok = round($money_score_ok,2);
            $money_score_wait = round($fanhuan_score_user['total'] - $money_score_ok,2);
        }

        $result = [
            'mode'=>$mode,
            'money_total'=>$money_total,
            'money_wait'=>$money_wait,
            'money_ok'=>$money_ok,
            'money_score_total'=>$money_score_total,
            'money_score_wait'=>$money_score_wait,
            'money_score_ok'=>$money_score_ok,
        ];

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

    public function setMode(){
        $config = $this->model->getSet();
        if($config['status'] != 1){
            __error("未开启功能");
        }
        if($config['switch_duration'] <= 0){
            __error("未配置模式切换时长");
        }
        $fanhuan_user = Db::name("bbfx_cumulative_fanhuan_user")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id'],'type'=>2])->find();
        if(empty($fanhuan_user)){
            __error("操作失败，您还没有累计返还");
        }
        $time = time() - $config['switch_duration'] * (24*60*60);
        if($fanhuan_user['modetime'] > 0 && $fanhuan_user['modetime'] > $time){
            $time = $fanhuan_user['modetime'] + $config['switch_duration'] * (24*60*60);

            __error("每隔{$config['switch_duration']}天才可重新切换模式，下次切换时间：".date('Y-m-d H:i',$time));
        }

        if($fanhuan_user['surplus'] > 0 && $fanhuan_user['mode'] == 0){
            __error("待释放金额为0时才可切换为增益模式");
        }

        if($fanhuan_user['mode'] == 1){
            $mode = 0;
        }
        else{
            $mode = 1;
        }

        Db::name("bbfx_cumulative_fanhuan_user")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id']])->update(['mode'=>$mode,'modetime'=>time()]);

        __success("操作成功");
    }
    
    public function getRecordList(){
        $params = $this->param();
        
        $user = $this->user;
        
        $page = max(1,intval($params['page']));
        $psize = 20;
        $where = ['uniacid'=>UNIACID,'user_id'=>$user['id'],'item_type'=>['in',[26,52,53,54]],'money'=>['>',0]];
        $type = $this->param("type/d");
        if($type > 0){
            $where['type'] = $type;
        }
        
        $list = Db::name("bbfx_money_log")->where($where)->order(['createtime desc','id desc'])->limit(($page-1)*$psize,$psize)->select();
        
        if(empty($list)){
            __error($page==1?'暂无数据~':'没有更多了~');
        }
        else{
            
            foreach ($list as $key => &$val){
                $val['money'] = bd_number($val['money']);
                
                if($val['type'] == 1){
                    $val['moneyText'] = "+".floatval($val['money'])."元";
                }
                else{
                    $val['moneyText'] = "+".floatval($val['money'])."积分";
                }
                
                $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
                $val['info'] = iunserializer($val['info']);
                $val['surplus'] = 0;
                if(!is_null($val['info']['surplus'])){
                    $val['surplus'] = round($val['info']['surplus'],2);
                }
            }
            unset($val);
            
            __success("success",$list);
        }
        
    }
}