is_set_date_changed_notice.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. from enum import Enum
  2. import time
  3. from datetime import datetime
  4. from typing import Optional
  5. from pydantic import BaseModel
  6. from fastapi import FastAPI, Query, UploadFile, File
  7. from fastapi import FastAPI
  8. from fastapi.middleware.cors import CORSMiddleware
  9. import json
  10. import urllib.request
  11. import dataset,json
  12. import dataset
  13. from datetime import datetime, timedelta
  14. from email.mime.text import MIMEText
  15. from email.mime.image import MIMEImage
  16. from email.mime.multipart import MIMEMultipart
  17. from email.mime.base import MIMEBase
  18. from email.mime.application import MIMEApplication
  19. import os
  20. import boto3
  21. from botocore.exceptions import ClientError
  22. app = FastAPI()
  23. origins = [
  24. "http://139.162.121.30",
  25. "http://139.162.121.30:8002",
  26. ]
  27. app.add_middleware(
  28. CORSMiddleware,
  29. # allow_origins=origins,
  30. allow_origins=["*"],
  31. allow_credentials=True,
  32. allow_methods=["*"],
  33. allow_headers=["*"],
  34. )
  35. # 取得資料庫連線
  36. def get_db_connection():
  37. # Choozmo DB
  38. # db = dataset.connect("mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4")
  39. # 測試機 DB
  40. db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57-cluster.cluster-cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/stage?charset=utf8mb4')
  41. # 正式機 DB
  42. # db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57-cluster.cluster-cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4')
  43. return db
  44. # 寄送執行項目預定日修改Email通知
  45. def hhh_send_mail(email, subject, html):
  46. SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
  47. RECIPIENT = email
  48. AWS_REGION = "us-east-1"
  49. CHARSET = "UTF-8"
  50. client = boto3.client('ses',region_name=AWS_REGION)
  51. try:
  52. msg = MIMEMultipart()
  53. msg["Subject"] = subject
  54. msg["From"] = "noreply@hhh.com.tw"
  55. msg["To"] = email
  56. # Set message body
  57. body = MIMEText(html, "html")
  58. msg.attach(body)
  59. response = client.send_raw_email(
  60. Source=msg["From"],
  61. Destinations=[msg["To"]],
  62. RawMessage={"Data": msg.as_string()}
  63. )
  64. print(response)
  65. # Display an error if something goes wrong.
  66. except ClientError as e:
  67. print(e.response['Error']['Message'])
  68. else:
  69. print("Email sent! Message ID:"),
  70. print(response['MessageId'])
  71. @app.get("/is_set_date_changed_notice")
  72. async def is_set_date_changed_notice(exd_id: str = '', exf_id: str = '', set_date: str = ''):
  73. print ('exd_id:' + exd_id + ', exf_id:' + exf_id + ', set_date:' + set_date) # test
  74. # 取得資料庫連線
  75. db = get_db_connection()
  76. subject = ""
  77. # 取得執行項目資料
  78. q = "SELECT * \
  79. FROM execute_detail \
  80. WHERE exd_id = " + exd_id
  81. q_result_count = len(list(db.query(q)))
  82. if q_result_count == 0: # 無合約資料
  83. return
  84. for r in db.query(q):
  85. print ('set_date = ' + set_date + ', r[set_date] = ' + r['set_date']) # test
  86. # 判斷執行項目預定日是否有修改
  87. if set_date == r['set_date']: # 執行項目預定日未修改
  88. print ('執行項目預定日未修改,不須通知')
  89. return
  90. else: # 執行項目預定日有修改
  91. print ('執行項目預定日有修改,須通知')
  92. subject = '[執行項目-預定日修改通知]' + datetime.now().strftime("%Y-%m-%d %H:%M")
  93. html = """
  94. <!DOCTYPE html>
  95. <head>
  96. <meta charset="UTF-8">
  97. </head>
  98. <body>
  99. """
  100. '''
  101. # 發送通知給負責業務、Kevin、Sam
  102. email_list = ['sam@hhh.com.tw','kevin.h@hhh.com.tw']
  103. # 負責業務
  104. print('負責業務: ' + r['sales_man'])
  105. email_list.append(r['sales_man'])
  106. '''
  107. '''
  108. # 列出該合約底下所有執行項目(含已完成、未完成)
  109. q1 = "SELECT * \
  110. FROM execute_detail \
  111. WHERE exf_id=" + exf_id + \
  112. " ORDER BY execute_man"
  113. q1_result_count = len(list(db.query(q1)))
  114. if q1_result_count > 0: # 有執行項目
  115. html += "<br><table style='width:100%;border-collapse: collapse;' border='1'> \
  116. <tr style='text-align:left;'> \
  117. <th style='width:5%;'>#</th> \
  118. <th>大項目</th> \
  119. <th>執行項</th> \
  120. <th>單位</th> \
  121. <th>是否完成</th> \
  122. <th>完成人</th> \
  123. <th>備註</th> \
  124. <th>預計排程日期</th> \
  125. <th>第一次提醒日</th> \
  126. </tr>";
  127. idx = 1
  128. for r1 in db.query(q1):
  129. if r1['set_date'] is None:
  130. r1['set_date'] = '無'
  131. if r1['alert_date_1'] is None:
  132. r1['alert_date_1'] = '無'
  133. # 執行單位Email
  134. if r1['execute_man'] == 'hhh_edit@hhh.com.tw':
  135. r1['execute_man'] = '編輯採訪部'
  136. elif r1['execute_man'] == 'hhh_mk@hhh.com.tw':
  137. r1['execute_man'] = '行銷企劃部'
  138. elif r1['execute_man'] == 'hhh_web@hhh.com.tw':
  139. r1['execute_man'] = '網站工程部'
  140. elif r1['execute_man'] == 'hhh_video@hhh.com.tw':
  141. r1['execute_man'] = '影音企劃部'
  142. elif r1['execute_man'] == 'agent@hhh.com.tw':
  143. r1['execute_man'] = '幸福經紀人'
  144. elif r1['execute_man'] == 'hhh_admin@hhh.com.tw':
  145. r1['execute_man'] = '行政部'
  146. elif r1['execute_man'] == 'hhh_sales@hhh.com.tw':
  147. r1['execute_man'] = '業務部'
  148. html += "<tr><td>" + str(idx) + "</td><td>" + r1['lv1'] + "</td><td>" + r1['lv2'] + "</td><td>" + r1['execute_man'] + "</td><td>" + r1['is_complete'] + "</td><td>" + r1['complete_man'] + "</td><td>" + r1['note'] + "</td><td>" + str(r1['set_date']) + "</td><td>" + str(r1['alert_date_1']) + "</td></tr>"
  149. idx += 1
  150. html += "</table>"
  151. html += "<div><br>幸福空間經營團隊敬上<br><br>※此信為系統自動寄送,請勿直接回信。謝謝!</div>"
  152. html += """
  153. </body>
  154. </html>
  155. """
  156. '''
  157. '''
  158. # Email清單
  159. email_list = ['mike@choozmo.com']
  160. # 寄送合約狀態修改Email通知
  161. for email in email_list:
  162. hhh_send_mail(email, subject, html)
  163. '''