github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/service/optiontemplatemap/option_template_drop_cause.go (about)

     1  package optiontemplatemap
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"sync"
     7  )
     8  
     9  /*
    10  _____________________________________________________________________________
    11  |                 Field                   |    ID | Ent.ID | Offset |  Size |
    12  -----------------------------------------------------------------------------
    13  | DROP CAUSE ID                           | 12442 |      9 |      0 |     2 |
    14  | drop cause name                         | 12447 |      9 |      2 |    40 |
    15  | drop cause desc                         | 12448 |      9 |     42 |    40 |
    16  -----------------------------------------------------------------------------
    17  */
    18  
    19  type DropCauseDataType struct {
    20  	Name string
    21  	//	Description string
    22  }
    23  
    24  type DropCauseMapKey struct {
    25  	AgentID     string
    26  	DropCauseID uint16
    27  }
    28  
    29  var DropCauseTable sync.Map
    30  
    31  func UpdateDropCauseDatabase(AgentID string, dropcauseid uint16, name string) error {
    32  	key := DropCauseMapKey{
    33  		AgentID:     AgentID,
    34  		DropCauseID: dropcauseid,
    35  	}
    36  	data := DropCauseDataType{
    37  		Name: name,
    38  		//	Description: Description,
    39  	}
    40  	/*
    41  		//store will cause map lock, so try to load and compare before store
    42  		olddata, _ := FetchDropCauseDataTypebaseByIndex(AgentID, dropcauseid)
    43  		if olddata != data {
    44  			DropCauseTable.Store(key, data)
    45  		}
    46  	*/
    47  	DropCauseTable.Store(key, data)
    48  
    49  	return nil
    50  }
    51  
    52  func FetchDropCauseDataTypebaseByIndex(AgentID string, dropcauseid uint16) (DropCauseDataType, error) {
    53  	var err error
    54  	var i DropCauseDataType
    55  	key := DropCauseMapKey{
    56  		AgentID:     AgentID,
    57  		DropCauseID: dropcauseid,
    58  	}
    59  
    60  	value, ok := DropCauseTable.Load(key)
    61  	if ok {
    62  		i, valid := value.(DropCauseDataType)
    63  		if !valid {
    64  			err = errors.New("invalid type assertion")
    65  			return i, err
    66  		} else {
    67  			return i, nil
    68  		}
    69  	} else {
    70  		err = errors.New("DropCause does not found in database")
    71  		return i, err
    72  	}
    73  }
    74  
    75  func ShowDropCauseDataTypebase() {
    76  	DropCauseTable.Range(func(k, v interface{}) bool {
    77  		value := v.(DropCauseDataType)
    78  		key := k.(DropCauseMapKey)
    79  		fmt.Printf("DropCause Map[%10s:%-6d] | Name: %-20s | Description: %-50s \n", key.AgentID, key.DropCauseID, value.Name, value.Name)
    80  		return true
    81  	})
    82  }
    83  
    84  func UpdateDropCauseMap(d map[string]interface{}, AgentID string) error {
    85  	var err error
    86  	dropID, valid := d["drop_cause_id"]
    87  	if valid {
    88  		//isTemplate ?
    89  		dropName, hasNameShort := d["drop_cause_name"]
    90  		//dropDescription, hasNameLong := d["drop_cause_desc"]
    91  		if hasNameShort {
    92  			err = UpdateDropCauseDatabase(AgentID, dropID.(uint16), dropName.(string))
    93  		} else {
    94  			//data record contains DropCause field
    95  			r, err := FetchDropCauseDataTypebaseByIndex(AgentID, dropID.(uint16))
    96  			if err == nil {
    97  				d["drop_cause"] = r.Name
    98  			}
    99  		}
   100  	}
   101  	return err
   102  }