溯源智能合约 Solidity

// 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); // Solidity automatically throws when dividing by 0
    uint256 c = a / b;
    // assert(a == b * c + a % b); // There is no case in which this doesn't hold
    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));
    }
}









  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想买CT5的小曹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值