is_close_changed_notice.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 pandas as pd
  13. import dataset
  14. from datetime import datetime, timedelta
  15. from email.mime.text import MIMEText
  16. from email.mime.image import MIMEImage
  17. from email.mime.multipart import MIMEMultipart
  18. from email.mime.base import MIMEBase
  19. from email.mime.application import MIMEApplication
  20. import os
  21. import boto3
  22. from botocore.exceptions import ClientError
  23. app = FastAPI()
  24. origins = [
  25. "http://139.162.121.30",
  26. "http://139.162.121.30:8002",
  27. ]
  28. app.add_middleware(
  29. CORSMiddleware,
  30. # allow_origins=origins,
  31. allow_origins=["*"],
  32. allow_credentials=True,
  33. allow_methods=["*"],
  34. allow_headers=["*"],
  35. )
  36. # 寄送逾期執行表單Email通知
  37. def hhh_send_mail(email, subject, html):
  38. SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
  39. RECIPIENT = email
  40. AWS_REGION = "us-east-1"
  41. CHARSET = "UTF-8"
  42. client = boto3.client('ses',region_name=AWS_REGION)
  43. try:
  44. msg = MIMEMultipart()
  45. # msg["Subject"] = "逾期執行表單通知_" + str(datetime.date.today())
  46. msg["Subject"] = subject
  47. msg["From"] = "noreply@hhh.com.tw"
  48. msg["To"] = email
  49. # Set message body
  50. body = MIMEText(html, "html")
  51. msg.attach(body)
  52. response = client.send_raw_email(
  53. Source=msg["From"],
  54. Destinations=[msg["To"]],
  55. RawMessage={"Data": msg.as_string()}
  56. )
  57. print(response)
  58. # Display an error if something goes wrong.
  59. except ClientError as e:
  60. print(e.response['Error']['Message'])
  61. else:
  62. print("Email sent! Message ID:"),
  63. print(response['MessageId'])
  64. # Choozmo DB
  65. # db = dataset.connect("mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4")
  66. # 測試機 DB
  67. db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57-cluster.cluster-cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/stage?charset=utf8mb4')
  68. # 正式機 DB
  69. # db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57-cluster.cluster-cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4')
  70. # class Execute(BaseModel):
  71. # exf_id: str
  72. # num: str # 合約編號
  73. # company: str #設計公司
  74. # mobile: str # 手機
  75. # telete: str # 電話
  76. # designer: str #設計師
  77. # sdate: str # 上架日期
  78. # edate: str # 下架日期
  79. # note: str # 備註說明
  80. # contract_time: str # 到期日
  81. # contract_person: str # 聯絡人
  82. # sales_dept: str # 部門別
  83. # sales_man: str # 接案業務
  84. # is_close: str # 是否結案 (N: 未結案,Y: 已結案,T: 未上線,D: 續約,E: 不續約延期,F: 不續約需下線,G: 已到期未續約)
  85. # price: str
  86. @app.get("/is_close_changed_notice")
  87. async def is_close_changed_notice(exf_id: str = '', is_close: str = ''):
  88. subject = ""
  89. # 取得合約資料
  90. q = "SELECT * \
  91. FROM execute_form \
  92. WHERE exf_id = " + exf_id
  93. q_result_count = len(list(db.query(q)))
  94. if q_result_count == 0: # 無合約資料
  95. return
  96. for r in db.query(q):
  97. subject = '[合約狀態修改]:' + r['company'] + ' (合約編號:' + r['num'] + ')_' + datetime.now().strftime("%Y-%m-%d %H:%M")
  98. html = """
  99. <!DOCTYPE html>
  100. <head>
  101. <meta charset="UTF-8">
  102. </head>
  103. <body>
  104. """
  105. if is_close == 'D':
  106. html += '<div>本合約狀態已修改為 <span style="color:red;">續約</span></div>';
  107. elif is_close == 'E':
  108. html += '<div>本合約狀態已修改為 <span style="color:red;">不續約延期</span></div>';
  109. elif is_close == 'F':
  110. html += '<div>本合約狀態已修改為 <span style="color:red;">不續約需下線</span></div>';
  111. elif is_close == 'G':
  112. html += '<div>本合約狀態已修改為 <span style="color:red;">已到期未續約</span></div>';
  113. html += """
  114. </body>
  115. </html>
  116. """
  117. # Email清單
  118. email_list = ['mike@choozmo.com']
  119. # 寄送逾期執行表單Email通知
  120. for email in email_list:
  121. hhh_send_mail(email, subject, html)