<?php

namespace Bill\Lib;

use Dever;

class Cron
{
    public function sell_api()
    {
    	# 按月份统计团队业绩
        $month = Dever::input('month', date('Y-m'));
        $start_time = $month .'-01 00:00:00';
		$end_time = date('Y-m-d', strtotime($start_time. ' +1 month -1 day')).' 23:59:59';
		$where['type'] = '2,10';
        $where['start'] = Dever::maketime($start_time);
        $where['end'] = Dever::maketime($end_time);
        $data = Dever::db('bill/sell')->getNumByTime($where);
        if ($data) {
        	foreach ($data as $k => $v) {
        		if ($v['num'] > 0) {
        			$w['mid'] = $v['mid'];
        			$w['month'] = $where['start'];
        			$info = Dever::db('bill/sell_stat')->find($w);
        			$w['new_num'] = $v['num'];
        			$where['mid'] = $v['mid'];
        			unset($where['start']);
        			$total = Dever::db('bill/sell')->getNumByTimeOne($where);
        			if ($total) {
        				$w['num'] = $total['num'];
        			}
        			if ($info) {
        				$w['where_id'] = $info['id'];
        				Dever::db('bill/sell_stat')->update($w);
        			} else {
        				Dever::db('bill/sell_stat')->insert($w);
        			}
        		}
        	}
        }

        return 'ok';
    }

    # 区域分润
    public function area_api()
    {
        # 获取所有代理商
        $where['status'] = 2;
        $member = Dever::db('agent/member')->select($where);
        if ($member) {
            foreach ($member as $k => $v) {
                # 获取区域内门店的利润
                $role = Dever::db('setting/role')->find($v['role']);
                $temp = explode(',', $v['area']);
                if ($role['type'] == 2) {
                    if ($role['area'] == 1) {
                        # 城市
                    } elseif ($role['area'] == 2) {
                        # 区县
                    } elseif ($role['area'] == 3) {
                        # 街道
                    }
                }
            }
        }
    }
}