// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13
library SafeMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0
}
uint256 c = a * b
assert(c / a == b)
return c
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0)
uint256 c = a / b
// assert(a == b * c + a % b)
return c
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a)
return a - b
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b
assert(c >= a)
return c
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13
pragma experimental ABIEncoderV2
import "./SafeMath.sol"
contract ProductManager {
using SafeMath for uint256
uint256 productNum
event NewProduct(
string name,
uint8 productMode,
uint8 productType,
uint16 price,
uint16 weight,
uint16 batchNum,
uint256 productNum,
uint256 productionOfDate
)
struct Product {
uint8 productMode
uint8 productType
uint16 price
uint16 weight
uint16 batchNum
uint256 productNum
uint256 productionOfDate
string name
string photoURL
bool isUsed
}
mapping (uint256 => Product) product
mapping (uint16 => uint256[]) getbatchNumProduct
//构造函数
constructor() {
productNum=0
}
//添加商品数据上链
function addProduct(uint8 _productMode ,uint8 _productType, string memory _name, uint16 _batchNum, uint16 _price, uint16 _weight, uint _productionOfDate,
string memory _photoURL) public {
productNum = productNum.add(1)
Product memory product1 = Product({
productMode: _productMode,
productType: _productType,
price: _price,
weight: _weight,
batchNum: _batchNum,
productNum: productNum,
productionOfDate: _productionOfDate,
name: _name,
photoURL: _photoURL,
isUsed: true
})
product[productNum] = product1
getbatchNumProduct[_batchNum].push(product1.productNum)
emit NewProduct(_name, _productMode, _productType, _price, _weight, _batchNum, productNum, _productionOfDate)
}
//获取商品溯源信息根据溯源码编号
function getProductData(uint256 _productId) public view returns(Product memory) {
require(_productId>0,"The parameter must be greater than 0!")
Product memory pro = product[_productId]
return pro
}
//根据批次编号获取所有的商品上链的编码数据
function getProductBybatchNum(uint16 _batchNum) public view returns (uint256[] memory) {
return getbatchNumProduct[_batchNum]
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13
pragma experimental ABIEncoderV2
contract SellerManager {
event NewSeller(
address owner,
uint8 sellerType,
uint16 sellerNum,
uint256 productNum,
string sellerName,
string sellerPhone
)
struct Seller { //销售商结构体数据
address owner
uint8 sellerType
uint16 sellerNum
uint256 productNum
string sellerName
string sellerPhone
}
mapping (uint256 => Seller[]) sellerDatas
mapping (uint16 => uint256[]) getSellerProduct
//增加零售商数据上链
function addSeller (uint8 _sellerType, uint16 _sellerNum, uint256 _productNum, string memory _sellerName,string memory _sellerPhone) public {
Seller memory sel = Seller({
owner: msg.sender,
sellerType: _sellerType,
sellerNum: _sellerNum,
productNum: _productNum,
sellerName: _sellerName,
sellerPhone: _sellerPhone
})
sellerDatas[_productNum].push(sel)
getSellerProduct[_sellerNum].push(sel.productNum)
emit NewSeller(msg.sender, _sellerType, _sellerNum,_productNum, _sellerName, _sellerPhone)
}
//根据商品的编号获取零售商数据
function getSeller(uint256 _productNum) public view returns(Seller[] memory) {
require(_productNum>0,"The parameter must be greater than 0!")
Seller[] memory sel = sellerDatas[_productNum]
return sel
}
//根据销售商编号获取所有的商品上链的编码数据
function getProductBySeller(uint16 _sellerNum) public view returns (uint256[] memory) {
return getSellerProduct[_sellerNum]
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13
pragma experimental ABIEncoderV2
contract LogisticsManager {
enum State { Provider, Seller}
event NewLogistics(
address owner,
uint8 transportationMode,
uint256 productNum,
string logisticsPeopleName,
string logisticsPhone,
string trafficToolsNum
)
struct Logistics {
address owner
uint8 transportationMode
uint16 logisticsNum
uint256 productNum
uint256 date
uint256 endOfDate
string trafficToolsNum
string origin
string destination
string logisticsPeopleName
string logisticsPhone
State state
}
mapping (uint256 => Logistics[]) logisticsDatas
mapping (uint16 => uint256[]) getLogisticsProduct
//增加物流中转运输信息
function addLogistics(uint8 _transportationMode, uint16 _logisticsNum, uint256 _productNum, string memory _trafficToolsNum,string memory _origin,
string memory _destination, uint256 _date, uint256 _endOfDate,string memory _logisticsPeopleName,string memory _logisticsPhone, State _state) public {
Logistics memory log= Logistics({
owner: msg.sender,
transportationMode: _transportationMode,
logisticsNum: _logisticsNum,
productNum: _productNum,
date: _date,
endOfDate: _endOfDate,
trafficToolsNum: _trafficToolsNum,
origin: _origin,
destination: _destination,
logisticsPeopleName: _logisticsPeopleName,
logisticsPhone: _logisticsPhone,
state: _state
})
logisticsDatas[_productNum].push(log)
getLogisticsProduct[_logisticsNum].push(log.productNum)
emit NewLogistics(msg.sender, _transportationMode, _productNum, _logisticsPeopleName, _logisticsPhone, _trafficToolsNum)
}
//通过商品的编号获得物流相关信息
function getLogistics(uint256 _productNum) public view returns(Logistics[] memory) {
require(_productNum>0,"The parameter must be greater than 0!")
Logistics[] memory node = logisticsDatas[_productNum]
return node
}
//根据物流商编号获取所有的商品上链的编码数据
function getProductByLogistics(uint16 _logisticsNum) public view returns (uint256[] memory) {
return getLogisticsProduct[_logisticsNum]
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13
pragma experimental ABIEncoderV2
contract ProviderManager{
event NewProvider(
address owner,
uint16 provideNum,
string ownerName,
string phoneNum,
uint256 productNum
)
struct Provider { //生产商结构体数据
address owner
uint16 providerNum
uint256 productNum
string ownerName
string phoneNum
bool test
}
mapping (uint256 => Provider) provider
mapping (uint16 => uint256[]) getProviderProduct
//增加商品生产商数据上链
function addProvider (uint16 _providerNum, uint256 _productNum, string memory _ownerName,string memory _phoneNum) public {
Provider memory pro = Provider({
owner: msg.sender,
providerNum: _providerNum,
productNum: _productNum,
ownerName: _ownerName,
phoneNum: _phoneNum,
test: true
})
provider[_productNum] = pro
getProviderProduct[_providerNum].push(pro.productNum)
emit NewProvider(msg.sender, _providerNum, _ownerName, _phoneNum, _productNum)
}
//根据商品的编号获取提供商信息
function getProvider(uint256 _productNum) public view returns(Provider memory) {
require(_productNum>0,"The parameter must be greater than 0!")
Provider memory pro = provider[_productNum]
return pro
}
//根据生产商编号获取所有的商品上链的编码数据
function getProductByProvider(uint16 _providerNum) public view returns (uint256[] memory) {
return getProviderProduct[_providerNum]
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13
import "./ProductManager.sol"
import "./SellerManager.sol"
import "./LogisticsManager.sol"
import "./ProviderManager.sol"
contract trace is ProductManager, SellerManager,LogisticsManager, ProviderManager {
address private owner
ProductManager productManager
ProviderManager providerManager
LogisticsManager logisticsManager
SellerManager sellerManager
modifier onlyOwner() {
require(msg.sender == owner)
_
}
//构造函数
constructor(address product, address provider,address logistics,address seller) {
owner = msg.sender
productManager = ProductManager(product)
providerManager = ProviderManager(provider)
logisticsManager = LogisticsManager(logistics)
sellerManager = SellerManager(seller)
}
//只有合约所有者才有权限调用这个函数
function changeAddress(address product, address provider,address logistics,address seller) public onlyOwner {
productManager = ProductManager(product)
providerManager = ProviderManager(provider)
logisticsManager = LogisticsManager(logistics)
sellerManager = SellerManager(seller)
}
//获取商品溯源信息根据溯源码编号
function getProductTraceInfor(uint256 _productId) public view returns (Provider memory, Product memory, Logistics[] memory , Seller[]memory) {
return (providerManager.getProvider(_productId), productManager.getProductData(_productId),logisticsManager.getLogistics(_productId),sellerManager.getSeller(_productId))
}
}