233 lines
8.1 KiB
PHP
233 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace app\api\model;
|
|
|
|
use app\common\library\DatabaseRoute;
|
|
use ba\Random;
|
|
use think\facade\Db;
|
|
use think\Model;
|
|
|
|
class UserSignRecord extends Model
|
|
{
|
|
|
|
|
|
public static function month($month = '-30 days')
|
|
{
|
|
return date('Y-m-d 00:00:00', strtotime($month));
|
|
}
|
|
|
|
|
|
public static function getTaskWCount($user_id, $wCount)
|
|
{
|
|
$user_sign_record_db = DatabaseRoute::getDb('user_sign_record', $user_id);
|
|
$day = self::month();
|
|
$noRecordTasks = $user_sign_record_db->where(['day' => 7])->where('create_time', '>', $day)->order('create_time', 'acs')
|
|
->field('id')
|
|
->select()
|
|
->toArray();
|
|
$taskWCount = Db::connect(DatabaseRoute::getConnection('user_sign_record', ['user_id' => $user_id]));
|
|
$taskWCount =
|
|
$taskWCount
|
|
->name('user_sign_record')
|
|
->alias('sign')
|
|
->field([
|
|
'sign.id as id',
|
|
"{$wCount} - COUNT(CASE WHEN spRecord.source_id IS NOT NULL THEN 1 END) AS `day`"
|
|
])
|
|
->leftJoin('disc_spinning_record spRecord', 'sign.id = spRecord.source_id')
|
|
->where('sign.day', 7)
|
|
->where('sign.user_id', $user_id)
|
|
->where('sign.create_time', '>', $day)
|
|
->whereIn('sign.id', $noRecordTasks)
|
|
->group('sign.id')
|
|
->order('sign.create_time', 'asc')
|
|
->select();
|
|
if($taskWCount) {
|
|
$days = 0;
|
|
$task_reward = [];
|
|
foreach ($taskWCount as $tk => $tv) {
|
|
$task_reward[][$tv['id']] = $days += $tv['day'];
|
|
}
|
|
return $task_reward;
|
|
}
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
public static function getWSignCount($user_id)
|
|
{
|
|
$user_sign_record_db = DatabaseRoute::getDb('user_sign_record', $user_id);
|
|
$noRecordTasks = $user_sign_record_db->where('create_time', '>', date('Y-m-d 00:00:00', strtotime('-1 day')))->order('create_time', 'desc')
|
|
->field('day')
|
|
->find();
|
|
return $noRecordTasks?$noRecordTasks['day']:null;
|
|
}
|
|
|
|
public static function getUserSignCount($user_id)
|
|
{
|
|
$user_sign_record_db = DatabaseRoute::getDb('user_sign_record', $user_id);
|
|
$noRecordTasks = $user_sign_record_db->where('sign_day', '>', date('Y-m') . '-00')->order('create_time', 'asc')
|
|
->count();
|
|
return $noRecordTasks;
|
|
}
|
|
|
|
public static function getTaskCenterRecordCount($user_id, $task_id, $day_date)
|
|
{
|
|
$data = DatabaseRoute::getDb('task_center_record', $user_id)->where(['task_id' => $task_id]);
|
|
if($day_date) {
|
|
$data = $data->where('create_time', '>', $day_date);
|
|
}
|
|
return $data->count();
|
|
}
|
|
public static function getTaskCenterRecordCountUserIdAll($user_id_s, $task_id):string|int
|
|
{
|
|
$count = 0;
|
|
foreach ($user_id_s as $k => $user_id) {
|
|
$count += DatabaseRoute::getDb('task_center_record', $user_id)->where(['task_id' => $task_id])->count();
|
|
}
|
|
return $count;
|
|
}
|
|
|
|
public static function getUserSignData($user_id, $user)
|
|
{
|
|
$dto = [
|
|
'userId' => $user_id,
|
|
'username' => $user['user_name'],
|
|
'mobile' => $user['phone'],
|
|
'signDays' => 0,
|
|
'enable' => 1,
|
|
'isReceived' => 0,
|
|
];
|
|
$config = CommonInfo::where(['type' => 918])->find()->value;
|
|
if(!$config) {
|
|
return returnErrorData('签到活动配置不存在');
|
|
}
|
|
// 活动天数
|
|
$parts = explode(',', $config);
|
|
$activeDays = isset($parts[0]) ? (int)$parts[0] : 0;
|
|
// 当前日期
|
|
$beginDay = date('Y-m-d');
|
|
// 签到记录
|
|
$recordList = [];
|
|
// 连续签到日期
|
|
$flowDays = buildFlowDays($beginDay, $activeDays);
|
|
$list = DatabaseRoute::getDb('user_sign_record', $user_id)->order('sign_day', 'asc')->order('create_time', 'asc')->order('id', 'asc')->select()->toArray();
|
|
// 第x天
|
|
$index = 1;
|
|
// 连续签到天数
|
|
$signDays = 0;
|
|
if(!$list) {
|
|
foreach ($flowDays as $k => $day) {
|
|
$recordList[] = [
|
|
'showText' => '第' . $index . '天',
|
|
'status' => 0,
|
|
'signDay' => $day,
|
|
'signDate' => '',
|
|
];
|
|
$index ++;
|
|
}
|
|
$dto['recordList'] = $recordList;
|
|
return returnSuccessData($dto);
|
|
}
|
|
$beginSignDay = $list[0]['sign_day'];
|
|
$enDay = date('Y-m-d', strtotime('+' . $activeDays - 1 . 'days', strtotime(date('Y-m-d'))));
|
|
|
|
$flowDays = buildFlowDaysTwo($beginSignDay, $enDay);
|
|
// 需要移除的记录
|
|
$removeList = [];
|
|
$signMap = [];
|
|
foreach ($list as $record) {
|
|
$signMap[$record['sign_day']] = $record['create_time'];
|
|
}
|
|
|
|
foreach ($flowDays as $k => $day) {
|
|
$date = !empty($signMap[$day])?$signMap[$day]:'';
|
|
if($date) {
|
|
$recordList[$k] = [
|
|
'signDay' => $day,
|
|
'status' => 1,
|
|
'signDate' => $date,
|
|
'showText' => '已签到',
|
|
];
|
|
$signDays ++;
|
|
}else {
|
|
$recordList[$k] = [
|
|
'signDay' => $day,
|
|
'status' => 0,
|
|
'signDate' => '',
|
|
];
|
|
$daysBetween = daysBetween($day);
|
|
if($daysBetween > 0) {
|
|
$signDays = 0;
|
|
$recordList[$k]['showText'] = '未签到';
|
|
}elseif($daysBetween == 0) {
|
|
$recordList[$k]['showText'] = '待签到';
|
|
}else {
|
|
$recordList[$k]['showText'] = '第' . $k + 1 .'天';
|
|
}
|
|
}
|
|
if($signDays == $activeDays) {
|
|
break;
|
|
}
|
|
|
|
// 1. 检查记录日期是否等于结束日期
|
|
|
|
if ($day === $enDay) {
|
|
// 2. 反转记录列表
|
|
$tempList = array_reverse($recordList);
|
|
// 3. 计算今天且状态为"1"的记录数
|
|
$today = date('Y-m-d');
|
|
$isInclude = 0;
|
|
|
|
foreach ($tempList as $item) {
|
|
if ($item['signDay'] === $today && $item['status'] == 1) {
|
|
$isInclude++;
|
|
}
|
|
}
|
|
// 4. 确定要移除的记录数量
|
|
$removeSize = $signDays;
|
|
if ($isInclude > 0) {
|
|
$removeSize = $signDays - 1;
|
|
}
|
|
// 5. 添加要移除的记录到 removeList
|
|
for ($i = 0; $i < $removeSize; $i++) {
|
|
if (isset($tempList[$i])) { // 确保索引存在
|
|
$removeList[] = $tempList[$i];
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
// 1. 移除元素
|
|
$recordList = array_udiff($recordList, $removeList, function($a, $b) {
|
|
return ($a === $b) ? 0 : 1;
|
|
});
|
|
// 2. 第一次反转
|
|
$recordList = array_reverse($recordList);
|
|
// 3. 截取前 activeDays 条记录
|
|
$recordList = array_slice($recordList, 0, $activeDays, true);
|
|
// 4. 第二次反转
|
|
$recordList = array_reverse($recordList);
|
|
$index = 1;
|
|
foreach ($recordList as $k => $v) {
|
|
$recordList[$k]['showText'] = sprintf($v['showText'], '第' . $index . '天');
|
|
$index ++;
|
|
}
|
|
$dto['recordList'] = $recordList;
|
|
$dto['signDays'] = $signDays;
|
|
if($signDays >= $activeDays) {
|
|
$dto['enable'] = 0;
|
|
}
|
|
|
|
$count = DatabaseRoute::getDb('user_money_details', $user_id)->where('type', 1)
|
|
->where('classify', 7)
|
|
->where('money_type', 1)
|
|
->where('title', 'like', '[连续签到%')
|
|
->where('title', 'like', '%天]')
|
|
->count();
|
|
$dto['isReceived'] = $count > 0 ? 1 : 0;
|
|
return returnSuccessData($dto);
|
|
}
|
|
|
|
} |