def post(self, *args, **kwargs): """ post请求,通过工厂id,页数和每页个数返回设备信息的列表 :param args: page: 第几页 per_page: 每页实现几条数据 :param kwargs: /factory_id :return: 成功返回201和包含设备信息的列表,未请求到数据返回 status_24,参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) factory_id = kwargs['factory_id'] request_data = kwargs['request_data'] page = request_data.get('page') per_page = request_data.get('per_page') if factory_id and page and per_page: equipment_obj = EquipmentDB(factory_id) limit = int(per_page) offset = (int(page) - 1) * limit result = equipment_obj.findall(offset, limit) equipment_obj.close() if result: SpiderApi.response(201) self.write_json(result, 201) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def delete(self, *args, **kwargs): """ delete请求,通过工厂id和生产线id,删除生产线,删除之前判断该生产线下是否还有设备 :param args: :param kwargs: /factory_id/product_line_id/ :return: 成功返回204和None,未找到数据返回status_24 参数错误返回status_22,该生产线下还有设备返回status_30 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) parameter = kwargs['parameter'] parameter_list = parameter.split('/') factory_id = parameter_list[0] product_line_id = parameter_list[1] equipment_obj = EquipmentDB(factory_id=factory_id) equipment_count = equipment_obj.get_counts_by_field('line_id', product_line_id) equipment_obj.close() if not equipment_count: if len(parameter_list) == 2: product_line_obj = ProductLineDB(factory_id) result = product_line_obj.remove(product_line_id) product_line_obj.close() if result: SpiderApi.response(204) self.write_json(None, 204) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22) else: SpiderApi.response(errors.status_30) raise HTTPError(**errors.status_30)
def get(self, *args, **kwargs): """ get请求,通过设备id和工厂id获取该设备详细信息接口 :param args: :param kwargs: /factory_id/equipment_id :return: 成功返回200和设备信息的json,未找到设备返回status_24 传参错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) parameter = kwargs['parameter'] parameter_list = parameter.split('/') if len(parameter_list) == 2: factory_id = parameter_list[0] equipment_id = parameter_list[1] equipment_obj = EquipmentDB(factory_id) result = equipment_obj.find(id=equipment_id) equipment_obj.close() if result: SpiderApi.response(200) self.write_json(result) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def delete(self, *args, **kwargs): """ delete请求,通过设备id和工厂id删除指定设备 删除设备的时候不需要检测该分类下是否还有子类,但是需要在班组的设备列表中删除该设备 :param args: :param kwargs:/factory_id/equipment_id :return:成功返回204,未能成功删除返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) parameter = kwargs['parameter'] parameter_list = parameter.split('/') if len(parameter_list) == 2: factory_id = parameter_list[0] equipment_id = parameter_list[1] equipment_obj = EquipmentDB(factory_id) workgroup_obj = WorkGroupDB(factory_id=factory_id) result = equipment_obj.remove(equipment_id) result1 = workgroup_obj.del_equipment_in_list( equipment_id) # 在班组的设备列表中删除该设备 equipment_obj.close() workgroup_obj.close() if result and result1: SpiderApi.response(204) self.write_json(None, status_code=204) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def get(self, *args, **kwargs): """ get请求,通过工厂id获取该工厂下的所有的工厂,生产线,设备,的层级关系树状图 :param args: :param kwargs: /factory_id :return: 成功返回包含所有层级关系的列表,参数错误返回status_22 """ factory_id = kwargs['factory_id'] if factory_id: workshop_obj = WorkShopDB(factory_id=factory_id) product_list_obj = ProductLineDB(factory_id=factory_id) equipment_obj = EquipmentDB(factory_id=factory_id) workshop_list = [] # 车间列表 for workshop in workshop_obj.find(factory_id=factory_id): workshop_list.append(workshop) product_list = [] # 生产线列表 for product_line in product_list_obj.find( workshop_id=workshop['_id']): equipment_list = [] # 设备列表 for equipment in equipment_obj.find( line_id=product_line['_id']): equipment_list.append(equipment) product_line['equipment'] = equipment_list product_list.append(product_line) workshop['product_line'] = product_list workshop_list.append(workshop) workshop_obj.close() product_list_obj.close() equipment_obj.close() self.write_json(workshop_list) else: raise HTTPError(**errors.status_22)
def get_equipment_iter(factory_id): """ 遍历设备表获取包含该工厂的设备特定字段信息的列表 :param factory_id: 工厂id :return: 成功返回该车间下的所有的设备id和出厂编号字典组成的列表 """ equipment_obj = EquipmentDB(factory_id=factory_id) equipment_iter = equipment_obj.get_id_and_number() return list(equipment_iter)
def post(self, *args, **kwargs): """ post请求,通过工厂ID和插入的设备详细插入设备 :param args: name: 设备名 workshop_id: 工厂id line_id: 生产线id factory_number: 出厂编号 factory_time: 出厂时间 product_number: 产品型号 :param kwargs: /factory_id/ :return: 成功返回设备的id和201,未能成功插入返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) parameter = kwargs['parameter'] # 这里要记得切分字符串 不然连接数据库的时候会多'/' parameter_list = parameter.split('/') request_data = kwargs['request_data'] name = request_data.get('name') workshop_id = request_data.get('workshop_id') line_id = request_data.get('line_id') factory_number = request_data.get('factory_number') factory_time = request_data.get('factory_time') product_number = request_data.get('product_number') if parameter_list and name and workshop_id and line_id and factory_number and factory_time and product_number: factory_id = parameter_list[0] equipment_obj = EquipmentDB(factory_id) result = equipment_obj.insert(name, workshop_id, line_id, factory_number, factory_time, product_number) # 同步缓存写入 equipment_obj.close() if result: SpiderApi.response(201) self.write_json(result, 201) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def put(self, *args, **kwargs): """ put请求,通过工厂和设备id以及需要修改的字段,更新设备信息 :param args: name: 设备名 workshop_id: 工厂id line_id: 生产线id factory_number: 出厂编号 factory_time: 出厂时间 product_number: 产品型号 :param kwargs: /factory_id/equipment_id :return: 成功返回设备的id和200,未能成功插入返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) parameter = kwargs['parameter'] parameter_list = parameter.split('/') request_data = kwargs['request_data'] name = request_data.get('name') workshop_id = request_data.get('workshop_id') line_id = request_data.get('line_id') factory_number = request_data.get('factory_number') factory_time = request_data.get('factory_time') product_number = request_data.get('product_number') if len(parameter_list) == 2 and name and workshop_id and line_id and factory_number \ and factory_time and product_number: factory_id = parameter_list[0] equipment_id = parameter_list[1] equipment_obj = EquipmentDB(factory_id) result = equipment_obj.update(equipment_id, name, factory_id, workshop_id, line_id, factory_number, factory_time, product_number) equipment_obj.close() if result: SpiderApi.response(200) self.write_json(result) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def findall(self, factory_id=None, number=None, page=None): try: result = [] workgroups = self.collection.find({}, { "create_time": 0 }).skip(number).limit(page) for workgroup in workgroups: workgroup['equipments'] = list() for equipment_id in workgroup['equipment_list']: equipment_obj = EquipmentDB(factory_id=factory_id) name = equipment_obj.find(id=equipment_id).get('name') workgroup['equipments'].append({ 'id': equipment_id, 'name': name }) result.append(workgroup) return result except: return None