<?php

namespace Admin\Controller;

use Org\Util\Date;
use Sdk\Controller\AgeController;
use User\Api\MemberApi as MemberApi;
use Org\WeixinSDK\Weixin;

class TestOrderController extends ThinkController
{
    public function lists($p = 0) {
        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        $arraypage = $page;
        $platformType = I('platform_type', 0);
        $startedAt = I('timestart', '') == '' ? I('start', '') : I('timestart', '');
        $endedAt = I('timeend', '') == '' ? I('end', '') : I('timeend', '');
        $gameId = I('game_id', 0);
        if (isset($_REQUEST['row'])) {
            $row = $_REQUEST['row'];
        } else {
            $row = 10;
        }

        $gameSets = M('game_set', 'tab_')->field(['game_id', 'pay_notify_url'])->select();
        $gameSets = index_by_column('game_id', $gameSets);
        $aggGameIds = getAggExceptIds($gameSets);

        $map['1'] = "1";
        $map['_string'] = '1=1';
        if (!empty($_REQUEST['server_type'])) {
            $map['t.server_type'] = $_REQUEST['server_type'];
        }
        if (!empty($_REQUEST['order_type'])||$_REQUEST['order_type'] == '0') {
            $map['order_type'] = $_REQUEST['order_type'];
        }
        if (!empty($_REQUEST['order_no'])) {
            $map['order_no'] = $_REQUEST['order_no'];
        }
        if (!empty($_REQUEST['user_account'])) {
            $map['user_account'] = $_REQUEST['user_account'];
        }
        if (!empty($_REQUEST['pay_way'])) {
            $map['pay_way'] = $_REQUEST['pay_way'];
        }
        if ($startedAt != '') {
            $map['_string'] .= ' and pay_time>=' . strtotime($startedAt . ' 00:00:00');
        }
        if ($endedAt != '') {
            $map['_string'] .= ' and pay_time<=' . strtotime($endedAt . ' 23:59:59');
        }
        if ($platformType == 1) {
            $map['_string'] .= ' and game_id not in(' . implode(',', $aggGameIds) . ')';
        }
        if ($platformType == 2) {
            $map['_string'] .= ' and game_id in(' . implode(',', $aggGameIds) . ')';
        }
        if ($gameId != 0) {
            $map['_string'] .= ' and game_id=' . $gameId;
        }
        if ($_REQUEST['merchant_id']) {
            $map['merchant_id'] = $_REQUEST['merchant_id'];
            unset($_REQUEST['merchant_id']);
        }
        $order_list = M('test_order t', 'tab_')
                      ->field("t.*,g.sdk_version")
                      ->join("tab_game g on t.game_id = g.id","left")
                      ->where($map)->page($page,$row)->order('add_time desc')->select();
        foreach ($order_list as $k => &$v) {
            $v['game_name'] = clearGameNameType($v['game_name']);
            $v['sdk_version'] = getGameTypeName($v['sdk_version']);
            $v['platform_type'] = in_array($v['game_id'], $aggGameIds) ? '聚合' : '联运';
        }
        $sum_order_amount = M('test_order', 'tab_')->alias("t")->where($map)->field('sum(order_amount) as sum_order_amount,count(1) as count')->find();
        $sum_pay_amount = M('test_order', 'tab_')->alias("t")->where($map)->field('sum(pay_amount) as sum_order_amount')->find();

        $count = $sum_order_amount['count'];
        $page = set_pagination($count, $row);
        if ($page) {
            $this->assign('_page', $page);//分页
        }

        $this->checkListOrCountAuthRestMap($map,[]);

        $this->assign('datas', $order_list);
        $this->assign('sum_order_amount', $sum_order_amount);
        $this->assign('sum_pay_amount', $sum_pay_amount);
        $this->display();
    }

    public function addOrder(){
        $this->display();
    }

    public function saveOrder() {

        $data = $_GET;

//        dump($data);die();
        $data['add_time'] = time();
        $data['pay_time'] = strtotime($data['pay_time']);
        $data['game_name'] = get_gamename($data['game_id']);

        $is_exist = M('test_order','tab_')->where(['order_no'=>$data['order_no'],'cp_order_no'=>$data['cp_order_no']])->find();

        if ($is_exist) {
            $this->ajaxReturn(['status'=>0,'msg'=>'订单重复录入']);
        }

        $isSuccess = M('test_order','tab_')->add($data);

        if ($isSuccess) {
            //操作日志
            addOperationLog(array(
                "op_type"=>0,
                "key"=>$data['order_no'],
                "menu"=>"充值-财务管理-测试服订单管理-订单录入",
                "url"=>U("TestOrder/lists",array("order_no"=>$data['order_no']))
            ));
            $this->ajaxReturn(['status'=>1,'msg'=>'订单录入成功']);
        } else {
            $this->ajaxReturn(['status'=>0,'msg'=>'订单录入失败']);
        }

    }

    public function del() {

        $id = I('id');

        if (!$id) {
            $this->ajaxReturn(['status'=>0,'msg'=>'id失效']);
        }

        $is_del = M('test_order','tab_')->where(['id'=>$id])->delete();

        if ($is_del) {

            $this->ajaxReturn(['status'=>1,'msg'=>'删除成功']);

        } else {

            $this->ajaxReturn(['status'=>0,'msg'=>'删除失败']);

        }

    }

}