checktools_area.go 66 KB

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