async def get_captcha(request: Request): _eid = get_equipment_id(request) try: _phone = request.query['phone'] except KeyError: raise HTTPForbidden(text='Miss phone') try: await send_sms(request, _eid, _phone) except RuntimeError: return code_response(SendSmsErrorResponse) except SmsLimitException: return code_response(SmsLimitResponse) return code_response(ResponseOk)
async def fix(request: Request): # data { name, phone, remark } """ H -> E 到达现场并开始处理 """ _oid = get_maintenance_id(request) _eid = get_equipment_id(request) data = await request.json() _time_str = datetime.now().strftime('%Y-%m-%d %H:%M:%S') try: _edit = data['name'] _phone = data['phone'] _content = "{time} {name}({phone}) 已解决上报问题".format( time=_time_str, name=_edit, phone=_phone, ) except KeyError: return code_response(InvalidFormFIELDSResponse) async with request.app['mysql'].acquire() as conn: async with conn.cursor() as cur: # 校验预留信息 o_cmd = f"SELECT pid FROM {TABLE_NAME} WHERE id=%s" await cur.execute(o_cmd, (_oid, )) if cur.rowcount == 0: return code_response(InvalidWorkerInformationResponse) row = await cur.fetchone() try: p_cmd = "SELECT name, phone FROM `profile` WHERE id=%s" await cur.execute(p_cmd, (row[0], )) if cur.rowcount == 0: return code_response(InvalidWorkerInformationResponse) row = await cur.fetchone() if row[0] != _edit or row[1] != _phone: return code_response(InvalidWorkerInformationResponse) except IndexError: return code_response(InvalidWorkerInformationResponse) # 更新order m_cmd = f"UPDATE {TABLE_NAME} SET `status`='E', `content`=%s WHERE id=%s AND status='H'" await cur.execute(m_cmd, (_content, _oid)) if cur.rowcount == 0: return code_response(ConflictStatusResponse) await handle_order_history(cur, _oid, 'E', _edit, _phone, data.get('remark'), _content) await handle_equipment_history( cur, _eid, _edit, '{} {} 修复设备故障'.format(_time_str, _edit)) await conn.commit() return code_response(ResponseOk)
async def cancel(request: Request): # data { name, phone, remark } """ *(E/F) -> C 取消工单 """ _oid = get_maintenance_id(request) _eid = get_equipment_id(request) data = await request.json() _time_str = datetime.now().strftime('%Y-%m-%d %H:%M:%S') try: _edit = data['name'] _phone = data['phone'] _content = "{time} {name}({phone}) 已取消工单".format( time=_time_str, name=_edit, phone=_phone, ) except KeyError: return code_response(InvalidFormFIELDSResponse) async with request.app['mysql'].acquire() as conn: async with conn.cursor() as cur: # 更新order m_cmd = f"UPDATE {TABLE_NAME} SET `status`='C', `content`=%s WHERE `id`=%s AND `status` NOT IN ('E', 'F')" await cur.execute(m_cmd, (_content, _oid)) if cur.rowcount == 0: return code_response(ConflictStatusResponse) # 更新equipment await cur.execute( "UPDATE equipment SET status=0, edit=%s WHERE id=%s AND status=1", (_edit, _eid)) if cur.rowcount == 0: return code_response(ConflictStatusResponse) await handle_order_history(cur, _oid, 'C', _edit, _phone, data.get('remark'), _content) # await cur.execute(H_CMD, (_oid, 'C', _edit, _phone, data.get('remark'), _content)) await handle_equipment_history( cur, _eid, _edit, '{} {} 取消工单'.format(_time_str, _edit)) # await cur.execute("INSERT INTO edit_history (eid, content, edit) VALUES (%s, %s, %s)", # (_eid, # '{} {} 取消工单'.format(_time_str, _edit), # _edit)) await conn.commit() return code_response(ResponseOk)
async def get_maintenance_by_equipment(request: Request): eid = get_equipment_id(request) async with request.app['mysql'].acquire() as conn: async with conn.cursor() as cur: await cur.execute( """\ SELECT `id`, `order_id`, `status` FROM `order` WHERE `eid`=%s AND `status`!='F' ORDER BY `id` DESC LIMIT 1""", eid) row = await cur.fetchone() if row: data = { 'oid': ItHashids.encode(row[0]), 'orderId': row[1], 'status': row[2] } await conn.commit() return code_response(ResponseOk, data) else: return code_response(EquipmentWithoutMaintenanceResponse)
async def get_patrol_detail(request: Request): """ 查看单个设备的巡检记录 """ eid = get_equipment_id(request) async with request.app['mysql'].acquire() as conn: async with conn.cursor() as cur: await cur.execute( """ SELECT b.id, b.patrol_id FROM patrol_detail a JOIN patrol_meta b ON a.pid = b.id WHERE a.eid = %s AND a.`check` = 0; """, eid) data = [] for row in await cur.fetchall(): data.append({ 'pid': ItHashids.encode(row[0]), 'patrolId': row[1], }) await conn.commit() return code_response( ResponseOk, data) if len(data) else code_response(EmtpyPatrolPlanResponse)
async def appraisal(request: Request): # data { name, phone, rank, remark } """ E -> F 评分,结束工单 """ _oid = get_maintenance_id(request) _eid = get_equipment_id(request) try: _appraisal_form = await request.json() assert all(k in APPRAISAL_FORM_FIELDS for k in _appraisal_form) except (KeyError, AssertionError, JSONDecodeError): return code_response(InvalidFormFIELDSResponse) _time_str = datetime.now().strftime('%Y-%m-%d %H:%M:%S') _name = _appraisal_form['name'] _phone = _appraisal_form['phone'] _rank = _appraisal_form['rank'] _content = "{time} {name}({phone})对工单打了{rank}分评价".format(time=_time_str, name=_name, phone=_phone, rank=_rank) async with request.app['mysql'].acquire() as conn: async with conn.cursor() as cur: # 更新order m_cmd = f"UPDATE {TABLE_NAME} SET `status`='F', `rank`=%s, `content`=%s WHERE `id`=%s AND `status`='E'" await cur.execute(m_cmd, (_rank, _content, _oid)) if cur.rowcount == 0: return code_response(ConflictStatusResponse) await handle_order_history(cur, _oid, 'F', _name, _phone, _appraisal_form.get('remark'), _content) # await cur.execute(H_CMD, (_oid, 'F', _name, _phone, _appraisal_form.get('remark'), _content)) # 更新equipment await cur.execute( "UPDATE equipment SET status=0, edit=%s WHERE id=%s AND status=1", (_name, _eid)) if cur.rowcount == 0: return code_response(ConflictStatusResponse) await conn.commit() return code_response(ResponseOk)