from entity.ChannelEntity import Channel from common.Utils import getSession from sqlalchemy import update class ChannelService: # 新增一个channel def insertOneByValues(channelId, channelTitle, channelLanguage, region, channelReptileTime=None): session = getSession() channel = Channel(channelId=channelId, channelTitle=channelTitle, channelLanguage=channelLanguage, region=region) if channelReptileTime: channel.channelReptileTime = channelReptileTime session.add(channel) session.commit() session.close() def queryOneById(id): session = getSession() channel = session.query(Channel).filter(Channel.id == id).first() session.close() return channel def queryOneByChannelId(channelId): session = getSession() channel = session.query(Channel).filter( Channel.channelId == channelId).one_or_none() session.close() return channel def updtaByChannel(channelId, channelTitle=None, channelLanguage=None, channelReptileTime=None, region=None): session = getSession() update_channel = session.query(Channel).filter( Channel.channelId == channelId).first() if update_channel: if channelTitle: update_channel.channelTitle = channelTitle if channelLanguage: update_channel.channelLanguage = channelLanguage if channelReptileTime: update_channel.channelReptileTime = channelReptileTime if region: update_channel.region = region session.commit() session.close() def queryAllChannel(): session = getSession() Channels = session.query(Channel).all() session.close() return Channels def updateTimeByChannelId(channelId, videoPublishTime): session = getSession() updateSql = update(Channel).where( Channel.channelId == channelId).values(channelReptileTime=videoPublishTime) resutl = session.execute(updateSql) session.commit() session.close()