PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/src/service/medal/srv/PwMedalService.php

https://gitlab.com/wuhang2003/phpwind
PHP | 327 lines | 220 code | 23 blank | 84 comment | 34 complexity | db80a7a3e0d5b1e27870fbbcb6f107ea MD5 | raw file
  1. <?php
  2. /**
  3. * the last known user to change this file in the repository <$LastChangedBy: xiaoxia.xuxx $>
  4. * @author $Author: xiaoxia.xuxx $ Foxsee@aliyun.com
  5. * @copyright ©2003-2103 phpwind.com
  6. * @license http://www.phpwind.com
  7. * @version $Id: PwMedalService.php 22364 2012-12-21 12:32:59Z xiaoxia.xuxx $
  8. * @package
  9. */
  10. class PwMedalService {
  11. /**
  12. * 从勋章用户表获取一个用户的勋章
  13. *
  14. * 全局缓存从pwMedalCahce.php里取
  15. * @param int $uid
  16. */
  17. public function getUserMedal($uid) {
  18. $_medals = array();
  19. $userMedal = $this->_getMedalUserDs()->getMedalUser($uid);
  20. if (!$userMedal) return array();
  21. $medalIds = empty($userMedal['medals']) ? array() : explode(',', $userMedal['medals']);
  22. $medalIds = array_unique($medalIds);
  23. $medals = $this->_getMedalDs()->fetchMedalInfo($medalIds);
  24. foreach ($medalIds AS $medalId) {
  25. if (!$medals[$medalId]) continue;
  26. $medals[$medalId]['image'] = $this->getMedalImage($medals[$medalId]['path'],$medals[$medalId]['image']);
  27. $medals[$medalId]['icon'] = $this->getMedalImage($medals[$medalId]['path'],$medals[$medalId]['icon']);
  28. $_medals[] = $medals[$medalId];
  29. }
  30. return $_medals;
  31. }
  32. /**
  33. * 从勋章用户表获取多个用户的勋章
  34. *
  35. * 全局缓存从pwMedalCahce.php里取
  36. * @param array $uid
  37. */
  38. public function fetchUserMedal($uids) {
  39. if (!is_array($uids)) return array();
  40. $_userMedalIds = $_allMedalId = $_medals = array();
  41. $userMedal = $this->_getMedalUserDs()->fetchMedalUser($uids);
  42. if (!$userMedal) return array();
  43. foreach ($uids AS $uid) {
  44. $_userMedalIds[$uid] = !$userMedal[$uid]['medals'] ? array() : explode(',', $userMedal[$uid]['medals']);
  45. $_allMedalId = array_merge($_allMedalId, $_userMedalIds[$uid]);
  46. }
  47. $_allMedalId = array_unique($_allMedalId);
  48. $medals = $this->_getMedalDs()->fetchMedalInfo($_allMedalId);
  49. $attachUrl =Pw::getPath(''). 'medal/';
  50. $localUrl = WindUrlHelper::checkUrl(PUBLIC_RES . '/images/medal/', PUBLIC_URL) . '/' ;
  51. foreach ($_userMedalIds AS $uid=>$medalIds) {
  52. $_medalInfo = array();
  53. foreach ($medalIds AS $medalId) {
  54. if (!$medals[$medalId]) continue;
  55. $path = $medals[$medalId]['path'] ? $attachUrl : $localUrl;
  56. $_tmp = $medals[$medalId];
  57. $_tmp['image'] = $path . $_tmp['image'];
  58. $_tmp['icon'] = $path . $_tmp['icon'];
  59. $_medalInfo[] = $_tmp;
  60. }
  61. $_medals[$uid] = $_medalInfo;
  62. }
  63. return $_medals;
  64. }
  65. /**
  66. * 勋章领取,用于申请任务的勋章
  67. *
  68. * @param int $logId
  69. * @param int $uid
  70. */
  71. public function awardMedal($logId, $uid) {
  72. $log = $this->_getMedalLogDs()->getMedalLog($logId);
  73. if (!isset($log['uid']) || $log['uid'] != $uid || $log['award_status'] != 3) return new PwError('MEDAL:info.error');
  74. $medal = $this->_getMedalDs()->getMedalInfo($log['medal_id']);
  75. $time = Pw::getTime();
  76. $expired = ($medal['expired_days'] > 0) ? ($time + $medal['expired_days']*86400) : 0;
  77. Wind::import('SRV:medal.dm.PwMedalLogDm');
  78. $dm = new PwMedalLogDm($logId);
  79. $dm->setAwardStatus(4)
  80. ->setExpiredTime($expired);
  81. $resource = $this->_getMedalLogDs()->updateInfo($dm);
  82. if ($resource instanceof PwError) return $resource->getError();
  83. return $this->updateMedalUser($uid);
  84. //return $this->sendNotice($uid, $logId, $log['medal_id'], 6);
  85. }
  86. /**
  87. * 颁发勋章,用于完成的自动任务获取的勋章
  88. *
  89. * @param int $uid
  90. * @param int $medalId
  91. */
  92. public function awardTaskMedal($uid, $medalId) {
  93. $info = $this->_getMedalDs()->getMedalInfo($medalId);
  94. if (!$info) return false;
  95. $userLog = $this->_getMedalLogDs()->getInfoByUidMedalId($uid, $medalId);
  96. Wind::import('SRV:medal.dm.PwMedalLogDm');
  97. $time = Pw::getTime();
  98. if ($userLog && $userLog['award_status'] < 4) {
  99. $dm = new PwMedalLogDm($userLog['log_id']);
  100. $dm->setAwardStatus(3);
  101. $resource = $this->_getMedalLogDs()->updateInfo($dm);
  102. } else {
  103. $dm = new PwMedalLogDm();
  104. $dm->setMedalid($medalId)
  105. ->setUid($uid)
  106. ->setAwardStatus(3)
  107. ->setCreatedTime($time);
  108. $resource = $this->_getMedalLogDs()->replaceMedalLog($dm);
  109. }
  110. }
  111. /**
  112. * 勋章消息发送
  113. *
  114. * @param int $uid
  115. * @param int $medelId
  116. * @param string $type 1.自动颁发|2.管理员颁发|3.申请通过|4.申请不通过|5.系统回收|6.领取勋章|7.管理员回收
  117. * @param string $reason
  118. */
  119. public function sendNotice($uid, $logId, $medelId, $type = 1, $reason = '') {
  120. $info = $this->_getMedalDs()->getMedalInfo($medelId);
  121. if (!$info) return false;
  122. $param = 0;
  123. switch ($type) {
  124. case 1:
  125. case 2:
  126. case 3:
  127. case 4:
  128. case 6:
  129. case 7:
  130. $extendParams = array( 'logid'=>$logId, 'name'=>$info['name'], 'medelId'=>$medelId, 'type'=>$type, 'reason'=>$reason);
  131. return Wekit::load('SRV:message.srv.PwNoticeService')->sendNotice($uid, 'medal', $param, $extendParams);
  132. case 5:
  133. $lang = Wind::getComponent('i18n');
  134. $awardType = $this->awardTypes($info['award_type']);
  135. if (!$reason) $reason = $info['receive_type'] == 1 ? '您的'.$lang->getMessage("MEDAL:awardtype.".$awardType) ."低于勋章设定值" .$info['award_condition'] : '';
  136. $extendParams = array( 'logid'=>$logId, 'name'=>$info['name'], 'medelId'=>$medelId, 'type'=>$type, 'reason'=>$reason);
  137. return Wekit::load('SRV:message.srv.PwNoticeService')->sendNotice($uid, 'medal', $param, $extendParams);
  138. }
  139. }
  140. /**
  141. * 停用勋章
  142. *
  143. * @param int $logid
  144. */
  145. public function stopAward($logid, $type = 5) {
  146. if ($logid <1) return new PwError('info_error');
  147. $info = $this->_getMedalLogDs()->getMedalLog($logid);
  148. if (!$info) return new PwError('info_error');
  149. $resource = $this->_getMedalLogDs()->deleteInfo($logid);
  150. if (!$resource) return new PwError('info_error');
  151. $this->sendNotice($info['uid'], $logid, $info['medal_id'], $type);
  152. return $this->updateMedalUser($info['uid']);
  153. }
  154. /**
  155. * 回收用户过期勋章
  156. *
  157. */
  158. public function recoverMedal($uid) {
  159. $time = Pw::getTime();
  160. $userMedalUser = $this->_getMedalUserDs()->getMedalUser($uid);
  161. if ($userMedalUser['expired_time'] > 0 && $userMedalUser['expired_time'] < $time) {
  162. $this->updateMedalUser($uid);
  163. }
  164. }
  165. /**
  166. * 更新用户勋章统计
  167. *
  168. * @param int $uid
  169. */
  170. public function updateMedalUser($uid) {
  171. $expireds = $medalids = array();
  172. $time = Pw::getTime();
  173. $logs = $this->_getMedalLogDs()->getInfoListByUidStatus($uid, 4);
  174. foreach ($logs AS $log) {
  175. if ($log['expired_time'] > 0 && $log['expired_time'] < $time ){
  176. $this->_getMedalLogDs()->deleteInfo($log['log_id']);
  177. $this->sendNotice($uid, $log['log_id'], $log['medal_id'], 5);
  178. } else {
  179. $medalids[] = $log['medal_id'];
  180. $log['expired_time'] > 0 &&$expireds[] = $log['expired_time'];
  181. //$expired = $expired < $log['expired_time'] ? $log['expired_time'] : $expired;
  182. }
  183. }
  184. $expireds = array_filter($expireds);
  185. sort($expireds,SORT_NUMERIC);
  186. $expired = array_shift($expireds);
  187. /*user_data冗余*/
  188. $dm = Wind::import('SRV:user.dm.PwUserInfoDm');
  189. $dm = new PwUserInfoDm($uid);
  190. $dm->setMedalIds($medalids);
  191. Wekit::load('user.PwUser')->editUser($dm, PwUser::FETCH_DATA);
  192. /*end*/
  193. $dm = Wind::import('SRV:medal.dm.PwMedalUserDm');
  194. $dm = new PwMedalUserDm($uid);
  195. $dm->setMedals($medalids)
  196. ->setCounts(count($medalids))
  197. ->setExpiredTime($expired);
  198. return $this->_getMedalUserDs()->replaceInfo($dm);
  199. }
  200. /**
  201. * 勋章缓存更新策略
  202. *
  203. * @return void
  204. */
  205. public function updateCache() {
  206. $cacheDs = Wekit::cache();
  207. $cacheDs->set('medal_all', $this->getMedalAllCacheValue());
  208. $cacheDs->set('medal_auto', $this->getMedalAutoCacheValue());
  209. }
  210. /**
  211. * 获取所有勋章缓存内容
  212. *
  213. * @return array
  214. */
  215. public function getMedalAllCacheValue() {
  216. $medalAll = array();
  217. $all = $this->_getMedalDs()->getAllMedal();
  218. foreach ($all AS $medal) {
  219. $medalAll[$medal['medal_id']] = array(
  220. 'name' => $medal['name'],
  221. 'path' => $medal['path'],
  222. 'image' => $medal['image'],
  223. 'icon' => $medal['icon'],
  224. );
  225. }
  226. return $medalAll;
  227. }
  228. /**
  229. * 获取所有自动勋章缓存内容
  230. *
  231. * @return array
  232. */
  233. public function getMedalAutoCacheValue() {
  234. $medalAuto = array();
  235. $auto = $this->_getMedalDs()->getInfoListByReceiveType(1, 1);
  236. foreach ($auto AS $medal) {
  237. $medalAuto[] = $medal['medal_id'];
  238. }
  239. return $medalAuto;
  240. }
  241. /**
  242. * 判断勋章用户组与用户组的领取权限
  243. *
  244. * @param string $userGids 1,2,3,4
  245. * @param string $medalGids 1,2,3,4
  246. */
  247. public function allowAwardMedal($userGids, $medalGids = '') {
  248. $medalGids = !is_array($medalGids) && $medalGids ? explode(',', $medalGids) : $medalGids ;
  249. $userGids = !is_array($userGids) && $userGids ? explode(',', $userGids) : $userGids;
  250. if ($medalGids && !array_intersect($userGids, $medalGids)) return false;
  251. return true;
  252. }
  253. public function getUserBehavior($uid) {
  254. $_array = array();
  255. $behaviors = Wekit::load('user.PwUserBehavior')->getBehaviorList($uid);
  256. $awardTypes = $this->awardTypes();
  257. foreach ($behaviors AS $behavior) {
  258. $_array[$behavior['behavior']] = $behavior['number'];
  259. }
  260. $statistics = Wekit::load('user.PwUser')->getUserByUid($uid, PwUser::FETCH_DATA);
  261. $_array['like_count'] = $statistics['likes'];
  262. $_array['follow_number'] = $statistics['follows'];
  263. $_array['fans_number'] = $statistics['fans'];
  264. return $_array;
  265. }
  266. public function awardTypes($type = '') {
  267. $_array = array(
  268. 1=>'login_days',
  269. 2=>'post_days',
  270. 3=>'thread_days',
  271. 4=>'safa_times',
  272. 5=>'fans_number',
  273. 6=>'belike_times',
  274. 7=>'thread_count',
  275. 8=>'follow_number',
  276. 9=>'like_count',
  277. 10=>'login_count'
  278. );
  279. if (!empty($type)) return $_array[$type];
  280. return $_array;
  281. }
  282. public function getMedalImage($path = '', $filename = '') {
  283. if ($path) {
  284. return Pw::getPath($path . $filename);
  285. } else {
  286. return WindUrlHelper::checkUrl(PUBLIC_RES . '/images/medal/', PUBLIC_URL) . '/' . $filename;
  287. }
  288. /*if ($type == 'image'){
  289. return WindUrlHelper::checkUrl(PUBLIC_RES . '/images/medal/big', PUBLIC_URL);
  290. } else {
  291. return WindUrlHelper::checkUrl(PUBLIC_RES . '/images/medal/small', PUBLIC_URL);
  292. }*/
  293. }
  294. private function _getMedalDs() {
  295. return Wekit::load('medal.PwMedalInfo');
  296. }
  297. private function _getMedalLogDs() {
  298. return Wekit::load('medal.PwMedalLog');
  299. }
  300. private function _getMedalUserDs() {
  301. return Wekit::load('medal.PwMedalUser');
  302. }
  303. }
  304. ?>