Storegoodsclass.php 7.8 KB

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