[day19] 优惠券检查

今天结膜炎,看完医生整个白天都躺在床上眼睛痒得要死动不了,晚餐後好一点,写一点昨天缺漏的小东西

讲个优惠券设定的小故事,疫情升3级後被赶到城市另一边的大楼上班,那边有很便宜的果汁,还有不少老店,大概是疫情加上数位转型的迫切吧,有些老店纷纷导入数位会员制与线上订餐,一整个现代了起来,但不知道是不是卖给它们这套系统的Oxxard业务没有教好它们要怎麽用,於是发生一些良心被谴责但又觉得好爽的事情

新会员见面礼很常见吧,通常都是一些不痛不痒的东西,什麽9折阿,送小菜之类的,但不知道这家老店是发了什麽疯,直接送等值一百元的抵用券,哇爽爆了,要知道一个今日便当才99元,而且该店也算是当地的名店,便当不错吃,店员热情的招呼我们说加会员送100元,本来还想说一定会有什麽限制吧,结果真的是送一百元,不仅不用加钱超过面额,也没有任何附加条件,如果你买99元的今日便当,等於免费送便当,好,好喔,更奇妙的还是後头,这个新会员见面礼居然是一个月送一张的,握草,一个月送你一百元的店家,吃爆,吃了几个月店员都认出来了,但每个月都用新会员见面礼,店员真的不觉得奇怪吗.....

检查是否已经发给过优惠券

加入一个新函数QUY_CPN(self, id, cptype),查询该类型的优惠券列表

def QUY_CPN(self, id, cptype):
    cur = self.conn.cursor()
    query = sql.SQL("SELECT * from coupon WHERE %s = ANY (userids) and type = %s").format(sql.Identifier('coupon'))
    cur.execute(query, (id, cptype))
    r = cur.fetchall()
    cur.close()
    return r

修改FollowEventHandler

def timedelta_bydays(self, days=7):
    s_time = datetime.now().isoformat()
    e_time = (datetime.now() + timedelta(days=days)).isoformat()
    return s_time, e_time

def gencode(self, mode=0):
    import string, random
    if(mode == 0):
        return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(8))

def INS_CPN(self, id, cptype):
    if(cptype == "new" or cptype == "back"):
        code = self.gencode()
        s_time, e_time = self.timedelta_bydays(days=7)
        cur = self.conn.cursor()
        id = '{' + id + '}'
        query = sql.SQL("INSERT INTO {}(type, code, s_time, e_time, times, userids) VALUES (%s, %s, %s, %s, %s, %s);").format(sql.Identifier('coupon'))
        cur.execute(query, (cptype, code, s_time, e_time, str(1), id))
        self.conn.commit()
        cur.close()
        return 1, code
    else:
        print()
        return 0, "placeholder Due no coupon"

@handler.add(FollowEvent)
def handle_follow(event):
  prof = line_bot_api.get_profile(event.source.user_id)
  r = dbpm.INS_UPD_cus(prof)
  if r == 1:
      msg = "Hello 欢迎铁人赛的勇者"
      cpl = dbpm.QUY_CPN(event.source.user_id, "new")
      if(cpl and len(cpl) > 0):
          app.logger.debug(f"已经发过优惠券:{cpl}")
      else:
          cr, code = dbpm.INS_CPN(event.source.user_id, "new")
          if(cr == 1):
              msg = msg + f"\n这是您的好友见面礼:{code}"
  else:
      msg = "Hello 欢迎铁人赛的勇者回来"
      cpl = dbpm.QUY_CPN(event.source.user_id, "back")
      if(cpl and len(cpl) > 0):
          app.logger.debug(f"已经发过优惠券:{cpl}")
      else:
          cr, code = dbpm.INS_CPN(event.source.user_id, "back")
          if(cr == 1):
              msg = msg + f"\n这是您的回归小礼物:{code}"
  
  line_bot_api.reply_message(
      event.reply_token,
      TextSendMessage(text=msg))

https://ithelp.ithome.com.tw/upload/images/20211002/20140853jMVpvdfUOY.png

等眼睛好一点,赶快写完订单页面.....


<<:  16 - Logs - 挖掘系统内部发生的状况 (4/4) - 透过 Filebeat 收集 Infrastructure 中各种服务的细节资讯

>>:  Day16 - this&Object Prototypes Ch3 Objects - Iteration 开头

那些被忽略但很好用的 Web API / DesignMode

DesignMode 让整个网站都是你的 textarea。 今天要介绍的 API 非常简单明了,...

linkedin-skill-assessments Java 检定 static import

前言 在更新Linkedkin 个人档案的时候 偶然发现他有技术检定测验 如果总成绩在前30%,会发...

用React刻自己的投资Dashboard Day16 - react-router-dom让SPA也有路由

tags: 2021铁人赛 React 前一篇提到的导览列的各个按钮,点击之後会跳到不同的页面,每个...

分布式可观测性 Tracing、Time Series Data、LSM浅谈

上篇回顾 分布式可观测性 Logging 浅谈 分布式可观测性 Structured Log 分布式...

Day 7 阿里云架设网站-服务器架设与自动化运维

云服务器(Elastic Compute Service,ECS) ECS是阿里云上提供服务器租用的...