my_goods_class.model.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * 商品类别模型
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class my_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 getClassInfo($param,$field='*') {
  21. if(empty($param)) {
  22. return false;
  23. }
  24. //得到条件语句
  25. $condition_str = $this->getCondition($param);
  26. $param = array();
  27. $param['table'] = 'store_goods_class';
  28. $param['where'] = $condition_str;
  29. $param['field'] = $field;
  30. $class_info = Db::select($param);
  31. return $class_info[0];
  32. }
  33. /**
  34. * 类别添加
  35. *
  36. * @param array $param 分类数组
  37. * @return array 数组结构的返回结果
  38. */
  39. public function addGoodsClass($param) {
  40. if(empty($param)) {
  41. return false;
  42. }
  43. $class_array = array();
  44. $class_array['stc_name'] = $param['stc_name'];
  45. $class_array['stc_parent_id']= $param['stc_parent_id'];
  46. $class_array['stc_state'] = $param['stc_state'];
  47. $class_array['store_id'] = $_SESSION['store_id'];
  48. $class_array['stc_sort'] = $param['stc_sort'];
  49. $result = Db::insert('store_goods_class',$class_array);
  50. if($result) {
  51. return true;
  52. } else {
  53. return false;
  54. }
  55. }
  56. /**
  57. * 类别导入
  58. *
  59. * @param array $param 分类数组
  60. * @return array 数组结构的返回结果
  61. */
  62. public function importGoodsClass($param) {
  63. if(empty($param)) {
  64. return false;
  65. }
  66. $class_array = array();
  67. $class_array['stc_name'] = $param['stc_name'];
  68. $class_array['stc_parent_id']= $param['stc_parent_id'];
  69. $class_array['stc_state'] = $param['stc_state'];
  70. $class_array['store_id'] = $_SESSION['store_id'];
  71. $class_array['stc_sort'] = $param['stc_sort'];
  72. $result = Db::insert('store_goods_class',$class_array);
  73. if($result) {
  74. return $result;
  75. } else {
  76. return false;
  77. }
  78. }
  79. /**
  80. * 类别修改
  81. *
  82. * @param array $param 分类数组
  83. * @return array 数组结构的返回结果
  84. */
  85. public function editGoodsClass($param,$class_id) {
  86. if(empty($param)) {
  87. return false;
  88. }
  89. $class_array = array();
  90. if($param['stc_name'] != ''){
  91. $class_array['stc_name'] = $param['stc_name'];
  92. }
  93. if($param['stc_parent_id'] != ''){
  94. $class_array['stc_parent_id'] = $param['stc_parent_id'];
  95. }
  96. if($param['stc_state'] != ''){
  97. $class_array['stc_state'] = $param['stc_state'];
  98. }
  99. if($param['stc_sort'] != ''){
  100. $class_array['stc_sort'] = $param['stc_sort'];
  101. }
  102. $state = Db::update('store_goods_class',$class_array," WHERE stc_id='$class_id'");
  103. if($state) {
  104. return true;
  105. } else {
  106. return false;
  107. }
  108. }
  109. /**
  110. * 店铺商品分类删除
  111. *
  112. */
  113. public function dropGoodsClass($store_class_id) {
  114. if(empty($store_class_id)) {
  115. return false;
  116. }
  117. $array = explode(',',$store_class_id);
  118. foreach ($array as $val) {
  119. $class_info = $this->getClassInfo(array('stc_id'=>$val,'store_id'=>$_SESSION['store_id']),'stc_id');
  120. if($class_info['stc_id'] != '') {
  121. Db::delete('store_goods_class'," WHERE stc_id='$val'");
  122. }
  123. }
  124. return true;
  125. }
  126. /**
  127. * 类别列表
  128. *
  129. * @param array $condition 检索条件
  130. * @return array 数组结构的返回结果
  131. */
  132. public function getClassList($condition){
  133. $where = $this->getCondition($condition);
  134. $where = ltrim($where, ' and');
  135. $order = $condition['order']?$condition['order']:'stc_parent_id asc,stc_sort asc,stc_id asc';
  136. $result = $this->where($where)->order($order)->select();
  137. return $result;
  138. }
  139. /**
  140. * 取分类列表(前台店铺页左侧用到)
  141. *
  142. * $param int $store_id 店铺ID
  143. * @return array 数组类型的返回结果
  144. */
  145. public function getShowTreeList($store_id){
  146. $show_class = array();
  147. $class_list = $this->getClassList(array('store_id'=>$store_id,'stc_state'=>'1','order'=>'stc_parent_id asc,stc_sort asc,stc_id asc'));
  148. if(is_array($class_list) && !empty($class_list)) {
  149. foreach ($class_list as $val) {
  150. if($val['stc_parent_id'] == 0) {
  151. $show_class[$val['stc_id']] = $val;
  152. } else {
  153. if(isset($show_class[$val['stc_parent_id']])){
  154. $show_class[$val['stc_parent_id']]['children'][] = $val;
  155. }
  156. }
  157. }
  158. unset($class_list);
  159. }
  160. return $show_class;
  161. }
  162. /**
  163. * 取分类列表,按照深度归类
  164. *
  165. * @param array $condition 检索条件
  166. * @param int $show_deep 显示深度
  167. * @return array 数组类型的返回结果
  168. */
  169. public function getTreeClassList($condition,$show_deep='2'){
  170. $class_list = $this->getClassList($condition);
  171. $show_deep = intval($show_deep);
  172. $result = array();
  173. if(is_array($class_list) && !empty($class_list)) {
  174. $result = $this->_getTreeClassList($show_deep,$class_list);
  175. }
  176. return $result;
  177. }
  178. /**
  179. * 递归 整理分类
  180. *
  181. * @param int $show_deep 显示深度
  182. * @param array $class_list 类别内容集合
  183. * @param int $deep 深度
  184. * @param int $parent_id 父类编号
  185. * @param int $i 上次循环编号
  186. * @return array $show_class 返回数组形式的查询结果
  187. */
  188. private function _getTreeClassList($show_deep,$class_list,$deep=1,$parent_id=0,$i=0){
  189. static $show_class = array();//树状的平行数组
  190. if(is_array($class_list) && !empty($class_list)) {
  191. $size = count($class_list);
  192. if($i == 0) $show_class = array();//从0开始时清空数组,防止多次调用后出现重复
  193. for ($i;$i < $size;$i++) {//$i为上次循环到的分类编号,避免重新从第一条开始
  194. $val = $class_list[$i];
  195. $stc_id = $val['stc_id'];
  196. $stc_parent_id = $val['stc_parent_id'];
  197. if($stc_parent_id == $parent_id) {
  198. $val['deep'] = $deep;
  199. $show_class[] = $val;
  200. if($deep < $show_deep && $deep < 2) {//本次深度小于显示深度时执行,避免取出的数据无用
  201. $this->_getTreeClassList($show_deep,$class_list,$deep+1,$stc_id,$i+1);
  202. }
  203. }
  204. if($stc_parent_id > $parent_id) break;//当前分类的父编号大于本次递归的时退出循环
  205. }
  206. }
  207. return $show_class;
  208. }
  209. /**
  210. * 取分类列表
  211. *
  212. * @param array $condition 检索条件
  213. * @return array 数组类型的返回结果
  214. */
  215. public function getClassTree($condition){
  216. $condition['order'] = ' stc_parent_id asc,stc_sort asc,stc_id asc';
  217. $class_list = $this->getClassList($condition);
  218. $d = array();
  219. if (is_array($class_list)){
  220. foreach($class_list as $v) {
  221. if($v['stc_parent_id'] == 0) {
  222. $d[$v['stc_id']] = $v;
  223. }else {
  224. if(isset($d[$v['stc_parent_id']])) $d[$v['stc_parent_id']]['child'][] = $v;//防止出现父类不显示时子类被调出
  225. }
  226. }
  227. }
  228. return $d;
  229. }
  230. /**
  231. * 获取某本店分类下的子分类或者包含其自身
  232. * @param int $stc_id 店铺分类
  233. * @param string $stc_state 分类是否显示
  234. * @return mixed
  235. */
  236. public function getChildAndSelfClass($stc_id,$stc_state=''){
  237. $condition = array('stc_parent_id'=>$stc_id,'order'=>'stc_parent_id asc,stc_sort asc,stc_id asc');
  238. if ($stc_state != '') {//分类是否显示
  239. $condition['stc_state'] = $condition['stc_state'];
  240. }
  241. $stc_id_arr = array();
  242. $sgoodsclasslist = $this->getClassList($condition);
  243. if (is_array($sgoodsclasslist) && count($sgoodsclasslist)>0){
  244. foreach ($sgoodsclasslist as $v){
  245. $stc_id_arr[] = $v['stc_id'];
  246. }
  247. }
  248. if (is_array($stc_id_arr) && count($stc_id_arr)>0){
  249. $stc_id_arr[] = $stc_id;//加上一级分类本身
  250. return $stc_id_arr;
  251. }else{
  252. return $stc_id;
  253. }
  254. }
  255. /**
  256. * 将条件数组组合为SQL语句的条件部分
  257. *
  258. * @param array $condition_array
  259. * @return string
  260. */
  261. private function getCondition($condition_array){
  262. $condition_sql = '';
  263. if ($condition_array['stc_top'] != '') {
  264. $condition_sql .= " and stc_parent_id=0";
  265. }
  266. if ($condition_array['stc_id'] != '') {
  267. $condition_sql .= " and stc_id= '{$condition_array['stc_id']}'";
  268. }
  269. if (!empty($condition_array['stc_id_in'])) {
  270. $condition_sql .= " and stc_id in (".implode(',',$condition_array['stc_id_in']).")";
  271. }
  272. if($condition_array['store_id'] != '') {
  273. $condition_sql .= " and store_id= '{$condition_array['store_id']}'";
  274. }
  275. if($condition_array['stc_parent_id'] != '') {
  276. $condition_sql .= " and stc_parent_id= '{$condition_array['stc_parent_id']}'";
  277. }
  278. if ($condition_array['stc_state'] == '0' || $condition_array['stc_state'] == '1') {
  279. $condition_sql .= " and stc_state= '{$condition_array['stc_state']}'";
  280. }
  281. return $condition_sql;
  282. }
  283. }
  284. ?>