Storegoodsclass.php 7.7 KB

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