<?php

namespace app\order\controller;

use app\order\model\UserChargeOrder;
use fastphp\base\Controller;

class ChargeOrder extends Controller
{
    public function getList()
    {
        $get = $_GET;
        $where = [];
        $date1 = date('Y-m-d',time());
        $date2 = date("Y-m-d H:i:s", time());
        if(!empty(trim($get['keyWord']))){
            $where['Channel'] = $get['keyWord'];
        }
        if (!empty($get['Btime'])) {
            $date1 = date('Y-m-d', strtotime($get['Btime']));
            $date2 = date("Y-m-d 23:59:59", strtotime($get['Etime']));
        }
        $charge_model = new UserChargeOrder;
        $list = $charge_model->field('Channel,sum(PaymentAmount) Amount')->where($where)->where("Channel IS NOT NULL AND TradeNo IS NOT NULL AND CreateTime>'".$date1."' AND CreateTime<'".$date2."'")->group(['Channel'])->fetchAll();
        $data = [
            'Code' => 10000,
            'TotalCount' => count($list),
            'Data' => $list,
        ];
        echo json_encode($data);
    }

    public function takeCharge()
    {
        $charge_model = new UserChargeOrder;
        $get = $_GET;
        if($charge_model->updateTake(['UserName'=>$get['phone'],'PaymentAmount'=>$get['amount']],"CreateTime>'".date('Y-m-d H:i:s',time()-3600)."' AND Channel IS NULL AND TradeNo IS NOT NULL AND CreateTime<'".date("Y-m-d H:i:s", time())."'",['Channel'=>$this->userinfo['LoginName']])){
            $data = [
                'Code' => 20000,
                'Message' => '认领成功'
            ];
        } else {
            $data = [
                'Code' => 20000,
                'Message' => '认领失败'
            ];
        }
        echo json_encode($data);
    }
}
