UserSignRepository.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\common\repositories\user;
  3. use app\common\dao\user\UserSignDao;
  4. use app\common\repositories\BaseRepository;
  5. use app\common\repositories\system\groupData\GroupDataRepository;
  6. use app\common\repositories\system\groupData\GroupRepository;
  7. use think\exception\ValidateException;
  8. use think\facade\Db;
  9. class UserSignRepository extends BaseRepository
  10. {
  11. /**
  12. * @var UserSignDao
  13. */
  14. protected $dao;
  15. /**
  16. * UserSignRepository constructor.
  17. * @param UserSignDao $dao
  18. */
  19. public function __construct(UserSignDao $dao)
  20. {
  21. $this->dao = $dao;
  22. }
  23. /**
  24. * TODO 获取指定日期 用户的连续签到数
  25. * @param int $uid
  26. * @param string $day
  27. * @return array
  28. * @author Qinii
  29. * @day 6/8/21
  30. */
  31. public function getSign(int $uid,string $day)
  32. {
  33. return $this->dao->getSearch(['uid' => $uid,'day' => $day])->value('sign_num');
  34. }
  35. public function getDay($num)
  36. {
  37. if($num > 7) $num = ($num % 7);
  38. $title = $this->signConfig();
  39. if(empty($title)) throw new ValidateException('未开启签到功能');
  40. return $title[$num-1]['value'];
  41. }
  42. /**
  43. * TODO 签到操作
  44. * @param int $uid
  45. * @author Qinii
  46. * @day 6/8/21
  47. */
  48. public function create(int $uid)
  49. {
  50. /**
  51. * 用户昨天的签到情况,如果有就是连续签到,如果没有就是第一天签到
  52. * 根据签到天数计算签到积分等操作
  53. * 计算用户剩余积分
  54. *
  55. */
  56. return Db::transaction(function() use($uid){
  57. $yesterday = date("Y-m-d",strtotime("-1 day"));
  58. $sign_num = ($this->getSign($uid,$yesterday) ?: 0) + 1;
  59. //签到规则计算
  60. $sign_task = $this->getDay($sign_num);
  61. $integral = $sign_task['sign_integral'];
  62. $user_make = app()->make(UserRepository::class);
  63. $user = $user_make->get($uid);
  64. $integral_ = $user['integral'] + $sign_task['sign_integral'];
  65. $data = [
  66. 'uid' => $uid,
  67. 'sign_num' => $sign_num,
  68. 'number' => $sign_task['sign_integral'],
  69. 'integral' => $integral_,
  70. 'title' => '签到',
  71. ];
  72. $this->dao->create($data);
  73. //增加记录
  74. $arr = [
  75. 'status' => 1,
  76. 'mark' => '签到,获得积分'. $sign_task['sign_integral'],
  77. 'number' => $sign_task['sign_integral'],
  78. 'balance'=> $integral_,
  79. ];
  80. $user_make->incIntegral($uid,$sign_task['sign_integral'],'签到'.$sign_task['sign_day'],'sign_integral',$arr);
  81. return compact('integral');
  82. });
  83. }
  84. public function getList(array $where,int $page,int $limit)
  85. {
  86. $query = $this->dao->getSearch($where)->order('create_time DESC');
  87. $count = $query->count();
  88. $list = $query->page($page,$limit)->select();
  89. return compact('count','list');
  90. }
  91. public function info(int $uid)
  92. {
  93. /**
  94. * 连续签到日期展示 1 - 7天
  95. * 是否签到
  96. * 累计签到数
  97. */
  98. $ret = $this->signStatus($uid);
  99. $is_sign = $ret['is_sign'];
  100. $sign_num = $ret['sign_num'];
  101. $title = $this->signConfig();
  102. $userInfo = app()->make(UserRepository::class)->getWhere(['uid' => $uid],'uid,avatar,nickname,integral');
  103. $count = $this->dao->getSearch(['uid' => $uid])->count('*');
  104. return compact('userInfo','is_sign','sign_num','count','title');
  105. }
  106. public function signConfig(){
  107. $group_make = app()->make(GroupRepository::class);
  108. $sign_day_config = $group_make->keyById('sign_day_config');
  109. $title = app()->make(GroupDataRepository::class)
  110. ->getGroupDataWhere(0,$sign_day_config)
  111. ->where('status',1)->limit(7)
  112. ->hidden(['group_data_id','group_id','create_time','mer_id'])->select()->toArray();
  113. return $title;
  114. }
  115. /**
  116. * TODO 连续签到 获取 1- 7 天
  117. * @param $uid
  118. * @return array
  119. * @author Qinii
  120. * @day 6/10/21
  121. */
  122. public function signStatus($uid)
  123. {
  124. $day = date('Y-m-d',time());
  125. $sign_num = 0;
  126. $sign_num = $this->getSign($uid,$day);
  127. $is_sign = $sign_num ? 1 : 0;
  128. if($sign_num > 7){
  129. $sign_num = ($sign_num % 7);
  130. if(!$sign_num) $sign_num = 7;
  131. }
  132. if(!$is_sign){
  133. $yesterday = date("Y-m-d",strtotime("-1 day"));
  134. $sign_num = $this->getSign($uid,$yesterday) ?: 0;
  135. if($sign_num > 7){
  136. $sign_num = ($sign_num % 7);
  137. }
  138. }
  139. return compact('is_sign','sign_num');
  140. }
  141. /**
  142. * TODO 按月显示签到记录
  143. * @param array $where
  144. * @return array
  145. * @author Qinii
  146. * @day 6/10/21
  147. */
  148. public function month(array $where)
  149. {
  150. $group = $this->dao->getSearch($where)->field('FROM_UNIXTIME(unix_timestamp(create_time),"%Y-%m") as time')
  151. ->order('time DESC')->group('time')->select();
  152. $ret = [];
  153. foreach ($group as $k => $item){
  154. $ret[$k]['month'] = $item['time'];
  155. $query = $this->dao->getSearch($where)->field('title,number,create_time')->whereMonth('create_time',$item['time']);
  156. $ret[$k]['list'] = $query->order('create_time DESC')->select();
  157. }
  158. return $ret;
  159. }
  160. }