#!/usr/bin/env python # -*- coding: utf-8 -*- """ demeter web name:origin.py 溯源相关 author:rabin """ from __load__ import * import random class Code(object): @staticmethod def get(self, value): cdate = Demeter.date(value['cdate'], '%Y%m%d%H%M%S') #number = Demeter.compressUuid(value['id']).upper() min = 1000 max = 9999 number = 'NXH' + str(value['farm_id'] + 10000) + '-' + str(value['cdate']) + str(random.randint(min, max)) info = self.service('common').one('origin_batch', number=number) if info and info['id'] != value['id']: return Code.get(self, value) #url = Demeter.config['front']['qrcode'] + self.data['setting']['web'] + 'origin/view?qrcode=' + value['number'] update = {} update['number'] = number state = self.service('common').update('origin_batch', value['id'], update, cdate=False) return number class Origin(object): @staticmethod def get(self, value): value['zzdates'] = Demeter.date(value['zzdate'], '%Y-%m-%d') value['csdates'] = Demeter.date(value['csdate'], '%Y-%m-%d') device = self.service('common').one('device_gateway', id=value['gateway_id']) value['device'] = device['name'] land = self.service('common').one('farm_work_land', id=value['land_id']) if land: value['land'] = land['name'] if not value['number']: value['number'] = Code.get(self, value) self.data['url'] = Demeter.config['web']['originurl'] + value['id'] + '&code=' + value['number'] value['qrcode'] = Demeter.config['front']['qrcode'] + self.data['url'] return value class index_path(Load): """ @api {get} /origin 获取溯源批次列表 @apiVersion 1.0.0 @apiName getOrigin @apiGroup Farm @apiParam {String} page 页数 @apiSuccess {Object[]} setting 基本信息 @apiSuccess {String} setting.name 站点名 @apiSuccess {Number} setting.farm 当前的农场ID @apiSuccess {Object[]} list 溯源批次列表信息 @apiSuccess {String} list.name 名称 @apiSuccess {String} list.id ID @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": {"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": {}}} @apiErrorExample 操作失败 {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}} """ @auth @Web.setting def get(self): self.data['list'] = self.service('common').list('origin_batch', state=True, page=True, search={'farm_id': Demeter.config['setting']['farm']}) if self.data['list']: for key, value in enumerate(self.data['list']): self.data['list'][key] = Origin.get(self, value) self.out('yes', self.data) """ @api {post} /origin 更新溯源批次 @apiVersion 1.0.0 @apiName updateOrigin @apiGroup Farm @apiParam {String} id 批次ID,为空则插入新记录 @apiParam {String} update_name 批次名称 @apiParam {String} update_pic 图片,多个逗号隔开 @apiParam {String} update_device_id 设备网关ID @apiParam {String} update_land 地块ID @apiParam {String} update_zzdate 种植时间:2017-10-10 @apiParam {String} update_csdate 采收时间:2017-10-10 @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['gateway_id'] = self.input('update_device_id') update['pic_id'] = self.input('update_pic_id') update['land_id'] = self.input('update_land') update['zzdate'] = self.input('update_zzdate') update['csdate'] = self.input('update_csdate') update['name'] = self.input('update_name') if not update['gateway_id']: self.out(u'请选择设备') return if not update['land_id']: self.out(u'请选择地块') return if not update['zzdate']: self.out(u'请输入种植时间') return if not update['csdate']: self.out(u'请输入采收时间') return if not update['name']: self.out(u'请输入产品名称') return #update['zzdate'] = Demeter.mktime(update['zzdate'], '%Y-%m-%d') #update['csdate'] = Demeter.mktime(update['csdate'], '%Y-%m-%d') state = self.service('common').update('origin_batch', id, update, cdate=False) self.out('yes', {'id':state}) class info_path(Load): """ @api {get} /origin/info 获取溯源批次信息 @apiVersion 1.0.0 @apiName getOriginInfo @apiGroup Farm @apiParam {String} id 溯源批次id @apiSuccess {Object[]} setting 基本信息 @apiSuccess {String} setting.name 站点名 @apiSuccess {Number} setting.farm 当前的农场ID @apiSuccess {Object[]} info 溯源批次信息 @apiSuccess {String} info.name 名称 @apiSuccess {String} info.id ID @apiErrorExample 操作成功 {"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": {}}} @apiErrorExample 操作失败 {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}} """ @auth @Web.setting def get(self): id = self.input('id', None) if id: self.data['info'] = self.service('common').one('origin_batch', id=id) if self.data['info']: self.data['info'] = Origin.get(self, self.data['info']) self.data['pic'] = [] pic = self.service('common').list('device_info', state=True, search={'hardware_type':6, 'farm_id':self.data['info']['farm_id']}) if pic: for key, value in enumerate(pic): pic[key]['cdates'] = Demeter.date(value['cdate']) self.data['pic'] = pic self.out('yes', self.data) else: self.out('no')