| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 | <?phpnamespace 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, $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)    {        $update['status'] = 2;        $update['total_cash'] = $price;        $update['type'] = $type;        $update['uid'] = $uid;        $update['order_id'] = $order_id;        $update['bdate'] = time();        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';    }}
 |