<?php namespace Place\Lib;
use Dever;
use Place;
class Client
{
    public function get($info, $app, $type, $button)
    {
        if (Place::$user['client_id'] <= 0) {
            Dever::error('您不是源主');
        }
        $result = array();
        $result['status'] = 2;
        $result['text'] = '';
        $result['original'] = $info['price'];
        $result['value'] = $info['price'];
        $result['button'] = $button;
        $result['list'] = array();

        $client_resource = Dever::db('client_resource', 'place')->find(array('uid' => Place::$uid, 'client_id' => Place::$user['client_id'], 'type' => $type, 'type_id' => $info['id']));
        if ($client_resource && $client_resource['status'] == 1) {
            $result['status'] = 1;
        }

        # 基础折扣
        $base = Dever::db('client_price', 'place')->find(array('type' => $type, 'status' => 1));
        if ($base && $base['per']) {
            $per = $base['per'];
            $result['value'] = Dever::load('price', 'place')->getValue($per, $result['value'], 3);
        }
        
        $client = Dever::db('client', 'place')->select(array('status' => 1), array('order' => 'day asc,sell desc', 'col' => 'id,day,sell,per'));
        $price = false;
        if ($client) {
            $set = array('col' =>  'sum(num) as num');
            foreach ($client as $k => $v) {
                $v['value'] = Dever::load('price', 'place')->getValue($v['per'], $result['value'], 3);
                $v['name'] = $v['day'] . '天内总销量>=' . $v['sell'];
                $v['text'] = Dever::load('price', 'place')->getText($v['value'], $type);
                $result['list'][] = $v;
                # 获取时效内销量
                $day = time() - $v['day']*86400;
                $where = array('uid' => Place::$uid, 'type' => $type, 'day' => array('>=', $day));
                $sell = Dever::db('order_stat', 'place')->find($where, $set);
                if ($sell && $sell['num'] >= 0) {
                    # 获取自定义销量
                    $resource = Dever::db('client_price', $app)->select(array('client_id' => $v['id']), array('order' => 'sell desc'));
                    if ($resource) {
                        foreach ($resource as $v1) {
                            if ($v1['sell'] > 0) {
                                $v['value'] = Dever::load('price', 'place')->getValue($v1['value'], $info['value'], 3);
                                $v['name'] = $v['day'] . '天内本品销量>=' . $v1['sell'];
                                $v['text'] = Dever::load('price', 'place')->getText($v['value'], $type);
                                $result['list'][] = $v;
                                if (!$price) {
                                    $where['type_id'] = $v1['info_id'];
                                    $sell = Dever::db('order_stat', 'place')->find($where, $set);
                                    if ($sell && $sell['num'] >= $v1['sell']) {
                                        $price = $v['value'];
                                    }
                                }
                            }
                        }
                    }
                    if (!$price && $sell['num'] >= $v['sell']) {
                        $price = $v['value'];
                    }
                }
            }
        }
        if ($price) {
            $info['value'] = $price;
        }
        $result['text'] = Dever::load('price', 'place')->getText($result['value'], $type);
        return $result;
    }

    # 同步资源
    public function resource($place, $data, $resource, $info)
    {
        $relation['info'] = $info;
        $relation['cate'] = Dever::db('cate', $resource->app)->select(array('id' => array('in', $info['cate'])), array('order' => 'parent_id asc'));

        if ($data['type'] == 3 && $info['spec_type'] == 3) {
            # 还要同步一下sku
            $relation['spec'] = Dever::db('spec', $resource->app)->select(array('info_id' => $info['id']));
            if ($relation['spec']) {
                foreach ($relation['spec'] as &$v) {
                    $v['value'] = Dever::db('spec_value', $resource->app)->select(array('spec_id' => $v['id']));
                }
            }
            $relation['sku'] = Dever::db('sku', $resource->app)->select(array('info_id' => $info['id']));
        }

        $place_id = Place::$id;
        foreach ($place as $v1) {
            # 切换身份
            Dever::load('common', 'manage')->setAuth(Place::$system['partition'], Place::$system['id'], Place::$sector['id'], Place::$module['id'], $v1['id']);
            $resource->relation($place_id, $data, $relation);
        }
    }

    # 删除已同步的资源
    public function resourceDel($place, $data)
    {
        $resource = new \Place\Lib\Resource($data['type']);
        $place = explode(',', $place);
        $place_id = Place::$id;
        foreach ($place as $k => $v) {
            # 切换身份
            Dever::load('common', 'manage')->setAuth(Place::$system['partition'], Place::$system['id'], Place::$sector['id'], Place::$module['id'], $v);
            $resource->relationDel($place_id, $data);
        }
    }
}