1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
| class SupplyChainItem: """供应链物品类""" def __init__(self, item_id, name, category, origin): self.item_id = item_id self.name = name self.category = category self.origin = origin self.created_at = datetime.now() self.current_owner = origin self.status = "created" self.location = "origin" self.quality_checks = [] self.certifications = [] self.transfer_history = [] def add_quality_check(self, inspector, result, notes=""): """添加质量检查记录""" check = { "inspector": inspector, "result": result, "notes": notes, "timestamp": datetime.now(), "location": self.location } self.quality_checks.append(check) return check def add_certification(self, authority, cert_type, cert_id): """添加认证""" cert = { "authority": authority, "type": cert_type, "cert_id": cert_id, "issued_at": datetime.now() } self.certifications.append(cert) return cert def transfer_ownership(self, new_owner, location, notes=""): """转移所有权""" transfer = { "from": self.current_owner, "to": new_owner, "location": location, "notes": notes, "timestamp": datetime.now() } self.transfer_history.append(transfer) self.current_owner = new_owner self.location = location return transfer
class SupplyChainContract(SmartContract): """供应链智能合约""" def __init__(self, contract_id, supply_chain_name): super().__init__(contract_id, "SupplyChain", []) self.supply_chain_name = supply_chain_name self.items = {} self.participants = {} self.compliance_rules = [] def register_participant(self, participant_id, name, role, location): """注册参与者""" participant = { "id": participant_id, "name": name, "role": role, "location": location, "registered_at": datetime.now(), "reputation_score": 100, "items_handled": 0 } self.participants[participant_id] = participant self.log_event(f"参与者注册: {name} ({role})") return participant def create_item(self, creator_id, item_id, name, category): """创建物品""" if creator_id not in self.participants: return False, "创建者未注册" creator = self.participants[creator_id] item = SupplyChainItem(item_id, name, category, creator_id) self.items[item_id] = item creator["items_handled"] += 1 self.log_event(f"物品创建: {name} (ID: {item_id})") return True, item def transfer_item(self, item_id, from_id, to_id, location, notes=""): """转移物品""" if item_id not in self.items: return False, "物品不存在" if from_id not in self.participants or to_id not in self.participants: return False, "参与者未注册" item = self.items[item_id] if item.current_owner != from_id: return False, "转移者不是当前所有者" transfer = item.transfer_ownership(to_id, location, notes) self.participants[to_id]["items_handled"] += 1 self.log_event(f"物品转移: {item.name} 从 {from_id} 到 {to_id}") return True, transfer def add_quality_check(self, item_id, inspector_id, result, notes=""): """添加质量检查""" if item_id not in self.items: return False, "物品不存在" if inspector_id not in self.participants: return False, "检查员未注册" item = self.items[item_id] check = item.add_quality_check(inspector_id, result, notes) inspector = self.participants[inspector_id] if result == "pass": inspector["reputation_score"] = min(100, inspector["reputation_score"] + 1) elif result == "fail": inspector["reputation_score"] = max(0, inspector["reputation_score"] - 5) self.log_event(f"质量检查: {item.name} - {result}") return True, check def trace_item_history(self, item_id): """追溯物品历史""" if item_id not in self.items: return None item = self.items[item_id] history = { "item_info": { "id": item.item_id, "name": item.name, "category": item.category, "origin": item.origin, "created_at": item.created_at, "current_owner": item.current_owner, "current_location": item.location, "status": item.status }, "transfer_history": item.transfer_history, "quality_checks": item.quality_checks, "certifications": item.certifications } return history def verify_authenticity(self, item_id): """验证物品真实性""" if item_id not in self.items: return False, "物品不存在" item = self.items[item_id] if not item.quality_checks: return False, "缺少质量检查记录" latest_check = item.quality_checks[-1] if latest_check["result"] != "pass": return False, "最近质量检查未通过" if len(item.transfer_history) == 0 and item.current_owner != item.origin: return False, "转移历史不完整" return True, "物品验证通过"
def demonstrate_supply_chain(): """演示供应链管理""" print("=== 供应链管理演示 ===") supply_chain = SupplyChainContract("SC001", "有机食品供应链") supply_chain.activate() print("\n1. 注册供应链参与者") participants = [ ("farm001", "绿色农场", "producer", "山东寿光"), ("dist001", "新鲜配送", "distributor", "北京朝阳"), ("store001", "有机超市", "retailer", "北京海淀"), ("qc001", "质检机构", "inspector", "北京"), ] for pid, name, role, location in participants: participant = supply_chain.register_participant(pid, name, role, location) print(f"注册: {name} - {role}") print("\n2. 创建有机蔬菜") success, item = supply_chain.create_item( "farm001", "tomato001", "有机西红柿", "蔬菜" ) print(f"创建结果: {success}") print("\n3. 农场质量检查") success, check = supply_chain.add_quality_check( "tomato001", "qc001", "pass", "符合有机标准" ) print(f"质检结果: {check['result'] if success else '失败'}") print("\n4. 转移到配送中心") success, transfer = supply_chain.transfer_item( "tomato001", "farm001", "dist001", "配送中心", "冷链运输" ) print(f"转移结果: {success}") print("\n5. 配送中心质量检查") success, check = supply_chain.add_quality_check( "tomato001", "qc001", "pass", "运输过程保持新鲜" ) print(f"质检结果: {check['result'] if success else '失败'}") print("\n6. 转移到零售店") success, transfer = supply_chain.transfer_item( "tomato001", "dist001", "store001", "有机超市", "上架销售" ) print(f"转移结果: {success}") print("\n7. 追溯物品历史") history = supply_chain.trace_item_history("tomato001") if history: print(f"物品: {history['item_info']['name']}") print(f"原产地: {history['item_info']['origin']}") print(f"当前位置: {history['item_info']['current_location']}") print(f"转移次数: {len(history['transfer_history'])}") print(f"质检次数: {len(history['quality_checks'])}") print("\n转移历史:") for i, transfer in enumerate(history['transfer_history']): print(f" {i+1}. {transfer['from']} -> {transfer['to']} ({transfer['location']})") print("\n8. 验证物品真实性") is_authentic, message = supply_chain.verify_authenticity("tomato001") print(f"验证结果: {message}")
|