|
|
@@ -355,4 +355,98 @@ class Member
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 到期清除
|
|
|
+ * @param $userId
|
|
|
+ * @return bool
|
|
|
+ * @throws \think\db\exception\DataNotFoundException
|
|
|
+ * @throws \think\db\exception\ModelNotFoundException
|
|
|
+ * @throws \think\exception\DbException
|
|
|
+ */
|
|
|
+ public static function clearSignRedHeart($userId){
|
|
|
+ $cacheKey = "cache:signs:clear:{$userId}_".date('Ym');
|
|
|
+ if(PRedis::get($cacheKey)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $siteInfo = cmf_get_option('site_info');
|
|
|
+ $signClearDay = isset($siteInfo['sign_clear_day'])? intval($siteInfo['sign_clear_day']) : 0;
|
|
|
+ $signClearDay = $signClearDay? min($signClearDay, 28) : 1;
|
|
|
+ $signClearDay = $signClearDay<10? '0'.$signClearDay : $signClearDay;
|
|
|
+ if(date('Y-m-d') < date('Y-m-'.$signClearDay)){
|
|
|
+ PRedis::set($cacheKey, ['error' =>'未到清除时间','date'=> date('Y-m-d H:i:s'),'day'=> $signClearDay], rand(10, 30));
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $month = date('Y-m-01', strtotime(date('Y-m-01')) - 86400);
|
|
|
+ $check = AccountLog::where(['user_id' => $userId, 'type'=> 13, 'status' => 2])
|
|
|
+ ->where('created_at', '>=', $month)
|
|
|
+ ->find();
|
|
|
+ if($check){
|
|
|
+ PRedis::set($cacheKey, ['error' =>'已清除过','info'=>$check,'date'=> date('Y-m-d H:i:s'),'day'=> $signClearDay], 86400);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $info = MemberModel::where(['id'=> $userId])->field('id,openid,user_nickname,redheart')->find();
|
|
|
+ if(empty($info)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 最早
|
|
|
+ $firstSign = AccountLog::where(['user_id'=> $userId,'type'=> 12,'status'=> 2])
|
|
|
+ ->where('created_at','>=', $month)
|
|
|
+ ->where('created_at','<', date('Y-m-01'))
|
|
|
+ ->order('created_at','asc')
|
|
|
+ ->find();
|
|
|
+ $time = isset($firstSign['created_at'])? $firstSign['created_at'] : '';
|
|
|
+ if(empty($firstSign) || empty($time)){
|
|
|
+ PRedis::set($cacheKey, ['first' => $firstSign,'user'=> $info], rand(10, 30));
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 签到后总消费
|
|
|
+ $totalCost = AccountLog::where(['user_id'=> $userId,'status'=> 2,'account_type'=> 1,'change_type'=>2])
|
|
|
+ ->where('created_at','>=', $time)
|
|
|
+ ->where('created_at','<', date('Y-m-01'))
|
|
|
+ ->sum('money');
|
|
|
+
|
|
|
+
|
|
|
+ $totalSign = AccountLog::where(['user_id'=> $userId,'type'=> 12,'status'=> 2])
|
|
|
+ ->where('created_at','>=', $time)
|
|
|
+ ->where('created_at','<', date('Y-m-01'))
|
|
|
+ ->sum('money');
|
|
|
+
|
|
|
+ // 清除还有未消费爱心
|
|
|
+ $redheart = isset($info['redheart'])? $info['redheart'] : 0;
|
|
|
+ $clearRedheart = ($totalSign-$totalCost);
|
|
|
+ PRedis::set($cacheKey, ['first' => $firstSign,'cost'=> $totalCost,'sign'=> $totalSign,'user'=> $info], 120);
|
|
|
+ if($clearRedheart>0 && $clearRedheart <= $redheart){
|
|
|
+ db()->startTrans();
|
|
|
+ if(!db()->name('user')->where(['id' => $userId])->setDec('redheart',$clearRedheart)){
|
|
|
+ db()->rollback();
|
|
|
+ return 2145;
|
|
|
+ }
|
|
|
+
|
|
|
+ $accountData = [
|
|
|
+ 'type' => 13,
|
|
|
+ 'account_type' => 1,
|
|
|
+ 'change_type' => 2,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'money' => $clearRedheart,
|
|
|
+ 'balance' => $redheart,
|
|
|
+ 'created_at' => date('Y-m-d H:i:s'),
|
|
|
+ 'remark' => "签到爱心过期扣除",
|
|
|
+ ];
|
|
|
+ PRedis::set("cache:signs:clearLog:{$userId}", ['log' => $accountData,'user'=> $info], 86400);
|
|
|
+ db('account_log')->insertGetId($accountData);
|
|
|
+
|
|
|
+ db()->commit();
|
|
|
+ PRedis::set($cacheKey, ['log' => $accountData,'first' => $firstSign,'cost'=> $totalCost,'sign'=> $totalSign,'user'=> $info], 7*24*3600);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|