<?php

namespace app\common\model;

use think\Db;
use think\Model;

class Goodsclasstag extends Model {
    public $page_info;
    /**
     * 删除TAG分类
     * 
     * @param array $condition
     * @return boolean
     */
    public function delGoodsClassTag($condition) {
        return db('goodsclasstag')->where($condition)->delete();
    }

    /**
     * TAG列表
     *
     * @param array $condition 检索条件
     * @param object $page 
     * @return array 数组结构的返回结果
     */
    public function getTagList($condition = array(), $page = '', $field = '*') {
        if ($page) {
            $result = db('goodsclasstag')->where($condition)->paginate($page,false,['query' => request()->param()]);
            $this->page_info = $result;
            return $result->items();
        } else {
            $result = db('goodsclasstag')->where($condition)->select();
            return $result;
        }
    }

    /**
     * TAG添加
     * 
     * @param array $param
     * @return bool
     */
    public function tagAdd($param) {
        $class_id_1 = '';
        $class_id_2 = '';
        $class_id_3 = '';
        $class_name_1 = '';
        $class_name_2 = '';
        $class_name_3 = '';
        $class_id = '';
        $type_id = '';
        $condition_str = '';

        if (is_array($param) && !empty($param)) { //一级
            foreach ($param as $value) {
                $class_id_1 = $value['gc_id'];
                $class_name_1 = trim($value['gc_name']);
                $class_id = $value['gc_id'];
                $type_id = $value['type_id'];
                $class_id_2 = '';
                $class_id_3 = '';
                $class_name_2 = '';
                $class_name_3 = '';

                if (is_array($value['sub_class']) && !empty($value['sub_class'])) { //二级
                    foreach ($value['sub_class'] as $val) {
                        $class_id_2 = $val['gc_id'];
                        $class_name_2 = trim($val['gc_name']);
                        $class_id = $val['gc_id'];
                        $type_id = $val['type_id'];

                        if (is_array($val['sub_class']) && !empty($val['sub_class'])) { //三级
                            foreach ($val['sub_class'] as $v) {
                                $class_id_3 = $v['gc_id'];
                                $class_name_3 = trim($v['gc_name']);
                                $class_id = $v['gc_id'];
                                $type_id = $v['type_id'];

                                //合并成sql语句
                                $condition_str .= '("' . $class_id_1 . '", "' . $class_id_2 . '", "' . $class_id_3 . '", "' . $class_name_1 . '&nbsp;&gt;&nbsp;' . $class_name_2 . '&nbsp;&gt;&nbsp;' . $class_name_3 . '", "' . $class_name_1 . ',' . $class_name_2 . ',' . $class_name_3 . '", "' . $class_id . '", "' . $type_id . '"),';
                            }
                        } else {
                            //合并成sql语句
                            $condition_str .= '("' . $class_id_1 . '", "' . $class_id_2 . '", "", "' . $class_name_1 . '&nbsp;&gt;&nbsp;' . $class_name_2 . '", "' . $class_name_1 . ',' . $class_name_2 . '", "' . $class_id . '", "' . $type_id . '"),';
                        }
                    }
                } else {
                    //合并成sql语句
                    $condition_str .= '("' . $class_id_1 . '", "", "", "' . $class_name_1 . '", "' . $class_name_1 . '", "' . $class_id . '", "' . $type_id . '"),';
                }
            }
        } else {
            return false;
        }

        $condition_str = trim($condition_str, ',');
        return Db::query("insert into `" . "ds_goodsclasstag` (`gc_id_1`,`gc_id_2`,`gc_id_3`,`gc_tag_name`,`gc_tag_value`,`gc_id`,`type_id`) values " . $condition_str);
    }

    /**
     * TAG清空
     */
    public function clearTag() {
        return Db::query("TRUNCATE TABLE `" . 'ds_' . "goodsclasstag`");
    }

    /**
     * TAG更新
     * 
     * @param array $param
     * @return bool
     */
    public function updateTag($param,$gc_tag_id) {
        return db('goodsclasstag')->where("gc_tag_id = '" . $gc_tag_id . "'")->update($param);
    }

    /**
     * TAG删除
     *
     * @param string $id
     * @return bool
     */
    public function delTagByIds($id) {
        return db('goodsclasstag')->where('goods_class_tag', 'gc_tag_id in (' . $id . ')')->delete();
    }

    /**
     * 根据条件删除
     * 
     * @param array $condition
     * @return bool
     */
    public function delByCondition($condition) {
        return db('goodsclasstag')->where($condition)->delete();
    }

    /**
     * TAG添加
     */
    public function addOneTag($param) {
        return db('goodsclasstag')->insertGetId($param);
    }

    /**
     * 构造检索条件
     *
     * @param array $condition
     * @return string 字符串类型的返回结果
     */
    private function _condition($condition = array()) {
        $condition_str = '';

        if ($condition['gc_parent_id'] != '') {
            $condition_str .= " and gc_parent_id = '" . intval($condition['gc_parent_id']) . "'";
        }
        if ($condition['gc_tag_id'] != '') {
            $condition_str .= " and gc_tag_id = '" . $condition['gc_tag_id'] . "'";
        }
        if ($condition['in_tag_id'] != '') {
            $condition_str .= " and gc_tag_id in (" . $condition['in_tag_id'] . ")";
        }
        if ($condition['gc_tag_value'] != '') {
            $condition_str .= " and gc_tag_value = '" . $condition['gc_tag_value'] . "'";
        }
        if ($condition['gc_condition'] != '') {
            $condition_str .= " and ( gc_id_1='" . $condition['gc_condition'] . "' or gc_id_2='" . $condition['gc_condition'] . "' or gc_id_3='" . $condition['gc_condition'] . "')";
        }
        if ($condition['gc_id'] != '') {
            $condition_str .= ' and gc_id = "' . $condition['gc_id'] . '"';
        }
        if ($condition['gc_id_1'] != '') {
            $condition_str .= ' and gc_id_1 = "' . $condition['gc_id_1'] . '"';
        }
        if ($condition['gc_id_2'] != '') {
            $condition_str .= ' and gc_id_2 = "' . $condition['gc_id_2'] . '"';
        }
        if ($condition['gc_id_3'] != '') {
            $condition_str .= ' and gc_id_3 = "' . $condition['gc_id_3'] . '"';
        }
        if ($condition['type_id'] != '') {
            $condition_str .= ' and type_id = "' . $condition['type_id'] . '"';
        }

        return $condition_str;
    }

}

?>
