import { EErrorCode } from '../Config/_error_'; import { MongoBaseModel } from 'cgserver'; import { MongoBaseService } from 'cgserver'; export class CheckInModel extends MongoBaseModel { user_id:number=-1 club_id:number=-1 month_info:number=0//按位标识 create_time:number=0 } export let GCheckInSer:CheckInService=null class CheckInService extends MongoBaseService { constructor() { super("check_in",CheckInModel) this._tryClearAll() } //setTimeout 的延时时间最多是32位整数的最大值, //而一个月的毫秒数会超过这个数字,导致setTimeout被立即执行,暂时没有解决方案,所以使用的超过一天就一天检测一次 protected _tryClearAll() { let one_day = 24*60*60*1000 let day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] let today = new Date() //获得当前日期 if (this._checkLeap(today.getFullYear())) { day[1]=29 } let day_time = (today.getDate()-1)*one_day let h = today.getHours() let h_time = h*60*60*1000 let m = today.getMinutes() let m_time = m*60*1000 let s = today.getSeconds() let s_time = s*1000 let cur_time = day_time+h_time+m_time+s_time+today.getMilliseconds() let total_time = day[today.getMonth()]*one_day let left_time = total_time-cur_time if(left_time>one_day) { setTimeout(() => { this._tryClearAll() }, one_day)//一天后再试 return } setTimeout(() => { this._clearAll() }, left_time) } protected _clearAll() { let one_day = 24*60*60*1000 this.updateOne({month_info:0},{$gt:{user_id:0}}) let day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] let today = new Date() //获得当前日期 if (this._checkLeap(today.getFullYear())) { day[1] = 29 } let nextMonth = new Date().getMonth()+1 if(nextMonth>11) { nextMonth = 1 } setTimeout(() => { this._tryClearAll() }, one_day) } protected _checkLeap(value) { value = value + 0 if ((value % 4 === 0 && value % 100 !== 0) || value % 400 === 0) { return true } else { return false } } async getByUserId(user_id,club_id=-1) { club_id=club_id||-1 let cm:CheckInModel = null cm = await this.get(null,{user_id:user_id,club_id:club_id}) if(!cm) { return cm } let time = new Date() //每月1号需要检测当天的是否有效 if(time.getDate()==1) { let ct = new Date(cm.create_time) if(ct.getFullYear()!=time.getFullYear() ||ct.getMonth()!=time.getMonth() ||ct.getDate()!=time.getDate()) { cm.month_info = 0 } else { cm.month_info = 0x1 } } return cm } async check(user_id,club_id=-1) { club_id=club_id||-1 let rs = {cm:null,errcode:null} let cm = await this.getByUserId(user_id,club_id) if(!cm) { let add_rs = await this.addOne(user_id,club_id) if(add_rs.errcode) { return add_rs } cm = add_rs.cm } let time = new Date() let d = time.getDate() if((cm.month_info&(1<<(d-1)))>0) { rs.errcode = EErrorCode.Has_Checked return rs } cm.month_info|=(1<<(d-1)) cm.create_time = time.getTime() let sr = await this.updateOne(cm,{user_id:user_id,club_id:club_id}) if(sr.errcode||sr.rs.modifiedCount==0) { rs.errcode = EErrorCode.Mysql_Error return rs } rs.cm=cm return rs } async addOne(user_id,club_id:number) { club_id=club_id||-1 let rs={cm:null,errcode:null} let cm = new CheckInModel() cm.user_id = user_id cm.club_id = club_id let sr = await this.insert(cm) if(sr.errcode) { rs.errcode = sr.errcode return rs } cm._id=sr.rs.insertedId rs.cm = cm return rs } } GCheckInSer=new CheckInService()