PageRenderTime 60ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/src/service/like/srv/PwLikeService.php

https://gitlab.com/wuhang2003/phpwind
PHP | 419 lines | 291 code | 42 blank | 86 comment | 45 complexity | f56d47e258f95edfb9b309549354ae64 MD5 | raw file
  1. <?php
  2. /**
  3. * @author Foxsee@aliyun.com
  4. * @copyright ?2003-2103 phpwind.com
  5. * @license http://www.phpwind.com
  6. * @version $Id: PwLikeService.php 20027 2012-10-22 11:49:23Z gao.wanggao $
  7. * @package
  8. */
  9. class PwLikeService {
  10. /**
  11. * 喜欢增加策略
  12. * Enter description here ...
  13. * @param PwUserBo $userBo
  14. * @param int $typeid
  15. * @param int $fromid
  16. */
  17. public function addLike(PwUserBo $userBo, $typeid, $fromid = 0) {
  18. $uid = $userBo->uid;
  19. $likeDs = $this->_getLikeContentDs();
  20. list($beLikeUid, $isspecial, $count, $fid, $extend) = $this->_getSourceInfo($typeid, $fromid);
  21. if ($beLikeUid == $uid) return new PwError('BBS:like.fail.myself.post');
  22. $time = Pw::getTime();
  23. //判断是否存在喜欢内容
  24. $info = $likeDs->getInfoByTypeidFromid($typeid, $fromid);
  25. $likeid = isset($info['likeid']) ? (int)$info['likeid'] : 0;
  26. Wind::import('SRV:like.dm.PwLikeDm');
  27. $dm = new PwLikeDm();
  28. $dm->setTypeid($typeid)
  29. ->setFromid($fromid)
  30. ->setIsspecial($isspecial);
  31. if ($likeid < 1) $likeid = $likeDs->addInfo($dm);
  32. //判断是否喜欢过
  33. $logInfo = $this->_getLikeLogDs()->getInfoByUidLikeid($uid, $likeid);
  34. if ($logInfo) return new PwError('BBS:like.fail.already.liked');
  35. //写入喜欢记录
  36. Wind::import('SRV:like.dm.PwLikeLogDm');
  37. $logDm = new PwLikeLogDm();
  38. $logDm->setUid($uid)
  39. ->setLikeid($likeid)
  40. ->setCreatedTime($time);
  41. $logid = $this->_getLikeLogDs()->addInfo($logDm);
  42. if ($logid < 1 ) return new PwError('BBS:like.fail');
  43. //更新喜欢内容
  44. $count ++;
  45. $this->_updateLikeCount($typeid, $fromid, $count);
  46. $likeDs->updateUsers($likeid, $uid);
  47. //写入用户喜欢统计
  48. Wind::import('SRV:user.dm.PwUserInfoDm');
  49. $likeNumber = isset($userBo->info['likes']) ? intval($userBo->info['likes']) : 0;
  50. $likeNumber++;
  51. $infoDm = new PwUserInfoDm($uid);
  52. $infoDm->setLikes($likeNumber);
  53. $userDs = Wekit::load('user.PwUser');
  54. $userDs->editUser($infoDm, PwUser::FETCH_DATA);
  55. //用户积分
  56. Wind::import('SRV:forum.bo.PwForumBo');
  57. $forumBo = new PwForumBo($fid);
  58. Wind::import('SRV:credit.bo.PwCreditBo');
  59. $credit = PwCreditBo::getInstance();
  60. $credit->operate('belike', new PwUserBo($beLikeUid), true, array('forumname' =>$userBo->username), $forumBo->getCreditSet('belike'));
  61. $credit->execute();
  62. //喜欢挂勾
  63. $dm->setBeLikeUid($beLikeUid);
  64. //$this->_getHook()->runDo('addLike', $userBo, $dm);
  65. PwSimpleHook::getInstance('PwLikeService_addLike')->runDo($userBo, $dm);
  66. //喜欢后续操作 如果不需要排行,return true
  67. $this->setLikeBrand($likeid, $count, $typeid, $fromid);
  68. return array('likeCount'=>$likeNumber, 'extend'=>$extend);
  69. }
  70. public function delLike($uid, $logid) {
  71. $info = $this->allowEditLike($uid, $logid);
  72. if ($info instanceof PwError) return false;
  73. if (!$this->_getLikeLogDs()->deleteInfo($logid)) return false;
  74. $likeInfo = $this->_getLikeContentDs()->getLikeContent($info['likeid']);
  75. if (!$likeInfo) return false;
  76. list($beLikeUid, $isspecial, $count, $fid) = $this->_getSourceInfo($likeInfo['typeid'], $likeInfo['fromid']);
  77. $count--;
  78. $this->_updateLikeCount($likeInfo['typeid'], $likeInfo['fromid'], $count);
  79. //删除喜欢tag
  80. if ($info['tagids']) {
  81. $this->_getLikeRelationsDs()->deleteInfosBylogid($logid);
  82. $tagids = explode(',', $info['tagids']);
  83. foreach ($tagids AS $tagid) {
  84. $this->_getLikeTagDs()->updateNumber($tagid, false);
  85. }
  86. }
  87. //写入喜欢统计
  88. Wind::import('SRV:user.dm.PwUserInfoDm');
  89. $userDs = Wekit::load('user.PwUser');
  90. $userStatistics = $userDs->getUserByUid($uid, PwUser::FETCH_DATA);
  91. $likeNumber = isset($userStatistics['likes']) ? intval($userStatistics['likes']) : 0;
  92. $likeNumber--;
  93. $dm = new PwUserInfoDm($uid);
  94. $dm->setLikes($likeNumber);
  95. Wekit::load('user.PwUser')->editUser($dm, PwUser::FETCH_DATA);
  96. //喜欢后续操作
  97. //$this->_getHook()->runDo('delLike', $uid, $beLikeUid);
  98. PwSimpleHook::getInstance('PwLikeService_delLike')->runDo($uid, $beLikeUid);
  99. return true;
  100. }
  101. /**
  102. * 标签更新策略 不再使用
  103. *
  104. * @param int $uid
  105. * @param array $tagids
  106. * @param int $logid
  107. * @param array $tags
  108. */
  109. public function addLikeTag($uid, $tagids, $logid, $tags) {
  110. if (!$tags) return new PwError('BBS:like.tagname.not.empty');
  111. $tagnames = array_filter(explode(' ', $tags));
  112. $tagnames = array_unique($tagnames);
  113. foreach ($tagnames AS $k=>$tag) {
  114. $tag = trim($tag);
  115. if (Pw::strlen($tag) < 2 || Pw::strlen($tag) > 10) unset($tagnames[$k]);
  116. }
  117. $_tagids = empty($tagids)? array() : explode(',', $tagids);
  118. if (count($tagnames) > 5) $tagnames = array_slice($tagnames, 0, 5);
  119. $newTags = $this->diffTagNames($tagnames, $uid);
  120. //写入新的Tag
  121. if ($newTags) {
  122. Wind::import('SRV:like.dm.PwLikeTagDm');
  123. foreach ($newTags AS $newTag) {
  124. $dm = new PwLikeTagDm();
  125. $dm->setTagname($newTag)
  126. ->setUid($uid)
  127. ->setNumber(0);
  128. $tagid = $this->_getLikeTagDs()->addInfo($dm);
  129. $_tagids[] = $tagid;
  130. }
  131. }
  132. $logInfo = $this->_getLikeLogDs()->getLikeLog($logid);
  133. //更新log Tag
  134. Wind::import('SRV:like.dm.PwLikeLogDm');
  135. $logDm = new PwLikeLogDm($logid);
  136. $logDm->setTagids($_tagids);
  137. $this->_getLikeLogDs()->updateInfo($logDm);
  138. //删除关系表
  139. $this->_getLikeRelationsDs()->deleteInfosBylogid($logid);
  140. //增加新的关系表
  141. foreach ($_tagids AS $tagid) {
  142. $this->_getLikeRelationsDs()->addInfo($logid, $tagid);
  143. }
  144. //对原tag计数减1
  145. $_logTagids = empty($logInfo['tagids'])? array() : explode(',', $logInfo['tagids']);
  146. foreach ( $_logTagids AS $tagid) {
  147. $this->_getLikeTagDs()->updateNumber($tagid, false);
  148. }
  149. //对所有tag计数加1
  150. foreach ($_tagids AS $tagid) {
  151. $this->_getLikeTagDs()->updateNumber($tagid);
  152. }
  153. //返回修改后的tag列表
  154. return $this->_getLikeTagDs()->fetchLikeTag($_tagids);
  155. }
  156. /**
  157. * 新增分类
  158. * Enter description here ...
  159. * @param int $logid
  160. * @param string $tagname
  161. */
  162. public function addTag($uid, $tagname) {
  163. if (Pw::strlen($tagname) < 2) return new PwError('BBS:like.tagname.is.short');
  164. if (Pw::strlen($tagname) > 10) return new PwError('BBS:like.tagname.is.lenth');
  165. $tagInfos = $this->_getLikeTagDs()->getInfoByUid($uid);
  166. foreach ($tagInfos AS $info) {
  167. if ($tagname == $info['tagname']) return new PwError('BBS:like.tagname.is.already');
  168. }
  169. Wind::import('SRV:like.dm.PwLikeTagDm');
  170. $dm = new PwLikeTagDm();
  171. $dm->setTagname($tagname)
  172. ->setUid($uid)
  173. ->setNumber(0);
  174. return $this->_getLikeTagDs()->addInfo($dm);
  175. }
  176. /**
  177. * 对喜欢所属分类进行增减
  178. * @param int $type 1 增加, 0 减
  179. */
  180. public function editLogTag($logid, $tagid, $type = 0) {
  181. $logInfo = $this->_getLikeLogDs()->getLikeLog($logid);
  182. $tagids = explode(',', $logInfo['tagids']);
  183. if ($type) {
  184. $tagids[] = $tagid;
  185. $this->_getLikeRelationsDs()->addInfo($logid, $tagid);
  186. $this->_getLikeTagDs()->updateNumber($tagid);
  187. } else {
  188. $k = array_search($tagid,$tagids);
  189. if ($k) unset($tagids[$k]);
  190. $this->_getLikeRelationsDs()->deleteInfo($logid, $tagid);
  191. $this->_getLikeTagDs()->updateNumber($tagid, false);
  192. }
  193. //更新log Tag
  194. Wind::import('SRV:like.dm.PwLikeLogDm');
  195. $logDm = new PwLikeLogDm($logid);
  196. $logDm->setTagids($tagids);
  197. $this->_getLikeLogDs()->updateInfo($logDm);
  198. return true;
  199. }
  200. /**
  201. * 获取喜欢榜单
  202. *
  203. * @param string $key
  204. * @param int $start
  205. * @param int $limit
  206. * @param bool $isthread
  207. */
  208. public function getLikeBrand($key, $start = 0, $limit = 10, $isthread = false) {
  209. $statis = $this->_getLikeStatisticsDs()->getInfoList($key, $start, $limit, $isthread);
  210. $likeids = $tids = array();
  211. if (empty($statis)) return array();
  212. $likeds = $this->_getLikeContentDs();
  213. foreach ($statis AS $val) {
  214. if ($val['typeid'] != PwLikeContent::THREAD) continue;
  215. $tids[] = $val['fromid'];
  216. $likeids[] = $val['likeid'];
  217. }
  218. $threads = $this->_getThreadDs()->fetchThread($tids);
  219. $likes = $likeds->fetchLikeContent($likeids);
  220. foreach ($likes AS $key=>$val) {
  221. if (!$threads[$val['fromid']]['subject']) {
  222. unset($likes[$key]);
  223. } else {
  224. $likes[$key]['subject'] = $threads[$val['fromid']]['subject'];
  225. }
  226. }
  227. return $likes;
  228. }
  229. /**
  230. * 新标签过滤
  231. *
  232. * @param string $tagnames
  233. * @param array $tagids
  234. */
  235. public function diffTagNames($tagnames, $uid) {
  236. $_tagnames = array();
  237. if (!is_array($tagnames) || count($tagnames) <1 ) return false;
  238. $tagInfos = $this->_getLikeTagDs()->getInfoByUid($uid);
  239. foreach ($tagInfos AS $info) {
  240. $_tagnames[] = $info['tagname'];
  241. }
  242. return array_diff($tagnames, $_tagnames);
  243. }
  244. /**
  245. * 喜欢增加策略后续操作:更新喜欢排行榜
  246. *
  247. * $signKeys 排行榜时间,按相对时间排行
  248. * $countKeys 每种排行的当前记录数
  249. * $maxStatis 最大记录数
  250. * @param int $likeid
  251. * @param int $count
  252. */
  253. public function setLikeBrand($likeid, $count, $typeid, $fromid) {
  254. $signKeys = array('day7'=>604800, 'day2'=>172800, 'day1'=>86400);
  255. $countKeys = array('day7_count', 'day2_count', 'day1_count');
  256. $minInfo = $this->_getLikeStatisticsDs()->getMinInfo('day7');
  257. $minCount = $minInfo ? $minInfo['number'] : 0 ;
  258. $maxStatis = 100;
  259. $time = Pw::getTime();
  260. if ($minCount > $count) return false;
  261. foreach ($signKeys AS $key => $value) {
  262. $startTime = $time - $value;
  263. $keyInfo = $this->_getLikeStatisticsDs()->getLikeStatistics($key.'_count');
  264. $keyCount = $keyInfo ? $keyInfo['number'] : 0 ;
  265. if ($minCount < $count || $keyCount < $maxStatis) {
  266. $logCount = $this->_getLikeLogDs()->getLikeidCount($likeid, $startTime);
  267. Wind::import('SRV:like.dm.PwLikeStatisticsDm');
  268. $dm = new PwLikeStatisticsDm();
  269. $dm->setSignkey($key)
  270. ->setLikeid($likeid)
  271. ->setTypeid($typeid)
  272. ->setFromid($fromid)
  273. ->setNumber($logCount);
  274. $msg = $this->_getLikeStatisticsDs()->addInfo($dm);
  275. if (is_numeric($msg) && $keyCount < $maxStatis) {
  276. $dm = new PwLikeStatisticsDm();
  277. $keyCount++;
  278. $dm->setSignkey($key.'_count')
  279. ->setLikeid(0)
  280. ->setNumber($keyCount);
  281. $this->_getLikeStatisticsDs()->addInfo($dm);
  282. }
  283. }
  284. }
  285. return true;
  286. }
  287. /**
  288. * 判断喜欢编辑部权限
  289. *
  290. * @param $logid
  291. */
  292. public function allowEditLike($uid, $logid) {
  293. if($logid < 1) return new PwError('BBS:like.fail');
  294. $info = $this->_getLikeLogDs()->getLikeLog($logid);
  295. if ($info['uid'] < 1 || $info['uid'] != $uid) return new PwError('BBS:like.fail');
  296. return $info;
  297. }
  298. public function allowEditTag($uid, $tagid) {
  299. if ($tagid < 1 ) return new PwError('BBS:like.fail');
  300. $info = $this->_getLikeTagDs()->getLikeTag($tagid);
  301. if (!$info || $info['uid'] < 1 )return new PwError('BBS:like.tagname.empty');
  302. if ( $info['uid'] != $uid) return new PwError('BBS:like.permissions.fail');
  303. return $info;
  304. }
  305. private function _getSourceInfo($typeid, $fromid) {
  306. $extend = array();
  307. switch ($typeid) {
  308. case PwLikeContent::THREAD:
  309. $msg = Wekit::load('forum.PwThread')->getThread($fromid);
  310. //needcheck
  311. Wind::import('SRV:forum.srv.post.PwReplyPost');
  312. Wind::import('SRV:forum.srv.PwPost');
  313. $postAction = new PwReplyPost($fromid);
  314. $post = new PwPost($postAction);
  315. if ($post->getDisabled()){
  316. $extend = array('needcheck'=>true);
  317. }
  318. return array($msg['created_userid'], $msg['special'], $msg['like_count'], $msg['fid'], $extend);
  319. case PwLikeContent::POST:
  320. $msg = Wekit::load('forum.PwThread')->getPost($fromid);
  321. return array($msg['created_userid'], 0, $msg['like_count'], $msg['fid']);
  322. case PwLikeContent::WEIBO:
  323. $msg = Wekit::load('weibo.PwWeibo')->getWeibo($fromid);
  324. return array($msg['created_userid'], 0, $msg['like_count'], 0);
  325. case PwLikeContent::APP:
  326. $msg = Wekit::load('like.PwLikeSource')->getSource($fromid);
  327. return array(0, 0, $msg['like_count'], 0);
  328. }
  329. }
  330. private function _updateLikeCount($typeid, $fromid, $count) {
  331. switch ($typeid) {
  332. case PwLikeContent::THREAD:
  333. Wind::import('SRV:forum.dm.PwTopicDm');
  334. $dm = new PwTopicDm($fromid);
  335. $dm->setLikeCount($count);
  336. return Wekit::load('forum.PwThread')->updateThread($dm, PwThread::FETCH_MAIN);
  337. case PwLikeContent::POST:
  338. Wind::import('SRV:forum.dm.PwReplyDm');
  339. $dm = new PwReplyDm($fromid);
  340. $dm->setLikeCount($count);
  341. return Wekit::load('forum.PwThread')->updatePost($dm);
  342. case PwLikeContent::WEIBO:
  343. Wind::import('SRV:weibo.dm.PwWeiboDm');
  344. $dm = new PwWeiboDm($fromid);
  345. $dm->setLikeCount($count);
  346. return Wekit::load('weibo.PwWeibo')->updateWeibo($dm);
  347. case PwLikeContent::APP:
  348. Wind::import('SRV:like.dm.PwLikeSourceDm');
  349. $dm = new PwLikeSourceDm($fromid);
  350. $dm->setLikeCount($count);
  351. return Wekit::load('like.PwLikeSource')->updateSource($dm);
  352. }
  353. }
  354. private function _getThreadDs() {
  355. return Wekit::load('forum.PwThread');
  356. }
  357. private function _getLikeTagDs() {
  358. return Wekit::load('like.PwLikeTag');
  359. }
  360. private function _getLikeLogDs() {
  361. return Wekit::load('like.PwLikeLog');
  362. }
  363. private function _getLikeContentDs() {
  364. return Wekit::load('like.PwLikeContent');
  365. }
  366. private function _getLikeStatisticsDs() {
  367. return Wekit::load('like.PwLikeStatistics');
  368. }
  369. private function _getLikeRelationsDs() {
  370. return Wekit::load('like.PwLikeRelations');
  371. }
  372. /*
  373. private function _getHook() {
  374. return new PwHookService('PwLikeService', 'PwLikeDoBase');
  375. }*/
  376. }
  377. ?>