<?php

namespace Card\Lib;

use Dever;

class Manage
{
    # 获取用户信息
    public function user($id)
    {
        $info = Dever::db('card/order')->one($id);

        if ($info['uid'] && $info['uid'] > 0) {
            $user = Dever::db('passport/user')->one($info['uid']);

            $result = $user['username'];
            if ($info['mobile']) {
                $result .= '('.$info['mobile'].')';
            }
        } else {
            $result = $info['mobile'];
        }
        
        return $result;
    }

    /**
     * 获取礼品卡数量
     *
     * @return mixed
     */
    public function getNum($id)
    {
        //$info = Dever::db('card/info')->find($id);
        $where['card_id'] = $id;
        return Dever::db('card/code')->total($where);
    }
    public function getCardNum($id)
    {
        $where['card_id'] = $id;
        $html = array();
        #兑换的数量
        $dnum = Dever::db('card/code')->getNum(array('card_id'=>$id,'status'=>3,'state'=>1));
        #发放的数量
        $num = Dever::db('card/code')->total($where);
        return $dnum.'/'.$num;
    }
    public function getDprice($id,$type=false){
        $card = Dever::db('card/info')->find($id);
        #已兑换的面值
        $dui_total = 0;
        if($card['create_type'] != 3){
            $dnum = Dever::db('card/code')->getNum(array('card_id'=>$id,'status'=>3,'state'=>1));
            $dui_total = $dnum*$card['value'];
            
        }else{
            $dui = Dever::db('card/code')->getDuiprice(array('card_id'=>$id,'status'=>3,'state'=>1));
            $dui_total = $dui['total'];
        }
        if(!$type){
            return $dui_total;
        }
        #已使用的面值
        $info = Dever::db('card/code')->getDprice(array('card_id'=>$id,'state'=>1));
        $use_total = sprintf ('%01.2f',$info['total']);
        if($type==1){
            return $use_total;
        }
        #未兑换的面值
         $wdui_total = 0;
        if($card['create_type'] != 3){
            $where['card_id'] = $id;
            $num = Dever::db('card/code')->total($where);
            $wdui_total = ($num-$dnum) *$card['value'];
        }else{
            $wdui = Dever::db('card/code')->getDuiprice(array('card_id'=>$id,'status'=>'1,2','state'=>1));
            $wdui_total = $wdui['total'];
        }
        if($type == 2){
            return $wdui_total;
        }
       
    }
    /**
     * 显示用户信息
     *
     * @return mixed
     */
    public function showUserInfo($id)
    {
        $info = Dever::db('card/code')->find($id);
        $table = array();
        if ($info && $info['status'] > 1) {
            if ($info['uid']) {
                $user = Dever::load('passport/user-one', $info['uid']);
                if ($user) {
                    $table['购买人'] = $user['username'] . '('.$user['mobile'].')';
                    $table['下单时间'] = date('Y-m-d H:i:s', $info['cdate']);
                    $table['支付时间'] = date('Y-m-d H:i:s', $info['bdate']);
                }
            }

            if (isset($info['dh_uid']) && $info['dh_uid'] && $info['dh_uid'] > 0) {
                $user = Dever::load('passport/user-one', $info['dh_uid']);
                if ($user) {
                    $table['兑换人'] = $user['username'] . '('.$user['mobile'].')';
                    $table['兑换时间'] = date('Y-m-d H:i:s', $info['ddate']);
                }
            }
        }

        if (!$info['total_cash']) {
            $card_info = Dever::db('card/info')->find($info['card_id']);
            $info['total_cash'] = $card_info['value'];
        }
        if (!$info['use_cash']) {
            $info['use_cash'] = 0;
        }
        $table['总面值'] = $info['total_cash'];
        $table['已用面值'] = $info['use_cash'];
        $table['剩余面值'] = $info['total_cash'] - $info['use_cash'];

        if ($table) {
            return Dever::table($table);
        }

        return '暂无';
    }

    /**
     * 更新信息
     *
     * @return mixed
     */
    public function orderPs($id, $name, $data)
    {
        $order_id = Dever::param('order_id', $data);
        if ($order_id) {
            $update['where_id'] = $order_id;
            $update['set_status'] = 3;
            Dever::db('card/order')->update($update);
        }
    }

    /**
     * 更新信息
     *
     * @return mixed
     */
    public function orderSh($id, $name, $data)
    {
        $order_id = Dever::param('order_id', $data);
        if ($order_id) {
            $update['where_id'] = $order_id;
            $update['set_status'] = 4;
            Dever::db('card/order')->update($update);
        }
    }

    # 收货
    public function send_api()
    {
        return Dever::load('card/lib/buy')->send();
    }

    /**
     * 作废
     *
     * @return mixed
     */
    public function drop_api($id)
    {
        $update['where_id'] = $id;
        $update['type'] = 4;
        Dever::db('card/code')->update($update);
        return 'ok';
    }

    public function recovery_api($id)
    {
        $update['where_id'] = $id;
        $update['type'] = 1;
        Dever::db('card/code')->update($update);
        return 'ok';
    }

    public function outCode($data)
    {
        $header = array('礼品卡名称', '卡号', '总面值', '已用面值', '剩余面值', '使用状态', '兑换时间');
        $body = array();
        
        $config = Dever::db('card/code')->config;
        foreach($data as $k => $v) {
        
            $card = Dever::db('card/info')->find($v['card_id']);
            $date = '';
            if ($v['ddate']) {
                $date = date('Y-m-d H:i', $v['ddate']);
            }
            $status = $config['status'][$v['status']];
            $d = array
            (
                $card['name'],
                $v['card'] . "\t", 
                $v['total_cash'],
                $v['use_cash'],
                round($v['total_cash'] - $v['use_cash'], 2),
                $status,
                $date,

            );

            $body[] = $d;
        }
        $file = Dever::input('excel_name');
        return Dever::excelExport($body, $header, $file);
    }
}