import sqlite3
import random
import json
import sys
#sqlite読み込み
dbname = '/var/www/db/coupon.sqlite'
conn = sqlite3.connect(dbname)
cur = conn.cursor()
# 空リストを作成
coupon=[]
src_box={}
# 重複ありの数列を作成する関数(a=最小値、b=最大値、k=件数)
def rand_ints_dup(a, b, k):
  return [random.randint(a, b) for i in range(k)]
#パラメーター摘出し、キー.バリューでsrc_boxにぶち込んでいく
for src in sys.argv:
  if(not "get_coupon.py" in src):
    src=src.split('=')
    src_box[src[0]]=src[1]
#標準出力にJSONで対応したデータを返す(print()すればok)
#src_boxにshop_idがある時そのデータ１つだけを返す
if 'shop_id' in src_box:
  cur.execute('SELECT * FROM jrdCoupon WHERE shop_id = ?',(src_box['shop_id'],))
  results = cur.fetchall()
  coupon_list = {}
  for r in results:
    coupon_list["shop_id"] = r[0]
    coupon_list["shop_name"] = r[1]
    coupon_list["shop_name_kana"] = r[2]
    coupon_list["catch_copy"] = r[3]
    coupon_list["pref_cd"] = r[4]
    coupon_list["path"] = r[5]
    coupon.append(coupon_list)
  print(coupon)
else :
  #県コードと件数があり、shop_idがない時、一致する県コードからランダムに件数分返す
  if 'pref_cd' in src_box and 'num' in src_box:
    cur.execute('SELECT * FROM jrdCoupon WHERE pref_cd = ?',(src_box['pref_cd'],))
    results = cur.fetchall()
    pref=int(src_box['pref_cd'])
    num=int(src_box['num'])
    length=len(results)
    for r in results:
      if (pref==r[4]):
        coupon_list = {}
        coupon_list["shop_id"] = r[0]
        coupon_list["shop_name"] = r[1]
        coupon_list["shop_name_kana"] = r[2]
        coupon_list["catch_copy"] = r[3]
        coupon_list["pref_cd"] = r[4]
        coupon_list["path"] = r[5]
        coupon.append(coupon_list)
    length=length-1
    number=rand_ints_dup(0, length, num)
    for x in number:
      print(coupon[x])
  #県コードがあり、shop_idと件数がない時、一致する県コードからランダムに１件返す
  elif 'pref_cd' in src_box:
    cur.execute('SELECT * FROM jrdCoupon WHERE pref_cd = ?',(src_box['pref_cd'],))
    results = cur.fetchall()
    length=len(results)
    pref=int(src_box['pref_cd'])
    for r in results:
      if (pref==r[4]):
        coupon_list = {}
        coupon_list["shop_id"] = r[0]
        coupon_list["shop_name"] = r[1]
        coupon_list["shop_name_kana"] = r[2]
        coupon_list["catch_copy"] = r[3]
        coupon_list["pref_cd"] = r[4]
        coupon_list["path"] = r[5]
        coupon.append(coupon_list)  
    length=length-1
    num = random.randint(0,length)
    print(coupon[num])
  #件数があり、shop_idと県コードがない時、ランダムに件数分返す
  elif 'num' in src_box:
    cur.execute('SELECT * FROM jrdCoupon')
    results = cur.fetchall()
    length=len(results)
    num=int(src_box['num'])
    for r in results:
      coupon_list = {}
      coupon_list["shop_id"] = r[0]
      coupon_list["shop_name"] = r[1]
      coupon_list["shop_name_kana"] = r[2]
      coupon_list["catch_copy"] = r[3]
      coupon_list["pref_cd"] = r[4]
      coupon_list["path"] = r[5]
      coupon.append(coupon_list)
    length=length-1
    number=rand_ints_dup(0, length, num)
    for x in number:
      print(coupon[x])
  #何も値がない時、ランダムに１件返す
  else :
    cur.execute('SELECT * FROM jrdCoupon')
    results = cur.fetchall()
    length=len(results)
    for r in results:
      coupon_list = {}
      coupon_list["shop_id"] = r[0]
      coupon_list["shop_name"] = r[1]
      coupon_list["shop_name_kana"] = r[2]
      coupon_list["catch_copy"] = r[3]
      coupon_list["pref_cd"] = r[4]
      coupon_list["path"] = r[5]
      coupon.append(coupon_list)
    length=length-1
    num = random.randint(0,length)
    print(coupon[num])
# DBとの接続を閉じる(必須)
conn.close()