Storegoodsclass.php 8.0 KB

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