check_sysmodel_ied_func_fcda.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package bo
  2. import (
  3. "errors"
  4. "fmt"
  5. "scd_check_tools/logger"
  6. "scd_check_tools/models/enum"
  7. "scd_check_tools/tools"
  8. "strconv"
  9. "strings"
  10. "github.com/astaxie/beego/orm"
  11. )
  12. //系统内置模型-装置功能点端子管理
  13. type T_data_model_func_fcda struct {
  14. Id int `orm:"pk"`
  15. ModelId int // '模型ID' ,
  16. FuncId int // '功能ID',
  17. FcdaName string // 端子设计名称
  18. FcdaMatchExp string // 端子匹配表达式
  19. Svorgoose string
  20. Inorout string
  21. ReceiveIedType string
  22. Cr int // '创建人' ,
  23. Ct string `orm:"-"` // '创建时间' ,
  24. Ur int // '更新人' ,
  25. Ut string `orm:"-"` // '更新时间'
  26. }
  27. //内置检测模型-装置功能点端子管理
  28. type SysCheckModelIedFuncFcdaMgr struct {
  29. Model T_data_model_func_fcda
  30. DeviceBaseModel
  31. }
  32. var sysCheckModel_iedFuncFcdaDesc = "内置检测模型-装置功能点端子管理"
  33. func init() {
  34. orm.RegisterModel(new(T_data_model_func_fcda))
  35. }
  36. //保存检测模型装置功能信息
  37. func (c *SysCheckModelIedFuncFcdaMgr) Save() (id int, err error) {
  38. dblog := new(SystemLog)
  39. dblog.SetUserInfo(c.GetUserInfo())
  40. dblog.Audittype = enum.AuditType_check_model
  41. dblog.Logtype = enum.LogType_Insert
  42. dblog.Eventtype = enum.OptEventType_Bus
  43. dblog.Eventlevel = enum.OptEventLevel_Hight
  44. db := orm.NewOrm()
  45. /*
  46. hasName, _, err := c.Exist()
  47. if err != nil {
  48. return 0, err
  49. }
  50. if hasName {
  51. return 0, errors.New("端子名称[" + c.Model.FcdaName + "]已存在")
  52. }
  53. */
  54. newid := int64(0)
  55. if c.Model.Id > 0 {
  56. //编辑
  57. _, err = db.Update(&c.Model)
  58. } else {
  59. //新增
  60. newid, err = db.Insert(&c.Model)
  61. c.Model.Id = int(newid)
  62. }
  63. if err != nil {
  64. logger.Logger.Error(err)
  65. dblog.Description = fmt.Sprintf("保存%s信息失败:%s,操作数据:%+v", sysCheckModel_iedFuncFcdaDesc, err.Error(), c.Model)
  66. dblog.Fail2()
  67. return 0, err
  68. } else {
  69. dblog.Description = fmt.Sprintf("保存%s信息成功,操作数据:%+v", sysCheckModel_iedFuncFcdaDesc, c.Model)
  70. dblog.Success2()
  71. }
  72. return c.Model.Id, nil
  73. }
  74. func (c *SysCheckModelIedFuncFcdaMgr) Exist() (bool, int, error) {
  75. db := orm.NewOrm()
  76. if c.Model.FcdaName == "" {
  77. return false, 0, errors.New("端子设计名称不能为空")
  78. }
  79. rowset := []orm.Params{}
  80. _, err := db.Raw("select id from t_data_model_func_fcda where model_id=? and func_id=? and fcda_name=?", c.Model.ModelId, c.Model.FuncId, c.Model.FcdaName).Values(&rowset)
  81. if len(rowset) > 0 {
  82. findId := tools.IsEmpty(rowset[0]["id"])
  83. findIdint, _ := strconv.Atoi(findId)
  84. if tools.IsEmpty(c.Model.Id) != findId {
  85. //端子名称重复
  86. return true, c.Model.Id, nil
  87. } else {
  88. return false, findIdint, nil
  89. }
  90. }
  91. return false, 0, err
  92. }
  93. func (c *SysCheckModelIedFuncFcdaMgr) Copy(oldFuncId, newModelId, newFuncId int) error {
  94. db := orm.NewOrm()
  95. rowset := []orm.Params{}
  96. db.Raw("select * from t_data_model_func_fcda where model_id=? and func_id=?", c.Model.ModelId, oldFuncId).Values(&rowset)
  97. for _, row := range rowset {
  98. oldFcdaId, _ := strconv.Atoi(tools.IsEmpty(row["id"]))
  99. //复制端子数据
  100. newFcda := T_data_model_func_fcda{
  101. ModelId: newModelId,
  102. FuncId: newFuncId,
  103. FcdaName: tools.IsEmpty(row["fcda_name"]),
  104. FcdaMatchExp: tools.IsEmpty(row["fcda_match_exp"]),
  105. Svorgoose: tools.IsEmpty(row["svorgoose"]),
  106. Inorout: tools.IsEmpty(row["inorout"]),
  107. }
  108. newid, err := db.Insert(&newFcda)
  109. if err != nil {
  110. logger.Logger.Error(err)
  111. return err
  112. }
  113. db.Raw("update t_data_model_fcda_ref set from_func_id=?,from_fcda_id=? where model_id=? and from_func_id=? and from_fcda_id=?", newFuncId, newid, newModelId, oldFuncId, oldFcdaId).Exec()
  114. db.Raw("update t_data_model_fcda_ref set to_func_id=?,to_fcda_id=? where model_id=? and to_func_id=? and to_fcda_id=?", newFuncId, newid, newModelId, oldFuncId, oldFcdaId).Exec()
  115. }
  116. return nil
  117. }
  118. //自动建立端子关系
  119. func (c *SysCheckModelIedFuncFcdaMgr) AutoRelation(modelid int, relationrow []T_data_model_func_def) {
  120. if len(relationrow) == 0 {
  121. return
  122. }
  123. fcdaMap := map[string]T_data_model_func_def{}
  124. for _, row := range relationrow {
  125. receiveIedType := strings.Split(row.ReceiveIedType, ",")
  126. for _, rit := range receiveIedType {
  127. key := fmt.Sprintf("%s%s%s", row.IedType, strings.ReplaceAll(row.FcdaName, "*", ""), rit)
  128. if strings.Index(row.FuncName, "额定延") > -1 {
  129. if strings.Index(row.FcdaName, "额定") > -1 && (strings.Index(row.FcdaName, "延迟") > -1 || strings.Index(row.FcdaName, "延时") > -1) {
  130. key = fmt.Sprintf("%s%s%s", row.IedType, "额定延时", rit)
  131. }
  132. }
  133. fcdaMap[key] = row
  134. }
  135. }
  136. //获取装置分组信息
  137. bgm := new(SysCheckModelIedtypeGroupMgr)
  138. bgm.Model = T_data_model_iedtype_group{ModelId: modelid}
  139. groupList := bgm.List()
  140. var getRealIedCode = func(ied_type string, groupList map[string]string) string {
  141. /*
  142. if groupList != nil && groupList[ied_type] != "" {
  143. ied_type = groupList[ied_type]
  144. }
  145. */
  146. return ied_type
  147. }
  148. fr := new(SysCheckModelFcdaRalationMgr)
  149. fr.Model = T_data_model_fcda_ref{ModelId: modelid}
  150. noMatchList := map[string][]interface{}{} //未完全匹配的列表
  151. for i, row := range relationrow {
  152. if row.ReceiveIedType != "" && strings.Index(row.Inorout, "输出") > -1 {
  153. tmplst := strings.Split(row.ReceiveIedType, ",")
  154. for _, item := range tmplst {
  155. key := fmt.Sprintf("%s%s%s", item, strings.ReplaceAll(row.FcdaName, "*", ""), row.IedType)
  156. if strings.Index(row.FuncName, "额定延") > -1 {
  157. if strings.Index(row.FcdaName, "额定") > -1 && (strings.Index(row.FcdaName, "延迟") > -1 || strings.Index(row.FcdaName, "延时") > -1) {
  158. key = fmt.Sprintf("%s%s%s", item, "额定延时", row.IedType)
  159. }
  160. }
  161. if r, h := fcdaMap[key]; h {
  162. if r.FuncFcdaId == row.FuncFcdaId {
  163. continue
  164. }
  165. fr.Model.FromFcdaId = row.FuncFcdaId
  166. fr.Model.ToFcdaId = r.FuncFcdaId
  167. fr.Model.FromFuncId = row.Id
  168. fr.Model.ToFuncId = r.Id
  169. fr.Model.FromIedCode = getRealIedCode(row.IedType, groupList)
  170. fr.Model.ToIedCode = getRealIedCode(r.IedType, groupList)
  171. fr.Model.Goosesv = row.Svorgoose
  172. fr.Save()
  173. } else {
  174. //未完全匹配到端子时,查找其接收端的对侧装置为当前装置的端子
  175. k := getRealIedCode(row.IedType, groupList) + "," + getRealIedCode(item, groupList) + "," + row.FuncName
  176. if _, h := noMatchList[k]; !h {
  177. tmp := map[string]interface{}{"no": i, "fromrow": row}
  178. noMatchList[k] = []interface{}{tmp}
  179. } else {
  180. noMatchList[k] = append(noMatchList[k], map[string]interface{}{"no": i, "fromrow": row})
  181. }
  182. }
  183. }
  184. }
  185. }
  186. //未完全匹配到端子时,查找其接收端的对侧装置为当前装置的端子
  187. for iedtype, obj := range noMatchList {
  188. ks := strings.Split(iedtype, ",")
  189. //logger.Logger.Debug(fmt.Sprintf("装置类型%s,未匹配的端子列表:\n%+v", iedtype, obj))
  190. subNo := 0 //相差的行数
  191. fromrow := T_data_model_func_def{}
  192. for i, row := range relationrow {
  193. outIedType := getRealIedCode(row.IedType, groupList)
  194. receiveIedType := getRealIedCode(row.ReceiveIedType, groupList)
  195. if outIedType == ks[1] && strings.Index(receiveIedType, ks[0]) > -1 && row.FuncName == ks[2] {
  196. tmp := obj[0].(map[string]interface{})
  197. no, _ := tmp["no"].(int)
  198. subNo = no - i //相差的行数
  199. break
  200. }
  201. }
  202. logger.Logger.Debug(fmt.Sprintf("未建立关系的装置:%s,间隔行数:%d", iedtype, subNo))
  203. for _, item := range obj {
  204. item2 := item.(map[string]interface{})
  205. rowno := item2["no"].(int) - subNo
  206. if rowno < 0 || rowno > len(relationrow)-1 {
  207. logger.Logger.Debug(fmt.Sprintf("建立关系异常数据:%+v", item))
  208. return
  209. }
  210. fromrow = item2["fromrow"].(T_data_model_func_def)
  211. r := relationrow[rowno]
  212. if r.FuncFcdaId == fromrow.FuncFcdaId || r.IedType == fromrow.IedType {
  213. continue
  214. }
  215. fr.Model.FromFcdaId = fromrow.FuncFcdaId
  216. fr.Model.ToFcdaId = r.FuncFcdaId
  217. fr.Model.FromFuncId = fromrow.Id
  218. fr.Model.ToFuncId = r.Id
  219. fr.Model.FromIedCode = getRealIedCode(fromrow.IedType, groupList)
  220. fr.Model.ToIedCode = getRealIedCode(r.IedType, groupList)
  221. fr.Model.Goosesv = fromrow.Svorgoose
  222. fr.Save()
  223. }
  224. }
  225. }
  226. //根据model中指定的id删除
  227. func (c *SysCheckModelIedFuncFcdaMgr) Delete() (err error) {
  228. dblog := new(SystemLog)
  229. dblog.SetUserInfo(c.GetUserInfo())
  230. dblog.Audittype = enum.AuditType_check_model
  231. dblog.Logtype = enum.LogType_Delete
  232. dblog.Eventtype = enum.OptEventType_Bus
  233. dblog.Eventlevel = enum.OptEventLevel_Hight
  234. db := orm.NewOrm()
  235. one := T_data_model_func_fcda{Id: c.Model.Id}
  236. if c.Model.Id > 0 {
  237. db.Read(&one)
  238. _, err = db.Raw("delete from t_data_model_func_fcda where id=?", c.Model.Id).Exec()
  239. } else if c.Model.FuncId > 0 {
  240. _, err = db.Raw("delete from t_data_model_func_fcda where model_id=? and func_id=?", c.Model.ModelId, c.Model.FuncId).Exec()
  241. } else {
  242. _, err = db.Raw("delete from t_data_model_func_fcda where model_id=? ", c.Model.ModelId).Exec()
  243. }
  244. if err != nil {
  245. logger.Logger.Error(err)
  246. dblog.Description = fmt.Sprintf("删除%s(%d)失败:%s", sysCheckModel_iedFuncFcdaDesc, c.Model.ModelId, err.Error())
  247. dblog.Fail2()
  248. } else {
  249. fcdaMgr := new(SysCheckModelFcdaRalationMgr)
  250. fcdaMgr.Model = T_data_model_fcda_ref{ModelId: c.Model.ModelId}
  251. if c.Model.Id > 0 {
  252. fcdaMgr.Model.FromFcdaId = c.Model.Id
  253. } else if c.Model.FuncId > 0 {
  254. fcdaMgr.Model.FromFuncId = c.Model.FuncId
  255. }
  256. fcdaMgr.Delete()
  257. dblog.Description = fmt.Sprintf("删除%s(%d)成功", sysCheckModel_iedFuncFcdaDesc, c.Model.ModelId)
  258. dblog.Success2()
  259. if c.Model.Id > 0 {
  260. //判断端子是否全部删除
  261. if one.FuncId > 0 {
  262. c.Model.ModelId = one.ModelId
  263. c.Model.FuncId = one.FuncId
  264. r, _ := c.GetList("")
  265. if len(r) == 0 {
  266. //删除功能定义数据
  267. funcMgr := new(SysCheckModelIedFuncMgr)
  268. funcMgr.Model.Id = one.FuncId
  269. funcMgr.Delete(true)
  270. }
  271. }
  272. } else if c.Model.FuncId > 0 {
  273. funcMgr := new(SysCheckModelIedFuncMgr)
  274. funcMgr.Model.Id = c.Model.FuncId
  275. funcMgr.Delete(true)
  276. } else {
  277. funcMgr := new(SysCheckModelIedFuncMgr)
  278. funcMgr.Model.ModelId = c.Model.ModelId
  279. funcMgr.Delete(true)
  280. }
  281. }
  282. return err
  283. }
  284. func (c *SysCheckModelIedFuncFcdaMgr) One(id int) (T_data_model_func_fcda, error) {
  285. tmp := T_data_model_func_fcda{Id: id}
  286. db := orm.NewOrm()
  287. err := db.Read(&tmp)
  288. return tmp, err
  289. }
  290. //获取指定模型及装置功能下的端子信息列表
  291. //funcids:指定的功能编码列表。可不传
  292. func (c *SysCheckModelIedFuncFcdaMgr) GetList(refIedtype string, funcids ...[]string) ([]orm.Params, error) {
  293. o := orm.NewOrm()
  294. sqlParamters := []interface{}{c.Model.ModelId}
  295. outsql := ""
  296. desc := ""
  297. /*
  298. mappresult := new(SysCheckModelIedtypeMappingMgr).GetMappingType(c.Model.ModelId, refIedtype)
  299. if mappresult != "" {
  300. refIedtype = mappresult
  301. }
  302. */
  303. //获取装置分组信息
  304. bgm := new(SysCheckModelIedtypeGroupMgr)
  305. bgm.Model = T_data_model_iedtype_group{ModelId: c.Model.ModelId}
  306. groupList := bgm.List()
  307. tmpAry := []string{}
  308. if len(groupList) > 0 {
  309. for k, v := range groupList {
  310. if v == refIedtype {
  311. tmpAry = append(tmpAry, k)
  312. }
  313. }
  314. if len(tmpAry) > 0 {
  315. tmpAry = append(tmpAry, refIedtype) //包含分组编码本身
  316. refIedtype = strings.Join(tmpAry, "','")
  317. }
  318. }
  319. if c.Model.Inorout == "接收" {
  320. outsql = ",(select GROUP_CONCAT(from_fcda_id) from_fcda_id from t_data_model_fcda_ref where model_id=? and to_fcda_id=t.id and to_func_id=t1.id and from_ied_code in ('" + refIedtype + "')) from_fcda_id"
  321. sqlParamters = append(sqlParamters, c.Model.ModelId)
  322. desc = " from_fcda_id," //已有关联关系的端子排在前面
  323. }
  324. if c.Model.Inorout == "输出" {
  325. outsql = ",(select GROUP_CONCAT(to_fcda_id) to_fcda_id from t_data_model_fcda_ref where model_id=? and from_fcda_id=t.id and from_func_id=t1.id and to_ied_code in ('" + refIedtype + "')) to_fcda_id"
  326. sqlParamters = append(sqlParamters, c.Model.ModelId)
  327. }
  328. sql := "select t.*,t1.func_name" + outsql + " from t_data_model_func_fcda t,t_data_model_func_def t1 where t1.id=t.func_id and t.model_id=? "
  329. if c.Model.FuncId > 0 {
  330. sql = sql + " and t.func_id=?"
  331. sqlParamters = append(sqlParamters, c.Model.FuncId)
  332. }
  333. if len(funcids) > 0 {
  334. sql = sql + " and t.func_id in(" + strings.Join(funcids[0], ",") + ")"
  335. }
  336. if c.Model.Svorgoose != "" {
  337. sql = sql + " and t.svorgoose=?"
  338. sqlParamters = append(sqlParamters, c.Model.Svorgoose)
  339. }
  340. if c.Model.Inorout != "" {
  341. sql = sql + " and t.inorout like ?"
  342. sqlParamters = append(sqlParamters, "%"+c.Model.Inorout+"%")
  343. }
  344. sql = sql + " order by " + desc + " t.id"
  345. if c.Model.Inorout == "输出" {
  346. sql = "select a.id,a.model_id,a.func_id,a.func_name,a.fcda_name,a.fcda_match_exp,a.svorgoose,a.inorout,ifnull(a.to_fcda_id,0) to_fcda_id,ifnull(ff1.fcda_name,'') to_fcda_name,ifnull(fd1.ied_type,'') to_ied_type from (" +
  347. sql + ") a left join t_data_model_func_fcda ff1 on a.to_fcda_id=ff1.id LEFT JOIN (select f1.id,case when f2.ied_type is null then f1.ied_type else f2.ied_type end ied_type from t_data_model_func_def f1 LEFT JOIN t_data_model_iedtype_group f2 on f1.ied_type=f2.children_type and f2.model_id=f1.model_id WHERE f1.model_id=?) fd1 on ff1.func_id=fd1.id"
  348. sqlParamters = append(sqlParamters, c.Model.ModelId)
  349. } else if c.Model.Inorout == "接收" {
  350. sql = "select a.id,a.model_id,a.func_id,a.func_name,a.fcda_name,a.fcda_match_exp,a.svorgoose,a.inorout,ifnull(a.from_fcda_id,0) from_fcda_id,ifnull(ff1.fcda_name,'') from_fcda_name,ifnull(fd1.ied_type,'') from_ied_type from (" +
  351. sql + ") a left join t_data_model_func_fcda ff1 on a.from_fcda_id=ff1.id LEFT JOIN (select f1.id,case when f2.ied_type is null then f1.ied_type else f2.ied_type end ied_type from t_data_model_func_def f1 LEFT JOIN t_data_model_iedtype_group f2 on f1.ied_type=f2.children_type and f2.model_id=f1.model_id WHERE f1.model_id=?) fd1 on ff1.func_id=fd1.id"
  352. sqlParamters = append(sqlParamters, c.Model.ModelId)
  353. }
  354. rowset := []orm.Params{}
  355. _, err := o.Raw(sql, sqlParamters).Values(&rowset)
  356. if err != nil {
  357. logger.Logger.Error(err)
  358. }
  359. return rowset, err
  360. }