<?php

namespace App\Modules\CP\Services;

use App\Modules\CpSub\Models\CpSub;
use App\Modules\CpSub\Models\CpCalculate;
use App\Modules\CpSub\Models\CpBookCalculate;
use App\Modules\Book\Models\BookConfig;
use App\Modules\CpUser\Services\CpUserService;

class GenerateCpBookStatService
{
  
    /**
     * 生成每日统计
     */
    public static function generateCpBookDailyStat($date) {
    	if(empty($date)) $data = date('Y-m-d');
    	$result = array();
    	$all_cp_list = BookConfig::getAllCps();
    	\Log::info($all_cp_list);
    	$special_cps = array('17k','wangyi');

    	$today = date('Y-m-d');
//     	$today = date('Y-m-01');
    	// 只算昨天
    	$month_start_date = date('Y-m-01');
    	$yestoday = $start_date = $end_date = $date;
    	$month = date('Y-m');
    	$calculate_date = date('Y-m-d');
    	
    	// 每个月1号算上个月的
    	if($today == $month_start_date){
//     		$start_date = date('Y-m-01',strtotime(' -1 month'));
    		$month = date('Y-m',strtotime(' -1 month'));
    	}
    	\Log::info('generateCpBookDailyStat,start_date:'.$start_date.' end_date:'.$end_date.' month:'.$month.' calculate_date:'.$calculate_date);
    	$all_data_list = BookConfig::getAllCpBookConsume($start_date,$end_date,$all_cp_list,$yestoday);
    	
    	self::insert_daily_stat($all_data_list,$month,$calculate_date);
    	
        return $result;
    }
    /**
     * 生成每日统计
     */
    public static function fixCpBookDailyStat($date) {
        $result = array();
        $all_cp_list = BookConfig::getAllCps();
        \Log::info($all_cp_list);

        $today = $date;

        $month_start_date = date('Y-m-01');
        $yestoday = $start_date = $end_date = $date;
        $month = date('Y-m');
        $calculate_date = date('Y-m-d',strtotime('+1 day',strtotime($date)));
        // 每个月1号算上个月的
        if($today == $month_start_date){
            $month = date('Y-m',strtotime(' -1 month'));
        }

        \Log::info('fixCpBookDailyStat,start_date:'.$start_date.' end_date:'.$end_date.' month:'.$month.' calculate_date:'.$calculate_date);
        $all_data_list = BookConfig::getAllCpBookConsume($start_date,$end_date,$all_cp_list,$yestoday);
        self::insert_daily_stat($all_data_list,$month,$calculate_date);

        return $result;
    }
    
    /**
     * 生成指定月份数据
     */
    public static function generateCpBookBetweenStat($start_date,$end_date,$month,$calculate_date) {
    	\Log::info('generateCpBookBetweenStat_start,start_date:'.$start_date.' end_date:'.$end_date.' month:'.$month.' calculate_date:'.$calculate_date);
    	if(empty($start_date) || empty($end_date) || empty($month) || empty($calculate_date)) {
    		\Log::info('generateCpBookBetweenStat isnull,start_date:'.$start_date.' end_date:'.$end_date.' month:'.$month.' calculate_date:'.$calculate_date);
    		return false;
    	}
    	$result = array();
    	$all_cp_list = BookConfig::getAllCps();
    	\Log::info($all_cp_list);

    	$all_data_list = BookConfig::getAllCpBookConsume($start_date,$end_date,$all_cp_list);
    	 
    	self::insert_daily_stat($all_data_list,$month,$calculate_date);
    	 
    	return $result;
    }

    /**
     * 生成汇总数据
     */
    public static function insert_daily_stat($item_list,$month,$calculate_date) {
    	\Log::info('insert_daily_stat_start');
    	$cp_book_calculate_bids = CpBookCalculate::getAllCpBookCalculateBids();
    	foreach($item_list as $key=>$item){
    		try{
    			// 算当天
    			// 单本书指定date的充值数据
    			$insert_data = array();
    			$insert_data['month'] = empty($month)?date('Y-m'):$month;
    			$insert_data['calculate_date'] = empty($calculate_date)?date('Y-m-d'):$calculate_date;
    			$cp_user = CpUserService::getCpUser($item['cp_source']);
    			$insert_data['cp_user_id'] = isset($cp_user->id)?$cp_user->id:'0';
    			$insert_data['cp_source'] = $item['cp_source'];
    			$insert_data['bid'] = $item['bid'];
    			$insert_data['book_name'] = $item['book_name'];
    			 
    			\Log::info('insert_daily_stat_cp_source:'.$insert_data['cp_source'].' cp_user_id:'.$insert_data['cp_user_id'].' calculate_date:'.$insert_data['calculate_date'].' bid:'.$insert_data['bid']);
    
    			//  计算公式从数据库拿
    			$cp_calculate = CpCalculate::getCpCalculate($item['cp_source']);
    			 
    			$insert_data['yestoday_charge_balance'] = $item['yestoday_charge_balance'];// 昨天书币数量
    
    			// 某些书籍特殊结算
    			// 昨天
    			if(in_array($insert_data['bid'],$cp_book_calculate_bids)){
    				$cp_book_calculate = CpBookCalculate::getCpBookCalculate($insert_data['cp_source'], $insert_data['bid']);

                    $insert_data['yestoday_available_amount'] = $item['yestoday_charge_balance']*$cp_book_calculate['available_amount_ratio'];
                    $insert_data['yestoday_last_amount'] = $item['yestoday_charge_balance']*$cp_book_calculate['last_amount_ratio'];
                    $insert_data['yestoday_total_amount'] = $item['yestoday_charge_balance']*$cp_book_calculate['total_amount_ratio'];

    			}else{
    				$insert_data['yestoday_available_amount'] = $item['yestoday_charge_balance']*$cp_calculate['available_amount_ratio'];
    				$insert_data['yestoday_last_amount'] = $item['yestoday_charge_balance']*$cp_calculate['last_amount_ratio'];
    				$insert_data['yestoday_total_amount'] = $item['yestoday_charge_balance']*$cp_calculate['total_amount_ratio'];
    			}
    			 
    			$current_cp_sub = CpSub::getCpSub($insert_data['cp_source'],$insert_data['calculate_date'],$insert_data['bid']);

    			if(empty($current_cp_sub)){
    				\Log::info('insert:'.$insert_data['bid']);
    				CpSub::firstOrcreate($insert_data);
    			}else{
    				\Log::info('not_insert:'.$insert_data['bid']);
    			}
    			 
    		}catch(\Exception $e){
    			\Log::info($e->getMessage());
    		}
    		 
    	}
    }
    
    /**
     * 生成每日统计
     */
    public static function generateCpBookDailyStat2($date) {
        if(empty($date)) return '';
        //$result = array();
        $all_cp_list = BookConfig::getAllCps();

        $start_date = date('Y-m-01');
        $end_date = date('Y-m-d',time()-86400);
        $month = date('Y-m');
        //$calculate_date = date('Y-m-d');


        //\Log::info('generateCpBookDailyStat,start_date:'.$start_date.' end_date:'.$end_date.' month:'.$month.' calculate_date:'.$calculate_date);
        $all_data_list = BookConfig::getAllCpBookConsume($start_date,$end_date,$all_cp_list);

        self::insert_daily_stat($all_data_list,$month,$date);

        //return $result;
    }
}
