checktools_area.go 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  1. package bo
  2. import (
  3. "errors"
  4. "fmt"
  5. "scd_check_tools/logger"
  6. "scd_check_tools/models/enum"
  7. "scd_check_tools/models/node_attr"
  8. "scd_check_tools/tools"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "github.com/astaxie/beego/orm"
  13. )
  14. //检测时间隔管理
  15. type CheckAreaMgr struct {
  16. DeviceBaseModel
  17. //SCD文件ID
  18. ScdId int64
  19. //电压等级定义
  20. VoltageLevelDef map[string]int
  21. //设备类型定义
  22. DeviceTypeDef map[string]int
  23. CacheAreaID map[string]int64
  24. CacheAreaIDByIedNameNo map[string]int
  25. //检测模型列表
  26. CheckModelList []orm.Params
  27. //模型内装置关系定义
  28. CheckModelDef sync.Map
  29. CacheLock sync.RWMutex
  30. }
  31. type T_data_check_area struct {
  32. Id int64 `orm:"pk"`
  33. AreaName string
  34. ScdId int64
  35. VoltageLevel int
  36. AreaType string
  37. ModelId int
  38. Cr int // '创建人' ,
  39. Ct string `orm:"-"` // '创建时间' ,
  40. Ur int // '更新人' ,
  41. Ut string `orm:"-"` // '更新时间'
  42. }
  43. type t_data_check_area_ied struct {
  44. Id int64 `orm:"pk"`
  45. AreaId int64
  46. IedName string
  47. IedType string
  48. ScdId int64
  49. PType string
  50. Cr int // '创建人' ,
  51. Ct string `orm:"-"` // '创建时间' ,
  52. Ur int // '更新人' ,
  53. Ut string `orm:"-"` // '更新时间'
  54. }
  55. func init() {
  56. orm.RegisterModel(new(T_data_check_area))
  57. orm.RegisterModel(new(t_data_check_area_ied))
  58. }
  59. func (c *CheckAreaMgr) Init(scdid int64) {
  60. c.VoltageLevelDef = map[string]int{}
  61. c.DeviceTypeDef = map[string]int{}
  62. c.CacheAreaIDByIedNameNo = map[string]int{}
  63. c.CacheAreaID = map[string]int64{}
  64. c.CacheLock = sync.RWMutex{}
  65. c.ScdId = scdid
  66. db := orm.NewOrm()
  67. rowset := []orm.Params{}
  68. db.Raw("select * from global_const_code where parentcode=?", "voltage_level").Values(&rowset)
  69. for _, row := range rowset {
  70. vl := strings.ToLower(tools.IsEmpty(row["code"]))
  71. id, _ := strconv.ParseInt(tools.IsEmpty(row["id"]), 10, 32)
  72. c.VoltageLevelDef[strings.ReplaceAll(vl, "v_level_", "")] = int(id)
  73. }
  74. db.Raw("select * from global_const_code where parentcode=?", "device_type").Values(&rowset)
  75. for _, row := range rowset {
  76. vl := tools.IsEmpty(row["code"])
  77. c.DeviceTypeDef[vl] = 1
  78. }
  79. v, _ := GetSysParamValue("OtherIedNameList", "")
  80. otherIedNameList = map[string]bool{}
  81. if v != "" {
  82. vs := strings.Split(v, ",")
  83. for _, vv := range vs {
  84. otherIedNameList[vv] = true
  85. }
  86. }
  87. //先清除ied与间隔关联关系
  88. clearSql := "delete from t_data_check_area_ied where area_id in(select id from t_data_check_area where scd_id=?)"
  89. _, err := db.Raw(clearSql, c.ScdId).Exec()
  90. if err != nil {
  91. logger.Logger.Error(err, fmt.Sprintf("SQL:%s 参数:%+v", clearSql, []interface{}{c.ScdId}))
  92. }
  93. clearSql = "delete from t_data_check_area where scd_id=?"
  94. _, err = db.Raw(clearSql, c.ScdId).Exec()
  95. if err != nil {
  96. logger.Logger.Error(err, fmt.Sprintf("SQL:%s 参数:%+v", clearSql, []interface{}{c.ScdId}))
  97. }
  98. //获取当前scd信息中获取到对应的电压等级id
  99. /*
  100. scdMgr := new(ScdMgr)
  101. scdinfo, _ := scdMgr.One(fmt.Sprintf("%d", c.ScdId))
  102. stationid := tools.IsEmpty(scdinfo["station_id"])
  103. volid := 0
  104. if stationid != "" {
  105. basearea := new(BasicArea)
  106. stationonof, _ := basearea.One(stationid)
  107. volid = stationonof.AreaLevel
  108. }
  109. */
  110. tmplist, _ := new(SysCheckModelMgr).GetModelsByVolid(2)
  111. scdModels, _, _ := new(TaskMgr).GetModelsByScdID(c.ScdId)
  112. ms := map[string]interface{}{}
  113. for _, row := range scdModels {
  114. ms[tools.IsEmpty(row["model_id"])] = row
  115. }
  116. for _, row := range tmplist {
  117. modelid := tools.IsEmpty(row["id"])
  118. if ms[modelid] != nil {
  119. c.CheckModelList = append(c.CheckModelList, row)
  120. }
  121. }
  122. logger.Logger.Debug(fmt.Sprintf("任务的模型列表:%+v", c.CheckModelList))
  123. ms = nil
  124. tmplist = nil
  125. }
  126. //保存指定间隔所属的电压等级
  127. func (c *CheckAreaMgr) SetVoltageLevel(id string, voltagelevel int) error {
  128. db := orm.NewOrm()
  129. _, err := db.Raw("update t_data_check_area set voltage_level=? where id=?", voltagelevel, id).Exec()
  130. return err
  131. }
  132. //修改指定间隔的名称
  133. func (c *CheckAreaMgr) UpdateName(scdid int64, area_id int, name string) error {
  134. db := orm.NewOrm()
  135. areaM := T_data_check_area{Id: int64(area_id), ScdId: scdid}
  136. err := db.Read(&areaM)
  137. if err != nil {
  138. logger.Logger.Error(err)
  139. return err
  140. }
  141. areaM.AreaName = name
  142. _, err = db.Update(&areaM)
  143. if err != nil {
  144. logger.Logger.Error(err)
  145. }
  146. return err
  147. }
  148. //修改指定IED的所属间隔
  149. func (c *CheckAreaMgr) UpdateIedArea(scdid int64, iedname string, area_id int) error {
  150. db := orm.NewOrm()
  151. _, err := db.Raw("update t_data_check_area_ied set area_id=? where scd_id=? and ied_name=?", area_id, scdid, iedname).Exec()
  152. return err
  153. }
  154. //获取指定scd的间隔信息
  155. func (c *CheckAreaMgr) GetAreaList(scdid int64) ([]orm.Params, error) {
  156. db := orm.NewOrm()
  157. sql := "select t.*,(select count(1) from t_data_check_area_ied where area_id=t.id) iedcount from t_data_check_area t where t.scd_id=? order by t.name"
  158. rowset := []orm.Params{}
  159. _, err := db.Raw(sql, scdid).Values(&rowset)
  160. sqllog := fmt.Sprintf("SQL:%s 参数:%+v", sql, []interface{}{scdid})
  161. if err != nil {
  162. logger.Logger.Error(err, sqllog)
  163. new(SystemLog).Fail(enum.AuditType_scd_show, enum.LogType_Query, enum.OptEventType_Bus, enum.OptEventLevel_Low, sqllog, c.GetUserInfo())
  164. } else {
  165. new(SystemLog).Success(enum.AuditType_scd_show, enum.LogType_Query, enum.OptEventType_Bus, enum.OptEventLevel_Low, sqllog, c.GetUserInfo())
  166. }
  167. return rowset, nil
  168. }
  169. //获取指定scd和电压等级的间隔信息
  170. func (c *CheckAreaMgr) GetAreaListByVol(scdid int64, vl, linkstyleid int) ([]orm.Params, error) {
  171. db := orm.NewOrm()
  172. sql := "select t1.id model_id, t1.model_name,t1.vol_id,g1.name vol_name,t1.line_link_style,ls.name,t.id area_id, t.area_name,t.ut,t.ur from t_data_check_area t right join t_data_model_defualt t1 on t.model_id=t1.id left join t_data_link_style ls on t1.line_link_style=ls.id left join global_const_code g1 on t1.vol_id=g1.id and g1.parentcode='voltage_level' where t.scd_id=? "
  173. params := []interface{}{scdid}
  174. if vl > 0 {
  175. sql = sql + " and t1.vol_id=? "
  176. params = append(params, vl)
  177. }
  178. if linkstyleid > 0 {
  179. sql = sql + " and t1.line_link_style=? "
  180. params = append(params, linkstyleid)
  181. }
  182. rowset := []orm.Params{}
  183. _, err := db.Raw(sql+" order by t1.vol_id,t1.line_link_style,t1.id", params).Values(&rowset)
  184. sqllog := fmt.Sprintf("SQL:%s 参数:%+v", sql, []interface{}{scdid, vl})
  185. if err != nil {
  186. logger.Logger.Error(err, sqllog)
  187. new(SystemLog).Fail(enum.AuditType_scd_show, enum.LogType_Query, enum.OptEventType_Bus, enum.OptEventLevel_Low, sqllog, c.GetUserInfo())
  188. } else {
  189. new(SystemLog).Success(enum.AuditType_scd_show, enum.LogType_Query, enum.OptEventType_Bus, enum.OptEventLevel_Low, sqllog, c.GetUserInfo())
  190. }
  191. return rowset, nil
  192. }
  193. //获取指定间隔下的IED列表
  194. func (c *CheckAreaMgr) GetIedList(scdid int64, areaid int) ([]orm.Params, error) {
  195. db := orm.NewOrm()
  196. sql := "select t1.* from t_data_check_area t,t_data_check_area_ied t1 where t.scd_id=? and t.id=t1.area_id "
  197. sqlParamters := []interface{}{}
  198. sqlParamters = append(sqlParamters, scdid)
  199. if areaid > 0 {
  200. sql = sql + " and t1.area_id=?"
  201. sqlParamters = append(sqlParamters, areaid)
  202. }
  203. scdXmlObj, serr := new(ScdParse).GetScdXmlObjectBySCDID(tools.IsEmpty(scdid))
  204. if serr != nil {
  205. return nil, serr
  206. }
  207. if scdXmlObj == nil {
  208. return nil, errors.New("无效的SCD")
  209. }
  210. rowset := []orm.Params{}
  211. _, err := db.Raw(sql, sqlParamters).Values(&rowset)
  212. sqllog := fmt.Sprintf("SQL:%s 参数:%+v", sql, sqlParamters)
  213. if err != nil {
  214. logger.Logger.Error(err, sqllog)
  215. new(SystemLog).Fail(enum.AuditType_scd_show, enum.LogType_Query, enum.OptEventType_Bus, enum.OptEventLevel_Low, sqllog, c.GetUserInfo())
  216. } else {
  217. scdNode := new(ScdNode)
  218. for i, row := range rowset {
  219. iedObj := scdNode.GetIed(scdXmlObj, tools.IsEmpty(scdid), tools.IsEmpty(row["ied_name"]))
  220. if iedObj == nil {
  221. continue
  222. }
  223. rowset[i]["attr_name"] = iedObj.Name
  224. rowset[i]["attr_desc"] = iedObj.Desc
  225. rowset[i]["attr_config_version"] = iedObj.ConfigVersion
  226. rowset[i]["attr_type"] = iedObj.Type
  227. rowset[i]["attr_manufacturer"] = iedObj.Manufacturer
  228. rowset[i]["ied_id"] = iedObj.NodeId
  229. }
  230. new(SystemLog).Success(enum.AuditType_scd_show, enum.LogType_Query, enum.OptEventType_Bus, enum.OptEventLevel_Low, sqllog, c.GetUserInfo())
  231. }
  232. return rowset, nil
  233. }
  234. //更新指定间隔下的装置定义
  235. func (c *CheckAreaMgr) UpdateIeds(scdid int64, areaid int, ieds string) error {
  236. db := orm.NewOrm()
  237. iedlist := strings.Split(ieds, ",")
  238. _, err := db.Raw("delete from t_data_check_area_ied where scd_id=? and area_id=?", scdid, areaid).Exec()
  239. if err != nil {
  240. logger.Logger.Error(err)
  241. return err
  242. }
  243. for _, row := range iedlist {
  244. _, err = db.Raw("insert into t_data_check_area_ied(scd_id,area_id,ied_name)values(?,?,?)", scdid, areaid, row).Exec()
  245. if err != nil {
  246. logger.Logger.Error(err)
  247. break
  248. }
  249. }
  250. return err
  251. }
  252. //获取指定SCD的IED类型列表
  253. func (c *CheckAreaMgr) GetIedTypeList(scdid int64) ([]orm.Params, error) {
  254. db := orm.NewOrm()
  255. sql := "select t2.* from t_data_check_area_ied t1,global_const_code t2 where t1.scd_id=? and t1.ied_type=t2.code and t2.parentcode='device_type' group by t1.ied_type "
  256. sqlParamters := []interface{}{}
  257. rowset := []orm.Params{}
  258. sqlParamters = append(sqlParamters, scdid)
  259. _, err := db.Raw(sql, sqlParamters).Values(&rowset)
  260. sqllog := fmt.Sprintf("SQL:%s 参数:%+v", sql, sqlParamters)
  261. if err != nil {
  262. logger.Logger.Error(err, sqllog)
  263. new(SystemLog).Fail(enum.AuditType_scd_show, enum.LogType_Query, enum.OptEventType_Bus, enum.OptEventLevel_Low, sqllog, c.GetUserInfo())
  264. }
  265. return rowset, nil
  266. }
  267. func (c *CheckAreaMgr) One(id string) (interface{}, error) {
  268. db := orm.NewOrm()
  269. idInt, err := strconv.ParseInt(id, 10, 64)
  270. if err != nil {
  271. return nil, err
  272. }
  273. areaM := T_data_check_area{Id: idInt}
  274. err = db.Read(&areaM)
  275. if err == nil {
  276. return areaM, nil
  277. }
  278. return nil, err
  279. }
  280. //重置scd的间隔信息
  281. func (c *CheckAreaMgr) Reset() error {
  282. dbo := orm.NewOrm()
  283. dbo.Raw("delete from t_data_check_area where scd_id=?", c.ScdId).Exec()
  284. dbo.Raw("delete from t_data_check_area_ied where scd_id=?", c.ScdId).Exec()
  285. c.ParseModelArea()
  286. logdesc := fmt.Sprintf("重置SCD[%d]检测模型间隔成功", c.ScdId)
  287. new(SystemLog).Success(enum.AuditType_check_task, enum.LogType_bind, enum.OptEventType_Bus, enum.OptEventLevel_Mid, logdesc, c.GetUserInfo())
  288. return nil
  289. }
  290. var areaCheckInfo = sync.Map{}
  291. //获取解析模型需要的基础数据信息
  292. func (c *CheckAreaMgr) getAreaCheckInfo() (*node_attr.SCL, []orm.Params, string, error) {
  293. key := fmt.Sprintf("%d-checkinfo", c.ScdId)
  294. if v, h := areaCheckInfo.Load(key); h {
  295. v1 := v.([]interface{})
  296. return v1[0].(*node_attr.SCL), v1[1].([]orm.Params), v1[2].(string), nil
  297. }
  298. arealist := []orm.Params{}
  299. db := orm.NewOrm()
  300. _, err := db.Raw("select id,area_name,area_type,model_id from t_data_check_area where scd_id=?", c.ScdId).Values(&arealist)
  301. if err != nil {
  302. logger.Logger.Error(err)
  303. return nil, nil, "", err
  304. }
  305. scdNodeRule := new(ScdNodeRule)
  306. area_ruleid := ""
  307. area_ruleList, _, _ := scdNodeRule.GetDefList(map[string]interface{}{"check_name": "间隔装置与检查模型不符"}, 1, 1)
  308. if len(area_ruleList) > 0 {
  309. area_ruleid = tools.IsEmpty(area_ruleList[0]["id"])
  310. }
  311. if area_ruleid == "" {
  312. return nil, nil, "", errors.New(fmt.Sprintf("未定义间隔装置的检查规则“间隔装置与检查模型不符”"))
  313. }
  314. scdParseMgr := new(ScdParse)
  315. scdXmlObj, serr := scdParseMgr.GetScdXmlObjectBySCDID(tools.IsEmpty(c.ScdId))
  316. if serr != nil {
  317. return nil, nil, "", serr
  318. }
  319. if scdXmlObj == nil {
  320. return nil, nil, "", errors.New("无效的SCD")
  321. }
  322. areaCheckInfo.Store(key, []interface{}{scdXmlObj, arealist, area_ruleid})
  323. return scdXmlObj, arealist, area_ruleid, nil
  324. }
  325. //检测装置端子分析:检测间隔装置关系正确性(已废弃)
  326. func (c *CheckAreaMgr) CheckAreaIedRelation() error {
  327. // 获取当前scd中需要检查的间隔
  328. scdXmlObj, arealist, area_ruleid, err := c.getAreaCheckInfo()
  329. db := orm.NewOrm()
  330. if err != nil {
  331. logger.Logger.Error(err)
  332. return err
  333. }
  334. if area_ruleid == "" {
  335. return errors.New(fmt.Sprintf("未定义间隔装置的检查规则“间隔装置与检查模型不符”"))
  336. }
  337. if scdXmlObj == nil {
  338. return errors.New("无效的SCD")
  339. }
  340. scdNodeRule := new(ScdNodeRule)
  341. scdNode := new(ScdNode)
  342. model_refs := map[string][]orm.Params{} //模型定义的装置关系定义
  343. area_ieds := map[string][]orm.Params{} //间隔下的装置列表
  344. iedRelationMgr := new(SysCheckModelIedRelationMgr)
  345. for _, row := range arealist {
  346. //获取间隔标准装置及关系
  347. modelid, _ := strconv.Atoi(tools.IsEmpty(row["model_id"]))
  348. //area_name := tools.IsEmpty(row["area_name"])
  349. //area_type := tools.IsEmpty(row["area_type"]) //间隔模型类型
  350. area_id := tools.IsEmpty(row["id"])
  351. s, err := iedRelationMgr.GetListByModelid(modelid)
  352. if err != nil {
  353. logger.Logger.Error(err)
  354. return err
  355. }
  356. if len(s) == 0 {
  357. return errors.New(fmt.Sprintf("模型%d还未配置装置关系", modelid))
  358. }
  359. model_refs[fmt.Sprintf("%d", modelid)] = s
  360. /*
  361. hasIeds := map[string]bool{}
  362. for _, row1 := range s {
  363. iedname := tools.IsEmpty(row1["from_ied_code"])
  364. if !hasIeds[iedname] {
  365. hasIeds[iedname] = true
  366. }
  367. iedname = tools.IsEmpty(row1["to_ied_code"])
  368. if !hasIeds[iedname] {
  369. hasIeds[iedname] = true
  370. }
  371. }
  372. */
  373. s1 := []orm.Params{}
  374. _, err = db.Raw("select ied_name from t_data_check_area_ied where area_id=?", area_id).Values(&s1)
  375. if err != nil {
  376. logger.Logger.Error(err)
  377. return err
  378. }
  379. if len(s) == 0 {
  380. return errors.New(fmt.Sprintf("间隔%s未发现任何装置", tools.IsEmpty(row["area_name"])))
  381. }
  382. area_ieds[area_id] = s1
  383. }
  384. //装置关联关系分析
  385. //先分析母联间隔,如果没有选择母联间隔时,主变间隔中不包含母联终端装置
  386. HasAreaJ := false
  387. for _, row := range arealist {
  388. area_name := tools.IsEmpty(row["area_name"])
  389. areaCode := tools.IsEmpty(row["area_type"]) //间隔模型类型
  390. area_id := tools.IsEmpty(row["id"])
  391. if areaCode == "J" {
  392. HasAreaJ = true
  393. modelid := tools.IsEmpty(row["model_id"])
  394. c.cJ(modelid, scdXmlObj, scdNode, scdNodeRule, area_name, model_refs[modelid], area_ieds[area_id], "PE", area_ruleid)
  395. c.cJ(modelid, scdXmlObj, scdNode, scdNodeRule, area_name, model_refs[modelid], area_ieds[area_id], "PJ", area_ruleid)
  396. c.cJ(modelid, scdXmlObj, scdNode, scdNodeRule, area_name, model_refs[modelid], area_ieds[area_id], "PK", area_ruleid)
  397. c.cJ(modelid, scdXmlObj, scdNode, scdNodeRule, area_name, model_refs[modelid], area_ieds[area_id], "PF", area_ruleid)
  398. }
  399. }
  400. for _, row := range arealist {
  401. area_name := tools.IsEmpty(row["area_name"])
  402. areaCode := tools.IsEmpty(row["area_type"]) //间隔模型类型
  403. area_id := tools.IsEmpty(row["id"])
  404. modelid := tools.IsEmpty(row["model_id"])
  405. if areaCode == "J" {
  406. continue
  407. }
  408. if areaCode == "L" {
  409. //线路间隔
  410. c.cL(modelid, scdXmlObj, scdNode, scdNodeRule, area_name, model_refs[modelid], area_ieds[area_id], area_ruleid)
  411. }
  412. if areaCode == "T" {
  413. //变压器齐间隔
  414. c.cT(modelid, scdXmlObj, scdNode, scdNodeRule, area_name, model_refs[modelid], area_ieds[area_id], area_ruleid, HasAreaJ)
  415. }
  416. }
  417. scdNodeRule.CheckFinish()
  418. scdNodeRule.Flush()
  419. return nil
  420. }
  421. //检测装置功能分析
  422. func (c *CheckAreaMgr) CheckIedFunc() error {
  423. return nil
  424. }
  425. //检测装置端子分析
  426. func (c *CheckAreaMgr) CheckIedFcda() error {
  427. scdXmlObj, arealist, area_ruleid, err := c.getAreaCheckInfo()
  428. if err != nil {
  429. logger.Logger.Error(err)
  430. return err
  431. }
  432. if area_ruleid == "" {
  433. return errors.New(fmt.Sprintf("未定义间隔装置的检查规则“间隔装置与检查模型不符”"))
  434. }
  435. if scdXmlObj == nil {
  436. return errors.New("无效的SCD")
  437. }
  438. db := orm.NewOrm()
  439. //获取当前站的各电压等级
  440. volRows := []orm.Params{}
  441. _, err = db.Raw("select t.vol, CAST(REPLACE(UPPER(g.name),'KV','') as SIGNED) volname from t_area_ied_relation t,global_const_code g where g.code=CONCAT('v_level_',t.vol) and g.parentcode='voltage_level' and t.vol!=999 and t.scd_id=? GROUP BY t.vol ORDER BY volname desc", c.ScdId).Values(&volRows)
  442. if err != nil {
  443. logger.Logger.Error(err)
  444. return err
  445. }
  446. if len(volRows) == 0 {
  447. logger.Logger.Error(errors.New("该scd未发现任何电压等级的装置"))
  448. return errors.New("该scd未发现任何电压等级的装置")
  449. }
  450. volMap := map[string]string{}
  451. volMap["hight"] = tools.IsEmpty(volRows[0]["vol"]) //高压电压
  452. if len(volRows) == 2 {
  453. volMap["middle"] = ""
  454. volMap["low"] = volRows[1]["vol"].(string) //低压电压等级
  455. } else {
  456. volMap["middle"] = volRows[1]["vol"].(string) //中压电压等级
  457. volMap["low"] = volRows[len(volRows)-1]["vol"].(string) //低压电压等级
  458. }
  459. scdNodeRule := new(ScdNodeRule)
  460. scdNodeRule.SetScdXmlObject(scdXmlObj)
  461. scdNodeMgr := new(ScdNode)
  462. //当前装置的所有信号接收端子列表
  463. var getIedExtRefs = func(iedname string) map[string]*node_attr.NExtRef {
  464. iedObj := scdNodeMgr.GetIed(scdXmlObj, tools.IsEmpty(c.ScdId), iedname)
  465. if iedObj == nil {
  466. logger.Logger.Debug(fmt.Sprintf("在scd:%d中未发现装置%s", c.ScdId, iedname))
  467. return nil
  468. }
  469. fcdaObjList := map[string]*node_attr.NExtRef{}
  470. for _, t1 := range iedObj.AccessPoint {
  471. if t1.Server == nil || len(t1.Server.LDevice) == 0 {
  472. continue
  473. }
  474. for _, ld := range t1.Server.LDevice {
  475. if ld.LN0 != nil && ld.LN0.Inputs != nil {
  476. for _, t2 := range ld.LN0.Inputs.ExtRef {
  477. doi := scdNodeRule.IedIntAddrExist(iedname, t2.IntAddr)
  478. if doi == nil {
  479. continue
  480. }
  481. fcdaObjList[doi.(*node_attr.NDOI).Desc] = t2
  482. }
  483. }
  484. for _, ln := range ld.LN {
  485. if ln.Inputs == nil {
  486. continue
  487. }
  488. for _, t2 := range ln.Inputs.ExtRef {
  489. doi := scdNodeRule.IedIntAddrExist(iedname, t2.IntAddr)
  490. if doi == nil {
  491. continue
  492. }
  493. fcdaObjList[doi.(*node_attr.NDOI).Desc] = t2
  494. }
  495. }
  496. }
  497. }
  498. return fcdaObjList
  499. }
  500. //当前装置的所有信号发送端子列表
  501. var getIedFcdas = func(iedname string) map[string]*node_attr.NFCDA {
  502. iedObj := scdNodeMgr.GetIed(scdXmlObj, tools.IsEmpty(c.ScdId), iedname)
  503. if iedObj == nil {
  504. return nil
  505. }
  506. fcdaObjList := map[string]*node_attr.NFCDA{}
  507. for _, t1 := range iedObj.AccessPoint {
  508. if t1.Server == nil || len(t1.Server.LDevice) == 0 {
  509. continue
  510. }
  511. for _, ld := range t1.Server.LDevice {
  512. if ld.LN0 != nil && len(ld.LN0.DataSet) > 0 {
  513. for _, t2 := range ld.LN0.DataSet {
  514. for _, t3 := range t2.FCDA {
  515. re, _ := scdNodeRule.IedFcdaExist(iedname, t3.LdInst, t3.LnClass, t3.LnInst, t3.Prefix, t3.DoName, t3.DaName)
  516. if re == nil {
  517. continue
  518. }
  519. doi := re.(*node_attr.NDOI)
  520. fcdaObjList[doi.Desc] = t3
  521. }
  522. }
  523. }
  524. }
  525. }
  526. return fcdaObjList
  527. }
  528. //从装置类型code中解析出类型代码和电压等级
  529. var getIedTypeAndVolCode = func(ied_type string) (string, string) {
  530. tmp := strings.Split(ied_type, "#")
  531. ied_type = tmp[0]
  532. vol := ""
  533. if len(tmp) == 2 {
  534. vol = tmp[1] //电压级别
  535. }
  536. return ied_type, vol
  537. }
  538. //从间隔装置中过滤出指定类型的IED
  539. var filterAreaIeds = func(ied_type, volLevelCode string, s1 []orm.Params) []orm.Params {
  540. iedlst := []orm.Params{}
  541. if volLevelCode == "" || (volLevelCode != "H" && volLevelCode != "M" && volLevelCode != "L") {
  542. for _, r := range s1 {
  543. if strings.HasPrefix(tools.IsEmpty(r["ied_name"]), ied_type) {
  544. iedlst = append(iedlst, r)
  545. }
  546. }
  547. } else {
  548. tmpLst := map[string]orm.Params{}
  549. for _, r := range s1 {
  550. iedname := tools.IsEmpty(r["ied_name"])
  551. if strings.HasPrefix(iedname, ied_type) {
  552. tmpLst[iedname] = r
  553. }
  554. }
  555. h, m, l := c.getIedListByVol(ied_type, tmpLst, volMap)
  556. if volLevelCode == "H" {
  557. iedlst = h
  558. }
  559. if volLevelCode == "M" {
  560. iedlst = m
  561. }
  562. if volLevelCode == "L" {
  563. iedlst = l
  564. }
  565. }
  566. return iedlst
  567. }
  568. //iedRelationMgr := new(SysCheckModelIedRelationMgr)
  569. modelFcda := sync.Map{}
  570. for _, row := range arealist {
  571. //获取间隔标准装置及关系
  572. modelid, _ := strconv.Atoi(tools.IsEmpty(row["model_id"]))
  573. area_name := tools.IsEmpty(row["area_name"])
  574. //area_type := tools.IsEmpty(row["area_type"]) //间隔模型类型
  575. area_id := tools.IsEmpty(row["id"])
  576. logger.Logger.Debug(fmt.Sprintf("开始检查模型%d的间隔%s", modelid, area_name))
  577. //获取装置分组信息
  578. bgm := new(SysCheckModelIedtypeGroupMgr)
  579. bgm.Model = T_data_model_iedtype_group{ModelId: modelid}
  580. groupList := bgm.List()
  581. //获取该间隔模型配置的所有端子关系列表
  582. tmpMgr := new(SysCheckModelFcdaRalationMgr)
  583. tmpMgr.Model.ModelId = modelid
  584. funclist := map[string][]orm.Params{}
  585. key := fmt.Sprintf("%d-%d", modelid, area_id)
  586. v, h := modelFcda.Load(key)
  587. if h {
  588. funclist = v.(map[string][]orm.Params)
  589. } else {
  590. funclist, _ = tmpMgr.GetModelAllFcdaRef()
  591. if funclist == nil || len(funclist) == 0 {
  592. logger.Logger.Error(errors.New(fmt.Sprintf("模型%d下的间隔%s还未配置装置端子关联关系", modelid, area_name)))
  593. continue
  594. }
  595. modelFcda.Store(key, funclist)
  596. }
  597. //获取该间隔下该类型的装置
  598. s1 := []orm.Params{}
  599. _, err = db.Raw("select ied_name from t_data_check_area_ied where area_id=?", area_id).Values(&s1)
  600. //循环处理关联关系
  601. for to_ied_type2, refrow := range funclist {
  602. ts := strings.Split(to_ied_type2, ",")
  603. //从间隔中获取当前同类型的信号接收装置
  604. ied_type := c.getIedTypeCode(modelid, ts[0])
  605. if v, h := groupList[ied_type]; h {
  606. ied_type = v
  607. }
  608. ied_type, vol := getIedTypeAndVolCode(ied_type)
  609. iedlst := filterAreaIeds(ied_type, vol, s1)
  610. logger.Logger.Debug(fmt.Sprintf("正在检查装置类型:%s的装置(%+v)端子关系", ied_type, iedlst))
  611. //从间隔中获取当前信号输出装置
  612. outiedlist := map[string]orm.Params{}
  613. fromiedcode := c.getIedTypeCode(modelid, ts[1])
  614. if v, h := groupList[fromiedcode]; h {
  615. fromiedcode = v
  616. }
  617. from_ied_type, vol2 := getIedTypeAndVolCode(fromiedcode)
  618. outiedlist2 := filterAreaIeds(from_ied_type, vol2, s1)
  619. for _, iedrow := range outiedlist2 {
  620. iedname := tools.IsEmpty(iedrow["ied_name"])
  621. outiedlist[iedname] = iedrow
  622. }
  623. if len(outiedlist) == 0 {
  624. logger.Logger.Debug(fmt.Sprintf("装置类型%s未发现输出任何装置", ied_type))
  625. continue
  626. }
  627. logger.Logger.Debug(fmt.Sprintf("当前装置类型:%s的装置(%+v)存在以下信号输出装置:%+v", ied_type, iedlst, outiedlist))
  628. //logger.Logger.Debug(fmt.Sprintf("设计端子关联关系:%+v", refrow))
  629. for _, ied := range iedlst {
  630. iedname := tools.IsEmpty(ied["ied_name"])
  631. iedObj := scdNodeMgr.GetIed(scdXmlObj, fmt.Sprintf("%d", c.ScdId), iedname)
  632. iedObjDesc := ""
  633. if iedObj != nil {
  634. iedObjDesc = iedObj.Desc
  635. } else {
  636. logger.Logger.Error(fmt.Sprintf("信号接收装置%s未找到!", iedname))
  637. continue
  638. }
  639. extreflist := getIedExtRefs(iedname)
  640. for outiedname, _ := range outiedlist {
  641. if iedname == outiedname {
  642. continue
  643. }
  644. logger.Logger.Debug(fmt.Sprintf("正在匹配装置%s与%s的端子关联关系", iedname, outiedname))
  645. outIedObj := scdNodeMgr.GetIed(scdXmlObj, fmt.Sprintf("%d", c.ScdId), outiedname)
  646. outIedObjDesc := ""
  647. if outIedObj == nil {
  648. logger.Logger.Error(fmt.Sprintf("信号输出装置%s未找到!", outiedname))
  649. } else {
  650. outIedObjDesc = outIedObj.Desc
  651. }
  652. if iedObj == nil && outIedObj == nil {
  653. continue
  654. }
  655. outiedFcdaList := getIedFcdas(outiedname) //输入装置的信号输出端子
  656. //检查是否有错误和缺失的端子
  657. hasYaoXinFunc := false //是否具备遥信功能
  658. for _, r := range refrow {
  659. //判断装置是否具备遥信功能,具备时需要单独处理遥信类端子
  660. //遥信类端子处理规则:以scd中实际配置为准,通过检查装置双方的端子名称是否完全一致,以及是否有缺失和多余的遥信端子
  661. if strings.Contains(tools.IsEmpty(r["to_func_name"]), "遥信") || strings.Contains(tools.IsEmpty(r["from_func_name"]), "遥信") {
  662. hasYaoXinFunc = true
  663. }
  664. extref_name := tools.IsEmpty(r["to_fcda_name"])
  665. fcda_name := tools.IsEmpty(r["from_fcda_name"])
  666. extref_name_exp := tools.IsEmpty(r["to_fcda_match_exp"])
  667. fcda_name_exp := tools.IsEmpty(r["from_fcda_match_exp"])
  668. funcExist := false //判断接收端设计的端子是否存在
  669. extrefObj := new(node_attr.NExtRef)
  670. fcda2Exist := false
  671. fcdaObj := new(node_attr.NFCDA)
  672. for desc, item := range extreflist {
  673. //logger.Logger.Debug(fmt.Sprintf("接收装置%s的设计端子%s匹配SCD ExtRef DO端子%s", iedname, extref_name_exp, desc))
  674. if tools.RexGroupTestMatch(extref_name_exp, desc) {
  675. funcExist = true
  676. extrefObj = item
  677. break
  678. }
  679. }
  680. if funcExist {
  681. //判断输出端端子是否存在
  682. for desc, item := range outiedFcdaList {
  683. //logger.Logger.Debug(fmt.Sprintf("输出装置%s的设计端子%s匹配SCD FCDA DO端子%s", outiedname, fcda_name_exp, desc))
  684. if tools.RexGroupTestMatch(fcda_name_exp, desc) {
  685. fcda2Exist = true
  686. fcdaObj = item
  687. break
  688. }
  689. }
  690. }
  691. if !funcExist || !fcda2Exist {
  692. if !funcExist {
  693. parse_result := fmt.Sprintf("间隔%s的装置%s缺失端子%s", area_name, iedname, extref_name)
  694. re := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  695. "ied_name": iedname,
  696. "ied_desc": iedObjDesc,
  697. "out_ied_name": "",
  698. "out_ied_desc": "",
  699. "fcda_desc": extref_name,
  700. "fcda_addr": "",
  701. "out_fcda_desc": "",
  702. "out_fcda_addr": "",
  703. "error_type": "3",
  704. }
  705. //检查未通过
  706. scdNodeRule.AppendFcdaCheckResult(re)
  707. }
  708. if !fcda2Exist {
  709. parse_result := fmt.Sprintf("间隔%s的装置%s缺失端子%s", area_name, outiedname, fcda_name)
  710. re := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  711. "ied_name": "",
  712. "ied_desc": "",
  713. "out_ied_name": outiedname,
  714. "out_ied_desc": outIedObjDesc,
  715. "fcda_desc": "",
  716. "fcda_addr": "",
  717. "out_fcda_desc": fcda_name,
  718. "out_fcda_addr": "",
  719. "error_type": "3",
  720. }
  721. //检查未通过
  722. scdNodeRule.AppendFcdaCheckResult(re)
  723. }
  724. } else {
  725. //检查端子是否关联正确
  726. if fcda2Exist && (extrefObj.LdInst != fcdaObj.LdInst ||
  727. extrefObj.LnInst != fcdaObj.LnInst ||
  728. extrefObj.LnClass != fcdaObj.LnClass ||
  729. extrefObj.Prefix != fcdaObj.Prefix ||
  730. extrefObj.DoName != fcdaObj.DoName) {
  731. //不正确
  732. logger.Logger.Debug(fmt.Sprintf("extref_name:%s extref_name_exp:%s extrefObj:%+v", extref_name, extref_name_exp, extrefObj))
  733. logger.Logger.Debug(fmt.Sprintf("fcda_name:%s fcda_name_exp:%s fcdaObj:%+v", fcda_name, fcda_name_exp, fcdaObj))
  734. iedObj := scdNodeMgr.GetIed(scdXmlObj, fmt.Sprintf("%d", c.ScdId), iedname)
  735. outIedObj := scdNodeMgr.GetIed(scdXmlObj, fmt.Sprintf("%d", c.ScdId), outiedname)
  736. parse_result := fmt.Sprintf("间隔%s的装置%s端子%s与装置%s端子%s关联错误", area_name, iedname, extref_name, outiedname, fcda_name)
  737. daname := ""
  738. if fcdaObj.DaName != "" {
  739. daname = "." + fcdaObj.DaName
  740. }
  741. re := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  742. "ied_name": iedname,
  743. "ied_desc": iedObj.Desc,
  744. "out_ied_name": outiedname,
  745. "out_ied_desc": outIedObj.Desc,
  746. "fcda_desc": extref_name,
  747. "fcda_addr": extrefObj.IntAddr,
  748. "out_fcda_desc": fcda_name,
  749. "out_fcda_addr": fmt.Sprintf("%s/%s%s%s.%s%s", fcdaObj.LdInst, fcdaObj.Prefix, fcdaObj.LnClass, fcdaObj.LnInst, fcdaObj.DoName, daname),
  750. "error_type": "2",
  751. }
  752. //检查未通过
  753. scdNodeRule.AppendFcdaCheckResult(re)
  754. }
  755. }
  756. }
  757. //检查是否有多余(SCD中有不存在于设计中)的端子
  758. for extrefdesc, r := range extreflist { //scd中的端子关系
  759. extref_name := ""
  760. fcda_name := ""
  761. isHave := false
  762. for _, r1 := range refrow { //设计的端子关系
  763. if strings.Contains(tools.IsEmpty(r1["to_func_name"]), "遥信") || strings.Contains(tools.IsEmpty(r1["from_func_name"]), "遥信") {
  764. hasYaoXinFunc = true
  765. }
  766. extref_name_exp := tools.IsEmpty(r1["to_fcda_match_exp"])
  767. if extrefdesc == extref_name_exp || tools.RexGroupTestMatch(extref_name_exp, extrefdesc) {
  768. //端子存在
  769. isHave = true
  770. break
  771. }
  772. }
  773. if !isHave {
  774. daname := ""
  775. if r.DaName != "" {
  776. daname = "." + r.DaName
  777. }
  778. doi := scdNodeRule.IedIntAddrExist(iedname, r.IntAddr)
  779. if doi != nil {
  780. extref_name = doi.(*node_attr.NDOI).Desc
  781. }
  782. doi, _ = scdNodeRule.IedFcdaExist(outiedname, r.LdInst, r.LnClass, r.LnInst, r.Prefix, r.DoName, "")
  783. if doi != nil {
  784. fcda_name = doi.(*node_attr.NDOI).Desc
  785. }
  786. parse_result := fmt.Sprintf("间隔%s下装置(%s)的端子%s在设计中不存在", area_name, iedname, extref_name)
  787. re := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  788. "ied_name": iedname,
  789. "ied_desc": iedObj.Desc,
  790. "out_ied_name": outiedname,
  791. "out_ied_desc": outIedObj.Desc,
  792. "fcda_desc": extref_name,
  793. "fcda_addr": r.IntAddr,
  794. "out_fcda_desc": fcda_name,
  795. "out_fcda_addr": fmt.Sprintf("%s/%s%s%s.%s%s", r.LdInst, r.Prefix, r.LnClass, r.LnInst, r.DoName, daname),
  796. "error_type": "1",
  797. }
  798. //检查未通过
  799. scdNodeRule.AppendFcdaCheckResult(re)
  800. }
  801. }
  802. if hasYaoXinFunc {
  803. //遥信端子检查:仅根据scd实际配置检查其双方端子的doi名称是否相同
  804. scdMgr := new(ScdMgr)
  805. yx := scdMgr.GetYxExtref(scdXmlObj, c.ScdId, iedname)
  806. if len(yx) > 0 {
  807. for extref, doiDesc := range yx {
  808. if extref.IedName != outiedname {
  809. //非当前输入装置的,不处理
  810. continue
  811. }
  812. daname := ""
  813. if extref.DaName != "" {
  814. daname = "." + extref.DaName
  815. }
  816. //获取输出端端子名称
  817. outFcdaDoi, _ := scdNodeRule.IedFcdaExist(extref.IedName, extref.LdInst, extref.LnClass, extref.LnInst, extref.Prefix, extref.DoName, extref.DaName)
  818. if outFcdaDoi == nil {
  819. //端子缺失
  820. parse_result := fmt.Sprintf("间隔%s的装置%s端子%s的关联端子不存在", area_name, iedname, doiDesc)
  821. re := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  822. "ied_name": iedname,
  823. "ied_desc": iedObj.Desc,
  824. "out_ied_name": outiedname,
  825. "out_ied_desc": outIedObj.Desc,
  826. "fcda_desc": doiDesc,
  827. "fcda_addr": extref.IntAddr,
  828. "out_fcda_desc": "",
  829. "out_fcda_addr": fmt.Sprintf("%s/%s%s%s.%s%s", extref.LdInst, extref.Prefix, extref.LnClass, extref.LnInst, extref.DoName, daname),
  830. "error_type": "3",
  831. }
  832. //检查未通过
  833. scdNodeRule.AppendFcdaCheckResult(re)
  834. } else if outFcdaDoi.(*node_attr.NDOI).Desc != doiDesc {
  835. //关联错误
  836. parse_result := fmt.Sprintf("间隔%s的装置%s端子%s与装置%s端子%s名称不匹配", area_name, iedname, doiDesc, outiedname, outFcdaDoi.(*node_attr.NDOI).Desc)
  837. re := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  838. "ied_name": iedname,
  839. "ied_desc": iedObj.Desc,
  840. "out_ied_name": outiedname,
  841. "out_ied_desc": outIedObj.Desc,
  842. "fcda_desc": doiDesc,
  843. "fcda_addr": extref.IntAddr,
  844. "out_fcda_desc": outFcdaDoi.(*node_attr.NDOI).Desc,
  845. "out_fcda_addr": fmt.Sprintf("%s/%s%s%s.%s%s", extref.LdInst, extref.Prefix, extref.LnClass, extref.LnInst, extref.DoName, daname),
  846. "error_type": "2",
  847. }
  848. //检查未通过
  849. scdNodeRule.AppendFcdaCheckResult(re)
  850. }
  851. }
  852. }
  853. }
  854. }
  855. }
  856. }
  857. }
  858. scdNodeRule.CheckFinish()
  859. scdNodeRule.Flush()
  860. return nil
  861. }
  862. //解析模型间隔。根据模型定义解析出间隔中的装置
  863. func (c *CheckAreaMgr) ParseModelArea() {
  864. c.Init(c.ScdId)
  865. key := fmt.Sprintf("%d-checkinfo", c.ScdId)
  866. areaCheckInfo.Delete(key)
  867. // 取得当前scd所有ied及名称解析结果
  868. dbo := orm.NewOrm()
  869. sql := "select t.*,t1.name area_name from t_area_ied_relation t,t_substation_area t1 where t.area_id=t1.id and t.scd_id=? order by t.p_type"
  870. iedlst := []orm.Params{}
  871. _, err := dbo.Raw(sql, c.ScdId).Values(&iedlst)
  872. if err != nil {
  873. logger.Logger.Error(err)
  874. return
  875. }
  876. logger.Logger.Debug(fmt.Sprintf("=====总装置数:%d", len(iedlst)))
  877. iedMap := map[string]orm.Params{}
  878. for _, r := range iedlst {
  879. iedMap[tools.IsEmpty(r["ied_name"])] = r
  880. }
  881. //先分析母联间隔,如果没有选择母联间隔时,主变间隔中不包含母联终端装置
  882. HasAreaJ := false
  883. for _, row := range c.CheckModelList {
  884. areaCode := tools.IsEmpty(row["area_type_code"])
  885. if areaCode == "J" {
  886. HasAreaJ = true
  887. modelid, _ := strconv.Atoi(tools.IsEmpty(row["id"]))
  888. //modelname := tools.IsEmpty(row["model_name"])
  889. iedtypes := tools.IsEmpty(row["ied_types"])
  890. mapptype := new(SysCheckModelIedtypeMappingMgr)
  891. mapptype.Model = T_data_model_iedtype_mapping{ModelId: modelid}
  892. mappresult := mapptype.List()
  893. if len(mappresult) > 0 {
  894. tmp := []string{}
  895. for _, r := range strings.Split(iedtypes, ",") {
  896. if mappresult[r] != "" {
  897. tmp = append(tmp, mappresult[r])
  898. } else {
  899. tmp = append(tmp, r)
  900. }
  901. }
  902. iedtypes = strings.Join(tmp, ",")
  903. }
  904. volcode := strings.ReplaceAll(tools.IsEmpty(row["vol_code"]), "v_level_", "")
  905. c.pJ(modelid, volcode, iedtypes, iedMap, "PE")
  906. c.pJ(modelid, volcode, iedtypes, iedMap, "PJ")
  907. c.pJ(modelid, volcode, iedtypes, iedMap, "PK")
  908. c.pJ(modelid, volcode, iedtypes, iedMap, "PF")
  909. }
  910. }
  911. for _, row := range c.CheckModelList {
  912. //逐一分析模型定义
  913. modelid, _ := strconv.Atoi(tools.IsEmpty(row["id"]))
  914. modelname := tools.IsEmpty(row["model_name"])
  915. iedtypes := tools.IsEmpty(row["ied_types"])
  916. //模型对应的间隔代码
  917. /*
  918. S 站用变压器
  919. C 电容器
  920. B 断路器
  921. K 母分
  922. J 母联
  923. M 母线
  924. X 电抗器
  925. L 线路
  926. T 主变压器
  927. */
  928. areaCode := tools.IsEmpty(row["area_type_code"])
  929. //模型对应的电压等级
  930. //10:10KV 35:35KV 66:66KV 11:110KV 22:220KV 50:500KV 75:750KV 33:330KV T0:1000KV
  931. volcode := strings.ReplaceAll(tools.IsEmpty(row["vol_code"]), "v_level_", "")
  932. if modelname == "" || iedtypes == "" {
  933. continue
  934. }
  935. //母联间隔已经提前分析,如果没有母联间隔时,主变间隔中不包含母联终端装置
  936. if areaCode == "J" {
  937. continue
  938. }
  939. //获取模型中的自定义装置类型编码
  940. mapptype := new(SysCheckModelIedtypeMappingMgr)
  941. mapptype.Model = T_data_model_iedtype_mapping{ModelId: modelid}
  942. mappresult := mapptype.List()
  943. if len(mappresult) > 0 {
  944. tmp := []string{}
  945. for _, r := range strings.Split(iedtypes, ",") {
  946. if mappresult[r] != "" {
  947. tmp = append(tmp, mappresult[r])
  948. } else {
  949. tmp = append(tmp, r)
  950. }
  951. }
  952. iedtypes = strings.Join(tmp, ",")
  953. }
  954. //获取模型内中装备关系定义
  955. //主变间隔分析:需要查站内该电压等级下的高中低压侧装置或者高低压装置组成一个间隔,以主变保护装置(PT)为起始
  956. if areaCode == "T" {
  957. c.pT(modelid, iedtypes, iedMap, HasAreaJ)
  958. }
  959. //线路保护间隔分析:以线路保护测控装置(PL)为开始分析
  960. if areaCode == "L" {
  961. c.pL(modelid, volcode, iedtypes, iedMap)
  962. }
  963. }
  964. }
  965. //根据默认装置类型获取其类型编码。如果没有自定义编码,则返回默认编码
  966. func (c *CheckAreaMgr) getIedTypeCode(modelid int, iedtype string) string {
  967. mapptype := new(SysCheckModelIedtypeMappingMgr)
  968. mapptype.Model = T_data_model_iedtype_mapping{ModelId: modelid}
  969. mapptype.Model.IedType = iedtype
  970. mappresult := mapptype.List()
  971. ptCode := iedtype
  972. if len(mappresult) > 0 {
  973. ptCode = mappresult[iedtype] //自定义主变保护编码
  974. }
  975. return ptCode
  976. }
  977. //变压器间隔分析
  978. func (c *CheckAreaMgr) pT(modelid int, iedtypes string, ieds map[string]orm.Params, HasAreaJ bool) {
  979. scdParseMgr := new(ScdParse)
  980. scdNodeMgr := new(ScdNode)
  981. db := orm.NewOrm()
  982. //获取装置分组信息
  983. bgm := new(SysCheckModelIedtypeGroupMgr)
  984. bgm.Model = T_data_model_iedtype_group{ModelId: modelid}
  985. groupList := bgm.List()
  986. scdXmlObj, _ := scdParseMgr.GetScdXmlObjectBySCDID(tools.IsEmpty(c.ScdId))
  987. //获取当前站的各电压等级
  988. volRows := []orm.Params{}
  989. _, err := db.Raw("select t.vol, CAST(REPLACE(UPPER(g.name),'KV','') as SIGNED) volname from t_area_ied_relation t,global_const_code g where g.code=CONCAT('v_level_',t.vol) and g.parentcode='voltage_level' and t.vol!=999 and t.scd_id=? GROUP BY t.vol ORDER BY volname desc", c.ScdId).Values(&volRows)
  990. if err != nil {
  991. logger.Logger.Error(err)
  992. return
  993. }
  994. if len(volRows) == 0 {
  995. logger.Logger.Error(errors.New("该scd未发现任何电压等级的装置"))
  996. return
  997. }
  998. volMap := map[string]string{}
  999. volMap["hight"] = tools.IsEmpty(volRows[0]["vol"]) //高压电压
  1000. if len(volRows) == 2 {
  1001. volMap["middle"] = ""
  1002. volMap["low"] = volRows[1]["vol"].(string) //低压电压等级
  1003. } else {
  1004. volMap["middle"] = volRows[1]["vol"].(string) //中压电压等级
  1005. volMap["low"] = volRows[len(volRows)-1]["vol"].(string) //低压电压等级
  1006. }
  1007. var getIedByDesc = func(iedinfo map[string]orm.Params, iedtpye string, desc string) ([]string, []string) {
  1008. scdParseMgr := new(ScdParse)
  1009. scdXmlObj, _ := scdParseMgr.GetScdXmlObjectBySCDID(tools.IsEmpty(c.ScdId))
  1010. r1 := []string{}
  1011. r2 := []string{}
  1012. for vn, row := range iedinfo {
  1013. if strings.HasPrefix(vn, iedtpye) {
  1014. v := tools.IsEmpty(row["ied_desc"])
  1015. if v == "" {
  1016. if scdXmlObj == nil {
  1017. continue
  1018. }
  1019. scdNode := new(ScdNode)
  1020. ied := scdNode.GetIed(scdXmlObj, "", vn)
  1021. if ied == nil {
  1022. continue
  1023. }
  1024. v = ied.Desc
  1025. }
  1026. if strings.Contains(v, desc) {
  1027. r1 = append(r1, v)
  1028. r2 = append(r2, vn)
  1029. }
  1030. }
  1031. }
  1032. return r1, r2
  1033. }
  1034. //判断是否将各电压等级的保护装置合并还是分开的间隔
  1035. //如果存在高中低压的保护装置,则说明是分开的主变间隔
  1036. //isMarge := true // 默认为合并的主变间隔
  1037. ptCode := c.getIedTypeCode(modelid, "PT")
  1038. for _, row := range ieds {
  1039. if tools.IsEmpty(row["ied_type"]) != ptCode[0:1] || tools.IsEmpty(row["p_type"]) != ptCode[1:] {
  1040. continue
  1041. }
  1042. if _, h := groupList[ptCode]; h {
  1043. //装置如果是分组成员装置,则不用检测
  1044. continue
  1045. }
  1046. //判断主变保护是否是按电压等级分开配置的模型
  1047. /*
  1048. if strings.Contains(iedtypes, "PT#H") || strings.Contains(iedtypes, "PT#M") || strings.Contains(iedtypes, "PT#L") {
  1049. h, m, l := c.getIedListByVol("PT", ieds, volMap)
  1050. } else {
  1051. pl_iedname := tools.IsEmpty(row["ied_name"])
  1052. }
  1053. */
  1054. //pmIedName := ""
  1055. //mmIedName := ""
  1056. pl_iedname := tools.IsEmpty(row["ied_name"])
  1057. iednameParts := scdParseMgr.ParseIedName(pl_iedname)
  1058. areadesc := tools.IsEmpty(row["ied_desc"])
  1059. if areadesc == "" {
  1060. iedobj := scdNodeMgr.GetIed(scdXmlObj, "", pl_iedname)
  1061. if iedobj != nil {
  1062. areadesc = iedobj.Desc
  1063. } else {
  1064. areadesc = tools.IsEmpty(row["area_name"])
  1065. }
  1066. }
  1067. areadesc = strings.ReplaceAll(areadesc, "装置", "")
  1068. //添加间隔数据
  1069. dbdata := T_data_check_area{
  1070. ModelId: modelid,
  1071. ScdId: c.ScdId,
  1072. AreaType: "T",
  1073. AreaName: areadesc + iednameParts[7],
  1074. }
  1075. newid, err := db.Insert(&dbdata)
  1076. if err != nil {
  1077. logger.Logger.Error(err)
  1078. return
  1079. }
  1080. ins1 := "insert into t_data_check_area_ied(scd_id,area_id,ied_name,ied_type,p_type)values"
  1081. insvalues := []string{}
  1082. inAreaIedName := pl_iedname
  1083. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, "P", "T"))
  1084. //判断间隔是否需要包含差动装置
  1085. if strings.Contains(iedtypes, ptCode+"#C") {
  1086. _, iedname := getIedByDesc(ieds, ptCode, "差动")
  1087. if len(iedname) > 0 {
  1088. for _, in := range iedname {
  1089. tmpIednameParts := scdParseMgr.ParseIedName(in)
  1090. if tmpIednameParts[6] == iednameParts[6] && tmpIednameParts[7] == iednameParts[7] {
  1091. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, in, "P", "TC"))
  1092. break
  1093. }
  1094. }
  1095. }
  1096. }
  1097. //判断间隔是否需要包含本体保护装置
  1098. if strings.Contains(iedtypes, ptCode+"#0") {
  1099. _, iedname := getIedByDesc(ieds, ptCode, "本体")
  1100. if len(iedname) > 0 {
  1101. for _, in := range iedname {
  1102. tmpIednameParts := scdParseMgr.ParseIedName(in)
  1103. if tmpIednameParts[6] == iednameParts[6] && tmpIednameParts[7] == iednameParts[7] {
  1104. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, in, "P", "T0"))
  1105. break
  1106. }
  1107. }
  1108. }
  1109. }
  1110. //合智一体IMT/MIT,分高中低压
  1111. imtcode := c.getIedTypeCode(modelid, "IMT")
  1112. h, m, l := c.getIedListByVol(imtcode, ieds, volMap)
  1113. h = append(h, m...)
  1114. h = append(h, l...)
  1115. for _, r := range h {
  1116. inAreaIedName = tools.IsEmpty(r["ied_name"])
  1117. tmpIednameParts := scdParseMgr.ParseIedName(inAreaIedName)
  1118. if tmpIednameParts[6] == iednameParts[6] && tmpIednameParts[7] == iednameParts[7] {
  1119. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, "IM", "T"))
  1120. }
  1121. }
  1122. imtcode = c.getIedTypeCode(modelid, "MIT")
  1123. h, m, l = c.getIedListByVol(imtcode, ieds, volMap)
  1124. h = append(h, m...)
  1125. h = append(h, l...)
  1126. for _, r := range h {
  1127. inAreaIedName = tools.IsEmpty(r["ied_name"])
  1128. tmpIednameParts := scdParseMgr.ParseIedName(inAreaIedName)
  1129. if tmpIednameParts[6] == iednameParts[6] && tmpIednameParts[7] == iednameParts[7] {
  1130. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, "MI", "T"))
  1131. }
  1132. }
  1133. //母联智能终端IE需要分高中压,无低压侧
  1134. if HasAreaJ {
  1135. imtcode = c.getIedTypeCode(modelid, "IE")
  1136. h, m, l = c.getIedListByVol(imtcode, ieds, volMap)
  1137. for _, r := range h {
  1138. //高压侧,AB套必须与PT装置相同
  1139. inAreaIedName = tools.IsEmpty(r["ied_name"])
  1140. tmpIednameParts := scdParseMgr.ParseIedName(inAreaIedName)
  1141. if tmpIednameParts[6] == iednameParts[6] && tmpIednameParts[7] == iednameParts[7] {
  1142. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, "I", "E"))
  1143. }
  1144. }
  1145. for _, r := range m {
  1146. //中压侧
  1147. inAreaIedName = tools.IsEmpty(r["ied_name"])
  1148. tmpIednameParts := scdParseMgr.ParseIedName(inAreaIedName)
  1149. lastChar := tmpIednameParts[7]
  1150. if tmpIednameParts[6] == iednameParts[6] && (lastChar == "" || tmpIednameParts[7] == iednameParts[7]) {
  1151. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, "I", "E"))
  1152. }
  1153. }
  1154. }
  1155. //合并单元MT分高中低压侧;低压侧无关联母线合并单元MM
  1156. imtcode = c.getIedTypeCode(modelid, "MT")
  1157. h, m, l = c.getIedListByVol(imtcode, ieds, volMap)
  1158. h = append(h, m...)
  1159. h = append(h, l...)
  1160. for _, r := range h {
  1161. inAreaIedName = tools.IsEmpty(r["ied_name"])
  1162. tmpIednameParts := scdParseMgr.ParseIedName(inAreaIedName)
  1163. if tmpIednameParts[6] == iednameParts[6] && tmpIednameParts[7] == iednameParts[7] {
  1164. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, "M", "T"))
  1165. }
  1166. }
  1167. //是否包含本体合并单元
  1168. if strings.Contains(iedtypes, imtcode+"#0") {
  1169. _, iedname := getIedByDesc(ieds, imtcode, "本体")
  1170. if len(iedname) > 0 {
  1171. for _, in := range iedname {
  1172. tmpIednameParts := scdParseMgr.ParseIedName(in)
  1173. if tmpIednameParts[6] == iednameParts[6] && tmpIednameParts[7] == iednameParts[7] {
  1174. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, in, "M", "T"))
  1175. break
  1176. }
  1177. }
  1178. }
  1179. }
  1180. //测控装置CT分高中低压侧,且可能是多套合并单元MT共用,既不分AB套
  1181. imtcode = c.getIedTypeCode(modelid, "CT")
  1182. h, m, l = c.getIedListByVol(imtcode, ieds, volMap)
  1183. h = append(h, m...)
  1184. h = append(h, l...)
  1185. for _, r := range h {
  1186. inAreaIedName = tools.IsEmpty(r["ied_name"])
  1187. tmpIednameParts := scdParseMgr.ParseIedName(inAreaIedName)
  1188. lastChar := tmpIednameParts[7]
  1189. tmpVol := tmpIednameParts[3] + tmpIednameParts[4]
  1190. //高中低压电压等级相同的
  1191. if (tmpVol == volMap["low"] || tmpVol == volMap["middle"] || tmpVol == volMap["hight"]) && tmpIednameParts[6] == iednameParts[6] && (lastChar == "" || lastChar == iednameParts[7]) {
  1192. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, "C", "T"))
  1193. }
  1194. }
  1195. //是否包含本体测控装置
  1196. if strings.Contains(iedtypes, imtcode+"#0") {
  1197. _, iedname := getIedByDesc(ieds, imtcode, "本体")
  1198. if len(iedname) > 0 {
  1199. for _, in := range iedname {
  1200. tmpIednameParts := scdParseMgr.ParseIedName(in)
  1201. if tmpIednameParts[6] == iednameParts[6] && tmpIednameParts[7] == iednameParts[7] {
  1202. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, in, "C", "T"))
  1203. break
  1204. }
  1205. }
  1206. }
  1207. }
  1208. //智能终端(IB:开关\IT:分支\IF:分段)分高中低压侧
  1209. imtcodeB := c.getIedTypeCode(modelid, "IB")
  1210. imtcodeT := c.getIedTypeCode(modelid, "IT")
  1211. imtcodeF := c.getIedTypeCode(modelid, "IF")
  1212. h, m, l = c.getIedListByVol(imtcodeB, ieds, volMap)
  1213. h = append(h, m...)
  1214. h = append(h, l...)
  1215. for _, r := range h {
  1216. inAreaIedName = tools.IsEmpty(r["ied_name"])
  1217. tmpIednameParts := scdParseMgr.ParseIedName(inAreaIedName)
  1218. lastChar := tmpIednameParts[7]
  1219. tmpVol := tmpIednameParts[3] + tmpIednameParts[4]
  1220. //高中低压电压等级相同的
  1221. if (tmpVol == volMap["low"] || tmpVol == volMap["middle"] || tmpVol == volMap["hight"]) && tmpIednameParts[6] == iednameParts[6] && (lastChar == "" || lastChar == iednameParts[7]) {
  1222. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, "I", "B"))
  1223. }
  1224. }
  1225. //是否包含本体测控装置
  1226. if strings.Contains(iedtypes, imtcodeT+"#0") {
  1227. for n, row := range ieds {
  1228. if strings.HasPrefix(n, imtcodeT) || strings.HasPrefix(n, imtcodeB) || strings.HasPrefix(n, imtcodeF) {
  1229. ieddesc := tools.IsEmpty(row["ied_desc"])
  1230. if strings.Contains(ieddesc, "本体") {
  1231. tmpIednameParts := scdParseMgr.ParseIedName(n)
  1232. if tmpIednameParts[6] == iednameParts[6] && tmpIednameParts[7] == iednameParts[7] {
  1233. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, n, "I", "T"))
  1234. break
  1235. }
  1236. }
  1237. }
  1238. }
  1239. }
  1240. h, m, l = c.getIedListByVol(imtcodeT, ieds, volMap)
  1241. h = append(h, m...)
  1242. h = append(h, l...)
  1243. for _, r := range h {
  1244. inAreaIedName = tools.IsEmpty(r["ied_name"])
  1245. tmpIednameParts := scdParseMgr.ParseIedName(inAreaIedName)
  1246. lastChar := tmpIednameParts[7]
  1247. tmpVol := tmpIednameParts[3] + tmpIednameParts[4]
  1248. //高中低压电压等级相同的
  1249. if (tmpVol == volMap["low"] || tmpVol == volMap["middle"] || tmpVol == volMap["hight"]) && tmpIednameParts[6] == iednameParts[6] && (lastChar == "" || lastChar == iednameParts[7]) {
  1250. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, "I", "T"))
  1251. }
  1252. }
  1253. h, m, l = c.getIedListByVol(imtcodeF, ieds, volMap)
  1254. h = append(h, m...)
  1255. h = append(h, l...)
  1256. for _, r := range h {
  1257. inAreaIedName = tools.IsEmpty(r["ied_name"])
  1258. tmpIednameParts := scdParseMgr.ParseIedName(inAreaIedName)
  1259. lastChar := tmpIednameParts[7]
  1260. tmpVol := tmpIednameParts[3] + tmpIednameParts[4]
  1261. //高中低压电压等级相同的
  1262. if (tmpVol == volMap["low"] || tmpVol == volMap["middle"] || tmpVol == volMap["hight"]) && tmpIednameParts[6] == iednameParts[6] && (lastChar == "" || lastChar == iednameParts[7]) {
  1263. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, "I", "F"))
  1264. }
  1265. }
  1266. //母线保护PM及母线合并单元MM,可能是多套合并单元MT共用,既不分AB套;低压侧智能终端无关联母线保护
  1267. imtcode = c.getIedTypeCode(modelid, "PM")
  1268. h, m, l = c.getIedListByVol(imtcode, ieds, volMap)
  1269. for _, r := range h {
  1270. //高压侧,AB套必须与PT装置相同
  1271. inAreaIedName = tools.IsEmpty(r["ied_name"])
  1272. tmpIednameParts := scdParseMgr.ParseIedName(inAreaIedName)
  1273. lastChar := tmpIednameParts[7]
  1274. if tmpIednameParts[6] == iednameParts[6] && lastChar == iednameParts[7] {
  1275. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, "P", "M"))
  1276. //MM装置
  1277. mmIedName := c.getMMName(tmpIednameParts, ieds, iednameParts[7])
  1278. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, mmIedName, "M", "M"))
  1279. break
  1280. }
  1281. }
  1282. for _, r := range m {
  1283. //中压侧
  1284. inAreaIedName = tools.IsEmpty(r["ied_name"])
  1285. tmpIednameParts := scdParseMgr.ParseIedName(inAreaIedName)
  1286. lastChar := tmpIednameParts[7]
  1287. if tmpIednameParts[6] == iednameParts[6] && (lastChar == "" || lastChar == iednameParts[7]) {
  1288. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, "P", "M"))
  1289. //MM装置
  1290. mmIedName := c.getMMName(tmpIednameParts, ieds, iednameParts[7])
  1291. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, mmIedName, "M", "M"))
  1292. break
  1293. }
  1294. }
  1295. _, err = db.Raw(ins1 + strings.Join(insvalues, ",")).Exec()
  1296. if err != nil {
  1297. logger.Logger.Error(err)
  1298. return
  1299. }
  1300. }
  1301. }
  1302. //线路间隔分析
  1303. //vol:电压等级
  1304. func (c *CheckAreaMgr) pL(modelid int, vol, iedtypes string, ieds map[string]orm.Params) {
  1305. scdParseMgr := new(ScdParse)
  1306. scdNodeMgr := new(ScdNode)
  1307. db := orm.NewOrm()
  1308. //获取装置分组信息
  1309. bgm := new(SysCheckModelIedtypeGroupMgr)
  1310. bgm.Model = T_data_model_iedtype_group{ModelId: modelid}
  1311. groupList := bgm.List()
  1312. scdXmlObj, _ := scdParseMgr.GetScdXmlObjectBySCDID(tools.IsEmpty(c.ScdId))
  1313. iedCode := c.getIedTypeCode(modelid, "PL")
  1314. for _, row := range ieds {
  1315. if tools.IsEmpty(row["vol"]) != vol || tools.IsEmpty(row["ied_type"]) != iedCode[0:1] || tools.IsEmpty(row["p_type"]) != iedCode[1:] {
  1316. continue
  1317. }
  1318. pmIedName := ""
  1319. mmIedName := ""
  1320. pl_iedname := tools.IsEmpty(row["ied_name"])
  1321. if _, h := groupList[pl_iedname]; h {
  1322. //装置如果是分组成员装置,则不用检测
  1323. continue
  1324. }
  1325. iednameParts := scdParseMgr.ParseIedName(pl_iedname)
  1326. areadesc := tools.IsEmpty(row["ied_desc"])
  1327. if areadesc == "" {
  1328. iedobj := scdNodeMgr.GetIed(scdXmlObj, "", pl_iedname)
  1329. if iedobj != nil {
  1330. areadesc = iedobj.Desc
  1331. } else {
  1332. areadesc = tools.IsEmpty(row["area_name"])
  1333. }
  1334. }
  1335. areadesc = strings.ReplaceAll(areadesc, "装置", "")
  1336. //添加间隔数据
  1337. dbdata := T_data_check_area{
  1338. ModelId: modelid,
  1339. ScdId: c.ScdId,
  1340. AreaType: "L",
  1341. AreaName: areadesc + iednameParts[7],
  1342. }
  1343. newid, err := db.Insert(&dbdata)
  1344. if err != nil {
  1345. logger.Logger.Error(err)
  1346. return
  1347. }
  1348. ins1 := "insert into t_data_check_area_ied(scd_id,area_id,ied_name,ied_type,p_type)values"
  1349. insvalues := []string{}
  1350. pmCode := c.getIedTypeCode(modelid, "PM")
  1351. mmCode := c.getIedTypeCode(modelid, "MM")
  1352. clCode := c.getIedTypeCode(modelid, "CL")
  1353. for _, ty := range strings.Split(iedtypes, ",") {
  1354. inAreaIedName := ""
  1355. if _, h := groupList[ty]; h {
  1356. //装置如果是分组成员装置,则不用检测
  1357. continue
  1358. }
  1359. tyCode := c.getIedTypeCode(modelid, ty)
  1360. if _, h := groupList[tyCode]; h {
  1361. //装置如果是分组成员装置,则不用检测
  1362. continue
  1363. }
  1364. if tyCode == pmCode {
  1365. //母线保护和母线合并单元装置编号跟随变压器
  1366. //查找变压器编号
  1367. //1号变压器->2号变压器->3号
  1368. inAreaIedName = c.getPMName(iednameParts, ieds)
  1369. pmIedName = inAreaIedName
  1370. if mmIedName == "" {
  1371. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, ty[0:1], ty[1:2]))
  1372. //pmIedNameParts := scdParseMgr.ParseIedName(pmIedName)
  1373. //使用PM装置的名称去定义MM的名称
  1374. tyCode = mmCode
  1375. inAreaIedName = c.getMMName(iednameParts, ieds, iednameParts[7])
  1376. mmIedName = inAreaIedName
  1377. }
  1378. } else if tyCode == mmCode {
  1379. if pmIedName != "" && mmIedName == "" {
  1380. //pmIedNameParts := scdParseMgr.ParseIedName(pmIedName)
  1381. //使用PM装置的名称去定义MM的名称
  1382. inAreaIedName = c.getMMName(iednameParts, ieds, iednameParts[7])
  1383. mmIedName = inAreaIedName
  1384. } else {
  1385. continue
  1386. }
  1387. } else {
  1388. inAreaIedName = tyCode + iednameParts[3] + iednameParts[4] + iednameParts[5] + iednameParts[6] + iednameParts[7]
  1389. }
  1390. if strings.Index("ABCDE", iednameParts[7]) > -1 {
  1391. //最后一位是字母则说明是AB套
  1392. switch tyCode {
  1393. case clCode:
  1394. if strings.Contains(iedtypes, "PLC") || strings.Contains(iedtypes, "PCL") {
  1395. //不处理CL装置
  1396. } else {
  1397. //测控装置,先判断是否分了AB套,没有标识(ied名称的最后一位是否是字母)则说明是多套共用装置
  1398. clIedname := inAreaIedName + iednameParts[7]
  1399. iedObj := ieds[inAreaIedName]
  1400. if iedObj != nil {
  1401. //当前测控装置也分了AB套
  1402. inAreaIedName = clIedname
  1403. }
  1404. }
  1405. break
  1406. default:
  1407. break
  1408. }
  1409. }
  1410. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, tyCode[0:1], tyCode[1:]))
  1411. }
  1412. _, err = db.Raw(ins1 + strings.Join(insvalues, ",")).Exec()
  1413. if err != nil {
  1414. logger.Logger.Error(err)
  1415. return
  1416. }
  1417. //如果mm装置还未确定
  1418. if mmIedName == "" {
  1419. //pmIedNameParts := scdParseMgr.ParseIedName(pmIedName)
  1420. inAreaIedName := c.getMMName(iednameParts, ieds, iednameParts[7])
  1421. _, err = db.Raw(ins1 + fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, "M", "M")).Exec()
  1422. if err != nil {
  1423. logger.Logger.Error(err)
  1424. return
  1425. }
  1426. }
  1427. }
  1428. }
  1429. //母联间隔分析
  1430. func (c *CheckAreaMgr) pJ(modelid int, vol, iedtypes string, ieds map[string]orm.Params, pjIed string) {
  1431. scdParseMgr := new(ScdParse)
  1432. scdNodeMgr := new(ScdNode)
  1433. db := orm.NewOrm()
  1434. pjIed = c.getIedTypeCode(modelid, pjIed)
  1435. scdXmlObject, _ := scdParseMgr.GetScdXmlObjectBySCDID(tools.IsEmpty(c.ScdId))
  1436. for _, row := range ieds {
  1437. if tools.IsEmpty(row["vol"]) != vol || tools.IsEmpty(row["ied_type"]) != pjIed[0:1] || tools.IsEmpty(row["p_type"]) != pjIed[1:2] {
  1438. continue
  1439. }
  1440. pmIedName := ""
  1441. mmIedName := ""
  1442. pl_iedname := tools.IsEmpty(row["ied_name"])
  1443. iednameParts := scdParseMgr.ParseIedName(pl_iedname)
  1444. areadesc := tools.IsEmpty(row["ied_desc"])
  1445. if areadesc == "" {
  1446. iedobj := scdNodeMgr.GetIed(scdXmlObject, "", pl_iedname)
  1447. if iedobj != nil {
  1448. areadesc = iedobj.Desc
  1449. } else {
  1450. areadesc = tools.IsEmpty(row["area_name"])
  1451. }
  1452. }
  1453. areadesc = strings.ReplaceAll(areadesc, "装置", "")
  1454. //添加间隔数据
  1455. dbdata := T_data_check_area{
  1456. ModelId: modelid,
  1457. ScdId: c.ScdId,
  1458. AreaType: "J",
  1459. AreaName: areadesc + iednameParts[7],
  1460. }
  1461. newid, err := db.Insert(&dbdata)
  1462. if err != nil {
  1463. logger.Logger.Error(err)
  1464. return
  1465. }
  1466. ins1 := "insert into t_data_check_area_ied(scd_id,area_id,ied_name,ied_type,p_type)values"
  1467. insvalues := []string{}
  1468. pmCode := c.getIedTypeCode(modelid, "PM")
  1469. mmCode := c.getIedTypeCode(modelid, "MM")
  1470. for _, ty := range strings.Split(iedtypes, ",") {
  1471. tyCode := c.getIedTypeCode(modelid, ty)
  1472. inAreaIedName := ""
  1473. if tyCode == pmCode {
  1474. //母线保护和母线合并单元装置编号跟随变压器
  1475. //查找变压器编号
  1476. //1号变压器->2号变压器->3号
  1477. inAreaIedName = c.getPMName(iednameParts, ieds)
  1478. pmIedName = inAreaIedName
  1479. if mmIedName == "" {
  1480. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, tyCode[0:1], tyCode[1:2]))
  1481. pmIedNameParts := scdParseMgr.ParseIedName(pmIedName)
  1482. //使用PM装置的名称去定义MM的名称
  1483. tyCode = "MM"
  1484. inAreaIedName = c.getMMName(pmIedNameParts, ieds, iednameParts[7])
  1485. mmIedName = inAreaIedName
  1486. }
  1487. } else if tyCode == mmCode {
  1488. if pmIedName != "" && mmIedName == "" {
  1489. pmIedNameParts := scdParseMgr.ParseIedName(pmIedName)
  1490. //使用PM装置的名称去定义MM的名称
  1491. inAreaIedName = c.getMMName(pmIedNameParts, ieds, iednameParts[7])
  1492. mmIedName = inAreaIedName
  1493. } else {
  1494. continue
  1495. }
  1496. } else {
  1497. tyCode = tyCode[0:1] + pjIed[1:2]
  1498. inAreaIedName = tyCode + iednameParts[3] + iednameParts[4] + iednameParts[5] + iednameParts[6] + iednameParts[7]
  1499. //判断与基准保护装置相同套号的装置是否存在
  1500. if ieds[inAreaIedName] == nil {
  1501. //尝试去除套号
  1502. inAreaIedName = tyCode + iednameParts[3] + iednameParts[4] + iednameParts[5] + iednameParts[6]
  1503. }
  1504. }
  1505. insvalues = append(insvalues, fmt.Sprintf("(%d,%d,'%s','%s','%s')", c.ScdId, newid, inAreaIedName, tyCode[0:1], tyCode[1:2]))
  1506. }
  1507. _, err = db.Raw(ins1 + strings.Join(insvalues, ",")).Exec()
  1508. if err != nil {
  1509. logger.Logger.Error(err)
  1510. return
  1511. }
  1512. }
  1513. }
  1514. //母联间隔装置关系检查
  1515. func (c *CheckAreaMgr) cJ(modelid string, scdXmlObj *node_attr.SCL, scdNodeMgr *ScdNode, scdNodeRule *ScdNodeRule, area_name string, ied_refs []orm.Params, area_ieds []orm.Params, pjIed, area_ruleid string) {
  1516. modelidInt, _ := strconv.Atoi(modelid)
  1517. pjIed = c.getIedTypeCode(modelidInt, pjIed)
  1518. masterIed := new(node_attr.NIED)
  1519. findIedName := ""
  1520. for _, row2 := range area_ieds {
  1521. findIedName = tools.IsEmpty(row2["ied_name"])
  1522. if strings.HasPrefix(findIedName, pjIed) {
  1523. masterIed = scdNodeMgr.GetIed(scdXmlObj, "", findIedName)
  1524. break
  1525. }
  1526. }
  1527. if masterIed == nil {
  1528. return
  1529. }
  1530. dealFromIed := map[string]int{}
  1531. for _, row := range ied_refs {
  1532. fromiedtype := c.getIedTypeCode(modelidInt, tools.IsEmpty(row["from_ied_code"]))
  1533. toiedtype := c.getIedTypeCode(modelidInt, tools.IsEmpty(row["to_ied_code"]))
  1534. reftype := tools.IsEmpty(row["in_type"])
  1535. tmpFromAreaIeds := []orm.Params{}
  1536. tmpToAreaIeds := []orm.Params{}
  1537. for _, row2 := range area_ieds {
  1538. findIedName = tools.IsEmpty(row2["ied_name"])
  1539. if strings.HasPrefix(findIedName, fromiedtype) {
  1540. masterIed = scdNodeMgr.GetIed(scdXmlObj, "", findIedName)
  1541. if masterIed != nil {
  1542. tmpFromAreaIeds = append(tmpFromAreaIeds, row2)
  1543. } else {
  1544. if dealFromIed[findIedName] == 0 {
  1545. parse_result := fmt.Sprintf("间隔%s的装置%s缺失", area_name, findIedName)
  1546. r := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result}
  1547. //检查未通过
  1548. scdNodeRule.AppendFcdaCheckResult(r)
  1549. }
  1550. dealFromIed[findIedName] = 1
  1551. }
  1552. }
  1553. if strings.HasPrefix(findIedName, toiedtype) {
  1554. tmpToAreaIeds = append(tmpToAreaIeds, row2)
  1555. }
  1556. }
  1557. if len(tmpFromAreaIeds) == 0 {
  1558. continue
  1559. }
  1560. if len(tmpToAreaIeds) == 0 {
  1561. parse_result := fmt.Sprintf("间隔%s缺失类型为%s的装置", area_name, toiedtype)
  1562. r := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result}
  1563. //检查未通过
  1564. scdNodeRule.AppendFcdaCheckResult(r)
  1565. continue
  1566. }
  1567. toIedname := ""
  1568. for _, row2 := range tmpFromAreaIeds {
  1569. findIedName = tools.IsEmpty(row2["ied_name"])
  1570. hasToIedRef := false
  1571. hasToIed := false
  1572. for _, row3 := range tmpToAreaIeds {
  1573. toIedname = tools.IsEmpty(row3["ied_name"])
  1574. masterIed = scdNodeMgr.GetIed(scdXmlObj, "", toIedname)
  1575. if masterIed != nil {
  1576. hasToIed = true
  1577. // 获取该ied的输出(ref_type为0)装置,并从中检测是否存在toiedtype类型的装置
  1578. inout, _ := scdNodeMgr.GetIedRelations(map[string]interface{}{"scd_id": c.ScdId, "ied_name": findIedName})
  1579. logger.Logger.Debug(fmt.Sprintf("ied:%s refs:%+v", findIedName, inout))
  1580. if inout != nil {
  1581. outiedlist := inout[findIedName].(orm.Params)["list"].([]orm.Params)
  1582. for _, ieditem := range outiedlist {
  1583. outiedname := ieditem["ref_ied_name"].(string)
  1584. if outiedname == toIedname && ieditem["ref_type"].(string) == "0" {
  1585. hasToIedRef = true
  1586. break
  1587. }
  1588. }
  1589. }
  1590. if !hasToIedRef {
  1591. parse_result := fmt.Sprintf("间隔%s的装置%s缺失与装置%s的%s信号关联", area_name, findIedName, toIedname, reftype)
  1592. r := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result}
  1593. //检查未通过
  1594. scdNodeRule.AppendFcdaCheckResult(r)
  1595. }
  1596. }
  1597. }
  1598. if !hasToIed {
  1599. parse_result := fmt.Sprintf("间隔%s的装置%s缺失关联装置%s", area_name, findIedName, toIedname)
  1600. r := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result}
  1601. //检查未通过
  1602. scdNodeRule.AppendFcdaCheckResult(r)
  1603. }
  1604. }
  1605. }
  1606. }
  1607. //线路间隔装置关系检查
  1608. func (c *CheckAreaMgr) cL(modelid string, scdXmlObj *node_attr.SCL, scdNodeMgr *ScdNode, scdNodeRule *ScdNodeRule, area_name string, ied_refs []orm.Params, area_ieds []orm.Params, area_ruleid string) {
  1609. modelidInt, _ := strconv.Atoi(modelid)
  1610. //获取装置分组信息
  1611. bgm := new(SysCheckModelIedtypeGroupMgr)
  1612. bgm.Model = T_data_model_iedtype_group{ModelId: modelidInt}
  1613. groupList := bgm.List()
  1614. plIed := c.getIedTypeCode(modelidInt, "PL")
  1615. if v, h := groupList[plIed]; h {
  1616. //装置如果是分组成员装置
  1617. plIed = v
  1618. }
  1619. masterIed := new(node_attr.NIED)
  1620. findIedName := ""
  1621. for _, row2 := range area_ieds {
  1622. findIedName = tools.IsEmpty(row2["ied_name"])
  1623. if strings.HasPrefix(findIedName, plIed) {
  1624. masterIed = scdNodeMgr.GetIed(scdXmlObj, "", findIedName)
  1625. break
  1626. }
  1627. }
  1628. if masterIed == nil {
  1629. return
  1630. }
  1631. logger.Logger.Debug(fmt.Sprintf("开始分析模型%d的线路间隔关系", modelidInt))
  1632. dealFromIed := map[string]int{}
  1633. for _, row := range ied_refs {
  1634. fromiedtype := c.getIedTypeCode(modelidInt, tools.IsEmpty(row["from_ied_code"]))
  1635. toiedtype := c.getIedTypeCode(modelidInt, tools.IsEmpty(row["to_ied_code"]))
  1636. reftype := tools.IsEmpty(row["in_type"])
  1637. tmpFromAreaIeds := []orm.Params{}
  1638. tmpToAreaIeds := []orm.Params{}
  1639. for _, row2 := range area_ieds {
  1640. findIedName = tools.IsEmpty(row2["ied_name"])
  1641. if strings.HasPrefix(findIedName, fromiedtype) {
  1642. masterIed = scdNodeMgr.GetIed(scdXmlObj, "", findIedName)
  1643. if masterIed != nil {
  1644. tmpFromAreaIeds = append(tmpFromAreaIeds, row2)
  1645. } else {
  1646. if dealFromIed[findIedName] == 0 {
  1647. parse_result := fmt.Sprintf("间隔%s的装置%s缺失", area_name, findIedName)
  1648. r := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  1649. "ied_name": findIedName,
  1650. "ied_desc": "",
  1651. "out_ied_name": "",
  1652. "out_ied_desc": "",
  1653. "fcda_desc": "",
  1654. "fcda_addr": "",
  1655. "out_fcda_desc": "",
  1656. "out_fcda_addr": "",
  1657. "error_type": "9",
  1658. }
  1659. //检查未通过
  1660. scdNodeRule.AppendFcdaCheckResult(r)
  1661. }
  1662. dealFromIed[findIedName] = 1
  1663. }
  1664. }
  1665. if strings.HasPrefix(findIedName, toiedtype) {
  1666. tmpToAreaIeds = append(tmpToAreaIeds, row2)
  1667. }
  1668. }
  1669. if len(tmpFromAreaIeds) == 0 {
  1670. continue
  1671. }
  1672. if len(tmpToAreaIeds) == 0 {
  1673. parse_result := fmt.Sprintf("间隔%s缺失类型为%s的装置", area_name, toiedtype)
  1674. r := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result}
  1675. //检查未通过
  1676. scdNodeRule.AppendFcdaCheckResult(r)
  1677. continue
  1678. }
  1679. toIedname := ""
  1680. for _, row2 := range tmpFromAreaIeds {
  1681. findIedName = tools.IsEmpty(row2["ied_name"])
  1682. hasToIedRef := false
  1683. hasToIed := false
  1684. for _, row3 := range tmpToAreaIeds {
  1685. toIedname = tools.IsEmpty(row3["ied_name"])
  1686. masterIed = scdNodeMgr.GetIed(scdXmlObj, "", toIedname)
  1687. if masterIed != nil {
  1688. hasToIed = true
  1689. // 获取该ied的输出(ref_type为0)装置,并从中检测是否存在toiedtype类型的装置
  1690. inout, _ := scdNodeMgr.GetIedRelations(map[string]interface{}{"scd_id": c.ScdId, "ied_name": findIedName})
  1691. //logger.Logger.Debug(fmt.Sprintf("ied:%s refs:%+v", findIedName, inout))
  1692. if inout != nil {
  1693. outiedlist := inout[findIedName].(orm.Params)["list"].([]orm.Params)
  1694. for _, ieditem := range outiedlist {
  1695. outiedname := ieditem["ref_ied_name"].(string)
  1696. if outiedname == toIedname && ieditem["ref_type"].(string) == "0" {
  1697. hasToIedRef = true
  1698. break
  1699. }
  1700. }
  1701. }
  1702. if !hasToIedRef {
  1703. parse_result := fmt.Sprintf("间隔%s的装置%s缺失与装置%s的%s信号关联", area_name, findIedName, toIedname, reftype)
  1704. r := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  1705. "ied_name": findIedName,
  1706. "ied_desc": "",
  1707. "out_ied_name": toIedname,
  1708. "out_ied_desc": "",
  1709. "fcda_desc": "",
  1710. "fcda_addr": "",
  1711. "out_fcda_desc": "",
  1712. "out_fcda_addr": "",
  1713. "error_type": "9",
  1714. }
  1715. //检查未通过
  1716. scdNodeRule.AppendFcdaCheckResult(r)
  1717. }
  1718. }
  1719. }
  1720. if !hasToIed {
  1721. parse_result := fmt.Sprintf("间隔%s的装置%s缺失关联装置%s", area_name, findIedName, toIedname)
  1722. r := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  1723. "ied_name": findIedName,
  1724. "ied_desc": "",
  1725. "out_ied_name": toIedname,
  1726. "out_ied_desc": "",
  1727. "fcda_desc": "",
  1728. "fcda_addr": "",
  1729. "out_fcda_desc": "",
  1730. "out_fcda_addr": "",
  1731. "error_type": "9",
  1732. }
  1733. //检查未通过
  1734. scdNodeRule.AppendFcdaCheckResult(r)
  1735. }
  1736. }
  1737. }
  1738. }
  1739. //变压器间隔装置关系检查
  1740. func (c *CheckAreaMgr) cT(modelid string, scdXmlObj *node_attr.SCL, scdNodeMgr *ScdNode, scdNodeRule *ScdNodeRule, area_name string, ied_refs []orm.Params, area_ieds []orm.Params, area_ruleid string, HasAreaJ bool) {
  1741. masterIed := new(node_attr.NIED)
  1742. findIedName := ""
  1743. modelidInt, _ := strconv.Atoi(modelid)
  1744. ptIed := c.getIedTypeCode(modelidInt, "PT")
  1745. for _, row2 := range area_ieds {
  1746. findIedName = tools.IsEmpty(row2["ied_name"])
  1747. if strings.HasPrefix(findIedName, ptIed) {
  1748. masterIed = scdNodeMgr.GetIed(scdXmlObj, "", findIedName)
  1749. break
  1750. }
  1751. }
  1752. if masterIed == nil {
  1753. return
  1754. }
  1755. scdParseMgr := new(ScdParse)
  1756. dealFromIed := map[string]int{}
  1757. for _, row := range ied_refs {
  1758. fromiedtype := c.getIedTypeCode(modelidInt, tools.IsEmpty(row["from_ied_code"]))
  1759. fromiedtype = strings.Split(fromiedtype, "#")[0] //去除后面的电压级别标识
  1760. toiedtype := strings.Split(c.getIedTypeCode(modelidInt, tools.IsEmpty(row["to_ied_code"])), "#")[0] //去除后面的电压级别标识
  1761. reftype := tools.IsEmpty(row["in_type"])
  1762. tmpFromAreaIeds := []orm.Params{}
  1763. tmpToAreaIeds := []orm.Params{}
  1764. for _, row2 := range area_ieds {
  1765. findIedName = tools.IsEmpty(row2["ied_name"])
  1766. if strings.HasPrefix(findIedName, fromiedtype) {
  1767. masterIed = scdNodeMgr.GetIed(scdXmlObj, "", findIedName)
  1768. if masterIed != nil {
  1769. tmpFromAreaIeds = append(tmpFromAreaIeds, row2)
  1770. } else {
  1771. if dealFromIed[findIedName] == 0 {
  1772. parse_result := fmt.Sprintf("间隔%s的装置%s缺失", area_name, findIedName)
  1773. r := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  1774. "ied_name": findIedName,
  1775. "ied_desc": "",
  1776. "out_ied_name": "",
  1777. "out_ied_desc": "",
  1778. "fcda_desc": "",
  1779. "fcda_addr": "",
  1780. "out_fcda_desc": "",
  1781. "out_fcda_addr": "",
  1782. "error_type": "9",
  1783. }
  1784. //检查未通过
  1785. scdNodeRule.AppendFcdaCheckResult(r)
  1786. }
  1787. dealFromIed[findIedName] = 1
  1788. }
  1789. }
  1790. if strings.HasPrefix(findIedName, toiedtype) {
  1791. tmpToAreaIeds = append(tmpToAreaIeds, row2)
  1792. }
  1793. }
  1794. if len(tmpFromAreaIeds) == 0 {
  1795. continue
  1796. }
  1797. if len(tmpToAreaIeds) == 0 {
  1798. logger.Logger.Debug(fmt.Sprintf("缺失类型关联装置 :%+v", row))
  1799. parse_result := fmt.Sprintf("间隔%s缺失类型为%s的%s信号装置", area_name, toiedtype, reftype)
  1800. r := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  1801. "ied_name": findIedName,
  1802. "ied_desc": "",
  1803. "out_ied_name": "",
  1804. "out_ied_desc": "",
  1805. "fcda_desc": "",
  1806. "fcda_addr": "",
  1807. "out_fcda_desc": "",
  1808. "out_fcda_addr": "",
  1809. "error_type": "9",
  1810. }
  1811. //检查未通过
  1812. scdNodeRule.AppendFcdaCheckResult(r)
  1813. continue
  1814. }
  1815. toIedname := ""
  1816. for _, row2 := range tmpFromAreaIeds {
  1817. findIedName = tools.IsEmpty(row2["ied_name"])
  1818. hasToIedRef := false
  1819. hasToIed := false
  1820. t1 := fromiedtype + "->" + toiedtype
  1821. volLevel := "" //电压等级
  1822. if t1 == "CT->IT" || t1 == "IT->CT" || t1 == "CT->IB" || t1 == "IB->CT" || t1 == "MM->MT" || t1 == "PM->IB" || t1 == "IB->PM" || t1 == "PM->IT" || t1 == "IT->PM" {
  1823. ps := scdParseMgr.ParseIedName(findIedName)
  1824. volLevel = ps[3] + ps[4]
  1825. }
  1826. hasSameVolIed := false
  1827. for _, row3 := range tmpToAreaIeds {
  1828. toIedname = tools.IsEmpty(row3["ied_name"])
  1829. if volLevel != "" {
  1830. ps := scdParseMgr.ParseIedName(toIedname)
  1831. if volLevel != ps[3]+ps[4] {
  1832. //排除不是同一电压等级的装置
  1833. continue
  1834. }
  1835. hasSameVolIed = true
  1836. }
  1837. if scdNodeMgr.GetIed(scdXmlObj, "", toIedname) != nil {
  1838. hasToIed = true
  1839. // 获取该ied的输出(ref_type为0)装置,并从中检测是否存在toiedtype类型的装置
  1840. inout, _ := scdNodeMgr.GetIedRelations(map[string]interface{}{"scd_id": c.ScdId, "ied_name": findIedName})
  1841. //logger.Logger.Debug(fmt.Sprintf("ied:%s refs:%+v", findIedName, inout))
  1842. if inout != nil {
  1843. outiedlist := inout[findIedName].(orm.Params)["list"].([]orm.Params)
  1844. for _, ieditem := range outiedlist {
  1845. outiedname := ieditem["ref_ied_name"].(string)
  1846. if outiedname == toIedname && ieditem["ref_type"].(string) == "0" {
  1847. hasToIedRef = true
  1848. break
  1849. }
  1850. }
  1851. }
  1852. if !hasToIedRef {
  1853. parse_result := fmt.Sprintf("间隔%s的装置%s缺失与装置%s的%s信号关联", area_name, findIedName, toIedname, reftype)
  1854. r := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  1855. "ied_name": findIedName,
  1856. "ied_desc": "",
  1857. "out_ied_name": toIedname,
  1858. "out_ied_desc": "",
  1859. "fcda_desc": "",
  1860. "fcda_addr": "",
  1861. "out_fcda_desc": "",
  1862. "out_fcda_addr": "",
  1863. "error_type": "9",
  1864. }
  1865. //检查未通过
  1866. scdNodeRule.AppendFcdaCheckResult(r)
  1867. }
  1868. }
  1869. if hasSameVolIed {
  1870. break
  1871. }
  1872. }
  1873. if toiedtype != ptIed {
  1874. if volLevel != "" && !hasSameVolIed {
  1875. parse_result := fmt.Sprintf("间隔%s的装置%s缺失同电压等级的关联类型%s装置", area_name, findIedName, toiedtype)
  1876. r := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  1877. "ied_name": findIedName,
  1878. "ied_desc": "",
  1879. "out_ied_name": "",
  1880. "out_ied_desc": "",
  1881. "fcda_desc": "",
  1882. "fcda_addr": "",
  1883. "out_fcda_desc": "",
  1884. "out_fcda_addr": "",
  1885. "error_type": "9",
  1886. }
  1887. //检查未通过
  1888. scdNodeRule.AppendFcdaCheckResult(r)
  1889. } else if !hasToIed {
  1890. parse_result := fmt.Sprintf("间隔%s的装置%s缺失关联装置%s", area_name, findIedName, toIedname)
  1891. r := map[string]interface{}{"scdid": c.ScdId, "lineno": 0, "ruleid": area_ruleid, "nodeid": 0, "parse_result": parse_result,
  1892. "ied_name": findIedName,
  1893. "ied_desc": "",
  1894. "out_ied_name": toIedname,
  1895. "out_ied_desc": "",
  1896. "fcda_desc": "",
  1897. "fcda_addr": "",
  1898. "out_fcda_desc": "",
  1899. "out_fcda_addr": "",
  1900. "error_type": "9",
  1901. }
  1902. //检查未通过
  1903. scdNodeRule.AppendFcdaCheckResult(r)
  1904. }
  1905. }
  1906. }
  1907. }
  1908. }
  1909. //根据参考ied name找出应该关联PM装置
  1910. func (c *CheckAreaMgr) getPMName(iednameParts []string, ieds map[string]orm.Params) string {
  1911. //如果只有一个PM装置,则直接返回该 装置
  1912. pmLst := []string{}
  1913. for n, _ := range ieds {
  1914. if strings.HasPrefix(n, "PM"+iednameParts[3]+iednameParts[4]) {
  1915. pmLst = append(pmLst, n)
  1916. }
  1917. }
  1918. if len(pmLst) == 1 {
  1919. return pmLst[0]
  1920. }
  1921. //暴力匹配
  1922. for _, n := range pmLst {
  1923. if strings.HasSuffix(n, iednameParts[7]) {
  1924. return n
  1925. }
  1926. }
  1927. return ""
  1928. }
  1929. //根据参考ied name找出应该关联MM装置
  1930. func (c *CheckAreaMgr) getMMName(iednameParts []string, ieds map[string]orm.Params, ab string) string {
  1931. mmLst := []string{}
  1932. for n, _ := range ieds {
  1933. if strings.HasPrefix(n, "MM"+iednameParts[3]+iednameParts[4]) {
  1934. mmLst = append(mmLst, n)
  1935. }
  1936. }
  1937. if len(mmLst) == 1 {
  1938. return mmLst[0]
  1939. }
  1940. for _, n := range mmLst {
  1941. if strings.HasSuffix(n, ab) {
  1942. return n
  1943. }
  1944. }
  1945. return ""
  1946. }
  1947. //根据当前设备列表,分析出电压等级(高、中、低)的设备列表
  1948. // CT测控、IT智能终端、MT合并单元需要判断是否是本体装置,是则将其归为主变高压侧
  1949. func (c *CheckAreaMgr) getIedListByVol(iedtype string, ieds map[string]orm.Params, vollevel map[string]string) (hightLst, middleLst, lowLst []orm.Params) {
  1950. tmpLst := map[string][]orm.Params{}
  1951. for _, v := range vollevel {
  1952. tmpLst[v] = []orm.Params{}
  1953. }
  1954. scdParseMgr := new(ScdParse)
  1955. for _, row := range ieds {
  1956. pl_iedname := tools.IsEmpty(row["ied_name"])
  1957. if pl_iedname[0:len(iedtype)] != iedtype {
  1958. continue
  1959. }
  1960. iednameParts := scdParseMgr.ParseIedName(pl_iedname)
  1961. volvalue := iednameParts[3] + iednameParts[4]
  1962. if tmpLst[volvalue] == nil {
  1963. tmpLst[volvalue] = []orm.Params{row}
  1964. } else {
  1965. tmpLst[volvalue] = append(tmpLst[volvalue], row)
  1966. }
  1967. }
  1968. return tmpLst[vollevel["hight"]], tmpLst[vollevel["middle"]], tmpLst[vollevel["low"]]
  1969. }
  1970. func (c *CheckAreaMgr) GetCheckResult(scdid int64) ([]orm.Params, error) {
  1971. db := orm.NewOrm()
  1972. rowset := []orm.Params{}
  1973. sql := "select *,case error_type when 1 then '多余' when 2 then '错误' when 3 then '缺失' else '其他' end error_type_desc from t_scd_fcda_check_result where scd_id=? order by ied_name"
  1974. _, err := db.Raw(sql, scdid).Values(&rowset)
  1975. return rowset, err
  1976. }