在日常工作中,需要对数据进行对比。一般来说,都是保存在数据库中,所以需要对sql语句非常熟练,常常一条命令都是几十行,表和表之间的关系是非常明显的。在得到异常数据后,需要告知特定的人群。在这里,我决定采用企业微信,为什么不用邮箱来群发呢?原因有很多,首先是安全问题,一般用程序发的邮件会被当做垃圾邮件;其次是人群中对邮箱不是经常看所以不是很方便。
一般来说,很多公司也会有企业微信端的,企业微信的消息可以到微信中更是方便了,同时企业微信也有相应的接口说明,很是方便。下面我将从企业微信创建应用,编写群发消息程序,最后实现在web中的应用。
同样的,我这里还是使用django框架下的web应用,视图就展示了查询列表,对比内容为了方便直接了网页跳转。
对比数据并显示视图
对比数据我直接采用了数据库比对:
models.py:
from django.db import models
class test_excel_info(models.Model):
name = models.CharField('名称',max_length=20)
pcnum = models.CharField('电脑编号',max_length=30)
cause = models.CharField('理由',max_length=30)
class oldinfo(models.Model):
name=models.CharField('name',max_length=20)
pcnum=models.CharField('pcnum',max_length=30)
views.py
这里显示了对应的视图,创建了查询,删除和对比数据。
def info_show(request):
info = models.test_excel_info.objects.all()
return render(request,'info_list.html',{'info_list':info})
def drop_info(request):
drop_id =request.GET.get('id')
drop_obj = models.test_excel_info.objects.get(id=drop_id)
drop_obj.delete()
return redirect('/info/')
def compare_sql(request):
cursor = connection.cursor()
cursor.execute("insert into TestDjan_oldinfo(name,pcnum) values ('kaik','pc112')")
return redirect('/info/')
有很多人问我,这里views.py是后端操作,那么如何实现前端交互呢?这里以info_show为例:
可以看到我这里前后端处理得还算好,对于后端的人只要知道接口特性就行,所以如果是一个良好的前后端分离项目,为了系统的维护性,必须让后端开发者写接口文档,否则会比较难受了!
前端的关于操作显示的代码:
views.py中的字典对应关系。
运行结果图:这样编辑和删除也都是页面操作(views.py)了。
企业微信创建
首先在自己手机下载企业微信,为了进行测试,自己先创建一个企业。登录企业微信平台:企业微信
在应用管理创建一个应用。同时记住这里的id和secret,保存下来。
在我的企业可以企业的id,同样需要。
下面就可以编写程序,python调用企业微信接口实现群发信息:接口文档
import requests, sys
class SendWeiXinWork():
def __init__(self):
self.CORP_ID = "ww" # 企业号的标识
self.SECRET = "" # 管理组凭证密钥
self.AGENT_ID = '1000002' # 应用ID
self.token = self.get_token()
def get_token(self):
url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
data = {
"corpid": self.CORP_ID,
"corpsecret": self.SECRET
}
req = requests.get(url=url, params=data)
res = req.json()
if res['errmsg'] == 'ok':
return res["access_token"]
else:
return res
def send_message(self, to_user, content):
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % self.token
data = {
#"touser": to_user, # 发送个人就填用户账号
"toparty": 1, # 发送组内成员就填部门ID
"msgtype": "text",
"agentid": self.AGENT_ID,
"text": {"content": content},
"safe": "0"
}
req = requests.post(url=url, json=data)
res = req.json()
if res['errmsg'] == 'ok':
print("send message succeed")
return "send message succeed"
else:
print("send message error", res)
return res
if __name__ == '__main__':
SendWeiXinWork = SendWeiXinWork()
SendWeiXinWork.send_message("2", "test for wechat")
可能会出现某些错误:如果显示代理问题,请关闭代理软件;如果ip是问题,请将自己的ip设置为白名单:选择自己创建的应用。注意不要看自己的ipconfig,因为这是内网ip,使用了NAT转换了,所以你可以先跑一下程序,他会报错会告诉你的ip内容。
群发消息
views.py
def send_sql(request):
cursor = connection.cursor()
cursor.execute("select a.name from TestDjan_test_excel_info a inner join TestDjan_oldinfo b on a.name=b.name and a.pcnum !=b.pcnum")
myre=cursor.fetchall()
for i in myre:
name = i[0]
SendWeiXinWork = testwechat.SendWeiXinWork()
SendWeiXinWork.send_message("2","有错误的信息:"+name)
return redirect('/info/')
urls.py
path('info/',views.info_show),
path('drop_info/',views.drop_info),
path('compare_sql/',views.compare_sql),
path('send_sql',views.send_sql),
为了方便我直接使用了跳转网页的形式来进行查询对比数据与发送,当然了在对比那块可以实现视图的展示。
这是用户收到的信息:
这是我的数据库测试表内容:
需要对比的表
关键的sql语句:
select a.name from TestDjan_test_excel_info a inner join TestDjan_oldinfo b on a.name=b.name and a.pcnum !=b.pcnum
然后部门的人便可以接到以下的内容了: