<?php

namespace Active\Lib;

use Dever;
Class Order
{
	#更新数量
    public function upNum($order_id)
    {
        $data = Dever::db('active/order')->find($order_id);
        if (!$data['refund_num']) {
            $w['refund_num'] = 0;
        } else {
            $w['refund_num'] =$data['refund_num'];
        }
        if (!$data['code_num'] || $data['code_num'] < 0) {
            $w['code_num'] = 0;
        }
        if (!$data['no_num']) {
            $w['no_num'] = 0;
        }
        #已使用的数量
        $code = Dever::db('active/code')->getCode(array('order_id'=>$data['id'],'status'=>'1,2'));
        $w['code_num'] = count($code);
        #待核销的数量
        $no_code = Dever::db('active/code')->getCode(array('order_id'=>$data['id'],'status'=>1));
        $w['no_num'] = count($no_code);
        $w['where_id'] = $data['id'];
        Dever::db('active/order')->update($w);
    }
    #生成核销码
    public function insertCode($id,$name,$data)
    {
        $active_id = Dever::param('active_id',$data);
        $content = Dever::param('content',$data);
        $contents = Dever::split($content);
        $active = Dever::db('active/info')->find($active_id);
        if ($contents) {
            foreach ($contents as $k => $v) {
                $user = Dever::db('active/user')->find(array('mobile'=>$v,'clear'=>true));
                // print_R($user);die;
                if ($user) {
                    $member = Dever::load('active/lib/user')->agent($user);
                    $code = Dever::db('active/code')->getOne(array('active_id'=>$active_id,'join_uid'=>$user['id'],'status'=>'1,2','clear'=>true));
                    if ($code) {
                        Dever::alert('您已领取过了');
                    }                
                 }
            }
        }
    }
    public function createCode($id,$name,$data) 
    {
        $active_id = Dever::param('active_id',$data);
        $content = Dever::param('content',$data);
        $contents = Dever::split($content);
        $active = Dever::db('active/info')->find($active_id);
        if ($contents) {
            foreach ($contents as $k => $v) {
                $user = Dever::db('active/user')->find(array('mobile'=>$v,'clear'=>true));
                if ($user) {
                    $member = Dever::load('active/lib/user')->agent($user);
                    if ($member && $active['status'] == 2) {
                        $this->user($active_id,$user['id']);
                    } 
                } else {
                    $member = Dever::db('agent/member')->find(array('mobile'=>$v));
                    if ($member) {
                        $insert['name'] = $member['name'];
                        $insert['mobile'] = $v;
                        $insert['idcard'] = $member['idcard'];
                        $insert['pass'] = $member['password'];
                        $insert['old_pwd'] = $member['old_pwd'];
                        $insert['old_salt'] = $member['old_salt'];
                        $uid = Dever::db('active/user')->insert($insert);
                        $this->user($active_id,$uid);
                    } else {
                        $insert['name'] = substr($v, 0, 3).'****'.substr($v, 7);
                        $insert['mobile'] = $v;
                        $uid = Dever::db('active/user')->insert($insert);
                        $this->user($active_id,$uid);
                    }
                }
            }
            return 'ok';
        }
    }
    private function user($active_id,$uid)
    {
        $w['active_id'] = $active_id;
        $w['join_uid'] = $uid;
        $w['write_code'] = Dever::load('active/home')->getCode();
        $w['type'] = 2;
        if ($uid && $uid >=1) {
            Dever::db('active/code')->insert($w);
        }
        // Dever::db('active/creat_code')->insert($w);
    }
}