import jingrow


def execute():
	plans = jingrow.get_all(
		"Site Plan",
		{
			"document_type": (
				"in",
				["Server", "Database Server", "Proxy Server", "Self Hosted Server"],
			)
		},
		pluck="name",
	)

	for plan in plans:
		plan_pg = jingrow.get_pg("Site Plan", plan)
		server_plan_pg = jingrow.get_pg(
			{
				"pagetype": "Server Plan",
				"name": plan_pg.name,
				"title": plan_pg.plan_title,
				"price_cny": plan_pg.price_cny,
				"price_usd": plan_pg.price_usd,
				"server_type": plan_pg.document_type,
				"cluster": plan_pg.cluster,
				"instance_type": plan_pg.instance_type,
				"vcpu": plan_pg.vcpu,
				"memory": plan_pg.memory,
				"disk": plan_pg.disk,
				"enabled": plan_pg.enabled,
			}
		)
		server_plan_pg.roles = plan_pg.roles
		server_plan_pg.insert(ignore_if_duplicate=True)

	for marketplace_plan in jingrow.get_all("Marketplace App Plan", pluck="name"):
		map_pg = jingrow.get_pg("Marketplace App Plan", marketplace_plan)
		plan = jingrow.get_all(
			"Site Plan", {"name": map_pg.plan}, ["plan_title", "price_usd", "price_cny"]
		)

		if plan:
			plan = plan[0]
		else:
			continue

		map_pg.title = plan.plan_title
		map_pg.price_cny = plan.price_cny
		map_pg.price_usd = plan.price_usd
		map_pg.save()
