<?php

namespace App\Models;
use Illuminate\Support\Facades\Log;
use DB;
/**
 *
 */
class Money_count extends BaseModel {
	protected $table = 'money_count';
	public $timestamps = false;

    /***获取统计对应数字类型
     * @param $str 年月日英文
     * @return mixed|null
     */
    protected function getType($str){
        $data=array(
            'year'=>1,
            'month'=>2,
            'day'=>3,
            'week'=>4,
            'season'=>5,
        );
        return isset($data[$str])?$data[$str]:null;
    }
    /***记录用户派奖金额
     * @param $u_id 用户id
     * @param $u_name 用户名
     * @param $money 金额
     * @param $timestmp
     */
    protected function increasePrizeMoney($u_id,$u_name,$money,$timestmp){
        $time_ree=GetpartTime($timestmp);
        //统计日的
        $uuid_day=$u_id.'_'.$this->getType('day').'_'.$time_ree['year'].$time_ree['month'].$time_ree['day'];
        $count_time_day=$time_ree['year'].'-'.$time_ree['month'].'-'.$time_ree['day'];
        $res_day=$this->PrizeMoney($uuid_day,$u_id,$u_name,$money,$count_time_day,$time_ree['day'],'day');
        //统计周的
        $uuid_week=$u_id.'_'.$this->getType('week').'_'.$time_ree['wyear'].$time_ree['week'];
        $count_time_week=$time_ree['wyear'].'-'.$time_ree['week'];
        $res_week=$this->PrizeMoney($uuid_week,$u_id,$u_name,$money,$count_time_week,$time_ree['week'],'week');
        //统计月
        $uuid_month=$u_id.'_'.$this->getType('month').'_'.$time_ree['year'].$time_ree['month'];
        $count_time_month=$time_ree['year'].'-'.$time_ree['month'];
        $res_month=$this->PrizeMoney($uuid_month,$u_id,$u_name,$money,$count_time_month,$time_ree['month'],'month');
        //统计季度
        $uuid_season=$u_id.'_'.$this->getType('season').'_'.$time_ree['year'].$time_ree['season'];
        $count_time_season=$time_ree['year'].'-'.$time_ree['season'];
        $res_season=$this->PrizeMoney($uuid_season,$u_id,$u_name,$money,$count_time_season,$time_ree['season'],'season');
        //统计年
        $uuid_year=$u_id.'_'.$this->getType('year').'_'.$time_ree['year'];
        $count_time_year=$time_ree['year'];
        $res_year=$this->PrizeMoney($uuid_year,$u_id,$u_name,$money,$count_time_year,$time_ree['year'],'year');
        if($res_year>0&&$res_season>0&&$res_month>0&&$res_week>0&&$res_day>0){
            return 1;
        }
        return -300223;
    }

    /**派奖金额统计
     * @param $u_id 用户id
     * @param $u_name 用户名
     * @param $money 金额
     * @param $timestmp
     * @param $str_type 字符型统计类型
     */
    protected function PrizeMoney($uuid,$u_id,$u_name,$money,$count_all_time,$count_time,$str_type){
        if(($res_day=$this->hasData($uuid))<0){//插入
            $data=array(
                'identity'=>$uuid,
                'account_identity'=>$u_id,
                'account'=>$u_name,
                'prize'=>$money,
                'ut_time'=>date('Y-m-d h:i:s'),
                'type'=>$this->getType($str_type),
                'count_alltime'=>$count_all_time,
                'count_time'=>$count_time
            );
            $res=$this->insert($data);
        }else{//更新
            $res=$this->where('identity',$uuid)->increment('prize',$money);
        }
        if(!$res){
            return -300330;
        }
        return 1;
    }

    /***记录用户测单金额
     * @param $u_id 用户id
     * @param $u_name 用户名
     * @param $money 金额
     * @param $timestmp
     * @return int
     */
    protected function increaseCancelMoney($u_id,$u_name,$money,$timestmp){
        $time_ree=GetpartTime($timestmp);
        //统计日的
        $uuid_day=$u_id.'_'.$this->getType('day').'_'.$time_ree['year'].$time_ree['month'].$time_ree['day'];
        $count_time_day=$time_ree['year'].'-'.$time_ree['month'].'-'.$time_ree['day'];
        $res_day=$this->CancelMoney($uuid_day,$u_id,$u_name,$money,$count_time_day,$time_ree['day'],'day');
        //统计周的
        $uuid_week=$u_id.'_'.$this->getType('week').'_'.$time_ree['wyear'].$time_ree['week'];
        $count_time_week=$time_ree['wyear'].'-'.$time_ree['week'];
        $res_week=$this->CancelMoney($uuid_week,$u_id,$u_name,$money,$count_time_week,$time_ree['week'],'week');
        //统计月
        $uuid_month=$u_id.'_'.$this->getType('month').'_'.$time_ree['year'].$time_ree['month'];
        $count_time_month=$time_ree['year'].'-'.$time_ree['month'];
        $res_month=$this->CancelMoney($uuid_month,$u_id,$u_name,$money,$count_time_month,$time_ree['month'],'month');
        //统计季度
        $uuid_season=$u_id.'_'.$this->getType('season').'_'.$time_ree['year'].$time_ree['season'];
        $count_time_season=$time_ree['year'].'-'.$time_ree['season'];
        $res_season=$this->CancelMoney($uuid_season,$u_id,$u_name,$money,$count_time_season,$time_ree['season'],'season');
        //统计年
        $uuid_year=$u_id.'_'.$this->getType('year').'_'.$time_ree['year'];
        $count_time_year=$time_ree['year'];
        $res_year=$this->CancelMoney($uuid_year,$u_id,$u_name,$money,$count_time_year,$time_ree['year'],'year');
        if($res_year>0&&$res_season>0&&$res_month>0&&$res_week>0&&$res_day>0){
            return 1;
        }
        return -300224;
    }

    /**测单金额统计
     * @param $u_id 用户id
     * @param $u_name 用户名
     * @param $money 金额
     * @param $timestmp
     * @param $str_type 字符型统计类型
     */
    protected function CancelMoney($uuid,$u_id,$u_name,$money,$count_all_time,$count_time,$str_type){
        if(($res_day=$this->hasData($uuid))<0){//插入
            $data=array(
                'identity'=>$uuid,
                'account_identity'=>$u_id,
                'account'=>$u_name,
                'cancel'=>$money,
                'ut_time'=>date('Y-m-d h:i:s'),
                'type'=>$this->getType($str_type),
                'count_alltime'=>$count_all_time,
                'count_time'=>$count_time
            );
            $res=$this->insert($data);
        }else{//更新
            $res=$this->where('identity',$uuid)->increment('cancel',$money);
        }
        if(!$res){
            return -300331;
        }
        return 1;
    }

    /***记录用户提现金额
     * @param $u_id 用户id
     * @param $u_name 用户名
     * @param $money 金额
     * @param $timestmp
     * @return int
     */
    protected function increaseTakeMoney($u_id,$u_name,$money,$timestmp){
        $time_ree=GetpartTime($timestmp);
        //统计日的
        $uuid_day=$u_id.'_'.$this->getType('day').'_'.$time_ree['year'].$time_ree['month'].$time_ree['day'];
        $count_time_day=$time_ree['year'].'-'.$time_ree['month'].'-'.$time_ree['day'];
        $res_day=$this->TakeMoney($uuid_day,$u_id,$u_name,$money,$count_time_day,$time_ree['day'],'day');
        //统计周的
        $uuid_week=$u_id.'_'.$this->getType('week').'_'.$time_ree['wyear'].$time_ree['week'];
        $count_time_week=$time_ree['wyear'].'-'.$time_ree['week'];
        $res_week=$this->TakeMoney($uuid_week,$u_id,$u_name,$money,$count_time_week,$time_ree['week'],'week');
        //统计月
        $uuid_month=$u_id.'_'.$this->getType('month').'_'.$time_ree['year'].$time_ree['month'];
        $count_time_month=$time_ree['year'].'-'.$time_ree['month'];
        $res_month=$this->TakeMoney($uuid_month,$u_id,$u_name,$money,$count_time_month,$time_ree['month'],'month');
        //统计季度
        $uuid_season=$u_id.'_'.$this->getType('season').'_'.$time_ree['year'].$time_ree['season'];
        $count_time_season=$time_ree['year'].'-'.$time_ree['season'];
        $res_season=$this->TakeMoney($uuid_season,$u_id,$u_name,$money,$count_time_season,$time_ree['season'],'season');
        //统计年
        $uuid_year=$u_id.'_'.$this->getType('year').'_'.$time_ree['year'];
        $count_time_year=$time_ree['year'];
        $res_year=$this->TakeMoney($uuid_year,$u_id,$u_name,$money,$count_time_year,$time_ree['year'],'year');
        if($res_year>0&&$res_season>0&&$res_month>0&&$res_week>0&&$res_day>0){
            return 1;
        }
        return -300225;
    }

    /**提现金额记录
     * @param $uuid
     * @param $u_id
     * @param $u_name
     * @param $money
     * @param $count_all_time
     * @param $count_time
     * @param $str_type
     * @return int
     */
    protected function TakeMoney($uuid,$u_id,$u_name,$money,$count_all_time,$count_time,$str_type){
        if(($res_day=$this->hasData($uuid))<0){//插入
            $data=array(
                'identity'=>$uuid,
                'account_identity'=>$u_id,
                'account'=>$u_name,
                'take'=>$money,
                'ut_time'=>date('Y-m-d h:i:s'),
                'type'=>$this->getType($str_type),
                'count_alltime'=>$count_all_time,
                'count_time'=>$count_time
            );
            $res=$this->insert($data);
        }else{//更新
            $res=$this->where('identity',$uuid)->increment('take',$money);
        }
        if(!$res){
            return -300332;
        }
        return 1;
    }

    /**存款，充值统计
     * @param $u_id
     * @param $u_name
     * @param $money
     * @param $timestmp
     * @return int
     */
    protected function increaseSaveMoney($u_id,$u_name,$money,$timestmp){
        $time_ree=GetpartTime($timestmp);
        //统计日的
        $uuid_day=$u_id.'_'.$this->getType('day').'_'.$time_ree['year'].$time_ree['month'].$time_ree['day'];
        $count_time_day=$time_ree['year'].'-'.$time_ree['month'].'-'.$time_ree['day'];
        $res_day=$this->SaveMoney($uuid_day,$u_id,$u_name,$money,$count_time_day,$time_ree['day'],'day');
        //统计周的
        $uuid_week=$u_id.'_'.$this->getType('week').'_'.$time_ree['wyear'].$time_ree['week'];
        $count_time_week=$time_ree['wyear'].'-'.$time_ree['week'];
        $res_week=$this->SaveMoney($uuid_week,$u_id,$u_name,$money,$count_time_week,$time_ree['week'],'week');
        //统计月
        $uuid_month=$u_id.'_'.$this->getType('month').'_'.$time_ree['year'].$time_ree['month'];
        $count_time_month=$time_ree['year'].'-'.$time_ree['month'];
        $res_month=$this->SaveMoney($uuid_month,$u_id,$u_name,$money,$count_time_month,$time_ree['month'],'month');
        //统计季度
        $uuid_season=$u_id.'_'.$this->getType('season').'_'.$time_ree['year'].$time_ree['season'];
        $count_time_season=$time_ree['year'].'-'.$time_ree['season'];
        $res_season=$this->SaveMoney($uuid_season,$u_id,$u_name,$money,$count_time_season,$time_ree['season'],'season');
        //统计年
        $uuid_year=$u_id.'_'.$this->getType('year').'_'.$time_ree['year'];
        $count_time_year=$time_ree['year'];
        $res_year=$this->SaveMoney($uuid_year,$u_id,$u_name,$money,$count_time_year,$time_ree['year'],'year');
        if($res_year>0&&$res_season>0&&$res_month>0&&$res_week>0&&$res_day>0){
            return 1;
        }
        return -300225;
    }

    /**存款
     * @param $uuid
     * @param $u_id
     * @param $u_name
     * @param $money
     * @param $count_all_time
     * @param $count_time
     * @param $str_type
     * @return int
     */
    protected function SaveMoney($uuid,$u_id,$u_name,$money,$count_all_time,$count_time,$str_type){
        if(($res_day=$this->hasData($uuid))<0){//插入
            $data=array(
                'identity'=>$uuid,
                'account_identity'=>$u_id,
                'account'=>$u_name,
                'save'=>$money,
                'ut_time'=>date('Y-m-d h:i:s'),
                'type'=>$this->getType($str_type),
                'count_alltime'=>$count_all_time,
                'count_time'=>$count_time
            );
            $res=$this->insert($data);
        }else{//更新
            $res=$this->where('identity',$uuid)->increment('save',$money);
        }
        if(!$res){
            return -300333;
        }
        return 1;
    }

    /**增加
     * @param $u_id
     * @param $u_name
     * @param $money
     * @param $timestmp
     * @return int
     */
    protected function increaseLotteryMoney($u_id,$u_name,$money,$timestmp){
        $time_ree=GetpartTime($timestmp);
        //统计日的
        $uuid_day=$u_id.'_'.$this->getType('day').'_'.$time_ree['year'].$time_ree['month'].$time_ree['day'];
        $count_time_day=$time_ree['year'].'-'.$time_ree['month'].'-'.$time_ree['day'];
        $res_day=$this->LotteryMoney($uuid_day,$u_id,$u_name,$money,$count_time_day,$time_ree['day'],'day');
        //统计周的
        $uuid_week=$u_id.'_'.$this->getType('week').'_'.$time_ree['wyear'].$time_ree['week'];
        $count_time_week=$time_ree['wyear'].'-'.$time_ree['week'];
        $res_week=$this->LotteryMoney($uuid_week,$u_id,$u_name,$money,$count_time_week,$time_ree['week'],'week');
        //统计月
        $uuid_month=$u_id.'_'.$this->getType('month').'_'.$time_ree['year'].$time_ree['month'];
        $count_time_month=$time_ree['year'].'-'.$time_ree['month'];
        $res_month=$this->LotteryMoney($uuid_month,$u_id,$u_name,$money,$count_time_month,$time_ree['month'],'month');
        //统计季度
        $uuid_season=$u_id.'_'.$this->getType('season').'_'.$time_ree['year'].$time_ree['season'];
        $count_time_season=$time_ree['year'].'-'.$time_ree['season'];
        $res_season=$this->LotteryMoney($uuid_season,$u_id,$u_name,$money,$count_time_season,$time_ree['season'],'season');
        //统计年
        $uuid_year=$u_id.'_'.$this->getType('year').'_'.$time_ree['year'];
        $count_time_year=$time_ree['year'];
        $res_year=$this->LotteryMoney($uuid_year,$u_id,$u_name,$money,$count_time_year,$time_ree['year'],'year');
        if($res_year>0&&$res_season>0&&$res_month>0&&$res_week>0&&$res_day>0){
            return 1;
        }
        return -300225;
    }

    /**彩金
     * @param $uuid
     * @param $u_id
     * @param $u_name
     * @param $money
     * @param $count_all_time
     * @param $count_time
     * @param $str_type
     * @return int
     */
    protected function LotteryMoney($uuid,$u_id,$u_name,$money,$count_all_time,$count_time,$str_type){
        if(($res_day=$this->hasData($uuid))<0){//插入
            $data=array(
                'identity'=>$uuid,
                'account_identity'=>$u_id,
                'account'=>$u_name,
                'lottery'=>$money,
                'ut_time'=>date('Y-m-d h:i:s'),
                'type'=>$this->getType($str_type),
                'count_alltime'=>$count_all_time,
                'count_time'=>$count_time
            );
            $res=$this->insert($data);
        }else{//更新
            $res=$this->where('identity',$uuid)->increment('lottery',$money);
        }
        if(!$res){
            return -300334;
        }
        return 1;
    }


    protected function increaseBetMoney($u_id,$u_name,$money,$timestmp){
        $time_ree=GetpartTime($timestmp);
        //统计日的
        $uuid_day=$u_id.'_'.$this->getType('day').'_'.$time_ree['year'].$time_ree['month'].$time_ree['day'];
        $count_time_day=$time_ree['year'].'-'.$time_ree['month'].'-'.$time_ree['day'];
        $res_day=$this->BetMoney($uuid_day,$u_id,$u_name,$money,$count_time_day,$time_ree['day'],'day');
        //统计周的
        $uuid_week=$u_id.'_'.$this->getType('week').'_'.$time_ree['wyear'].$time_ree['week'];
        $count_time_week=$time_ree['wyear'].'-'.$time_ree['week'];
        $res_week=$this->BetMoney($uuid_week,$u_id,$u_name,$money,$count_time_week,$time_ree['week'],'week');
        //统计月
        $uuid_month=$u_id.'_'.$this->getType('month').'_'.$time_ree['year'].$time_ree['month'];
        $count_time_month=$time_ree['year'].'-'.$time_ree['month'];
        $res_month=$this->BetMoney($uuid_month,$u_id,$u_name,$money,$count_time_month,$time_ree['month'],'month');
        //统计季度
        $uuid_season=$u_id.'_'.$this->getType('season').'_'.$time_ree['year'].$time_ree['season'];
        $count_time_season=$time_ree['year'].'-'.$time_ree['season'];
        $res_season=$this->BetMoney($uuid_season,$u_id,$u_name,$money,$count_time_season,$time_ree['season'],'season');
        //统计年
        $uuid_year=$u_id.'_'.$this->getType('year').'_'.$time_ree['year'];
        $count_time_year=$time_ree['year'];
        $res_year=$this->BetMoney($uuid_year,$u_id,$u_name,$money,$count_time_year,$time_ree['year'],'year');
        if($res_year>0&&$res_season>0&&$res_month>0&&$res_week>0&&$res_day>0){
            return 1;
        }
        return -300225;

    }

    /**
     * @param $uuid
     * @param $u_id
     * @param $u_name
     * @param $money
     * @param $count_all_time
     * @param $count_time
     * @param $str_type
     * @return int
     */
    protected function BetMoney($uuid,$u_id,$u_name,$money,$count_all_time,$count_time,$str_type){
        if(($res_day=$this->hasData($uuid))<0){//插入
            $data=array(
                'identity'=>$uuid,
                'account_identity'=>$u_id,
                'account'=>$u_name,
                'bet'=>$money,
                'ut_time'=>date('Y-m-d h:i:s'),
                'type'=>$this->getType($str_type),
                'count_alltime'=>$count_all_time,
                'count_time'=>$count_time
            );
            $res=$this->insert($data);
        }else{//更新
            $res=$this->where('identity',$uuid)->increment('bet',$money);
        }
        if(!$res){
            return -300329;
        }
        return 1;
    }

    /**检测数据是否存在
     * @param $uuid
     * @return int
     */
    protected function hasData($uuid){
        $res=$this->where('identity',$uuid)->first();
        if(!$res){
            return -300328;
        }
        return 1;
    }


    //添加最新金额到统计表
    protected function AddCountinfo($data,$mtype){
        $ts = GetpartTime($data['pass_time']);  //获取统计周期信息
        $type = $this->GetcountType();  //获取周期类型
        $indata = array(
            'account' => $data['account_name'],
            'account_identity' => $data['account_identity'],
            'ut_time' => $data['pass_time'],
            $mtype => $data['money'],
        );
        for($i=1;$i<6;$i++){
            $indata['count_time'] = $ts[$type[$i]];
            switch ($i) {
                case '1':
                    $indata['count_alltime'] = $indata['count_time'];
                    break;
                case '3':
                    $indata['count_alltime'] = $ts['year'] . '-' . $ts['month'] . '-' . $indata['count_time'];
                    break;
                case '4':
                    $indata['count_alltime'] = $ts['wyear'] . '-' . $indata['count_time'];
                    break;
                
                default:
                    $indata['count_alltime'] = $ts['year'] . '-' . $indata['count_time'];
                    break;
            }
            $indata['type'] = $i;
            $indata['identity'] =$data['account_identity'] . '_' . $i . '_' . $indata['count_alltime'];
            $res = $this->hasData($indata['identity']);
            if($res<0){
                $res = $this->insert($indata);
            }else{
                $res = $this->where('identity',$indata['identity'])->increment($mtype,$data['money']);
            }
            if($res<1)return -300326;
        }
        return 1;
    }

    //减少多余金额到统计表
    protected function DelCountinfo($data,$mtype){
        //DB::connection()->enableQueryLog();
        $ts = GetpartTime($data['pass_time']);  //获取统计周期信息
        $type = $this->GetcountType();  //获取周期类型
        $indata = array(
            'account' => $data['account_name'],
            'account_identity' => $data['account_identity'],
            'ut_time' => $data['pass_time'],
            $mtype => $data['money'],
        );
        for($i=1;$i<6;$i++){
            $indata['count_time'] = $ts[$type[$i]];
            switch ($i) {
                case '1':
                    $indata['count_alltime'] = $indata['count_time'];
                    break;
                case '3':
                    $indata['count_alltime'] = $ts['year'] . '-' . $ts['month'] . '-' . $indata['count_time'];
                    break;
                case '4':
                    $indata['count_alltime'] = $ts['wyear'] . '-' . $indata['count_time'];
                    break;
                
                default:
                    $indata['count_alltime'] = $ts['year'] . '-' . $indata['count_time'];
                    break;
            }
            $indata['type'] = $i;
            $indata['identity'] =$data['account_identity'] . '_' . $i . '_' . $indata['count_alltime'];
            $res = $this->hasData($indata['identity']);
            if($res<0){
                return 1;
            }else{
                $res = $this->where('identity',$indata['identity'])->decrement($mtype,$data['money']);
            }
            if($res<1)return -300327;
        }
        return 1;
    }

    //周期类型
    private function GetcountType(){
        return $type = array(
            '1' => 'year',
            '2' => 'month',
            '3' => 'day',
            '4' => 'week',
            '5' => 'season',
        );
    }

}

?>