<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/29
 * Time: 11:29
 */
namespace Home\Controller;
class PromoteGiftController extends BaseController{

    public function lists($p=0){
        $promote_id = session('promote_auth.pid');
        $this->getGift($promote_id);//礼包刷新
        $map['promote_id'] = array('in',array($promote_id,0));
        $map['status'] = 1;
        empty(I('post.game_name')) || $map['game_name'] = array('like','%'.I('post.game_name').'%');
        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        $row    = 15;
        $data = M('promote_gift','tab_')->where($map)->page($page, $row)->order('id desc')->select();
        $count = M('promote_gift','tab_')->where($map)->count();
        foreach ($data as $k=>$v) {
            $map['game_id'] = $v['game_id'];
            $map['status'] = 0;
            $data[$k]['novice_num'] = M('promote_gift_record','tab_')->where($map)->count();
        }
        if($count > $row){
            $page = new \Think\Page($count, $row);
            foreach($_REQUEST as $key=>$val) {
                $page->parameter[$key]   =  $val;
            }
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $this->assign('_page', $page->show());
        }
        $this->assign('list_data',$data);
        $this->meta_title = '渠道礼包列表';
        $this->display();
    }


    /**
     * 获取礼包
     * @param $promote_id 渠道ID
     */
    public function getGift($promote_id){
        $map['promote_id'] = array('in',array(0, $promote_id));
        $map['status'] = 1;
        $gift = M('promote_gift', 'tab_')->where($map)->select();//查找该渠道游戏礼包
        foreach ($gift as $key => $value) {
            if(empty($value['novice'])){
                continue;
            }
            //礼包记录
            $record_map['gift_id'] = $value['id'];
            $record_map['game_id'] = $value['game_id'];
            $record_map['server_id'] = $value['server_id'];
            $record_map['promote_id'] = $promote_id;
            $record_map['type'] = 0;
            $last_get_time = M('promote_gift_record', 'tab_')->where($record_map)->max('create_time');//最近一次领取时间
            //该渠道礼包的游戏流水
            $spend_map['promote_id'] = $promote_id;
            $spend_map['pay_status'] = 1;
            $spend_map['game_id'] = $value['game_id'];
            $spend_map['server_id'] = $value['server_id'];
            empty($last_get_time) || $spend_map['pay_time'] = array('between', array($last_get_time, time()));
            $pay_amount = M('spend', 'tab_')->where($spend_map)->sum('pay_amount');


            if ($pay_amount >= $value['condition']) {//该渠道礼包的流水是否大于礼包的领取条件
                $gift_num = $pay_amount / $value['condition'];
                $novice = explode(',', $value['novice']);
                $promote_novice = array_slice($novice, 0, $gift_num);//渠道礼包
                $value['novice'] = implode(',', array_slice($novice, $gift_num));//剩余礼包
                M('promote_gift', 'tab_')->save($value);//剩余礼包保存
                //礼包记录
                $gift_record = array();
                $record['game_id'] = $value['game_id'];
                $record['game_name'] = $value['game_name'];
                $record['server_id'] = $value['server_id'];
                $record['gift_id'] = $value['id'];
                $record['gift_name'] = $value['giftbag_name'];
                $record['status'] = 0;
                $record['promote_id'] = $promote_id;
                $record['month'] = time();//领取月份
                $record['create_time'] = time();
                $record['type'] = 0;
                foreach ($promote_novice as $k => $v) {//根据激活码数量保存到礼包领取记录表中
                    $record['novice'] = $v;
                    $gift_record[] = $record;
                }
                M('promote_gift_record', 'tab_')->addAll($gift_record);//批量存入礼包记录
            }

            //默认礼包
            $last_month = strtotime(date("Y-m", strtotime("-1 month")));//上个月1号
            $to_month = strtotime(date("Y-m"));//这个月1号

            $def_record_map['gift_id'] = $value['id'];
            $def_record_map['game_id'] = $value['game_id'];
            $def_record_map['server_id'] = $value['server_id'];
            $def_record_map['promote_id'] = $promote_id;
            $def_record_map['type'] = 1;
            $def_record_map['create_time'] = array('egt',$to_month); //领取时间大于这个月1号
            $def_num = M('promote_gift_record', 'tab_')->where($def_record_map)->count();//最近一次默认礼包领数量
            if($def_num < $value['def_num']){
                $gift_num = $value['def_num'] - $def_num;
                $novice = explode(',', $value['novice']);
                $promote_novice = array_slice($novice, 0, $gift_num);//渠道礼包
                $value['novice'] = implode(',', array_slice($novice, $gift_num));//剩余礼包
                M('promote_gift', 'tab_')->save($value);//剩余礼包保存
                //礼包记录
                $gift_record = array();
                $record['game_id'] = $value['game_id'];
                $record['game_name'] = $value['game_name'];
                $record['server_id'] = $value['server_id'];
                $record['gift_id'] = $value['id'];
                $record['gift_name'] = $value['giftbag_name'];
                $record['status'] = 0;
                $record['promote_id'] = $promote_id;
                $record['month'] = time();//领取月份
                $record['create_time'] = time();
                $record['type'] = 1;
                foreach ($promote_novice as $k => $v) {//根据激活码数量保存到礼包领取记录表中
                    $record['novice'] = $v;
                    $gift_record[] = $record;
                }
                M('promote_gift_record', 'tab_')->addAll($gift_record);//批量存入礼包记录
            }


        }
    }

    /**
     * 获取渠道礼包激活码
     * @param $promote_id
     * @param $game_id
     */
    public function getNovice($promote_id,$game_id,$status){
        $last_month = strtotime(date("Y-m", strtotime("-1 month")));//上个月1号
        $to_month = strtotime(date("Y-m"));//这个月1号
        $map['promote_id'] = $promote_id;
        $map['game_id'] = $game_id;
        $map['status'] = $status;
        $novice = M('promote_gift_record','tab_')->where($map)->select();
        $res['status'] = 1;
        if(empty($novice)){
            $res['status'] = 0;
        }
        $res['novice'] = $novice;
        if($status == 0){
            M('promote_gift_record','tab_')->where($map)->setField(array('status'=>1));
        }
        $this->ajaxReturn($res);
    }
}