# -*- coding: utf-8 -*- from __future__ import absolute_import from flask import Flask, jsonify from flask_restful import reqparse, abort, Api, Resource import logging from mysql.mmysql import MysqlBase from prod_config import mysql_promotion_config import pdb log = logging.getLogger(__name__) parser = reqparse.RequestParser() parser.add_argument('id') parser.add_argument('area') parser.add_argument('type') parser.add_argument('mode') parser.add_argument('in_used') parser.add_argument('gameid') parser.add_argument('channelid') class Location(Resource): def __init__(self): self.args = parser.parse_args() self.mydb = MysqlBase(**mysql_promotion_config) def get(self): gameid = self.args['gameid'] channelid = self.args['channelid'] or 6001 sql = f"select gameid,channelid,id,area,type,mode from location where gameid={gameid} and channelid={channelid} and in_used=1;" try: data = self.mydb.query(sql) except Exception: log.error("get data from location failed!", exc_info=True) return jsonify({'code': 500}) all_data = [] log.debug(f"get data from localtion was {data}") if data: for line in data: if line: localtion = {} localtion['gameid'], localtion['channelid'], localtion['id'], localtion['area'], localtion['type'], \ localtion['mode'] = line all_data.append(localtion) del localtion return jsonify({'code': 200, 'message': all_data}) else: log.error(f"{sql} not found in mysql !") return jsonify({'code': 200, 'message': ""}) def post(self): """ More Actions名称类型必填说明areastring是区域(第一元素:1=首页,2=游戏中,3=结算页,4=关闭,第二元素:X轴第三元素:Y轴,其中X,Y取值范围(0,15))如(1,0,0)代表首页左上typeint是类型(1=icon,2=banner,3=浮窗)modeint是广告单播或轮播模式(1=单播,2=轮播)gameidint是游戏IDchannelidint否渠道ID,默认是6001 :return: """ try: location = {} location['area'] = self.args['area'] location['type'] = self.args['type'] location['mode'] = self.args['mode'] location['gameid'] = self.args['gameid'] location['channelid'] = self.args['channelid'] or 6001 location['id'] = self.create_id() if location['area'] and location['type'] and location['mode'] and location['gameid'] : self.mydb.insert("location", location) else: return jsonify({'code': 500,"message":f"get args was area={location['area']},type={self.args['type']},mode={self.args['mode']},gameid={self.args['gameid']}"}) except Exception: log.error("set values to location mysql failed!", exc_info=True) return jsonify({'code': 500}) return jsonify({'code': 200}) def create_id(self): max_id = f"select id from location order by id desc limit 1" data = self.mydb.query(max_id) log.info(f"2 {data}") try: max = int(data[0][0]) + 1 log.info(f"max id was {max}!") except Exception: log.error(f"error with get location id ", exc_info=True) max = 1001 return max def put(self): try: location = {} location['id'] = self.args['id'] location['area'] = self.args['area'] location['type'] = self.args['type'] location['mode'] = self.args['mode'] location['gameid'] = self.args['gameid'] location['channelid'] = self.args['channelid'] location['in_used'] = self.args['in_used'] or 1 sel_sql = f"select area from location where id={self.args['id']};" data = self.mydb.query(sel_sql) if data: condition = f"id='{self.args['id']}'" self.mydb.update("location", location, condition) return jsonify({'code': 200}) else: return jsonify({'code': 404, 'message': f"{self.args['id']} not found in mysql!"}) except Exception: log.error("update values to location mysql failed!", exc_info=True) return jsonify({'code': 500}) def delete(self): id = self.args['id'] if not id: return jsonify({'code': 500, 'message': 'id not found'}) try: del_sql = f'update location set in_used=0 where id={id};' self.mydb.change(del_sql) except Exception: log.error(f"location remove {id} failed!", exc_info=True) return jsonify({'code': 500}) return jsonify({'code': 200})