PetTypes.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * User: charlie
  4. * 宠物类型模型
  5. */
  6. class PetTypes extends MongoAr
  7. {
  8. public $_id; //宠物类型的object id 如:4e12e3c3912b22d362bdc022
  9. public $name; //宠物类型的名字
  10. public $pic; //每种类型的宠物都有一张图片
  11. public $parent; //父节点的object id 这是树形结构 比如:狗狗=》贵宾
  12. public $level = 1; //等级
  13. public $status=1; //1 正常 0 删除
  14. public $common=0; //常见 1: 常见 0: 不常见
  15. public $weight=0; //排序权重
  16. public static $status_option = array(
  17. 0 => array('name' => '已删除'),
  18. 1 => array('name' => '正常', 'filter' => true)
  19. );
  20. public function __construct($scenario='insert'){
  21. $this->setMongoDBComponent(Yii::app()->getComponent('mongodb_o2o'));
  22. parent::__construct($scenario);
  23. }
  24. public static function model($className=__CLASS__)
  25. {
  26. return parent::model($className);
  27. }
  28. public function getCollectionName()
  29. {
  30. return 'pet_types';
  31. }
  32. public static function get($_id) {
  33. if(CommonFn::isMongoId($_id)){
  34. $criteria = new EMongoCriteria();
  35. $criteria->_id('==', $_id);
  36. $model = self::model()->find($criteria);
  37. return $model;
  38. }else{
  39. return false;
  40. }
  41. }
  42. /**
  43. * 父级分类设置
  44. */
  45. public static $parent_option = [
  46. 1 => [
  47. 'id' => [
  48. '54671c4b0eb9fb89048b45f5', // 狗狗
  49. ],
  50. 'name' => '狗狗',
  51. ],
  52. 2 => [
  53. 'id' => [
  54. '546805e40eb9fb32018b45fe', // 猫猫
  55. ],
  56. 'name' => '猫猫',
  57. ],
  58. 100 => [
  59. 'id' => [
  60. '56a88bc5a84ea0064e8cdddf', // 兔子
  61. '56a88c51a84ea0882f8b61c1', // 鼠类
  62. '56a88c7ba84ea0e0478d6d2b', // 其他
  63. ],
  64. 'name' => '其他',
  65. ],
  66. ];
  67. /**
  68. * 利用MongoDB-Distinct获取宠物类型列表
  69. * 过Redis,过期时间7200秒
  70. *
  71. * @return Array $list : 所有在售宠物的类型列表
  72. */
  73. public static function getPetTypeIds() {
  74. $redis_cache = new ARedisCache();
  75. $cache = $redis_cache->get('pet_type_id_list');
  76. if (!$cache) {
  77. $cache_data = [];
  78. } else {
  79. $cache_data = unserialize($cache);
  80. $cache_data['expire'] = CommonFn::getValFromArray($cache_data, 'expire', 0);
  81. }
  82. if (!$cache_data || $cache_data['expire'] < time()) {
  83. $list = [];
  84. } else {
  85. $list = $cache_data['data'];
  86. }
  87. if (!$list) {
  88. $distinct_query = ['status' => 1];
  89. $mongo = new MongoClient(DB_CONNETC);
  90. $db = $mongo->deal;
  91. $collection = $db->selectCollection('pets');
  92. $list = $collection->distinct('pet_type', $distinct_query);
  93. $cache_data = ['data' => $list, 'expire' => time() + 7200];
  94. $redis_cache->set('pet_type_id_list', serialize($cache_data));
  95. }
  96. return $list;
  97. }
  98. /**
  99. * 获取宠物类型列表
  100. * 过Redis,过期时间7200秒
  101. *
  102. * @param Int $page : 页码,默认为1
  103. * @param Int $rows : 每页显示数目,默认为8
  104. *
  105. * @return Array $data : 返回结果
  106. * $data = [
  107. * 'list' => $list, // 类型列表
  108. * 'page' => $page, // 页码
  109. * 'rows' => $rows, // 每页显示数目
  110. * 'totalCount' => $totalCount, // 总数
  111. * 'page_count' => $page_count, // 总页数
  112. * ];
  113. */
  114. public static function getPetTypeList($page = 1, $rows = 4) {
  115. $criteria = new EMongoCriteria;
  116. $criteria->status('==',1);
  117. $criteria->_id('in',self::getPetTypeIds());
  118. $criteria->sort('weight',EMongoCriteria::SORT_DESC);
  119. $criteria->offset(($page - 1) * $rows);
  120. $criteria->limit($rows);
  121. $cursor = self::model()->findAll($criteria);
  122. $totalCount = $cursor->count();
  123. $list = [];
  124. foreach ($cursor as $key => $item) {
  125. $list[] = self::model()->parseRow($item->attributes);
  126. }
  127. $data = [
  128. 'list' => $list,
  129. 'page' => $page,
  130. 'rows' => $rows,
  131. 'totalCount' => $totalCount,
  132. 'page_count' => CommonFn::getPageCount($rows, $totalCount),
  133. ];
  134. return $data;
  135. }
  136. /**
  137. * 获取父级分类列表
  138. * 过Redis,过期时间7200秒
  139. *
  140. * @return Array $list : 包含所有父级分类ObjectId的列表
  141. * $list = [
  142. * ObjectId('......'),
  143. * ];
  144. */
  145. public static function getParentPetTypes() {
  146. $redis_cache = new ARedisCache();
  147. $cache = $redis_cache->get('parent_pet_types');
  148. if (!$cache) {
  149. $cache_data = [];
  150. } else {
  151. $cache_data = unserialize($cache);
  152. $cache_data['expire'] = CommonFn::getValFromArray($cache_data, 'expire', 0);
  153. }
  154. if (!$cache_data || $cache_data['expire'] < time()) {
  155. $criteria = new EMongoCriteria;
  156. $criteria->status('==',1);
  157. $criteria->level('==',1);
  158. $criteria->sort('_id',EMongoCriteria::SORT_ASC);
  159. $cursor = self::model()->findAll($criteria);
  160. $list = [];
  161. foreach ($cursor as $key => $item) {
  162. $list[] = self::model()->parseRow($item->attributes);
  163. }
  164. $cache_data = ['data' => $list, 'expire' => time() + 7200];
  165. $redis_cache->set('parent_pet_types', serialize($cache_data));
  166. } else {
  167. $list = $cache_data['data'];
  168. }
  169. return $list;
  170. }
  171. //获取宠物类型列表
  172. public function getPetList($parent = null,$common=100,$level=0){
  173. $criteria = new EMongoCriteria();
  174. $criteria->status('==',1);
  175. if($parent){
  176. $criteria->parent('==',$parent->_id);
  177. }
  178. if($common != 100){
  179. if($common == 0){
  180. $criteria->addCond('common', 'or', 0);
  181. $criteria->addCond('common', 'or', null);
  182. }else{
  183. $criteria->common('==',1);
  184. }
  185. }
  186. if($level) {
  187. $criteria->level('==', $level);
  188. }
  189. $criteria->sort('weight',EMongoCriteria::SORT_DESC);
  190. $cursor = self::model()->findAll($criteria);
  191. $rows = CommonFn::getRowsFromCursor($cursor);
  192. $res_list = array();
  193. foreach ($rows as $key => $value) {
  194. $res_list[] = self::parseRow($value);
  195. }
  196. return $res_list;
  197. }
  198. public function parseRow($row,$output=array()){
  199. $newRow = array();
  200. $newRow['id'] = (string)$row['_id'];
  201. $newRow['name'] = CommonFn::get_val_if_isset($row,'name','');
  202. $newRow['pic'] = CommonFn::get_val_if_isset($row,'pic',Yii::app()->params['defaultPetTypePic']);//默认图标
  203. $newRow['parent'] = (string)CommonFn::get_val_if_isset($row,'parent','');
  204. $newRow['parent_name'] = '';
  205. if($newRow['parent']){
  206. $parent = $this->get($row['parent']);
  207. $newRow['parent_name'] = $parent->name;
  208. }
  209. $newRow['level'] = CommonFn::get_val_if_isset($row,'level',1);
  210. $newRow['status'] = CommonFn::get_val_if_isset($row,'status',1);
  211. $newRow['common'] = CommonFn::get_val_if_isset($row, 'common', 0);
  212. $newRow['weight'] = CommonFn::get_val_if_isset($row, 'weight', 0);
  213. if(APPLICATION=='api'||APPLICATION=='common'){
  214. unset($newRow['action_user']);
  215. unset($newRow['action_time']);
  216. unset($newRow['action_log']);
  217. unset($newRow['status']);
  218. }
  219. return $this->output($newRow,$output);
  220. }
  221. }