<?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;
        }
        $where['clear'] = true;
        $where['card'] = $card;
        $state = Dever::db('card/code')->find($where);

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

            return $card;
        } else {
            return false;
        }
    }

    private function createCode($info, $update = 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);
            }
            Dever::db('card/code')->insert($where);

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

    /**
     * 作废
     *
     * @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';
    }
}