async def invite_by(qq_id, invite_by): """ 更新用户的邀请者 :param qq_id: :param invite_by: :return: """ session = DBSession() user_invite = session.query(User_invite).filter( User_invite.qq_id == qq_id).first() if not user_invite: session.close() await init_user_invite(qq_id=qq_id, nickname=await get_qq_user_info(qq_id)) invite_by(qq_id, invite_by) else: if not user_invite.invite_by: user_invite.invite_by = invite_by else: raise RuntimeError session.merge(user_invite) session.commit() # 判断是否邀请到足够人数 invite_user_num = session.query(User_invite).filter( User_invite.invite_by == invite_by).count() if invite_user_num == 3: await invite.get_sell_card(invite_by) session.close() return True
async def add_QQUser(qq_id, nickname, group_id=None, group_card=None, sex=None, group_role=None, **kw): session = DBSession() qqUser = session.query(QQUser).filter(QQUser.qq_id == qq_id).first() if qqUser: qqUser.nickname = nickname if (group_id): qqUser.group_id = group_id qqUser.group_role = group_role qqUser.sex = sex session.merge(qqUser) session.commit() session.close() return 1 else: qqUser = QQUser(qq_id=qq_id, group_id=group_id, nickname=nickname, group_card=group_card, sex=sex, group_role=group_role, join_time=datetime.now()) session.add(qqUser) session.commit() session.close() return 0
async def add_homework_info(group_id, subject_name, content, end_date, assign_for=None, added_date=None, add_by=None): if added_date is None: added_date = date.today() homework = Homework(group_id=group_id, subject_name=subject_name, content=content, end_date=end_date, assign_for=assign_for, added_date=added_date, add_by=add_by) session = DBSession() session.add(homework) session.commit() homework = session.query(Homework).filter( Homework.group_id == group_id, Homework.subject_name == subject_name, Homework.content == content, Homework.assign_for == assign_for, Homework.added_date == added_date, Homework.add_by == add_by).first() session.close() return homework
async def add_group_info(group_id, group_name, last_weather_notify=None, is_morningcall_on=False, is_curriculumschedule_on=True, is_weather_notify_on=False, is_homework_daily_on=False): session = DBSession() group = session.query(GroupInfo).filter( GroupInfo.group_id == group_id).first() if group: group.group_id = group_id group.group_name = group_name session.merge(group) session.commit() session.close() return 1 else: group = GroupInfo(group_id=group_id, group_name=group_name, last_weather_notify=last_weather_notify, is_morningcall_on=is_morningcall_on, is_curriculumschedule_on=is_curriculumschedule_on, is_weather_notify_on=is_weather_notify_on, is_homework_daily_on=is_homework_daily_on) session.add(group) session.commit() session.close() return 0
async def add_item(item_name, item_info, seller_id, type='sell', from_group_id=None, add_time=None, **kw): if add_time is None: add_time = datetime.now() item = SellItem(item_name=item_name, item_info=item_info, seller_id=seller_id, from_group_id=from_group_id, add_time=add_time, type=type, is_onsell=True) session = DBSession() session.add(item) session.commit() item = session.query(SellItem).filter( SellItem.seller_id == seller_id, SellItem.item_name == item_name, SellItem.item_info == item_info, ).first() session.close() return item
async def del_item(id): session = DBSession() item = session.query(SellItem).filter(SellItem.id == id).first() item.is_onsell = not item.is_onsell session.merge(item) session.commit() session.close()
async def make_cp(user1, user2): session = DBSession() user1.is_paired = True user1.paired_to = user2.id user2.is_paired = True user2.paired_to = user1.id session.merge(user1) session.merge(user2) session.commit() session.close()
async def update_last_weather_notify(group_id, last_weather_notify): session = DBSession() try: group = session.query(GroupInfo).filter( GroupInfo.group_id == group_id).first() group.last_weather_notify = last_weather_notify session.merge(group) session.commit() except Exception: session.rollback() session.close()
async def update_item(id, item_name=None, item_info=None): session = DBSession() item = session.query(SellItem).filter(SellItem.id == id).first() if item_name: item.item_name = item_name if item_info: item.item_info = item_info item.add_time = datetime.now() session.merge(item) session.commit() session.close()
async def init_user_invite(qq_id, nickname): session = DBSession() user_invite = session.query(User_invite).filter( User_invite.qq_id == qq_id).first() if user_invite: session.close() return else: user_invite = User_invite(qq_id=qq_id, nickname=nickname, join_time=datetime.now()) session.add(user_invite) session.commit() session.close()
async def join_event(user_id, nickname, sex): session = DBSession() dbuser = session.query(CPuser).filter(CPuser.qq_id == user_id).first() if dbuser: session.close() return False cpuser = CPuser(qq_id=user_id, user_name=nickname, user_sex=sex, is_paired=False) session.add(cpuser) session.commit() session.close() session = DBSession() cpuser = session.query(CPuser).filter(CPuser.qq_id == user_id).first() session.close() return cpuser
async def del_homework(id): session = DBSession() session.query(Homework).filter(Homework.id == id).delete() session.commit() session.close()