<?php

namespace Card\Lib;

use Dever;

class Code
{
    /**
     * 分配礼品卡资源
     *
     * @return mixed
     */
    public function assign($order_id)
    {
        $card = Dever::db('card/order_card')->select(array('order_id' => $order_id));
        if ($card) {
            foreach ($card as $k => $v) {
            	$info = Dever::db('card/info')->find($v['card_id']);
            	if (!$info) {
            		continue;
            	}
            	$update = array();
            	$update['where_id'] = $v['id'];
            	$update['cards'] = array();

            	$w['option_card_id'] = $v['card_id'];
            	$w['option_status'] = 1;
            	$w['limit'] = $v['num'];
                $code = Dever::db('card/code')->getData($w);
                $total = $v['num'] - count($code);
                $u = array();
                $u['status'] = 2;
        		$u['uid'] = $v['uid'];
        		$u['order_id'] = $v['order_id'];
        		$u['order_card_id'] = $v['id'];
        		$u['city'] = $v['city'];
        		$u['total_cash'] = $info['value'];
        		$u['bdate'] = time();
                if ($code) {
                	foreach ($code as $k1 => $v1) {
                		$u['where_id'] = $v1['id'];
                		Dever::db('card/code')->update($u);
                		$update['cards'][$v1['card']] = $v1['card'];
                	}
                }
                if ($total > 0) {
                	for ($i = 0; $i< $total;$i++) {
		                $card = $this->createCode($info, $u);
		                $update['cards'][$card] = $card;
		            }
                }

                if ($update['cards']) {
                	$update['cards'] = implode(',', $update['cards']);
                	Dever::db('card/order_card')->update($update);
                }
            }
        }
    }

    /**
     * 创建兑换码
     *
     * @return mixed
     */
    public function createCard($id, $name, $param)
    {
    	$type = Dever::param('type', $param);
    	$content = Dever::param('content', $param);
        $num = Dever::param('num', $param);
        $card_id = Dever::param('card_id', $param);

        $info = Dever::db('card/info')->find($card_id);
        if ($info) {
        	if ($type == 1 && $num > 0) {
        		for ($i = 0; $i< $num;$i++) {
	                $this->createCode($info);
	            }
        	} elseif ($type == 2 && $content) {
        		$data = Dever::split($content);
        		foreach ($data as $k => $v) {
        			$this->upCode($info, $v);
        		}
        	}
        }
    }

    private function upCode($info, $card)
    {
        $card = trim($card);
        if (!$card) {
            return;
        }
        $cash = 0;
        if (strstr($card, ',')) {
            $temp = explode(',', $card);
            $card = $temp[0];
            $cash = $temp[1];
        }
        $where['clear'] = true;
        $where['card'] = $card;
        $state = Dever::db('card/code')->find($where);

        if (!$state) {
            $where['card_id'] = $info['id'];
            if ($cash > 0) {
                $where['total_cash'] = $cash;
            }
            Dever::db('card/code')->insert($where);

            return $card;
        } else {
            $where['where_id'] = $state['id'];
            $where['card_id'] = $info['id'];
            if ($cash > 0) {
                $where['total_cash'] = $cash;
            }
            Dever::db('card/code')->update($where);
            return $card;
        }
    }

    private function createCode($info, $update = false, $result = false)
    {
        # 生成卡号
        $card = $info['card_prefix'] . Dever::rand($info['card_len'], $info['card_type'] - 1);
        $where['clear'] = true;
        $where['card'] = $card;
        $state = Dever::db('card/code')->find($where);

        if (!$state) {
            $where['card_id'] = $info['id'];
            if ($update) {
            	$where = array_merge($where, $update);
            }
            $id = Dever::db('card/code')->insert($where);

            if ($result) {
                return $id;
            } else {
                return $card;
            }
        } else {
            return $this->createCode($info);
        }
    }

    # 直接创建已绑定的礼品卡
    public function create($info, $type, $uid, $order_id, $price, $bdate = false)
    {
        $update['status'] = 2;
        $update['total_cash'] = $price;
        $update['type'] = $type;
        $update['uid'] = $uid;
        $update['order_id'] = $order_id;
        if (!$bdate) {
            $bdate = time();
        }
        $update['bdate'] = $bdate;
        return $this->createCode($info, $update, true);
    }

    /**
     * 作废
     *
     * @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 check(&$code)
    {
        $code['info'] = Dever::db('card/info')->find($code['card_id']);
        if (!$code['info']) {
            return -1;
        }
        if ($code['info']['status'] == 2) {
            return -2;
        }
        if ($code['info']['end_dh_day'] && $code['info']['end_dh_day'] > 0) {
            $code['eddate'] = ($code['bdate'] ? $code['bdate'] : $code['cdate']) + ($code['info']['end_dh_day'] * 86400);
        }

        if (isset($code['eddate']) && $code['eddate'] && $code['eddate'] > 0) {
            $time = time();
            if ($code['eddate'] <= $time) {
                return -3;
            }
        }

        return 1;
    }
}