//pragma solidity >=0.6.10 <0.8.20;
pragma solidity ^0.4.24;
pragma experimental ABIEncoderV2;

import "../Table.sol";
import "./Provider.sol";
import "./Utils.sol";

//证书审核记录
contract CertRecord {
    event CreateResult(int256 count);
    event InsertResult(int256 count);
    event UpdateResult(int256 count);
    event RemoveResult(int256 count);
    
    
    TableFactory tableFactory = TableFactory(0x1001); 
    Utils utils = new Utils();
    Provider provider = new Provider();
    string constant TABLE_SUFFIX = "_cert_record";
    string constant KEY_NAME = "key";
    string constant KEY_VALUE = "key";
    
    struct CertRecordData{
        string  id;
        string  cert_id;
        string  user_id;
        string  org_id;
        string  audit_status;
        string  json_data;
        int256  create_time;
    }
    
    //根据id查询记录
    function selectById(string memory table_prefix,string memory id)
    public
    view
    returns (CertRecordData[]){
         //确定表格名称
        string memory table_name = string(abi.encodePacked(table_prefix, TABLE_SUFFIX));
        Table table = tableFactory.openTable(table_name);

        Condition condition = table.newCondition();
        condition.EQ("id", id);
        
        Entries entries = table.select(KEY_VALUE, condition);
         uint256  dataLen =   uint256(entries.size());
        CertRecordData[] memory recordDatas =  new CertRecordData[](dataLen);
        
        for (uint256 i = 0; i < dataLen; ++i) {
            Entry entry = entries.get(int256(i));
            recordDatas[i]=CertRecordData(entry.getString("id"),entry.getString("cert_id"),entry.getString("user_id"),entry.getString("org_id"),entry.getString("audit_status"),entry.getString("json_data"),entry.getInt("create_time"));
        }

        return (recordDatas);
    }
    
       //根据userid查询记录
    function selectByUserId(string memory table_prefix,string memory user_id)
    public
    view
    returns (CertRecordData[]){
         //确定表格名称
        string memory table_name = string(abi.encodePacked(table_prefix, TABLE_SUFFIX));
        Table table = tableFactory.openTable(table_name);

        Condition condition = table.newCondition();
        condition.EQ("user_id", user_id);
        
        Entries entries = table.select(KEY_VALUE, condition);
         uint256  dataLen =   uint256(entries.size());
        CertRecordData[] memory recordDatas =  new CertRecordData[](dataLen);
        
        for (uint256 i = 0; i < dataLen; ++i) {
            Entry entry = entries.get(int256(i));
            recordDatas[i]=CertRecordData(entry.getString("id"),entry.getString("cert_id"),entry.getString("user_id"),entry.getString("org_id"),entry.getString("audit_status"),entry.getString("json_data"),entry.getInt("create_time"));
        }

        return (recordDatas);
    }
    
       //根据userid查询记录
    function selectByCertId(string memory table_prefix,string memory cert_id)
    public
    view
    returns (CertRecordData[]){
         //确定表格名称
        string memory table_name = string(abi.encodePacked(table_prefix, TABLE_SUFFIX));
        Table table = tableFactory.openTable(table_name);

        Condition condition = table.newCondition();
        condition.EQ("cert_id", cert_id);
        
        Entries entries = table.select(KEY_VALUE, condition);
         uint256  dataLen =   uint256(entries.size());
        CertRecordData[] memory recordDatas =  new CertRecordData[](dataLen);
        
        for (uint256 i = 0; i < dataLen; ++i) {
            Entry entry = entries.get(int256(i));
            recordDatas[i]=CertRecordData(entry.getString("id"),entry.getString("cert_id"),entry.getString("user_id"),entry.getString("org_id"),entry.getString("audit_status"),entry.getString("json_data"),entry.getInt("create_time"));
        }

        return (recordDatas);
    }
    
    //根据orgid查询记录
    function selectByOrgId(string memory table_prefix,string memory org_id)
    public
    view
    returns (CertRecordData[]){
         //确定表格名称
        string memory table_name = string(abi.encodePacked(table_prefix, TABLE_SUFFIX));
        Table table = tableFactory.openTable(table_name);

        Condition condition = table.newCondition();
        condition.EQ("org_id", org_id);
        
        Entries entries = table.select(KEY_VALUE, condition);
         uint256  dataLen =   uint256(entries.size());
        CertRecordData[] memory recordDatas =  new CertRecordData[](dataLen);
        
        for (uint256 i = 0; i < dataLen; ++i) {
            Entry entry = entries.get(int256(i));
            recordDatas[i]=CertRecordData(entry.getString("id"),entry.getString("cert_id"),entry.getString("user_id"),entry.getString("org_id"),entry.getString("audit_status"),entry.getString("json_data"),entry.getInt("create_time"));
        }

        return (recordDatas);
    }
    
    //添加证书审核记录
    function insert(string memory id, string memory cert_id,string memory user_id,string memory org_id,string memory audit_status,string memory json_data,int256 create_time)
    public 
    returns(int256,string memory){
        //表格前缀
        string memory table_prefix = provider.selectPrefixByAddr(utils.toString(tx.origin));
        if(utils.strIsEmpty(table_prefix)){
            return (0,"table_prefix not find");
        }
        
        //确定表格名称
        string memory table_name = string(abi.encodePacked(table_prefix, TABLE_SUFFIX));
        Table table = tableFactory.openTable(table_name);
         //id重复校验
        if(existById(id,table)){
            return (0,"id exists,insert failed");
        }
        Entry entry = table.newEntry();
        entry.set(KEY_NAME, KEY_VALUE);
        entry.set("id", id);
        entry.set("cert_id", cert_id);
        entry.set("user_id", user_id);
        entry.set("org_id", org_id);
        entry.set("audit_status", audit_status);
        entry.set("json_data", json_data);
        entry.set("create_time", create_time);
        

        int256 count = table.insert(KEY_VALUE, entry);
        if(count>0){
            emit InsertResult(count);
        }
        return (count,"success");
        
    }
    
    //根据id判定是否存在
    function existById(string memory id,Table table)
    private
    view
    returns (bool){
        Condition condition = table.newCondition();
        condition.EQ("id", id);
        Entries entries = table.select(KEY_VALUE, condition);
        return entries.size()>0;
    }
    
}