# coding=utf-8 import click from base import init_env init_env(interactive = True) from apps.web.agent.models import Agent from apps.web.dealer.models import Dealer, VirtualCard @click.group() def cli(): click.echo(u"本脚本用于修改已经发行的虚拟卡卷 可续卡时间") def get_card_type_by_agent(agentId): agent = Agent.objects.get(id=agentId) if not agent: return dealerIds = [str(dealer.id) for dealer in Dealer.objects.filter(agentId=agentId).only("id", "agentId")] return VirtualCard.objects.filter(dealerId__in=dealerIds) def get_card_type_by_dealer(dealerId): return VirtualCard.objects.filter(dealerId=dealerId) def get_card_type_by_card(cardId): return VirtualCard.objects.filter(id=cardId) @cli.command() @click.option('--agent', prompt=u'代理商ID:', default="") @click.option('--dealer', prompt=u'经销商ID:', default="") @click.option('--card', prompt=u'发行的卡ID:', default="") @click.option('--nax', prompt=u'过期后可续费天数:') @click.option('--nin', prompt=u'过期前可续费天数:') def charge_need_renew_time(agent, dealer, card, nax, nin): if agent: cardTypes = get_card_type_by_agent(agent) elif dealer: cardTypes = get_card_type_by_dealer(dealer) elif card: cardTypes = get_card_type_by_card(card) else: click.secho(u"无任何输入信息", blink=True, bold=True, bg='red') return if not cardTypes: click.secho(u"未查询到有效卡", blink=True, bold=True, bg='red') return count = cardTypes.count() for card in cardTypes: card.needRenewMax = int(nax) card.needRenewMin = int(nin) card.save() click.secho(u"共修改{}张".format(count), blink=True, bold=True, bg='red') if __name__ == '__main__': charge_need_renew_time()