| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 | <?php# 核心类,数据获取类namespace Goods\Lib;use Dever;class Info{	private $table_info = 'goods/info';	private $table_attr = 'goods/info_attr';    /**     * instance     *     * @var string     */    protected static $instance;    /**     * init     *     * @return mixed     */    public static function init($top_category = false)    {        if (empty(self::$instance[$top_category])) {            self::$instance[$top_category] = new self($top_category);        }        return self::$instance[$top_category];    }	public function __construct($top_category = false)	{		$this->top_category = $top_category;        # 必有有分类才能进入到需求中        if (!$this->top_category) {            $this->top_category = -1;            //Dever::alert('错误的分类信息');        }	}    private function setAttr($cate, $order = 'list_reorder')    {        if ($order != 'list_reorder') {            Dever::config('base')->attrOrder = $order;        }                Dever::setInput('search_option_category', $cate);        $attr = Dever::db($this->table_attr, array(), $cate);        return $attr;    }    public function getAttr($cate)    {        return Dever::load('attr/api')->getAttrByCate($cate, 'list');    }    # 根据分类获取需求列表数据    public function getData($cate = false, $limit = '0,10', $page = false, $attr = true)    {        if (!$limit) {            $limit = '0,10';        }        $where = $data = array();        if (!$cate) {            if ($this->top_category > 0) {                $where['top_category_id'] = $this->top_category;            }        } elseif (strstr($cate, ',')) {            $where['category'] = $this->top_category . ',' . $cate;        } else {            $where['second_category_id'] = $cate;        }                if ($page) {            $method = 'getPageAll';        } else {            $where['limit'] = $limit;            $method = 'getAll';        }        $name = Dever::input('name');        if ($name) {            $where['name'] = $name;        }        $data = Dever::db($this->table_info)->$method($where);        if ($data && $attr) {            foreach ($data as $k => $v) {                $data[$k] = $this->info($v, 'list_reorder');            }        }        return $data;    }    # 根据属性获取需求列表数据 这里带有检索属性的功能 此处还需优化    public function getDataByAttr($cate = false, $where = array())    {        if (!$cate) {            Dever::alert('错误的分类信息');        }        $where_sql = array();        $name = Dever::input('name');        if ($name) {            //$where['name'] = $name;            $where_sql[] = ' t_2.name = "'.$name.'"';        }        $attr = $this->setAttr($this->top_category . ',' . $cate);        //print_r($attr->config['attr']);        $where_sql = Dever::load('attr/api')->getSql($attr->config['attr'], $where, $where_sql);        if (!$where_sql) {            return array();        }        # 这里用pdo的数据绑定,字符串字段的区间查询就是有问题。。。只能用sql了        //$data = $attr->getPageAll($where);        $page['template'] = 'list';        $page['num'] = 10;        $where_sql = implode(' and ', $where_sql);        $sql = 'SELECT t_2.* FROM `info_goods_'.$attr->config['name'].'` as t_1 Left Join `info_goods_info` AS t_2 ON t_1.`info_id`=t_2.`id` WHERE '.$where_sql.' order by t_2.`reorder` desc,t_2.`id` desc';        $data = $attr->fetchAll($sql, array(), $page);        if ($data) {            foreach ($data as $k => $v) {                $data[$k] = $this->info($v, 'list_reorder');            }        }        return $data;    }	# 获取列表页的搜索条件    public function getSearch($cate = false)    {    	# 获取搜索条件        if (!$cate) {            $search_cate = $cate = $this->top_category;        } else {            $search_cate = $cate;        }        # 1是当前用户所在的城市,暂时写死        # 获取搜索条件        $search = Dever::load('attr/api')->getSearch($search_cate, true, 1, false, 2);        # 下级分类        $search['cate'] = Dever::load('category/api')->getList($cate);        # 上级分类        $search['parent_cate'] = Dever::load('category/api')->getList($this->top_category);        return $search;    }    # 获取支付信息    public function getPayState($uid, $id, $category)    {        $pay_where['category'] = $category;        $pay = Dever::db('goods/pay')->one($pay_where);        $pay_state = 0;        if ($pay && $pay['num'] > 0) {            $pay_state = $pay['num'];        } elseif (strstr($category, ',')) {            $category = explode(',', $category);            $category = array_pop($category);            $pay_state = $this->getPayState($uid, $id, $category);        }        //$update['where_id'] = $id;        //$update['poster_pay_type'] = $pay_state;        //Dever::db($this->table_info)->update($update);        return $pay_state;    }    # 更新需求    public function update($top, $cate, $data, $id, $uid)    {        if ($cate) {            $category = $top . ',' . $cate;        } else {            $category = $top;        }        $attr = $this->setAttr($category);        $attr_update = Dever::preInput('attr_');        if ($attr_update) {            foreach ($attr_update as $k => $v) {                if (strstr($k, '_s')) {                    $k = str_replace('_s', '', $k);                    $attr_update[$k] = $v;                }                if (strstr($k, '_e')) {                    $k = str_replace('_e', '', $k);                    $attr_update[$k] .= ',' . $v;                }            }        }        $update = array();        $update['name'] = $data['name'];        $update['category'] = $category;        $update['pic'] = $data['pic'];        //$update['pic_cover'] = $data['pic'];        $update['poster_uid'] = $uid;        if (isset($data['view_price']) && $data['view_price']) {            $update['view_price'] = $data['view_price'];        }        if ($uid && $id) {            # 此处要判断是用户自己的            $info = Dever::db($this->table_info)->one($id);            /*            if ($uid == $info['poster_uid']) {                Dever::alert('错误的用户参数');            }            */            $update['where_id'] = $attr_update['where_id'] = $id;            Dever::db($this->table_info)->update($update);            Dever::db($this->table_attr)->update($attr_update);            $pay = 0;        } else {            $id = Dever::db($this->table_info)->insert($update);            $attr_update['category'] = $update['category'];            $attr_update['info_id'] = $id;            $attr_update['id'] = $id;            Dever::db($this->table_attr)->insert($attr_update);            # 验证是否需要支付,根据category判断            $pay = false;            //$pay = $this->getPayState($uid, $id, $category);            $info = Dever::db($this->table_info)->one($id);            $info = $this->getInfoLink($info);        }        return array('id' => $id, 'pay' => $pay, 'cate' => $category, 'top' => $top, 'info' => $info);    }    # 获取支付所需要的信息    public function getPayInfo($id, $sku, $num = 1, $user = array())    {        $info = Dever::db($this->table_info)->getOne($id);        if ($info) {            $info['freight_id'] = 0;            $info['freight_price'] = 0;            if ($sku > 0) {                $where['info_id'] = $info['id'];                $where['id'] = $sku;                $sku = Dever::db('goods/info_sku')->getOne($where);                if ($sku) {                    $info['price'] = $sku['price'];                    $info['num'] = $sku['num'];                }            }            $freight = $this->freight($info, $info['price'], $user, $num);            if ($freight) {                $info['freight_id'] = $freight['id'];                $info['freight_price'] = $freight['price'];            }            # 佣金计算            $info['reward'] = $this->reward($info, $info['price']*$num);        }        return $info;    }    # 获取自提信息    public function storeInfo($goods_id, $area_id = '')    {        $info = Dever::db($this->table_info)->getOne($goods_id);        if (!$info) {            return array();        }        $array = explode(',', $info['category']);        $cate = $store = array();        $total = count($array);        $total -= 1;        # 用户所在地理位置         if ($area_id) {            $where['area'] = $area_id;        }        foreach ($array as $k => $v) {            $cate[$k] = $v;            if ($k < $total) {                $cate[] = '-1';            }            $where['category'] = $cate;            $data = Dever::db('goods/store')->state($where);            if ($data) {                $store += $data;            }        }        return $store;    }    # 获取运费信息    public function getFreight($id, $goods_price, $user, $num)    {        $info = Dever::db($this->table_info)->getOne($id);        return $this->freight($info, $goods_price, $user, $num);    }    # 获取基本信息    public function getInfo($id, $attr = true, $order = 'view_reorder', $user = array(), $reward = false)    {        $info = Dever::db($this->table_info)->getOne($id);        if ($info && $attr) {            $info = $this->info($info, $order, false, $user, $reward);        }        if ($info['pic']) {            $info['pic'] = explode(',', $info['pic']);        }        return $info;    }    # 获取基本信息    public function info($info, $key = 'list_reorder', $is_sell = false, $user = array(), $reward = false)    {        if ($info['price_type'] == 2) {                        $attr = $this->setAttr($info['category'], $key);            $attr_data = $attr->one($info['id']);            $info['category_array'] = Dever::load('category/api')->string($info['category']);            $info['attr'] = $info['sell_attr'] = array();            if ($attr->config['attr']) {                foreach ($attr->config['attr'] as $k => $v) {                    if (isset($v[$key]) && $v[$key] > 0) {                        if (isset($attr_data['attr_' . $v['id']])) {                            $v['value'] = $attr_data['attr_' . $v['id']];                        } else {                            continue;                            $v['value'] = '';                        }                        $v['value_string'] = Dever::load('attr/api')->getValue($v);                                                if ($v['ename'] == 'price') {                            $info['price'] = $v['value_string'];                        } else {                            $v['bg'] = 'background-image: url('.$v['icon'].')';                            $v['pic'] = '<img src="'.$v['icon'].'" width="18" />';                            if ($is_sell > 0) {                                if ($is_sell == $v['is_sell']) {                                    $info['attr'][] = $v;                                }                                                            } else {                                $info['attr'][] = $v;                                # 设置sku                                $this->sku($v, $info);                            }                        }                    }                }            }        } else {            $price = $info['price'];            $s_price = $info['s_price'];            $info['price'] = array();            $info['price']['list'] = array();            $info['price']['min'] = array            (                'price' => $price,                's_price' => $s_price            );            $info['price']['max'] = array();        }        $info['platform_name'] = Dever::db('goods/info')->config['config_platform'][$info['platform']];               if (isset($info['cdate']) && $info['cdate']) {            $info['cdate_string'] = Dever::mdate($info['cdate'], 2);        }        $info = $this->getInfoLink($info);        $info['freight'] = 0;        if ($info['platform'] == 1 && $user) {            # 获取运费            $info['freight'] = $this->freight($info, $info['price']['min']['price'], $user);        }        # 佣金计算        if ($reward) {            $info['reward'] = $this->reward($info, $info['price']['min']['price']);        }    	return $info;    }    # 佣金计算    private function reward($info, $price)    {        if ($info['reward_value'] && $info['reward_value'] > 0) {            if ($info['reward_type'] == 2) {                $info['reward_value'] = round(($info['reward_value']/100) * $price, 2);            }            return $info['reward_value'];        }        $array = explode(',', $info['category']);        $cate = $reward = array();        $total = count($array);        $total -= 1;        foreach ($array as $k => $v) {            $cate[$k] = $v;            if ($k < $total) {                $cate[] = '-1';            }            $where['category'] = $cate;            $data = Dever::db('goods/reward')->one($where);            if ($data) {                $reward = $data;            }        }        if ($reward) {            if ($reward['value'] && $reward['value'] > 0) {                if ($reward['type'] == 2) {                    $reward['value'] = round(($reward['value']/100) * $price, 2);                }                return $reward['value'];            }        }        return 0;    }    # 运费计算,暂时用省份代表地区    private function freight($info, $goods_price, $user = array(), $num = 1)    {        $array = explode(',', $info['category']);        $cate = $freight = array();        $total = count($array);        $total -= 1;        if ($info && $info['goods_area']) {            $where['goods_area'] = $info['goods_area'];        }        # 用户所在地理位置         if ($user && isset($user['area_id']) && $user['area_id']) {            $area = explode(',', $user['area_id']);            $where['area'] = $area[0];        }        foreach ($array as $k => $v) {            $cate[$k] = $v;            if ($k < $total) {                $cate[] = '-1';            }            $where['category'] = $cate;            $data = Dever::db('goods/freight')->one($where);            if ($data) {                $freight = $data;            }        }        if ($freight) {            $price = $freight['first_price'];            if ($num > $freight['first_num']) {                $num = $num - $freight['first_num'];                $price = $freight['first_price'] + ($freight['next_price']*$num);            }            if ($freight['type'] == 2) {                $freight['price'] = round($goods_price*$num*($price/100), 2);            } else {                $freight['price'] = $price;            }        }        return $freight;    }    private function sku($data, &$info)    {        # 获取销售属性        if ($data['is_sell'] == 2) {            if ($data['option']) {                $value = explode(',', $data['value']);                foreach($data['option'] as $k => $v) {                    if (in_array($v['id'], $value)) {                        $data['option_sku'][] = array                        (                            'id' => $v['id'],                            'info_id' => $v['info_id'],                            'name' => $v['name'],                            'icon' => $v['icon'],                            'value' => $v['info_id'] . '-' . $v['id'],                        );                    }                }                unset($data['option']);            }                        $info['sell_attr'][] = $data;            # 获取最便宜的价格            $where['info_id'] = $info['id'];            $info['price'] = array();            $info['price']['list'] = Dever::db('goods/info_sku')->getData($where);            $info['price']['min'] = Dever::db('goods/info_sku')->getMinOne($where);            $info['price']['max'] = Dever::db('goods/info_sku')->getMaxOne($where);        }    }    public function getInfoLink($info)    {        if (isset($this->view_path)) {            $info['view_path'] = $this->view_path . '?top=' . $info['top_category_id'] . '&id=' . $info['id'];            $info['view_link'] = Dever::url($info['view_path']);        }        return $info;    }    public function setViewPath($path)    {        $this->view_path = $path;    }}
 |