origin.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. import random
  10. class Code(object):
  11. @staticmethod
  12. def get(self, value):
  13. cdate = Demeter.date(value['cdate'], '%Y%m%d%H%M%S')
  14. #number = Demeter.compressUuid(value['id']).upper()
  15. min = 1000
  16. max = 9999
  17. number = 'NXH' + str(value['farm_id'] + 10000) + '-' + str(value['cdate']) + str(random.randint(min, max))
  18. info = self.service('common').one('origin_batch', number=number)
  19. if info and info['id'] != value['id']:
  20. return Code.get(self, value)
  21. #url = Demeter.config['front']['qrcode'] + self.data['setting']['web'] + 'origin/view?qrcode=' + value['number']
  22. update = {}
  23. update['number'] = number
  24. state = self.service('common').update('origin_batch', value['id'], update, cdate=False)
  25. return number
  26. class Origin(object):
  27. @staticmethod
  28. def get(self, value):
  29. value['zzdates'] = Demeter.date(value['zzdate'], '%Y-%m-%d')
  30. value['csdates'] = Demeter.date(value['csdate'], '%Y-%m-%d')
  31. device = self.service('common').one('device_gateway', id=value['gateway_id'])
  32. value['device'] = device['name']
  33. land = self.service('common').one('farm_work_land', id=value['land_id'])
  34. if land:
  35. value['land'] = land['name']
  36. if not value['number']:
  37. value['number'] = Code.get(self, value)
  38. self.data['url'] = Demeter.config['web']['originurl'] + value['id'] + '&code=' + value['number']
  39. value['qrcode'] = Demeter.config['front']['qrcode'] + self.data['url']
  40. return value
  41. class index_path(Load):
  42. """
  43. @api {get} /origin 获取溯源批次列表
  44. @apiVersion 1.0.0
  45. @apiName getOrigin
  46. @apiGroup Farm
  47. @apiParam {String} page 页数
  48. @apiSuccess {Object[]} setting 基本信息
  49. @apiSuccess {String} setting.name 站点名
  50. @apiSuccess {Number} setting.farm 当前的农场ID
  51. @apiSuccess {Object[]} list 溯源批次列表信息
  52. @apiSuccess {String} list.name 名称
  53. @apiSuccess {String} list.id ID
  54. @apiSuccess {Object[]} page 分页信息
  55. @apiSuccess {String} page.current 当前页
  56. @apiSuccess {String} page.total 总页数
  57. @apiSuccess {String} page.num 每页数据条数
  58. @apiSuccess {String} page.totalNum 总数据条数
  59. @apiErrorExample 操作成功
  60. {"status": 1, "msg": "yes", "code": 0, "data": {"url": "http://192.168.15.10:8089/origin?id=919d41a1-de9b-51f4-a1b1-46ce826fe49e&code=NXH10001-15093486295276", "setting": {"farm": 1, "user": "229cfd36-9399-5599-b2b5-c53174c3de19", "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "list": [{"gateway_id": "542535db-6c0b-51fc-9f73-cafb3872ce76", "land": "\u5730\u57572\u53f7", "name": "test", "land_id": "6b18f8b5-c9e5-5600-8808-dad30fa4ea93", "farm_id": 1, "zzdates": "2017-10-10", "number": "NXH10001-15093486295276", "zzdate": 1507593600, "device": "\u9ed8\u8ba4\u7f51\u5173", "state": true, "csdates": "2017-10-11", "csdate": 1507680000, "qrcode": "http://pan.baidu.com/share/qrcode?w=300&h=300&url=http://192.168.15.10:8089/origin?id=919d41a1-de9b-51f4-a1b1-46ce826fe49e&code=NXH10001-15093486295276", "cdate": 1509348717, "id": "919d41a1-de9b-51f4-a1b1-46ce826fe49e"}], "page": {"current": 1, "ajax": false, "total": 1, "num": 15, "totalNum": 1}, "base": {}}}
  61. @apiErrorExample 操作失败
  62. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  63. """
  64. @auth
  65. @Web.setting
  66. def get(self):
  67. self.data['list'] = self.service('common').list('origin_batch', state=True, page=True, search={'farm_id': Demeter.config['setting']['farm']})
  68. if self.data['list']:
  69. for key, value in enumerate(self.data['list']):
  70. self.data['list'][key] = Origin.get(self, value)
  71. self.out('yes', self.data)
  72. """
  73. @api {post} /origin 更新溯源批次
  74. @apiVersion 1.0.0
  75. @apiName updateOrigin
  76. @apiGroup Farm
  77. @apiParam {String} id 批次ID,为空则插入新记录
  78. @apiParam {String} update_name 批次名称
  79. @apiParam {String} update_pic 图片,多个逗号隔开
  80. @apiParam {String} update_device_id 设备网关ID
  81. @apiParam {String} update_land 地块ID
  82. @apiParam {String} update_zzdate 种植时间:2017-10-10
  83. @apiParam {String} update_csdate 采收时间:2017-10-10
  84. @apiSuccess {Bool} status 状态
  85. @apiSuccess {String} msg 描述
  86. @apiSuccess {Number} code 状态码
  87. @apiErrorExample 操作成功
  88. {"status": 1, "msg": "操作成功", "code": 0, "data": {}}
  89. @apiErrorExample 操作失败
  90. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  91. """
  92. @auth
  93. @Web.setting
  94. def post(self):
  95. id = self.input('id', None)
  96. update = {}
  97. update['pic'] = self.input('update_pic')
  98. update['gateway_id'] = self.input('update_device_id')
  99. update['pic_id'] = self.input('update_pic_id')
  100. update['land_id'] = self.input('update_land')
  101. update['zzdate'] = self.input('update_zzdate')
  102. update['csdate'] = self.input('update_csdate')
  103. update['name'] = self.input('update_name')
  104. if not update['gateway_id']:
  105. self.out(u'请选择设备')
  106. return
  107. if not update['land_id']:
  108. self.out(u'请选择地块')
  109. return
  110. if not update['zzdate']:
  111. self.out(u'请输入种植时间')
  112. return
  113. if not update['csdate']:
  114. self.out(u'请输入采收时间')
  115. return
  116. if not update['name']:
  117. self.out(u'请输入产品名称')
  118. return
  119. #update['zzdate'] = Demeter.mktime(update['zzdate'], '%Y-%m-%d')
  120. #update['csdate'] = Demeter.mktime(update['csdate'], '%Y-%m-%d')
  121. state = self.service('common').update('origin_batch', id, update, cdate=False)
  122. self.out('yes', {'id':state})
  123. class info_path(Load):
  124. """
  125. @api {get} /origin/info 获取溯源批次信息
  126. @apiVersion 1.0.0
  127. @apiName getOriginInfo
  128. @apiGroup Farm
  129. @apiParam {String} id 溯源批次id
  130. @apiSuccess {Object[]} setting 基本信息
  131. @apiSuccess {String} setting.name 站点名
  132. @apiSuccess {Number} setting.farm 当前的农场ID
  133. @apiSuccess {Object[]} info 溯源批次信息
  134. @apiSuccess {String} info.name 名称
  135. @apiSuccess {String} info.id ID
  136. @apiErrorExample 操作成功
  137. {"status": 1, "msg": "yes", "code": 0, "data": {"info": {"gateway_id": "542535db-6c0b-51fc-9f73-cafb3872ce76", "land": "\u5730\u57572\u53f7", "name": "test", "land_id": "6b18f8b5-c9e5-5600-8808-dad30fa4ea93", "farm_id": 1, "zzdates": "2017-10-10", "number": "NXH10001-15093486295276", "zzdate": 1507593600, "device": "\u9ed8\u8ba4\u7f51\u5173", "state": true, "csdates": "2017-10-11", "csdate": 1507680000, "qrcode": "http://pan.baidu.com/share/qrcode?w=300&h=300&url=http://192.168.15.10:8089/origin?id=919d41a1-de9b-51f4-a1b1-46ce826fe49e&code=NXH10001-15093486295276", "cdate": 1509348717, "id": "919d41a1-de9b-51f4-a1b1-46ce826fe49e"}, "url": "http://192.168.15.10:8089/origin?id=919d41a1-de9b-51f4-a1b1-46ce826fe49e&code=NXH10001-15093486295276", "setting": {"farm": 1, "user": "229cfd36-9399-5599-b2b5-c53174c3de19", "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "base": {}}}
  138. @apiErrorExample 操作失败
  139. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  140. """
  141. @auth
  142. @Web.setting
  143. def get(self):
  144. id = self.input('id', None)
  145. if id:
  146. self.data['info'] = self.service('common').one('origin_batch', id=id)
  147. if self.data['info']:
  148. self.data['info'] = Origin.get(self, self.data['info'])
  149. self.data['pic'] = []
  150. pic = self.service('common').list('device_info', state=True, search={'hardware_type':6, 'farm_id':self.data['info']['farm_id']})
  151. if pic:
  152. for key, value in enumerate(pic):
  153. pic[key]['cdates'] = Demeter.date(value['cdate'])
  154. self.data['pic'] = pic
  155. self.out('yes', self.data)
  156. else:
  157. self.out('no')