risk_config.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. // RiskConfig 表示风险配置表
  7. type RiskConfig struct {
  8. Id int `description:"primaryKey;autoIncrement"`
  9. RiskName string `description:"size:10"` // 风险名称
  10. RiskType RiskTypeEnum `description:"type:enum('customer','product')"` // 风险类型
  11. CreatedTime time.Time `description:"default:null"` // 创建时间
  12. UpdatedTime time.Time `description:"default:null;onUpdate:CURRENT_TIMESTAMP"` // 更新时间
  13. }
  14. type RiskConfigView struct {
  15. Id int
  16. RiskName string
  17. RiskType string
  18. }
  19. // RiskTypeEnum 定义风险类型的枚举
  20. type RiskTypeEnum string
  21. const (
  22. Customer RiskTypeEnum = "customer"
  23. Product RiskTypeEnum = "product"
  24. )
  25. func GetCustomerRiskList() (list []*RiskConfig, err error) {
  26. o := orm.NewOrm()
  27. sql := "select id, risk_name,risk_type from risk_config where risk_type= ? ORDER BY CAST(substr(risk_name, 2) AS UNSIGNED)"
  28. _, err = o.Raw(sql, Customer).QueryRows(&list)
  29. return
  30. }
  31. func GetCustomerRisk(riskName string) (risk *RiskConfig, err error) {
  32. o := orm.NewOrm()
  33. sql := "select id, risk_name,risk_type from risk_config where risk_type= ? and risk_name=?"
  34. err = o.Raw(sql, Customer, riskName).QueryRow(&risk)
  35. return
  36. }
  37. func (r *RiskConfig) ToView() *RiskConfigView {
  38. return &RiskConfigView{
  39. Id: r.Id,
  40. RiskName: r.RiskName,
  41. RiskType: string(r.RiskType),
  42. }
  43. }
  44. func GetProductRiskList() (list []*RiskConfig, err error) {
  45. o := orm.NewOrm()
  46. sql := "select id, risk_name,risk_type from risk_config where risk_type= ? ORDER BY CAST(substr(risk_name, 2) AS UNSIGNED)"
  47. _, err = o.Raw(sql, Product).QueryRows(&list)
  48. return
  49. }
  50. func InsertProductRiskLevel(riskName string) (err error) {
  51. var riskConfig RiskConfig
  52. o := orm.NewOrm()
  53. sql := "select * from risk_config where risk_name =? and risk_type=?"
  54. err = o.Raw(sql, riskName, Product).QueryRow(&riskConfig)
  55. if err != nil && err != orm.ErrNoRows {
  56. return
  57. }
  58. if err == orm.ErrNoRows || riskConfig.Id == 0 {
  59. riskConfig.RiskType = Product
  60. riskConfig.RiskName = riskName
  61. _, err = o.Insert(&riskConfig)
  62. }
  63. return
  64. }