store_goods_class.model.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * 商品类别模型
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class store_goods_classModel extends Model {
  11. public function __construct(){
  12. parent::__construct('store_goods_class');
  13. }
  14. /**
  15. * 单个类别内容提取
  16. *
  17. * @param array $param 检索条件
  18. * @return array 数组结构的返回结果
  19. */
  20. public function getStoreGoodsClassInfo($param,$field='*') {
  21. if(empty($param)) {
  22. return false;
  23. }
  24. return $this->where($param)->field($field)->find();
  25. }
  26. /**
  27. * 类别添加
  28. *
  29. * @param array $param 分类数组
  30. * @return array 数组结构的返回结果
  31. */
  32. public function addStoreGoodsClass($param) {
  33. if(empty($param)) {
  34. return false;
  35. }
  36. $result = $this->insert($param);
  37. if($result) {
  38. $this->_dStoreGoodsClassCache($param['store_id']);
  39. }
  40. return $result;
  41. }
  42. /**
  43. * 类别修改
  44. *
  45. * @param array $param 分类数组
  46. * @return array 数组结构的返回结果
  47. */
  48. public function editStoreGoodsClass($param,$where) {
  49. if(empty($param)) {
  50. return false;
  51. }
  52. $result = $this->where($where)->update($param);
  53. if($result) {
  54. $this->_dStoreGoodsClassCache($where['store_id']);
  55. }
  56. return $result;
  57. }
  58. /**
  59. * 店铺商品分类删除
  60. *
  61. */
  62. public function delStoreGoodsClass($where) {
  63. if(empty($where)) {
  64. return false;
  65. }
  66. $result = $this->where($where)->delete();
  67. if ($result) {
  68. $this->_dStoreGoodsClassCache($where['store_id']);
  69. }
  70. return $result;
  71. }
  72. /**
  73. * 类别列表
  74. *
  75. * @param array $condition 检索条件
  76. * @return array 数组结构的返回结果
  77. */
  78. public function getStoreGoodsClassList($condition, $order = 'stc_parent_id asc,stc_sort asc,stc_id asc'){
  79. $result = $this->where($condition)->order($order)->select();
  80. return $result;
  81. }
  82. /**
  83. * 取分类列表(前台店铺页左侧用到)
  84. *
  85. * $param int $store_id 店铺ID
  86. * @return array 数组类型的返回结果
  87. */
  88. public function getShowTreeList($store_id){
  89. $info = $this->_rStoreGoodsClassCache($store_id);
  90. if (empty($info)) {
  91. $show_class = array();
  92. $class_list = $this->getStoreGoodsClassList (array('store_id'=>$store_id,'stc_state'=>'1'));
  93. if(is_array($class_list) && !empty($class_list)) {
  94. foreach ($class_list as $val) {
  95. if($val['stc_parent_id'] == 0) {
  96. $show_class[$val['stc_id']] = $val;
  97. } else {
  98. if(isset($show_class[$val['stc_parent_id']])){
  99. $show_class[$val['stc_parent_id']]['children'][] = $val;
  100. }
  101. }
  102. }
  103. }
  104. $info['info'] = serialize($show_class);
  105. $this->_wStoreGoodsClassCache($store_id, $info);
  106. }
  107. $show_class = unserialize($info['info']);
  108. return $show_class;
  109. }
  110. /**
  111. * 取分类列表,按照深度归类
  112. *
  113. * @param array $condition 检索条件
  114. * @param int $show_deep 显示深度
  115. * @return array 数组类型的返回结果
  116. */
  117. public function getTreeClassList($condition,$show_deep='2'){
  118. $class_list = $this->getStoreGoodsClassList ($condition);
  119. $show_deep = intval($show_deep);
  120. $result = array();
  121. if(is_array($class_list) && !empty($class_list)) {
  122. $result = $this->_getTreeClassList($show_deep,$class_list);
  123. }
  124. return $result;
  125. }
  126. /**
  127. * 递归 整理分类
  128. *
  129. * @param int $show_deep 显示深度
  130. * @param array $class_list 类别内容集合
  131. * @param int $deep 深度
  132. * @param int $parent_id 父类编号
  133. * @param int $i 上次循环编号
  134. * @return array $show_class 返回数组形式的查询结果
  135. */
  136. private function _getTreeClassList($show_deep,$class_list,$deep=1,$parent_id=0,$i=0){
  137. static $show_class = array();//树状的平行数组
  138. if(is_array($class_list) && !empty($class_list)) {
  139. $size = count($class_list);
  140. if($i == 0) $show_class = array();//从0开始时清空数组,防止多次调用后出现重复
  141. for ($i;$i < $size;$i++) {//$i为上次循环到的分类编号,避免重新从第一条开始
  142. $val = $class_list[$i];
  143. $stc_id = $val['stc_id'];
  144. $stc_parent_id = $val['stc_parent_id'];
  145. if($stc_parent_id == $parent_id) {
  146. $val['deep'] = $deep;
  147. $show_class[] = $val;
  148. if($deep < $show_deep && $deep < 2) {//本次深度小于显示深度时执行,避免取出的数据无用
  149. $this->_getTreeClassList($show_deep,$class_list,$deep+1,$stc_id,$i+1);
  150. }
  151. }
  152. if($stc_parent_id > $parent_id) break;//当前分类的父编号大于本次递归的时退出循环
  153. }
  154. }
  155. return $show_class;
  156. }
  157. /**
  158. * 取分类列表
  159. *
  160. * @param array $condition 检索条件
  161. * @return array 数组类型的返回结果
  162. */
  163. public function getClassTree($condition){
  164. $class_list = $this->getStoreGoodsClassList ($condition);
  165. $d = array();
  166. if (is_array($class_list)){
  167. foreach($class_list as $v) {
  168. if($v['stc_parent_id'] == 0) {
  169. $d[$v['stc_id']] = $v;
  170. }else {
  171. if(isset($d[$v['stc_parent_id']])) $d[$v['stc_parent_id']]['child'][] = $v;//防止出现父类不显示时子类被调出
  172. }
  173. }
  174. }
  175. return $d;
  176. }
  177. /**
  178. * 根据编号获取一条数据
  179. *
  180. * @param int $id
  181. * @return bool|array
  182. */
  183. public function getOneById($id){
  184. if(intval($id)<=0)return false;
  185. $param = array();
  186. $param['table'] = 'store_goods_class';
  187. $param['field'] = 'stc_id';
  188. $param['value'] = intval($id);
  189. return Db::getRow($param);
  190. }
  191. /**
  192. * 读取店铺商品分类缓存
  193. * @param int $store_id
  194. * @param string $fields
  195. * @return array
  196. */
  197. private function _rStoreGoodsClassCache($store_id) {
  198. return rcache($store_id, 'store_goods_class');
  199. }
  200. /**
  201. * 写入店铺商品分类缓存
  202. * @param int $store_id
  203. * @param array $info
  204. * @return boolean
  205. */
  206. private function _wStoreGoodsClassCache($store_id, $info) {
  207. return wcache($store_id, $info, 'store_goods_class');
  208. }
  209. /**
  210. * 删除店铺商品分类缓存
  211. * @param int $store_id
  212. * @return boolean
  213. */
  214. private function _dStoreGoodsClassCache($store_id) {
  215. return dcache($store_id, 'store_goods_class');
  216. }
  217. }
  218. ?>