<?php

namespace Code\Lib;

use Dever;

class Core
{
    public function create_test()
    {
        $order_id = Dever::input('order_id');
        $order = Dever::db('act/order')->one(array('order_id' => $order_id));
        print_r($order);
        $code = $this->createCodeByOrder($order, -1);

        return $code;
    }
	public function createCodeByOrder($order, $num = 0)
	{
		$code = '';
        if ($order['type'] == 3 && !$order['code']) {
        	Dever::setInput('set_product_id', $order['product_id']);
            # 购买兑换码
            $product_num = 1;
            $code_num = 1;
            if ($order['buy_id'] > 0) {
                $buy = Dever::load('act/order')->getBuy($order['buy_id']);
                $product_num = $buy['num'];
                $code_num = $buy['code'];
            }
            
            # 检查之前有多少个兑换码，防止多发
            $code_where['order_id'] = $order['order_id'];
        	$total = Dever::db('code/info')->total($code_where);
        	if ($total && $total > 0) {
        		if ($code_num > $total) {
        			$code_num = $code_num - $total;
        		} else {
        			$one = Dever::db('code/info')->one($code_where);
        			return $one['code'];
        		}
        	}

            if ($code_num > 1) {
                $product_num = intval($product_num/$code_num);

                if ($num < 0) {
                    # 订阅
                    $state = Dever::load('act/lib/subscribe')->submit($order['uid'], $order['product_id'], 1);
                    if ($state) {
                        $code = $this->createCode($order['cate_id'] . ',' . $order['product_id'], $order['cate_id'], $order['product_id'], $order['seller_id'], $product_num, $order['uid'], $order['order_id'], 0, 3, 1);
                    } else {
                        $num = 0;
                    }
                	
                }
                $result = $this->createCode($order['cate_id'] . ',' . $order['product_id'], $order['cate_id'], $order['product_id'], $order['seller_id'], $product_num, $order['uid'], $order['order_id'], 0, 1, $code_num + $num);
                if ($num == 0) {
                    $code = $result;
                }
            } else {
                # 订阅
                $state = false;
                if ($num < 0) {
                    $state = Dever::load('act/lib/subscribe')->submit($order['uid'], $order['product_id'], 1);
                }
                
                if ($state) {
                    $code = $this->createCode($order['cate_id'] . ',' . $order['product_id'], $order['cate_id'], $order['product_id'], $order['seller_id'], $product_num, $order['uid'], $order['order_id'], 0, 3);
                } else {
                    $code = $this->createCode($order['cate_id'] . ',' . $order['product_id'], $order['cate_id'], $order['product_id'], $order['seller_id'], $product_num, $order['uid'], $order['order_id'], 0, 1);
                }
            }
        }

        return $code;
	}

	public function createCode($product, $cate_id, $product_id, $seller_id, $product_num = 0, $create_uid = -1, $order_id = false, $score = 0, $type = 1, $num = 1)
	{
        if ($product_id > 0) {
            Dever::setInput('set_product_id', $product_id);
        }
        
		//$data['product'] = $product;
		$data['cate_id'] = $cate_id;
		$data['product_id'] = $product_id;
		$data['seller_id'] = $seller_id;
		$data['product_num'] = $product_num;
		
		$data['type'] = $type;
		if ($order_id) {
			$data['order_id'] = $order_id;
		}
		if ($create_uid > 0) {
			$data['create_uid'] = $create_uid;
            if ($type == 3) {
                $data['ldate'] = time();
                $data['uid'] = $create_uid;
            }
		}
        $data['score'] = $score;

        $db = Dever::db('code/info');
        if ($product_id > 0) {
            $code = $db->insert($data, $num);
            $code = $this->view($code, true);
            return $code;
        } else {
            for ($i = 0; $i <= $num; $i++) {
                $id = $db->insert($data);
                if ($id) {
                    $code = $this->view($id, false);
                    $db->update(array('code' => $code, 'where_id' => $id));
                }
            }

            return $code;
        }
	}

    public function view($id, $state)
    {
        if ($state) {
            $code = Dever::uid($id * 10000000);
            $code = 'M' . $code;
        } else {
            $code = Dever::rand(4);
            $code = 'T' . $code . $id;
        }
        
        return $code;
    }
}