DealPet.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. /**
  3. * 宠物模型
  4. */
  5. class DealPet extends MongoActiveRecord {
  6. public $_id;
  7. public $is_one_pay = 0; //一元购产品 是否为一元购产品 默认0 不是 1 是
  8. public $one_pay_counts = 0; //如果是一元购产品 总份数
  9. public $name; // String : 名称
  10. public $status = 0; // Int : 状态
  11. public $price; // Int : 价格
  12. public $pics = []; // Array : 图片,参考Topic模型pics字段
  13. public $root_parent; // ObjectId : 父分类id
  14. public $kennel; // ObjectId : 所属商家
  15. public $pet_type; // ObjectId : 宠物类型
  16. public $birth_date; // Int : 出生日期时间戳
  17. public $gender; // Int : 性别
  18. public $tags = []; // Array : 标签
  19. public $father_info; // Object : 父亲信息
  20. public $mother_info; // Object : 母亲信息
  21. public $desc; // String : 说明文字
  22. public $vaccine_info; // Array : 疫苗信息 数组 包含brand和time
  23. public $add_time; // Int : 添加时间
  24. public $sort_weight = 0; // int : 排序权重
  25. //public $delivery_time; // Int : 交货时间,时间范围,取值范围0-7
  26. //public $delivery_date; // 发货日期
  27. public $deworming_info; // array : 驱虫信息 数组 包含brand和time
  28. //public $hair_color; // Stirng : 毛色
  29. public $video; // Object : 视频 {url:'', length:0, 'avatar':''}
  30. public $carriage; // Object : 运费 {nonlocal:0, local:0}
  31. public $last_modify; // Int : 最后一次修改时间
  32. public $view_count = 0; // Int : 查看次数
  33. public $contact_count = 0; // Int : 咨询数
  34. public $region; // Object : 冗余字段,用于地区显示及筛选,同kennel
  35. public $last_deny_reason; // String : 最后一次审核未通过理由
  36. public $recommend=0; // Int 是否推荐 1推荐 0不推荐
  37. public $recommend_time;//推荐的截至时间 时间戳
  38. public $reply_count = 0;//回复数
  39. public $all_reply_count = 0;//所有的回复数
  40. public $last_post_time;//最后回复时间
  41. public $count = 1;//现存数量 针对小宠
  42. public $from;//数据来源 wozhua xinchong chongwushichang
  43. public static $status_option = [
  44. -100 => ['name' => '信息不完善'],
  45. -2 => ['name' => '审核未通过'],
  46. -1 => ['name' => '已删除'],
  47. 0 => ['name' => '待审核'],
  48. 1 => ['name' => '待售'],
  49. 2 => ['name' => '待支付'],
  50. 3 => ['name' => '已预订'],
  51. 4 => ['name' => '已交易'],
  52. ];
  53. public static $gender_option = [
  54. 0 => ['name' => '未知'],
  55. 1 => ['name' => 'DD'],
  56. 2 => ['name' => 'MM'],
  57. ];
  58. public static $from_option = [
  59. 'wozhua' => ['name' => 'wozhua'],
  60. 'xinchong' => ['name' => 'xinchong'],
  61. 'shichang' => ['name' => 'chang'],
  62. ];
  63. public static $tag_option = [
  64. 1 => ['name' => '30天保障'],
  65. 2 => ['name' => '①宠①拍'],
  66. 3 => ['name' => '先行赔付']
  67. ];
  68. public function __construct($scenario = 'insert') {
  69. $this->setMongoDBComponent(Yii::app()->getComponent('mongodb_o2o'));
  70. parent::__construct($scenario);
  71. }
  72. public function getCollectionName() {
  73. return 'pets';
  74. }
  75. public static function model($className = __CLASS__) {
  76. return parent::model($className);
  77. }
  78. public static function getNewPet(){
  79. $cache = new ARedisCache();
  80. $key = 'data_cache_new_pet_list_'.__CLASS__;
  81. $data_cache = $cache->get($key);
  82. $res = array();
  83. if($data_cache){
  84. $res = unserialize($data_cache);
  85. }else{
  86. $data = array();
  87. $criteria = new EMongoCriteria();
  88. $criteria->root_parent('==',new MongoId('54671c4b0eb9fb89048b45f5'));//狗狗
  89. $criteria->status('==',1);
  90. $criteria->limit(4);
  91. $criteria->offset(rand(0,100));
  92. $cursor = self::model()->findAll($criteria);
  93. foreach ($cursor as $key => $value) {
  94. $data[] = $value;
  95. }
  96. $dogs = self::model()->parseIndexList($cursor);
  97. $criteria = new EMongoCriteria();
  98. $criteria->root_parent('==',new MongoId('546805e40eb9fb32018b45fe'));//猫猫
  99. $criteria->status('==',1);
  100. $criteria->limit(5);
  101. $criteria->offset(rand(0,100));
  102. $cursor = self::model()->findAll($criteria);
  103. foreach ($cursor as $key => $value) {
  104. $data[] = $value;
  105. }
  106. // $criteria = new EMongoCriteria();
  107. // $criteria->root_parent('notin',[new MongoId('546805e40eb9fb32018b45fe'),new MongoId('54671c4b0eb9fb89048b45f5')]);//其他
  108. // $criteria->status('==',1);
  109. // $criteria->limit(3);
  110. // $criteria->offset(rand(0,10));
  111. // $cursor = self::model()->findAll($criteria);
  112. // foreach ($cursor as $key => $value) {
  113. // $data[] = $value;
  114. // }
  115. foreach ($data as $key => $obj) {
  116. $temp['id'] = (string)$obj->_id;
  117. $temp['name'] = $obj->name;
  118. $temp['price'] = $obj->price;
  119. $pics = $obj->pics;
  120. $temp['pic'] = (object)array();
  121. if($pics){
  122. $temp['pic'] = $pics[0];
  123. }
  124. $temp['pet_type_info'] = '';
  125. $temp['pet_type_str'] = '';
  126. $temp['pet_type_parent'] = '';
  127. if (CommonFn::isMongoId($obj->pet_type)) {
  128. $pet_type = PetTypes::get($obj->pet_type);
  129. if ($pet_type) {
  130. $temp['pet_type_info'] = PetTypes::model()->parseRow($pet_type);
  131. $temp['pet_type_str'] = $pet_type->name;
  132. $temp['pet_type_parent'] = (string)$pet_type->parent;
  133. }
  134. }
  135. $res[] = $temp;
  136. }
  137. $cache->set($key,serialize($res),86400);
  138. }
  139. return $res;
  140. }
  141. public static function get($_id) {
  142. if (CommonFn::isMongoId($_id)) {
  143. $criteria = new EMongoCriteria();
  144. $criteria->_id('==', $_id);
  145. $model = self::model()->find($criteria);
  146. return $model;
  147. } else {
  148. return false;
  149. }
  150. }
  151. /**
  152. * 根据出生日期时间戳计算时间
  153. * @param Int $time : 出生日期时间戳
  154. */
  155. public static function makeAge($time) {
  156. if (!$time) return '无';
  157. $days = (strtotime(date('Ymd')) - $time) / 86400;
  158. if ($days <= 31) {
  159. return $days.'天';
  160. } else if ($days == 365) {
  161. return '一岁';
  162. } else if ($days > 335 && $days < 365) {
  163. return '12月'.($days - 335).'天';
  164. } else if ($days < 365) {
  165. $age = (int)($days / 30).'个月';
  166. if ($days % 30) {
  167. $age .= ($days % 30).'天';
  168. }
  169. return $age;
  170. } else if ($days > 365) {
  171. $year = floor($days/365);
  172. $age = $year.'岁';
  173. if (intval(($days-$year*365) / 30)) {
  174. $age .= intval(($days-$year*365) / 30).'个月';
  175. }
  176. if (($days - $year*365) % 30) {
  177. $age .= (($days - $year*365) % 30).'天';
  178. }
  179. return $age;
  180. } else {
  181. return '无';
  182. }
  183. }
  184. /**
  185. * 宠物信息保存后的回调
  186. */
  187. public function afterSave() {
  188. parent::afterSave();
  189. // 类型
  190. // 更新总类型列表
  191. $type__cache = VariableRedis::get('pet_type_id_list');
  192. if ($type__cache) {
  193. $type_cache_data = unserialize($type__cache);
  194. if (!in_array($this->pet_type, $type_cache_data['data'])) {
  195. VariableRedis::remove('pet_type_id_list');
  196. }
  197. }
  198. // 该类型下宠物列表还未过缓存,不需要清除
  199. // 商家
  200. // 更新商家在售宠物类型列表
  201. $kennel_type_cache = VariableRedis::get('pet_types_' . (string)$this->kennel);
  202. if ($kennel_type_cache) {
  203. VariableRedis::remove('pet_types_' . (string)$this->kennel);
  204. }
  205. // 更新商家在售宠物列表
  206. $kennel_pet_cache = VariableRedis::get('pet_list_' . (string)$this->kennel);
  207. if ($kennel_pet_cache) {
  208. VariableRedis::remove('pet_list_' . (string)$this->kennel);
  209. }
  210. }
  211. /**
  212. * 宠物列表接口
  213. */
  214. public function parseIndexList($rows = []) {
  215. $data = [];
  216. foreach ($rows as $key => $item) {
  217. $temp['id'] = (string)$item['_id'];
  218. // 名字
  219. $temp['name'] = CommonFn::get_val_if_isset($item, 'name', '');
  220. // 价格
  221. $temp['price'] = CommonFn::get_val_if_isset($item, 'price', 0);
  222. // 商家名
  223. if (isset($item['kennel']) && CommonFn::isMongoId($item['kennel'])) {
  224. $kennel = Kennel::get($item['kennel']);
  225. $temp['kennel_name'] = $kennel->name;
  226. } else {
  227. $temp['kennel_name'] = '';
  228. }
  229. // 图片
  230. if (isset($item['pics']) && count($item['pics'])) {
  231. $temp['pic'] = $item['pics'][0];
  232. } else {
  233. $temp['pic'] = [
  234. 'url' => Yii::app()->params['defaultGoodsAvatar'],
  235. 'width' => 200,
  236. 'height' => 200,
  237. ];
  238. }
  239. $data[] = $temp;
  240. }
  241. return $data;
  242. }
  243. /**
  244. * 更新宠物的查看数并返回
  245. */
  246. public function updateViewCount() {
  247. // mongo
  248. $view_count = $this->getAttr('view_count', 0);
  249. // redis
  250. $redis_key = 'view_count_' . (string)$this->_id;
  251. $cache = VariableRedis::get($redis_key);
  252. if (!$cache) {
  253. $data = ['count' => 0, 'expire' => (time() + 7200)];
  254. VariableRedis::set($redis_key, serialize($data));
  255. $this->view_count = $view_count + 1;
  256. $this->save();
  257. return $view_count + 1;
  258. } else {
  259. $cache_data = unserialize($cache);
  260. if (time() < $cache_data['expire']) {
  261. $data = ['count' => $cache_data['count'] + 1, 'expire' => $cache_data['expire']];
  262. VariableRedis::set($redis_key, serialize($data));
  263. return $view_count + $cache_data['count'] + 1;
  264. } else {
  265. $data = ['count' => 0, 'expire' => (time() + 7200)];
  266. VariableRedis::set($redis_key, serialize($data));
  267. $this->view_count = $view_count + $cache_data['count'] + 1;
  268. $this->save();
  269. return $view_count + $cache_data['count'] + 1;
  270. }
  271. }
  272. }
  273. /**
  274. * 返回查看数
  275. */
  276. public function getViewCount() {
  277. $key = 'view_count_' . (string)$this->_id;
  278. $view_count = $this->getAttr('view_count', 0);
  279. $cache = VariableRedis::get($key);
  280. if ($cache) {
  281. $cache_data = unserialize($cache);
  282. $cache_count = isset($cache_data['count'])? $cache_data['count'] : 0;
  283. return $view_count + $cache_count;
  284. } else {
  285. return $view_count;
  286. }
  287. }
  288. public function parseRow($row, $output = []) {
  289. $newRow = [];
  290. $newRow['id'] = (string)$row['_id'];
  291. $newRow['name'] = CommonFn::get_val_if_isset($row, 'name', '');
  292. $newRow['status'] = CommonFn::get_val_if_isset($row, 'status', 0);
  293. $newRow['is_one_pay'] = CommonFn::get_val_if_isset($row, 'is_one_pay', 0);
  294. $newRow['one_pay_counts'] = CommonFn::get_val_if_isset($row, 'one_pay_counts', 0);
  295. //如果是一元购的话 在redis存放 还能被购买的份数
  296. $newRow['one_pay_left_counts'] = 0;
  297. if($newRow['is_one_pay']){
  298. $key = 'one_pay_left_counts_'.(string)$row['_id'];
  299. $result = VariableRedis::get($key);
  300. if(empty($result)){
  301. $newRow['one_pay_left_counts'] = $newRow['one_pay_counts'];
  302. }else{
  303. $newRow['one_pay_left_counts'] = $result;
  304. }
  305. }
  306. $newRow['status_str'] = self::$status_option[$newRow['status']]['name'];
  307. $newRow['price'] = CommonFn::get_val_if_isset($row, 'price', 0);
  308. $newRow['pics'] = CommonFn::get_val_if_isset($row, 'pics', []);
  309. $newRow['root_parent'] = (string)CommonFn::get_val_if_isset($row, 'root_parent', '');
  310. $newRow['birth_date'] = CommonFn::get_val_if_isset($row, 'birth_date', 0);
  311. $newRow['birth_date_str'] = $newRow['birth_date'] ? date('Y-m-d', $newRow['birth_date']) : '';
  312. $newRow['age'] = self::makeAge($newRow['birth_date']);
  313. $newRow['gender'] = CommonFn::get_val_if_isset($row, 'gender', 0);
  314. $newRow['gender_str'] = self::$gender_option[$newRow['gender']]['name'];
  315. $newRow['tags'] = CommonFn::get_val_if_isset($row, 'tags', []);
  316. $newRow['father_info'] = CommonFn::get_val_if_isset($row, 'father_info', []);
  317. $newRow['mother_info'] = CommonFn::get_val_if_isset($row, 'mother_info', []);
  318. $newRow['desc'] = CommonFn::get_val_if_isset($row, 'desc', '');
  319. $newRow['desc'] = str_replace("<br>","\n",$newRow['desc']);
  320. $newRow['vaccine_info'] = CommonFn::get_val_if_isset($row, 'vaccine_info', []);
  321. $newRow['count'] = CommonFn::get_val_if_isset($row, 'count', 1);
  322. $newRow['region'] = CommonFn::get_val_if_isset($row, 'region', []);
  323. $newRow['recommend_time'] = CommonFn::get_val_if_isset($row,'recommend_time',time());
  324. $newRow['reply_count'] = CommonFn::get_val_if_isset($row,'reply_count',0);
  325. $newRow['contact_count'] = CommonFn::get_val_if_isset($row,'contact_count',0);
  326. $newRow['all_reply_count'] = CommonFn::get_val_if_isset($row,'all_reply_count',0);
  327. $newRow['last_post_time'] = CommonFn::get_val_if_isset($row,'last_post_time',time());
  328. if($newRow['reply_count'] == 0){
  329. $newRow['last_post_time_str'] = '';
  330. }else{
  331. $newRow['last_post_time_str'] = CommonFn::sgmdate("Y年n月d日", $newRow['last_post_time'],1);
  332. }
  333. // 查看数统计
  334. $newRow['view_count'] = CommonFn::get_val_if_isset($row, 'view_count', 0);
  335. // 视频信息
  336. $newRow['video'] = CommonFn::get_val_if_isset($row, 'video', []);
  337. if (APPLICATION == 'api') {
  338. // 运费信息
  339. $newRow['carriage'] = CommonFn::get_val_if_isset($row, 'carriage', []);
  340. $newRow['carriage']['nonlocal'] = empty($newRow['carriage'])?0:CommonFn::get_val_if_isset($newRow['carriage'], 'nonlocal', 0);
  341. $newRow['carriage']['local'] = empty($newRow['carriage'])?0:CommonFn::get_val_if_isset($newRow['carriage'], 'local', 0);
  342. $newRow['carriage']['self'] = empty($newRow['carriage'])?0:CommonFn::get_val_if_isset($newRow['carriage'], 'self', 0);
  343. }
  344. $pet_type_str = '';
  345. - $pet_type_parent = '';
  346. - $kennel_str = '';
  347. // 品种信息
  348. $newRow['pet_type_info'] = [];
  349. // root_parent不存在时添加本字段
  350. if (CommonFn::isMongoId($row['pet_type'])) {
  351. $pet_type = PetTypes::get(new MongoId($row['pet_type']));
  352. if ($pet_type) {
  353. $newRow['pet_type_info'] = PetTypes::model()->parseRow($pet_type,array('id','name','parent','parent_name'));
  354. $pet_type_str = $pet_type->name;
  355. - $pet_type_parent = (string)$pet_type->parent;
  356. if(empty($newRow['root_parent'])){
  357. $self_update = self::get($row['_id']);
  358. $self_update->root_parent = $pet_type->parent;
  359. $self_update->update(array('root_parent'),true);
  360. }
  361. }
  362. }
  363. // 商家信息
  364. $newRow['kennel_info'] = [];
  365. if (CommonFn::isMongoId($row['kennel'])) {
  366. $kennel = Kennel::get(new MongoId($row['kennel']));
  367. $kennel_str = $kennel->name;
  368. if ($kennel) {
  369. $newRow['kennel_info'] = Kennel::model()->parseRow($kennel->attributes,array('id','name','avatar','phone','average','type','region','comment_count','address'));
  370. }
  371. }
  372. $pics = CommonFn::get_val_if_isset($row, 'pics', []);
  373. if(count($pics)){
  374. $newRow['avatar'] = $pics[0];
  375. }
  376. // 疫苗信息
  377. foreach ($newRow['vaccine_info'] as $key => &$item) {
  378. if (isset($item['time'])&&$item['time']) {
  379. $item['time_str'] = date('Y-m-d', $item['time']);
  380. } else {
  381. //$item['time_str'] = '';
  382. unset($newRow['vaccine_info'][$key]);
  383. }
  384. }
  385. // 驱虫信息
  386. $newRow['deworming_info'] = CommonFn::get_val_if_isset($row, 'deworming_info', []);
  387. foreach ($newRow['deworming_info'] as $key => &$item) {
  388. if (isset($item['time'])&&$item['time']) {
  389. $item['time_str'] = date('Y-m-d', $item['time']);
  390. } else {
  391. //$item['time_str'] = '';
  392. unset($newRow['deworming_info'][$key]);
  393. }
  394. }
  395. // 最后一次未通过理由
  396. $newRow['last_deny_reason'] = CommonFn::get_val_if_isset($row, 'last_deny_reason', '');
  397. $newRow['recommend'] = CommonFn::get_val_if_isset($row, 'recommend', 0);
  398. // action-info
  399. if (APPLICATION == 'admin') {
  400. $newRow['action_user'] = CommonFn::get_val_if_isset($row, 'action_user', '');
  401. $newRow['action_time'] = CommonFn::get_val_if_isset($row, 'action_time', '');
  402. $newRow['action_log'] = CommonFn::get_val_if_isset($row, 'action_log', '');
  403. }
  404. if (APPLICATION == 'api') {
  405. unset($newRow['debug']);
  406. unset($newRow['last_deny_reason']);
  407. unset($newRow['last_post_time_str']);
  408. unset($newRow['recommend_time']);
  409. unset($newRow['last_modify']);
  410. unset($newRow['sort_weight']);
  411. unset($newRow['add_time']);
  412. unset($newRow['add_time_str']);
  413. }
  414. if (APPLICATION == 'admin') {
  415. $newRow['last_modify'] = CommonFn::get_val_if_isset($row, 'last_modify', 0);
  416. $newRow['add_time'] = CommonFn::get_val_if_isset($row, 'add_time', 0);
  417. $newRow['add_time_str'] = $newRow['add_time']? date('Y-m-d H:i', $newRow['add_time']) : '';
  418. $newRow['sort_weight'] = CommonFn::get_val_if_isset($row, 'sort_weight', 0);
  419. $newRow['from'] = CommonFn::get_val_if_isset($row, 'from', '');
  420. $newRow['kennel'] = (string)CommonFn::get_val_if_isset($row, 'kennel', '');
  421. - $newRow['pet_type'] = (string)CommonFn::get_val_if_isset($row, 'pet_type', '');
  422. $newRow['carriage'] = CommonFn::get_val_if_isset($row, 'carriage', []);
  423. - $newRow['carriage_nonlocal'] = CommonFn::get_val_if_isset($newRow['carriage'], 'nonlocal', 0);
  424. - $newRow['carriage_local'] = CommonFn::get_val_if_isset($newRow['carriage'], 'local', 0);
  425. $newRow['pet_type_str'] = $pet_type_str;
  426. $newRow['pet_type_parent'] = $pet_type_parent;
  427. $newRow['kennel_str'] = $kennel_str;
  428. $parents_info = [];
  429. // 父亲信息
  430. $father_info = CommonFn::get_val_if_isset($row, 'father_info', []);
  431. //$parents_info['father_name'] = CommonFn::get_val_if_isset($father_info, 'name', '');
  432. $parents_info['father_avatar'] = CommonFn::get_val_if_isset($father_info, 'avatar', '');
  433. $parents_info['father_breeds'] = CommonFn::get_val_if_isset($father_info, 'breeds', '');
  434. //$parents_info['father_shoulder_height'] = CommonFn::get_val_if_isset($father_info, 'shoulder_height', 0);
  435. //$parents_info['father_weight'] = CommonFn::get_val_if_isset($father_info, 'weight', 0);
  436. //$parents_info['father_hair_color'] = CommonFn::get_val_if_isset($father_info, 'hair_color', '');
  437. // 母亲信息
  438. $mother_info = CommonFn::get_val_if_isset($row, 'mother_info', []);
  439. // $parents_info['mother_name'] = CommonFn::get_val_if_isset($mother_info, 'name', '');
  440. $parents_info['mother_avatar'] = CommonFn::get_val_if_isset($mother_info, 'avatar', '');
  441. $parents_info['mother_breeds'] = CommonFn::get_val_if_isset($mother_info, 'breeds', '');
  442. //$parents_info['mother_shoulder_height'] = CommonFn::get_val_if_isset($mother_info, 'shoulder_height', 0);
  443. //$parents_info['mother_weight'] = CommonFn::get_val_if_isset($mother_info, 'weight', 0);
  444. //$parents_info['mother_hair_color'] = CommonFn::get_val_if_isset($mother_info, 'hair_color', '');
  445. $newRow['parents_info'] = $parents_info;
  446. $deworming_info = CommonFn::get_val_if_isset($row, 'deworming_info', []);
  447. // 驱虫信息
  448. $newRow['deworming_vivo'] = isset($deworming_info['time'])&&$deworming_info['time']? date('Y-m-d', $deworming_info['time']) : '';
  449. $newRow['deworming_brand'] = isset($deworming_info['brand'])? $deworming_info['brand'] : '';
  450. }
  451. return $this->output($newRow, $output);
  452. }
  453. }