check_sysmodel_ied_fcda_relation.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package bo
  2. import (
  3. "fmt"
  4. "scd_check_tools/logger"
  5. "scd_check_tools/models/enum"
  6. "scd_check_tools/tools"
  7. "strings"
  8. "github.com/astaxie/beego/orm"
  9. )
  10. //系统内置模型-装置功能点端子关系管理
  11. type T_data_model_fcda_ref struct {
  12. Id int `orm:"pk"`
  13. ModelId int // '模型ID' ,
  14. RelationRef int //
  15. FromIedCode string //
  16. ToIedCode string //
  17. FromFuncId int
  18. FromFcdaId int
  19. ToFuncId int
  20. ToFcdaId int
  21. Goosesv string
  22. Cr int // '创建人' ,
  23. Ct string `orm:"-"` // '创建时间' ,
  24. Ur int // '更新人' ,
  25. Ut string `orm:"-"` // '更新时间'
  26. }
  27. //内置检测模型-装置功能点端子管理
  28. type SysCheckModelFcdaRalationMgr struct {
  29. Model T_data_model_fcda_ref
  30. DeviceBaseModel
  31. }
  32. var sysCheckModel_iedFcdaRelationDesc = "内置检测模型-装置功能点端子关系管理"
  33. func init() {
  34. orm.RegisterModel(new(T_data_model_fcda_ref))
  35. }
  36. //保存检测模型装置功能信息
  37. func (c *SysCheckModelFcdaRalationMgr) Save() (err error) {
  38. dblog := new(SystemLog)
  39. dblog.SetUserInfo(c.GetUserInfo())
  40. dblog.Audittype = enum.AuditType_check_model
  41. dblog.Logtype = enum.LogType_Insert
  42. dblog.Eventtype = enum.OptEventType_Bus
  43. dblog.Eventlevel = enum.OptEventLevel_Hight
  44. db := orm.NewOrm()
  45. if c.Model.Id > 0 {
  46. //编辑
  47. _, err = db.Update(&c.Model)
  48. } else {
  49. //新增
  50. _, err = db.Insert(&c.Model)
  51. }
  52. if err != nil {
  53. logger.Logger.Error(err)
  54. dblog.Description = fmt.Sprintf("保存%s信息失败:%s,操作数据:%+v", sysCheckModel_iedFcdaRelationDesc, err.Error(), c.Model)
  55. dblog.Fail2()
  56. } else {
  57. dblog.Description = fmt.Sprintf("保存%s信息成功,操作数据:%+v", sysCheckModel_iedFcdaRelationDesc, c.Model)
  58. dblog.Success2()
  59. }
  60. return err
  61. }
  62. //根据model中指定的id删除
  63. func (c *SysCheckModelFcdaRalationMgr) Delete() (err error) {
  64. dblog := new(SystemLog)
  65. dblog.SetUserInfo(c.GetUserInfo())
  66. dblog.Audittype = enum.AuditType_check_model
  67. dblog.Logtype = enum.LogType_Delete
  68. dblog.Eventtype = enum.OptEventType_Bus
  69. dblog.Eventlevel = enum.OptEventLevel_Hight
  70. db := orm.NewOrm()
  71. if c.Model.FromFcdaId > 0 && c.Model.ToFcdaId > 0 {
  72. _, err = db.Raw("delete from t_data_model_fcda_ref where model_id=? and from_fcda_id=? and to_fcda_id=?", c.Model.ModelId, c.Model.FromFcdaId, c.Model.ToFcdaId).Exec()
  73. } else if c.Model.FromFcdaId > 0 {
  74. _, err = db.Raw("delete from t_data_model_fcda_ref where model_id=? and from_fcda_id=? and to_ied_code=? and goosesv=?", c.Model.ModelId, c.Model.FromFcdaId, c.Model.ToIedCode, c.Model.Goosesv).Exec()
  75. } else if c.Model.ToFcdaId > 0 {
  76. _, err = db.Raw("delete from t_data_model_fcda_ref where model_id=? and to_fcda_id=? and to_ied_code=?", c.Model.ModelId, c.Model.ToFcdaId, c.Model.FromIedCode).Exec()
  77. } else if c.Model.FromFuncId > 0 {
  78. _, err = db.Raw("delete from t_data_model_fcda_ref where model_id=? and from_func_id=?", c.Model.ModelId, c.Model.FromFuncId).Exec()
  79. } else if c.Model.FromIedCode != "" {
  80. _, err = db.Raw("delete from t_data_model_fcda_ref where model_id=? and (from_ied_code=? or to_ied_code=?)", c.Model.ModelId, c.Model.FromIedCode, c.Model.FromIedCode).Exec()
  81. } else if c.Model.ToIedCode != "" {
  82. _, err = db.Raw("delete from t_data_model_fcda_ref where model_id=? and (from_ied_code=? or to_ied_code=?)", c.Model.ModelId, c.Model.ToIedCode, c.Model.ToIedCode).Exec()
  83. } else if c.Model.FromFuncId > 0 {
  84. _, err = db.Raw("delete from t_data_model_fcda_ref where model_id=? and (from_func_id=? or to_func_id=?)", c.Model.ModelId, c.Model.FromFuncId, c.Model.FromFuncId).Exec()
  85. } else if c.Model.ToFuncId > 0 {
  86. _, err = db.Raw("delete from t_data_model_fcda_ref where model_id=? and (from_func_id=? or to_func_id=?)", c.Model.ModelId, c.Model.ToFuncId, c.Model.ToFuncId).Exec()
  87. } else {
  88. _, err = db.Raw("delete from t_data_model_fcda_ref where model_id=? ", c.Model.ModelId).Exec()
  89. }
  90. if err != nil {
  91. logger.Logger.Error(err)
  92. dblog.Description = fmt.Sprintf("删除%s(%d)失败:%s", sysCheckModel_iedFcdaRelationDesc, c.Model.ModelId, err.Error())
  93. dblog.Fail2()
  94. } else {
  95. dblog.Description = fmt.Sprintf("删除%s(%d)成功", sysCheckModel_iedFcdaRelationDesc, c.Model.ModelId)
  96. dblog.Success2()
  97. }
  98. return err
  99. }
  100. func (c *SysCheckModelFcdaRalationMgr) GetList() ([]orm.Params, error) {
  101. o := orm.NewOrm()
  102. sqlParamters := []interface{}{c.Model.ModelId, c.Model.FromFcdaId}
  103. sql := "select t.* from t_data_model_fcda_ref t where t.model_id=? and t.from_fcda_id=?"
  104. if c.Model.ToIedCode != "" {
  105. sql = sql + " and t.to_ied_code=?"
  106. sqlParamters = append(sqlParamters, c.Model.ToIedCode)
  107. }
  108. if c.Model.Goosesv != "" {
  109. sql = sql + " and t.goosesv=?"
  110. sqlParamters = append(sqlParamters, c.Model.Goosesv)
  111. }
  112. rowset := []orm.Params{}
  113. _, err := o.Raw(sql, sqlParamters).Values(&rowset)
  114. if err != nil {
  115. logger.Logger.Error(err)
  116. }
  117. return rowset, err
  118. }
  119. func (c *SysCheckModelFcdaRalationMgr) GetModelAllFcdaRef() (map[string][]orm.Params, error) {
  120. //获取当前模型的装置类型关联
  121. refmgr := new(SysCheckModelIedRelationMgr)
  122. refLst, _ := refmgr.GetListByModelid(c.Model.ModelId)
  123. if refLst == nil || len(refLst) == 0 {
  124. return map[string][]orm.Params{}, nil
  125. }
  126. refMap := map[string]string{}
  127. for _, r := range refLst {
  128. key := fmt.Sprintf("%s,%s", tools.IsEmpty(r["to_ied_code"]), tools.IsEmpty(r["from_ied_code"]))
  129. refMap[key] = tools.IsEmpty(r["in_type"])
  130. }
  131. db := orm.NewOrm()
  132. sql := "select r1.*,(select func_name from t_data_model_func_def where id=f1.func_id) from_func_name,(select func_name from t_data_model_func_def where id=f2.func_id) to_func_name,f1.fcda_name from_fcda_name,f1.fcda_match_exp from_fcda_match_exp,f2.fcda_name to_fcda_name,f2.fcda_match_exp to_fcda_match_exp from t_data_model_fcda_ref r1 left join t_data_model_func_fcda f1 on r1.from_fcda_id=f1.id left join t_data_model_func_fcda f2 on r1.to_fcda_id=f2.id where r1.model_id=? "
  133. rowset := []orm.Params{}
  134. sqlParamters := []interface{}{c.Model.ModelId}
  135. _, err := db.Raw(sql, sqlParamters).Values(&rowset)
  136. if err != nil {
  137. logger.Logger.Error(err)
  138. return nil, err
  139. }
  140. //获取装置分组信息
  141. bgm := new(SysCheckModelIedtypeGroupMgr)
  142. bgm.Model = T_data_model_iedtype_group{ModelId: c.Model.ModelId}
  143. groupList := bgm.List()
  144. result := map[string][]orm.Params{}
  145. for _, row := range rowset {
  146. tc := tools.IsEmpty(row["to_ied_code"])
  147. fc := tools.IsEmpty(row["from_ied_code"])
  148. goosesv := tools.IsEmpty(row["goosesv"])
  149. if v, h := groupList[tc]; h {
  150. tc = v
  151. }
  152. if v, h := groupList[fc]; h {
  153. fc = v
  154. }
  155. key := fmt.Sprintf("%s,%s", tc, fc)
  156. if v, h := refMap[key]; !h || v != goosesv {
  157. //装置类型之间没有建立连接关系
  158. continue
  159. }
  160. extref_name_exp := tools.IsEmpty(row["to_fcda_match_exp"])
  161. if extref_name_exp == "" {
  162. extref_name_exp = tools.IsEmpty(row["to_fcda_name"])
  163. }
  164. fcda_name_exp := tools.IsEmpty(row["from_fcda_match_exp"])
  165. if fcda_name_exp == "" {
  166. fcda_name_exp = tools.IsEmpty(row["from_fcda_name"])
  167. }
  168. t1 := []string{extref_name_exp, fcda_name_exp}
  169. //中文字符替换
  170. for i, exp := range t1 {
  171. if strings.Index(exp, "/") > -1 {
  172. exp = strings.ReplaceAll(exp, "/", "|")
  173. }
  174. if strings.Index(exp, "-") > -1 {
  175. exp = strings.ReplaceAll(exp, "-", "\\-")
  176. }
  177. if strings.Index(exp, "|") > -1 {
  178. exp = strings.ReplaceAll(exp, "|", "|")
  179. }
  180. if strings.Index(exp, ",") > -1 {
  181. exp = strings.ReplaceAll(exp, ",", ",")
  182. }
  183. if strings.Index(exp, "(") > -1 {
  184. exp = strings.ReplaceAll(exp, "(", "(")
  185. }
  186. if strings.Index(exp, ")") > -1 {
  187. exp = strings.ReplaceAll(exp, ")", ")")
  188. }
  189. if strings.Index(exp, "【") > -1 {
  190. exp = strings.ReplaceAll(exp, "【", "[")
  191. }
  192. if strings.Index(exp, "】") > -1 {
  193. exp = strings.ReplaceAll(exp, "】", "]")
  194. }
  195. if strings.Index(exp, "{") > -1 {
  196. exp = strings.ReplaceAll(exp, "{", "{")
  197. }
  198. if strings.Index(exp, "}") > -1 {
  199. exp = strings.ReplaceAll(exp, "}", "}")
  200. }
  201. t1[i] = exp
  202. }
  203. row["to_fcda_match_exp"] = t1[0]
  204. row["from_fcda_match_exp"] = t1[1]
  205. if result[key] == nil {
  206. result[key] = []orm.Params{}
  207. }
  208. result[key] = append(result[key], row)
  209. }
  210. //logger.Logger.Debug(fmt.Sprintf("=====模型%d所有的端子关联关系=====\n%+v", c.Model.ModelId, result))
  211. return result, err
  212. }