# -*- coding: utf-8 -*- import logging from sqlalchemy import text from website import errors from website import settings from website import consts from website import db from website.util import shortuuid, aes from website.service import enterprise from website.handler import APIHandler, authenticated class EntityIndexHandler(APIHandler): """首页""" @authenticated def post(self): pageNo = self.get_int_argument("pageNo", 1) pageSize = self.get_int_argument("pageSize", 10) name = self.tostr(self.get_escaped_argument("name", "")) with self.app_mysql.connect() as conn: sql_text = "select id, name, industry, logo, create_time from enterprise where 1=1 " param = {} count_sql_text = "select count(id) from enterprise " count_param = {} if name: sql_text += "and name like :name" param["name"] = "%{}%".format(name) count_sql_text += "and name like :name" count_param["name"] = "%{}%".format(name) sql_text += " and del=0" count_sql_text += " and del=0" sql_text += " order by id desc limit :pageSize offset :offset" param["pageSize"] = pageSize param["offset"] = (pageNo - 1) * pageSize cur = conn.execute(text(sql_text), param) result = db.to_json_list(cur) count = conn.execute(text(count_sql_text), count_param).fetchone()[0] logging.info(count) logging.info(result) data = [] for item in result: modelCount = enterprise.get_enterprise_model_count(item["id"]) deviceCount = enterprise.get_enterprise_device_count(item["id"]) data.append( { "id": item["id"], "name": item["name"], "industry": item["industry"], "modelCount": modelCount, "deviceCount": deviceCount, "logo": item["logo"], "createTime": str(item["create_time"]) } ) self.finish({"count": count, "data": data}) class EntityIndexBasecountHandler(APIHandler): """首页基础统计书记""" @authenticated def post(self): entity = enterprise.get_enterprise_entity_count(self.app_mysql) model = enterprise.get_enterprise_model_count() device = enterprise.get_enterprise_device_count() self.finish({"entity": entity, "model": model, "device": device}) class EntityAddHandler(APIHandler): """添加企业""" @authenticated def post(self): name = self.tostr(self.get_escaped_argument("name", "")) province = self.get_escaped_argument("province", "") city = self.get_escaped_argument("city", "") addr = self.get_escaped_argument("addr", "") industry = self.get_int_argument("industry") contact = self.get_escaped_argument("contact", "") phone = self.get_escaped_argument("phone", "") summary = self.get_escaped_argument("summary", "") logo = self.get_escaped_argument("logo", "") if not name or not province or not city or not addr or not industry or not contact or not phone or not summary: raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数缺失") if industry not in consts.industry_map: raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "清选择行业类型") if logo and len(logo) * 0.75 / 1024 / 1024 > 1.2: raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "Logo图标大小超出1M限制") short_uid = shortuuid.ShortUUID().random(length=8) pwd = aes.encrypt(settings.enterprise_aes_key, short_uid) with self.app_mysql.connect() as conn: conn.execute( text( "insert into enterprise(suid, name, province, city, addr, industry, contact, phone, summary, logo, account, pwd) " "values(:suid, :name, :province, :city, :addr, :industry, :contact, :phone, :summary, :logo, :account, :pwd)" ), { "suid": shortuuid.ShortUUID().random(length=10), "name": name, "province": province, "city": city, "addr": addr, "industry": industry, "contact": contact, "phone": phone, "summary": summary, "logo": logo, "account": "admin", "pwd": pwd, } ) conn.commit() # self.db_app.insert( # "insert into enterprise(name, province, city, addr, industry, contact, phone, summary, logo, account, pwd) " # "values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", # name, province, city, addr, industry, contact, phone, summary, logo, "admin", pwd, # ) self.finish() class EntityEditHandler(APIHandler): """编辑企业""" @authenticated def post(self): eid = self.get_int_argument("id") name = self.tostr(self.get_escaped_argument("name", "")) province = self.get_escaped_argument("province", "") city = self.get_escaped_argument("city", "") addr = self.get_escaped_argument("addr", "") industry = self.get_int_argument("industry") contact = self.get_escaped_argument("contact", "") phone = self.get_escaped_argument("phone", "") summary = self.get_escaped_argument("summary", "") logo = self.get_escaped_argument("logo", "") account = self.get_escaped_argument("account", "") if not name or not province or not city or not addr or not industry or not contact or not phone or not summary: raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数缺失") if industry not in consts.industry_map: raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "清选择行业类型") if logo and len(logo) * 0.75 / 1024 / 1024 > 1.2: raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "Logo图标大小超出1M限制") with self.app_mysql.connect() as conn: conn.execute( text( # "insert into enterprise(name, province, city, addr, industry, contact, phone, summary, logo, account, pwd) " # "values(:name, :province, :city, :addr, :industry, :contact, :phone, :summary, :logo, :account, :pwd)" "update enterprise set name=:name, province=:province, city=:city, addr=:addr, industry=:industry, contact" "=:contact, phone=:phone, summary=:summary, logo=:logo, account=:account where id=:id", ), { "name": name, "province": province, "city": city, "addr": addr, "industry": industry, "contact": contact, "phone": phone, "summary": summary, "logo": logo, "account": account, } ) conn.commit() self.finish() class EntityInfoHandler(APIHandler): """企业信息""" @authenticated def post(self): eid = self.get_int_argument("id") row = {} with self.app_mysql.connect() as conn: cur = conn.execute( text("select * from enterprise where id=:id"), {"id": eid} ) # keys = list(cur.keys()) # # one = cur.fetchone() # row = dict(zip(keys, one)) # logging.info(db.Row(itertools.zip_longest(keys, one))) row = db.to_json(cur) cur.close() if not row: raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "请求失败") data = { "name": row["name"], "province": row["province"], "city": row["city"], "industry": row["industry"], "contact": row["contact"], "phone": row["phone"], "summary": row["summary"], "logo": row["logo"], "createTime": str(row["create_time"]), "account": row["account"], # 企业账号 } self.finish(data) class EntityDeleteHandler(APIHandler): """删除企业""" @authenticated def post(self): eid = self.get_int_argument("id") with self.app_mysql.connect() as conn: conn.execute( text("update enterprise set del=1 where id=:id"), {"id": eid} ) conn.commit() self.finish() class EntityPwdcheckHandler(APIHandler): """查看企业密码""" @authenticated def post(self): eid = self.get_int_argument("id") with self.app_mysql.connect() as conn: cur = conn.execute(text("select pwd from enterprise where id=:id"), {"id": eid}) # row = cur.fetchone() logging.info(cur) row = db.to_json(cur) if not row: raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "请求失败") pwd = row["pwd"] cur.close() pwd_dcrypt = aes.decrypt(settings.enterprise_aes_key, pwd) self.finish({"pwd": pwd_dcrypt})