<?php
/**
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */

namespace Common\Model;
use Think\Model;
class PurchaseCategoryModel extends Model
{

    /**
     * 获取当前分类和子类或者当前分类和上级分类
     * @param $cat_id 分类id
     * @return array
     */
    function get_current_cate($cat_id = 0)
    {
        if ($cat_id == 0) {//没有分类显示全部一二级
            return $this->get_categories_tree($cat_id);
        } elseif ($cat_id > 0) {
            GLOBAL $PurchaseCategory;
            $cate = array();
            if (empty($PurchaseCategory)) $PurchaseCategory = getcache('PurchaseCategory');
            foreach ($PurchaseCategory as $k => $v) {
                if ($v['pid'] == $cat_id) {
                    $cate[$k] = $v;
                }
            }
            if ($cate) {//如有子类 显示当前和子类 其它不显示
                $info = $PurchaseCategory[$cat_id];
                $info['child'] = $cate;
                $info['hover'] = 1;
                $data[] = $info;
            } else {//如无子分类 显示当前级和上级
                $info = $PurchaseCategory[$cat_id];
                $pre_info = $PurchaseCategory[$info['pid']];
                $catt = array();
                foreach ($PurchaseCategory as $ke => $va) {
                
                    if ($va['pid'] == $info['pid']) {
                        $catt[$ke] = $va;
                        if ($va['id'] == $cat_id) {
                            $catt[$ke]['hover'] = 1;
                        }
                    }
                }
                $pre_info['hover'] = 1;
                $pre_info['child'] = $catt;
                $data[] = $pre_info;
            }
            return $data;
        }

    }

    /**
     * 获得指定分类同级的所有分类以及该分类下的子分类
     * @access  public
     * @param   integer $cat_id 分类编号
     * @param   bool $cache 是否缓存
     * @return  array
     */
    function get_categories_tree($cat_id = 0, $cache = true)
    {
        if ($cache) {
            $cachename = 'get_categories_tree' . $cat_id;
            $cachecontent = S($cachename);
            if (!empty($cachecontent)) return $cachecontent;
        }
        GLOBAL $PurchaseCategory;
        if (empty($PurchaseCategory)) $PurchaseCategory = getcache('PurchaseCategory');
        $hover = array($cat_id);
        if ($cat_id > 0) {
            $hover[] = $PurchaseCategory[$cat_id]['pid'];
            if ($PurchaseCategory[$cat_id]['allson'] == $cat_id) $parent_id = $PurchaseCategory[$PurchaseCategory[$cat_id]['pid']]['pid'];
            else $parent_id = $PurchaseCategory[$cat_id]['pid'];
        }
        if (empty($parent_id)) $parent_id = 0;
        $hover[] = $parent_id;
        $cat_arr = array();
        foreach ($PurchaseCategory as $v) {
            if ($v['pid'] == $parent_id && $v['status'] == 1) {
                $cat_arr[$v['id']] = $v;
                $cat_arr[$v['id']]['hover'] = in_array($v['id'], $hover) ? 1 : 0;
                $child = array();
                if ($v['allson'] != $v['id']) {
                    $catson = explode(',', $v['allson']);
                    foreach ($catson as $sonid) {
                        $val = $PurchaseCategory[$sonid];
                        if ($val['status'] == 1 && $val['id'] != $v['id'] && $val['pid'] == $v['id']) {
                            $child[$val['id']] = $val;
                            $child[$val['id']]['hover'] = in_array($val['id'], $hover) ? 1 : 0;
                        }
                    }
                }
                $cat_arr[$v['id']]['child'] = $child;
            }
        }
        if ($cache) {
            S($cachename, $cat_arr);
        }
        return $cat_arr;
    }

    /**
     * 获取缓存数据列表
     * <assign name="topnav" value=':getcatelist()'/>
     * @param  $pid 基础标签 pid
     * @return array
     */
    function getcatelist($pid = 0)
    {
        $tags = 'Purchase';
        $cacheres = S($tags);
        if ($cacheres) return $cacheres;
        $name = 'PurchaseCategory';
        $pid = !empty($pid) ? $pid : '0';
        GLOBAL $PurchaseCategory;
        if (!empty($PurchaseCategory)) $cachelist = $PurchaseCategory;
        if (empty($cachelist)) $cachelist = getcache($name);
        $Tree = new \Common\Vendor\Tree();
        $Tree->init($cachelist);
        $data = $Tree->get_tree_array($pid);
        $result = empty($tag['return']) ? $data : array_values($data);
        S($tags, $result);
        return $result;
    }

    /**
     * 获得该分类的一级分类
     * @param $cate_id 分类id
     */
    function get_parent_pid($cate_id)
    {
        $c_map['id'] = $cate_id;
        $cate = M("PurchaseCategory")->where($c_map)->field("id,pid,balance_rate")->find();
        if ($cate['pid'] > 0) {
            $cate_id =$this->get_parent_pid($cate['pid']);
            if ($cate_id['pid'] == 0) {
                $balance_rate = $cate_id['balance_rate'];
            }
        } else {
            $balance_rate = $cate['balance_rate'];
        }
        return $balance_rate;
    }
}
?>