<?php
namespace Active\Lib;

Use Dever;
Class Cron{
	#活动订单定时关闭
    public function job_api()
    {
    	$data = Dever::db('active/order')->state(array('status'=>1));
    	$time = time();
    	if ($data) {
    		foreach ($data as $k => $v) {
    			if ($v['cdate'] && $time-$v['cdate']>900) {
    				Dever::db('active/order')->update(array('where_id'=>$v['id'],'set_status'=>3));
    			}
                $active = Dever::db('active/info')->find($v['active_id']);
                if ($time > $active['act_end']) {
                    Dever::db('active/order')->update(array('where_id'=>$v['id'],'set_status'=>4));
                }
    		}
    	}
    	return 'ok';
    }
    #活动状态
    public function status_api()
    {
        $data = Dever::db('active/info')->state();
        $time = time();
        if ($data) {
            $where = array();
            foreach ($data as $k =>$v) {
                if ($v['sign_start']) {
                    $ids = $this->start($v['cdate'],$v['act_start'],$v['act_end'],$v['sign_start'],$v['sign_end']);
                } else {
                   $ids = $this->start($v['cdate'],$v['act_start'],$v['act_end']);
                }
                $where['where_id'] = $v['id'];
                $where['act_status'] = $ids['act_status'];
                Dever::db('active/info')->update($where);
            }
        }
        return 'ok';
    }
    public function code_api()
    {
        $data = Dever::db('active/code')->state();
        if ($data) {
            foreach ($data as $k => $v) {
                if ($v['active_id']) {
                    $active = Dever::db('active/info')->find($v['active_id']);
                    if ($active['sign_start']) {
                        $ids = $this->start($active['cdate'],$active['act_start'],$active['act_end'],$active['sign_start'],$active['sign_end']);
                    } else {
                       $ids =  $this->start($active['cdate'],$active['act_start'],$active['act_end']);
                    }
                    $where['where_id'] = $v['id'];
                    $where['act_status'] = $ids['act_status'];
                    Dever::db('active/code')->update($where);
                }
            }
            return 'ok';
        }
    }

    public function start($cdate, $act_start, $act_end, $sign_start = false, $sign_end = false)
    {
        $time = time();
        if ($sign_start) {
            if ($time < $sign_start){
                $where['act_status'] = 1;
            } elseif ($sign_end) {
                if ($time >= $sign_start && $time < $sign_end) {
                    $where['act_status'] = 2;
                } elseif ($time > $sign_end && $time < $act_start) {
                    $where['act_status'] = 3;
                } elseif ($time > $act_start && $time < $act_end) {
                    $where['act_status'] = 4;
                } elseif ($time > $act_end) {
                    $where['act_status'] = 5;
                }
            } 
        } else {
            if($time < $cdate) {
                $where['act_status'] = 1;
            } elseif ($time > $cdate && $time < $act_start) {
                $where['act_status'] = 2;
            } elseif ($time > $act_start && $time < $act_end) {
                $where['act_status'] = 4;
            } elseif ($time > $act_end) {
                $where['act_status'] = 5;
            }
        }
        return $where;
    }
    #更新user表
    public function user_api()
    {
        $data = Dever::db('active/order')->state();
        foreach ($data as $k => $v) {
            if ($v['mid'] && $v['mid'] > 0) {
                $member = Dever::load('active/lib/user')->agent($v, 'mid');
                if ($member && $member['mobile']) {
                    $user = Dever::db('active/user')->find(array('mobile'=>$member['mobile'], 'clear' => true));
                    if (!$user) {
                        $insert['name'] = $member['name'];
                        $insert['mobile'] = $member['mobile'];
                        $insert['idcard'] = $member['idcard'];
                        $insert['pass'] = $member['password'];
                        $insert['old_pwd'] = $member['old_pwd'];
                        $insert['old_salt'] = $member['old_salt'];
                        $insert['clear'] = true;
                        $uid = Dever::db('active/user')->insert($insert);
                        $ids = Dever::db('active/order')->update(array('where_id' => $v['id'], 'uid' => $uid));
                    } else {
                        $uid = $user['id'];
                        $ids = Dever::db('active/order')->update(array('where_id' => $v['id'], 'uid' => $user['id']));
                    }
                    if ($v['status'] == 2 && $uid) {
                        $code = Dever::db('active/code')->find(array('active_id'=> $v['active_id'],'join_uid'=>$uid,'clear'=>true));
                        if (!$code) {
                            $w['active_id'] = $v['active_id'];
                            $w['join_uid'] = $uid;
                            $w['buy_uid'] = $uid;
                            $w['order_id'] = $v['id'];
                            $w['clear'] = true;
                            $w['write_code'] = $this->getCode();
                            $code_id = Dever::db('active/code')->insert($w);
                            if ($code_id) {
                                $where['where_id'] = $v['id'];
                                $where['code_num'] = 1;
                                $where['no_num'] = 0;
                                $where['clear'] = true;
                                Dever::db('active/order')->update($where);
                            }
                        }
                    }
                } 
            }
        }
    }
    public function bcode_api()
    {
        $order_id = Dever::input('order_id');
        $status = 2;
        $order = Dever::db('active/order')->one(array('id' => $order_id, 'clear' => true));
        if ($order && $status == 2) {
            
            $code = Dever::db('active/code')->find(array('join_uid'=>$order['uid'],'active_id'=>$order['active_id']));
            if (!$code) {
                $active = Dever::db('active/info')->find($order['active_id']);
                $insert['join_uid'] = $order['uid'];
                $insert['active_id'] = $order['active_id'];
                $insert['buy_uid'] = $order['uid'];
                $insert['order_id'] = $order['id'];
                $insert['write_code'] = $this->getCode();
                $insert['act_status'] = $active['act_status'];
                $ids = Dever::db('active/code')->insert($insert);
                if ($ids) {
                    $update['code_num'] = 1;
                }
            } 
            $update['status'] = 2;
            $update['where_id'] = $order['id'];
            
            Dever::db('active/order')->update($update);
        }
        return 'ok';
    }
    #活动核销码
    public function getCode()
    {
        $where['write_code'] = Dever::rand(6, 0);
        $state = Dever::db('active/code')->one($where);
        if (!$state) {
            return $where['write_code'];
        } else {
            return $this->getCode();
        }
    }
    #更改订单状态
    public function update_order_api()
    {
        $data = Dever::db('active/order')->state();
        foreach ($data as $k => $v) {
            if ($v['num'] == $v['refund_num']) {
                $where['where_id'] = $v['id'];
                $where['status'] = 6; 
            }
            if ($v['refund_num'] >0 && $v['refund_num'] != $v['num']) {
                $where['where_id'] = $v['id'];
                $where['status'] = 7;
            }
            if (isset($where) && $where) {
                Dever::db('active/order')->update($where);
            }
        }
        return 'ok';
    }
}