#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
	demeter web
	name:farm.py 农场相关
	author:rabin
"""
from __load__ import *

class index_path(Load):
	"""
	@api {get} /farm 获取农场基本信息
	@apiVersion 1.0.0
	@apiName getFarm
	@apiGroup Farm

	@apiSuccess {Object[]} setting 基本信息
	@apiSuccess {String}   setting.name  站点名
	@apiSuccess {Number}   setting.farm  当前的农场ID

	@apiSuccess {Object[]} farm 农场基本信息
	@apiSuccess {String}   farm.name  农场名
	@apiSuccess {String}   farm.master_name  联系人
	@apiSuccess {String}   farm.tel  联系电话
	@apiSuccess {String}   farm.address  农场地址

	@apiErrorExample 操作成功
	{"status": 1, "msg": "yes", "code": 0, "data": {"farm": {"info": "", "master_name": "", "tel": "", "name": "\u9ed8\u8ba4\u56ed\u533a", "pic": "", "cdate": 1503915445, "state": true, "address": "", "id": 1}, "setting": {"farm": 1, "user": 0, "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}}}
	@apiErrorExample 操作失败
	{"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
	"""
	@auth
	@Web.setting
	def get(self):
		# 获取农场信息
		self.data['farm'] = self.service('common').one('farm', id=Demeter.config['setting']['farm'])
		self.out('yes', self.data)

	"""
	@api {post} /farm 更新农场信息
	@apiVersion 1.0.0
	@apiName updateFarm
	@apiGroup Farm

	@apiParam {String} id 农事ID，为空则插入新记录
	@apiParam {String} update_name 农场名称
	@apiParam {String} update_address 农场地址
	@apiParam {String} update_master_name 联系人
	@apiParam {String} update_tel 联系电话
	@apiParam {String} update_pic 农场照片

	@apiSuccess {Bool} status 状态
	@apiSuccess {String}   msg  描述
	@apiSuccess {Number}   code  状态码

	@apiErrorExample 操作成功
	{"status": 1, "msg": "操作成功", "code": 0, "data": {}}
	@apiErrorExample 操作失败
	{"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
	"""
	@auth
	@Web.setting
	def post(self):
		id = self.input('id', None)
		update = {}
		update['pic'] = self.input('update_pic')
		update['name'] = self.input('update_name')
		update['master_name'] = self.input('update_master_name')
		update['address'] = self.input('update_address')
		update['tel'] = self.input('update_tel')
		if not update['name']:
			self.out(u'请输入农场名称')
			return
		"""
		if not update['master_name']:
			self.out(u'请输入农场联系人')
			return
		if not update['address']:
			self.out(u'请输入农场地址')
			return
		if not update['tel']:
			self.out(u'请输入联系电话')
			return
		"""
		state = self.service('common').update('farm', id, update, cdate=False)
		self.out('yes', {'id':state})

class msg_path(Load):
	"""
	@api {get} /farm/msg 获取消息通知
	@apiVersion 1.0.0
	@apiName getMsg
	@apiGroup Farm

	@apiParam {String} page 页数

	@apiSuccess {Object[]} setting 基本信息
	@apiSuccess {String}   setting.name  站点名
	@apiSuccess {Number}   setting.farm  当前的农场ID

	@apiSuccess {Object[]} list 消息列表
	@apiSuccess {Number}   list.id	消息ID
	@apiSuccess {String}   list.content  消息内容
	@apiSuccess {String}   list.cdates	消息时间
	@apiSuccess {String}   list.cdate	消息时间戳

	@apiSuccess {Object[]} page 分页信息
	@apiSuccess {String}   page.current  当前页
	@apiSuccess {String}   page.total	总页数
	@apiSuccess {String}   page.num	每页数据条数
	@apiSuccess {String}   page.totalNum	总数据条数

	@apiErrorExample 操作成功
	{"status": 1, "msg": "yes", "code": 0, "data": {"setting": {"farm": 1, "user": 0, "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "list": []}}
	@apiErrorExample 操作失败
	{"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
	"""
	@auth
	@Web.setting
	def get(self):
		self.data['list'] = self.service('common').list('msg', search={'farm_id':self.data['setting']['farm']}, page=True, order='status asc, cdate desc')
		for key, value in enumerate(self.data['list']):
			self.data['list'][key]['cdates'] = Demeter.date(value['cdate'])
			type_info = self.service('common').one('msg_type', id=value['type_id'])
			self.data['list'][key]['type'] = type_info['name']
			id = value['id']
			update = {}
			update['status'] = True
			self.service('common').update('msg', id, update, cdate=False)

		self.out('yes', self.data)


class qrcode_path(Load):
	@auth
	@Web.setting
	def get(self):
		url = self.input('url')
		url = Demeter.config['front']['qrcode_create'] + url
		data = Demeter.curl(url)
		self.write(data)
		self.set_header("Content-type", "image/jpg")
