origin.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter web
  5. name:origin.py 溯源相关
  6. author:rabin
  7. """
  8. from __load__ import *
  9. class index_path(Load):
  10. """
  11. @api {get} /origin 获取溯源批次列表
  12. @apiVersion 1.0.0
  13. @apiName getOrigin
  14. @apiGroup Farm
  15. @apiParam {String} page 页数
  16. @apiSuccess {Object[]} setting 基本信息
  17. @apiSuccess {String} setting.name 站点名
  18. @apiSuccess {Number} setting.farm 当前的农场ID
  19. @apiSuccess {Object[]} list 溯源批次列表信息
  20. @apiSuccess {String} list.name 名称
  21. @apiSuccess {String} list.id ID
  22. @apiSuccess {Object[]} page 分页信息
  23. @apiSuccess {String} page.current 当前页
  24. @apiSuccess {String} page.total 总页数
  25. @apiSuccess {String} page.num 每页数据条数
  26. @apiSuccess {String} page.totalNum 总数据条数
  27. @apiErrorExample 操作成功
  28. {"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": [{"pic": "http://192.168.15.10:8087/upload/2017/08/30/455f9d6c26b5c989b32eda1809e58d5f.jpg", "method_id": null, "land_id": "249be20d-5759-5af0-9859-b770d9f44e2a", "farm_id": 1, "state": true, "amount": "100ml", "workdate": 1504105397, "category_id": "9365ea66-be55-56f7-a511-059fc85a9478", "cdate": 1504076614, "id": "19e05f00-ae55-5605-be12-86dcd2866df9"}], "page": {"current": 1, "ajax": false, "total": 1.0, "num": 15, "totalNum": 1}}}
  29. @apiErrorExample 操作失败
  30. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  31. """
  32. @auth
  33. def get(self):
  34. self.data['list'] = self.service('common').list('origin_batch', state=True, page=True)
  35. if self.data['list']:
  36. for key, value in enumerate(self.data['list']):
  37. self.data['list'][key]['zzdates'] = Demeter.date(value['zzdate'], '%Y-%m-%d')
  38. self.data['list'][key]['csdates'] = Demeter.date(value['csdate'], '%Y-%m-%d')
  39. device = self.service('common').one('device_gateway', id=value['gateway_id'])
  40. self.data['list'][key]['device'] = device['name']
  41. land = self.service('common').one('gateway_id', id=value['gateway_id'])
  42. self.data['list'][key]['land'] = land['name']
  43. self.out('yes', self.data)
  44. """
  45. @api {post} /origin 更新溯源批次
  46. @apiVersion 1.0.0
  47. @apiName updateOrigin
  48. @apiGroup Farm
  49. @apiParam {String} id 批次ID,为空则插入新记录
  50. @apiParam {String} update_name 批次名称
  51. @apiParam {String} update_pic 图片,多个逗号隔开
  52. @apiParam {String} update_device_id 设备网关ID
  53. @apiParam {String} update_land 地块ID
  54. @apiParam {String} update_zzdate 种植时间:2017-10-10
  55. @apiParam {String} update_csdate 采收时间:2017-10-10
  56. @apiSuccess {Bool} status 状态
  57. @apiSuccess {String} msg 描述
  58. @apiSuccess {Number} code 状态码
  59. @apiErrorExample 操作成功
  60. {"status": 1, "msg": "操作成功", "code": 0, "data": {}}
  61. @apiErrorExample 操作失败
  62. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  63. """
  64. @auth
  65. def post(self):
  66. id = self.input('id', None)
  67. if not id:
  68. self.out('no')
  69. return
  70. update = {}
  71. update['pic'] = self.input('update_pic')
  72. update['gateway_id'] = self.input('update_device_id')
  73. update['land'] = self.input('update_land')
  74. update['zzdate'] = self.input('update_zzdate')
  75. update['csdate'] = self.input('update_csdate')
  76. update['name'] = self.input('update_name')
  77. if not update['gateway_id']:
  78. self.out(u'请选择设备')
  79. return
  80. if not update['land']:
  81. self.out(u'请选择地块')
  82. return
  83. if not update['zzdate']:
  84. self.out(u'请输入种植时间')
  85. return
  86. if not update['csdate']:
  87. self.out(u'请输入采收时间')
  88. return
  89. if not update['name']:
  90. self.out(u'请输入产品名称')
  91. return
  92. update['zzdate'] = Demeter.mktime(update['zzdate'], '%Y-%m-%d')
  93. update['csdate'] = Demeter.mktime(update['csdate'], '%Y-%m-%d')
  94. state = self.service('common').update('origin_batch', id, update)
  95. self.out('yes', {'id':state})
  96. class info_path(Load):
  97. """
  98. @api {get} /origin/info 获取溯源批次信息
  99. @apiVersion 1.0.0
  100. @apiName getOrigin
  101. @apiGroup Farm
  102. @apiSuccess {Object[]} setting 基本信息
  103. @apiSuccess {String} setting.name 站点名
  104. @apiSuccess {Number} setting.farm 当前的农场ID
  105. @apiSuccess {Object[]} info 溯源批次信息
  106. @apiSuccess {String} info.name 名称
  107. @apiSuccess {String} info.id ID
  108. @apiErrorExample 操作成功
  109. {"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": [{"pic": "http://192.168.15.10:8087/upload/2017/08/30/455f9d6c26b5c989b32eda1809e58d5f.jpg", "method_id": null, "land_id": "249be20d-5759-5af0-9859-b770d9f44e2a", "farm_id": 1, "state": true, "amount": "100ml", "workdate": 1504105397, "category_id": "9365ea66-be55-56f7-a511-059fc85a9478", "cdate": 1504076614, "id": "19e05f00-ae55-5605-be12-86dcd2866df9"}], "page": {"current": 1, "ajax": false, "total": 1.0, "num": 15, "totalNum": 1}}}
  110. @apiErrorExample 操作失败
  111. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  112. """
  113. @auth
  114. def get(self):
  115. id = self.input('id', None)
  116. if id:
  117. self.data['info'] = self.service('common').one('origin_batch', {id:id})
  118. if self.data['info']:
  119. self.data['info']['zzdates'] = Demeter.date(self.data['info']['zzdate'], '%Y-%m-%d')
  120. self.data['info']['csdates'] = Demeter.date(self.data['info']['csdate'], '%Y-%m-%d')
  121. self.out('yes', self.data)
  122. else:
  123. self.out('no')