PointLog.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/4/12
  6. * Time: 下午2:39
  7. */
  8. namespace app\frontend\modules\finance\models;
  9. use Illuminate\Support\Collection;
  10. use Intervention\Image\Point;
  11. use app\common\helpers\Cache;
  12. class PointLog extends \app\common\models\finance\PointLog
  13. {
  14. /*public static function getPointTotal($member_id, $type = null)
  15. {
  16. $builder = PointLog::select()->byMemberId($member_id)->type($type)->limit(5)->orderBy('id', 'desc');
  17. return $builder;
  18. }
  19. public static function getLastTime($member_id, $type)
  20. {
  21. $time = PointLog::select('created_at')->byMemberId($member_id)->type($type)->first();
  22. return $time;
  23. }*/
  24. /**
  25. * 新的获取积分明细方法
  26. * @param $member_id
  27. * @param null $type
  28. * @param null $mode
  29. * @param null $month
  30. * @return array
  31. */
  32. public function getPointRecord($member_id, $type = null, $mode = null, $month = null){
  33. $original=PointLog::select(['relation_id','id','created_at','member_id','point_mode','point','before_point','after_point','point_income_type','point_mode'])
  34. ->byMemberId($member_id)->type($type)->mode($mode)->month($month)
  35. ->orderBy('created_at', 'desc')->orderBy('id','desc')->paginate(15)->toArray();
  36. if(empty($original['data'])&&!$month){
  37. return ['record_list'=>['data'=>[date('Y-m')=>[]]],'point_arr'=>[date('Y-m')=>['income'=>0,'expand'=>0]],'source_comment'=>$this->getSource($member_id),];
  38. }
  39. $month=date('Y-m',strtotime($month));
  40. $list=$this->getFilterData($original,$month);
  41. $monthData=$this->getMonthData($list['month_list']?:[$month],$member_id);
  42. $source=(new \app\common\models\finance\PointLog())->sourceComment();
  43. return ['record_list'=>$list['record_list'],'source_comment'=>$this->getSource($member_id),'point_arr'=>$monthData];
  44. }
  45. /**
  46. * 1.1.138前取积分明细方法
  47. * @param $member_id
  48. * @param null $type
  49. * @param null $mode
  50. * @param null $month
  51. * @return mixed
  52. */
  53. public static function getPointLogList($member_id, $type = null)
  54. {
  55. $builder = PointLog::select()->byMemberId($member_id)->type($type)->orderBy('id', 'desc')->paginate(15)->toArray();
  56. return $builder;
  57. }
  58. /**
  59. * 获取用户最新的三条记录
  60. * @param $member_id
  61. * @return mixed
  62. */
  63. public function getLatestRecord($member_id){
  64. return PointLog::select(['id','created_at','member_id','point_mode','point','before_point','after_point','point_income_type'])->byMemberId($member_id)->orderBy('created_at','desc')->limit(3)->get()->toArray();
  65. }
  66. public function scopeByMemberId($query, $member_id)
  67. {
  68. return $query->where('member_id', $member_id)->uniacid();
  69. }
  70. public function scopeType($query, $type)
  71. {
  72. if (!isset($type) || $type == 0) {
  73. return $query;
  74. }
  75. return $query->where('point_income_type', $type);
  76. }
  77. public function scopeMode($query, $mode)
  78. {
  79. if (!isset($mode) || $mode == 0) {
  80. return $query;
  81. }
  82. return $query->where('point_mode', $mode);
  83. }
  84. public function scopeMonth($query, $month)
  85. {
  86. if (!isset($month) || $month == '') {
  87. return $query;
  88. }
  89. return $query->where('created_at', '<', strtotime('last day of ' . $month)+86399);
  90. }
  91. /**
  92. * 获取按月份分类后的积分明细数据
  93. * @param $data
  94. * @return array
  95. */
  96. public function getFilterData($data,$month)
  97. {
  98. $list = [];
  99. foreach ($data['data'] as $val) {
  100. $list[date('Y-m', strtotime($val['created_at']))][] = $val;
  101. }
  102. if(empty($list)){
  103. $list[$month]=[];
  104. }
  105. $month=array_keys($list);
  106. $data['data']=$list;
  107. return ['record_list'=>$data,'month_list'=>$month];
  108. }
  109. /**
  110. * 获取月份区间中,每个月的收入和支出
  111. * @param $month
  112. * @param $member_id
  113. * @return array
  114. */
  115. private function getMonthData($month,$member_id){
  116. $startMonth=strtotime($month[count($month)-1]);
  117. $endMonth=strtotime('last day of '.$month[0])+86399;
  118. $original=PointLog::selectRaw("YEAR(FROM_UNIXTIME(created_at,'%Y-%m-%d')) as years ,Right(100 + MONTH(FROM_UNIXTIME(created_at,'%Y-%m-%d')),2) as months ,sum(if(point_income_type=1,point,0)) as income,sum(if(point_income_type=-1,point,0))as expand")
  119. ->byMemberId($member_id)
  120. ->whereBetween('created_at',[$startMonth,$endMonth])
  121. ->groupByRaw("years,months")
  122. ->get()->toArray();
  123. $result=[];
  124. if(count($original)){
  125. foreach($original as $val){
  126. $result[$val['years'].'-'.$val['months']]=['income'=>$val['income'],'expand'=>$val['expand']];
  127. }
  128. }else{
  129. $result[$month[0]]=['income'=>0,'expand'=>0];
  130. }
  131. return $result;
  132. }
  133. /**
  134. * 获取积分来源
  135. * @param $member_id
  136. * @return mixed
  137. */
  138. private function getSource($member_id){
  139. $redis_key = "ServiceTypeList:".$member_id;
  140. $result=[];
  141. if(Cache::has($redis_key)){
  142. $result = Cache::get($redis_key);
  143. }else{
  144. $service_type_arr = (new \app\common\models\finance\PointLog())->sourceComment();
  145. $service_type_key = PointLog::where('member_id',$member_id)
  146. ->select(['point_mode'])
  147. ->groupBy('point_mode')
  148. ->pluck('point_mode');
  149. $service_type_arr = $service_type_key->map(function ($serviceKey) use ($service_type_arr){
  150. return [
  151. 'id' => $serviceKey,
  152. 'name' => $service_type_arr[$serviceKey],
  153. ];
  154. })->values();
  155. foreach($service_type_arr as $key=>$value){
  156. $result[$value['id']]=$value['name'];
  157. }
  158. //根据当前会员的积分明细查出所拥有的服务类型,存进缓存
  159. Cache::put($redis_key, $result, 1440);
  160. }
  161. return $result;
  162. }
  163. }