from typing import Optional from sqlalchemy.orm import Session from mooc.crud.crud_base import CRUDBase from mooc.models.goouc_fullexam_user import ( # 导入全部 FullExamUser, UserDoexam, UserDoOtherExamAnswer, UserExamAnswer, UserSpecial, UserSpequence, UserMember, UserCollectionPraction, UserGift, UserQhigh, UserQintensive, UserRead, UserDootherExam, UserWrongPraction, UserPool, ) from mooc.schemas.goouc_fullexam_user import ( # 导入全部create和update FullExamUserCreate, FullExamUserUpdate, UserDoexamCreate, UserDoexamUpdate, UserDoOtherExamAnswerCreate, UserDoOtherExamAnswerUpdate, UserExamAnswerCreate, UserExamAnswerUpdate, UserSpecialCreate, UserSpecialUpdate, UserSpequenceCreate, UserSpequenceUpdate, UserMemberCreate, UserMemberUpdate, UserCollectionPractionCreate, UserCollectionPractionUpdate, UserGiftCreate, UserGiftUpdate, UserQHighCreate, UserQHighUpdate, UserQIntensiveCreate, UserQIntensiveUpdate, UserReadCreate, UserReadUpdate, UserDoOtherExamCreate, UserDoOtherExamUpdate, UserWrongPractionCreate, UserWrongPractionUpdate, UserPoolCreate, UserPoolUpdate, ) class CRUDUserDoexam(CRUDBase[UserDoexam, UserDoexamCreate, UserDoexamUpdate]): def get_user_doexam(self, db: Session, user_doexam_id: int): return self.get_by_field(db, "id", user_doexam_id) class CRUDUserDoOtherExamAnswer( CRUDBase[UserDoOtherExamAnswer, UserDoOtherExamAnswerCreate, UserDoOtherExamAnswerUpdate]): def get_user_doother_exam_answer(self, db: Session, user_doother_exam_answer_id: int): return self.get_by_field(db, "id", user_doother_exam_answer_id) class CRUDUserExamAnswer(CRUDBase[UserExamAnswer, UserExamAnswerCreate, UserExamAnswerUpdate]): def get_user_exam_answer(self, db: Session, user_exam_answer_id: int): return self.get_by_field(db, "id", user_exam_answer_id) class CRUDUserSpecial(CRUDBase[UserSpecial, UserSpecialCreate, UserSpecialUpdate]): def get_user_special(self, db: Session, user_special_id: int): return self.get_by_field(db, "id", user_special_id) class CRUDUserSpequence(CRUDBase[UserSpequence, UserSpequenceCreate, UserSpequenceUpdate]): def get_user_spequence(self, db: Session, user_spequence_id: int): return self.get_by_field(db, "id", user_spequence_id) class CRUDUserMember(CRUDBase[UserMember, UserMemberCreate, UserMemberUpdate]): def get_user_member(self, db: Session, user_member_id: int): return self.get_by_field(db, "id", user_member_id) def get_by_openid_and_weid(self, db: Session, openid: str, weid: int, istatus: int = 1): """通过openid和weid获取用户""" return db.query(self.model).filter( self.model.openid == openid, self.model.weid == weid, self.model.istatus == istatus ).first() class CRUDUserCollectionPraction( CRUDBase[UserCollectionPraction, UserCollectionPractionCreate, UserCollectionPractionUpdate]): def get_user_collection_praction(self, db: Session, user_collection_praction_id: int): return self.get_by_field(db, "id", user_collection_praction_id) def get_user_collection_praction_1( self, session: Session, uniacid: int, uid: int, testid: int ) -> dict: result = session.query( self.model ).filter( self.model.weid == uniacid, self.model.istatus == 1, self.model.uid == uid, self.model.testid == testid ).first() return result.__dict__ if result else {} def delete_user_collection_praction( self, session: Session, uniacid: int, uid: int, testid: int ) -> int: try: # 执行删除操作 affected_rows = session.query( self.model ).filter( self.model.weid == uniacid, self.model.uid == uid, self.model.testid == testid ).delete() session.commit() return affected_rows except Exception as e: session.rollback() print(f"删除失败: {str(e)}") return 0 class CRUDUserGift(CRUDBase[UserGift, UserGiftCreate, UserGiftUpdate]): def get_user_gift(self, db: Session, user_gift_id: int): return self.get_by_field(db, "id", user_gift_id) class CRUDUserQHigh(CRUDBase[UserQhigh, UserQHighCreate, UserQHighUpdate]): def get_user_qhigh(self, db: Session, user_qhigh_id: int): return self.get_by_field(db, "id", user_qhigh_id) class CRUDUserQIntensive(CRUDBase[UserQintensive, UserQIntensiveCreate, UserQIntensiveUpdate]): def get_user_qintensive(self, db: Session, user_qintensive_id: int): return self.get_by_field(db, "id", user_qintensive_id) class CRUDUserRead(CRUDBase[UserRead, UserReadCreate, UserReadUpdate]): def get_user_read(self, db: Session, user_read_id: int): return self.get_by_field(db, "id", user_read_id) class CRUDUserDoOtherExam(CRUDBase[UserDootherExam, UserDoOtherExamCreate, UserDoOtherExamUpdate]): def get_user_doother_exam(self, db: Session, user_doother_exam_id: int): return self.get_by_field(db, "id", user_doother_exam_id) class CRUDUserWrongPraction(CRUDBase[UserWrongPraction, UserWrongPractionCreate, UserWrongPractionUpdate]): def get_user_wrong_praction(self, db: Session, user_wrong_praction_id: int): return self.get_by_field(db, "id", user_wrong_praction_id) class CRUDUserPool(CRUDBase[UserPool, UserPoolCreate, UserPoolUpdate]): def get_user_pool(self, db: Session, user_pool_id: int): return self.get_by_field(db, "id", user_pool_id) class CRUDFullExamUser(CRUDBase[FullExamUser, FullExamUserCreate, FullExamUserUpdate]): def get_full_exam_user(self, db: Session, full_exam_user_id: int): return self.get_by_field(db, "id", full_exam_user_id) def get_user_info(self, session: Session, uniacid: int, uid: int): # 构建查询并获取第一条结果 result = session.query( self.model.id, self.model.integral, self.model.status ).filter( self.model.weid == uniacid, self.model.id == uid ).first() # 转换为字典格式 fields = ["id", "integral", "status"] if result is not None: return dict(zip(fields, result)) return {} def update_user_info( self, session: Session, update_data: dict, user_id: int ) -> int: try: # 执行更新操作 affected_rows = session.query(self.model).filter( self.model.id == user_id ).update(update_data) session.commit() return affected_rows except Exception as e: session.rollback() print(f"更新失败: {str(e)}") return 0 def get_by_openid_and_weid(self, db: Session, openid: str, weid: int, istatus: int = 1): """通过openid和weid获取用户""" return db.query(self.model).filter( self.model.openid == openid, self.model.weid == weid, self.model.istatus == istatus ).first() def get_by_id_and_weid(self, db: Session, id: int, weid: int, istatus: int = 1): """通过id和weid获取用户""" return db.query(self.model).filter( self.model.id == id, self.model.weid == weid, self.model.istatus == istatus ).first() # 创建实例 user_doexam = CRUDUserDoexam(UserDoexam) user_doother_exam_answer = CRUDUserDoOtherExamAnswer(UserDoOtherExamAnswer) user_exam_answer = CRUDUserExamAnswer(UserExamAnswer) user_special = CRUDUserSpecial(UserSpecial) user_spequence = CRUDUserSpequence(UserSpequence) user_member = CRUDUserMember(UserMember) user_collection_praction = CRUDUserCollectionPraction(UserCollectionPraction) user_gift = CRUDUserGift(UserGift) user_qhigh = CRUDUserQHigh(UserQhigh) user_qintensive = CRUDUserQIntensive(UserQintensive) user_read = CRUDUserRead(UserRead) user_doother_exam = CRUDUserDoOtherExam(UserDootherExam) user_wrong_praction = CRUDUserWrongPraction(UserWrongPraction) user_pool = CRUDUserPool(UserPool) full_exam_user = CRUDFullExamUser(FullExamUser)