check_sysmodel_ied_func_fcda.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. key := fmt.Sprintf("%s%s", row.IedType, row.FcdaName)
  126. if row.FuncName == "额定延时" {
  127. if strings.Index(row.FcdaName, "额定") > -1 || strings.Index(row.FcdaName, "延迟") > -1 || strings.Index(row.FcdaName, "延时") > -1 {
  128. key = fmt.Sprintf("%s%s", row.IedType, "额定延时")
  129. }
  130. }
  131. fcdaMap[key] = row
  132. }
  133. //获取装置分组信息
  134. bgm := new(SysCheckModelIedtypeGroupMgr)
  135. bgm.Model = T_data_model_iedtype_group{ModelId: modelid}
  136. groupList := bgm.List()
  137. var getRealIedCode = func(ied_type string, groupList map[string]string) string {
  138. /*
  139. if groupList != nil && groupList[ied_type] != "" {
  140. ied_type = groupList[ied_type]
  141. }
  142. */
  143. return ied_type
  144. }
  145. fr := new(SysCheckModelFcdaRalationMgr)
  146. fr.Model = T_data_model_fcda_ref{ModelId: modelid}
  147. noMatchList := map[string][]interface{}{} //未完全匹配的列表
  148. for i, row := range relationrow {
  149. if row.ReceiveIedType != "" && strings.Index(row.Inorout, "输出") > -1 {
  150. tmplst := strings.Split(row.ReceiveIedType, ",")
  151. for _, item := range tmplst {
  152. key := fmt.Sprintf("%s%s", item, row.FcdaName)
  153. if row.FuncName == "额定延时" {
  154. if strings.Index(row.FcdaName, "额定") > -1 || strings.Index(row.FcdaName, "延迟") > -1 || strings.Index(row.FcdaName, "延时") > -1 {
  155. key = fmt.Sprintf("%s%s", item, "额定延时")
  156. }
  157. }
  158. if r, h := fcdaMap[key]; h {
  159. fr.Model.FromFcdaId = row.FuncFcdaId
  160. fr.Model.ToFcdaId = r.FuncFcdaId
  161. fr.Model.FromFuncId = row.Id
  162. fr.Model.ToFuncId = r.Id
  163. fr.Model.FromIedCode = getRealIedCode(row.IedType, groupList)
  164. fr.Model.ToIedCode = getRealIedCode(r.IedType, groupList)
  165. fr.Model.Goosesv = row.Svorgoose
  166. fr.Save()
  167. } else {
  168. //未完全匹配到端子时,查找其接收端的对侧装置为当前装置的端子
  169. k := getRealIedCode(row.IedType, groupList) + "," + getRealIedCode(item, groupList)
  170. if _, h := noMatchList[k]; !h {
  171. tmp := map[string]interface{}{"no": i, "fromrow": row}
  172. noMatchList[k] = []interface{}{tmp}
  173. } else {
  174. noMatchList[k] = append(noMatchList[k], map[string]interface{}{"no": i, "fromrow": row})
  175. }
  176. }
  177. }
  178. }
  179. }
  180. //未完全匹配到端子时,查找其接收端的对侧装置为当前装置的端子
  181. for iedtype, obj := range noMatchList {
  182. ks := strings.Split(iedtype, ",")
  183. subNo := 0 //相差的行数
  184. fromrow := T_data_model_func_def{}
  185. for i, row := range relationrow {
  186. outIedType := getRealIedCode(row.IedType, groupList)
  187. receiveIedType := getRealIedCode(row.ReceiveIedType, groupList)
  188. if outIedType == ks[1] && strings.Index(receiveIedType, ks[0]) > -1 {
  189. tmp := obj[0].(map[string]interface{})
  190. no, _ := tmp["no"].(int)
  191. subNo = no - i //相差的行数
  192. break
  193. }
  194. }
  195. logger.Logger.Debug(fmt.Sprintf("未建立关系的装置:%s,间隔行数:%d", iedtype, subNo))
  196. for _, item := range obj {
  197. item2 := item.(map[string]interface{})
  198. rowno := item2["no"].(int) - subNo
  199. if rowno < 0 || rowno > len(relationrow)-1 {
  200. logger.Logger.Debug(fmt.Sprintf("建立关系异常数据:%+v", item))
  201. return
  202. }
  203. fromrow = item2["fromrow"].(T_data_model_func_def)
  204. r := relationrow[rowno]
  205. fr.Model.FromFcdaId = fromrow.FuncFcdaId
  206. fr.Model.ToFcdaId = r.FuncFcdaId
  207. fr.Model.FromFuncId = fromrow.Id
  208. fr.Model.ToFuncId = r.Id
  209. fr.Model.FromIedCode = getRealIedCode(fromrow.IedType, groupList)
  210. fr.Model.ToIedCode = getRealIedCode(r.IedType, groupList)
  211. fr.Model.Goosesv = fromrow.Svorgoose
  212. fr.Save()
  213. }
  214. }
  215. }
  216. //根据model中指定的id删除
  217. func (c *SysCheckModelIedFuncFcdaMgr) Delete() (err error) {
  218. dblog := new(SystemLog)
  219. dblog.SetUserInfo(c.GetUserInfo())
  220. dblog.Audittype = enum.AuditType_check_model
  221. dblog.Logtype = enum.LogType_Delete
  222. dblog.Eventtype = enum.OptEventType_Bus
  223. dblog.Eventlevel = enum.OptEventLevel_Hight
  224. db := orm.NewOrm()
  225. one := T_data_model_func_fcda{Id: c.Model.Id}
  226. if c.Model.Id > 0 {
  227. db.Read(&one)
  228. _, err = db.Raw("delete from t_data_model_func_fcda where id=?", c.Model.Id).Exec()
  229. } else if c.Model.FuncId > 0 {
  230. _, err = db.Raw("delete from t_data_model_func_fcda where model_id=? and func_id=?", c.Model.ModelId, c.Model.FuncId).Exec()
  231. } else {
  232. _, err = db.Raw("delete from t_data_model_func_fcda where model_id=? ", c.Model.ModelId).Exec()
  233. }
  234. if err != nil {
  235. logger.Logger.Error(err)
  236. dblog.Description = fmt.Sprintf("删除%s(%d)失败:%s", sysCheckModel_iedFuncFcdaDesc, c.Model.ModelId, err.Error())
  237. dblog.Fail2()
  238. } else {
  239. fcdaMgr := new(SysCheckModelFcdaRalationMgr)
  240. fcdaMgr.Model = T_data_model_fcda_ref{ModelId: c.Model.ModelId}
  241. if c.Model.Id > 0 {
  242. fcdaMgr.Model.FromFcdaId = c.Model.Id
  243. } else if c.Model.FuncId > 0 {
  244. fcdaMgr.Model.FromFuncId = c.Model.FuncId
  245. }
  246. fcdaMgr.Delete()
  247. dblog.Description = fmt.Sprintf("删除%s(%d)成功", sysCheckModel_iedFuncFcdaDesc, c.Model.ModelId)
  248. dblog.Success2()
  249. if c.Model.Id > 0 {
  250. //判断端子是否全部删除
  251. if one.FuncId > 0 {
  252. c.Model.ModelId = one.ModelId
  253. c.Model.FuncId = one.FuncId
  254. r, _ := c.GetList("")
  255. if len(r) == 0 {
  256. //删除功能定义数据
  257. funcMgr := new(SysCheckModelIedFuncMgr)
  258. funcMgr.Model.Id = one.FuncId
  259. funcMgr.Delete(true)
  260. }
  261. }
  262. } else if c.Model.FuncId > 0 {
  263. funcMgr := new(SysCheckModelIedFuncMgr)
  264. funcMgr.Model.Id = c.Model.FuncId
  265. funcMgr.Delete(true)
  266. } else {
  267. funcMgr := new(SysCheckModelIedFuncMgr)
  268. funcMgr.Model.ModelId = c.Model.ModelId
  269. funcMgr.Delete(true)
  270. }
  271. }
  272. return err
  273. }
  274. func (c *SysCheckModelIedFuncFcdaMgr) One(id int) (T_data_model_func_fcda, error) {
  275. tmp := T_data_model_func_fcda{Id: id}
  276. db := orm.NewOrm()
  277. err := db.Read(&tmp)
  278. return tmp, err
  279. }
  280. //获取指定模型及装置功能下的端子信息列表
  281. //funcids:指定的功能编码列表。可不传
  282. func (c *SysCheckModelIedFuncFcdaMgr) GetList(refIedtype string, funcids ...[]string) ([]orm.Params, error) {
  283. o := orm.NewOrm()
  284. sqlParamters := []interface{}{c.Model.ModelId}
  285. outsql := ""
  286. desc := ""
  287. //获取装置分组信息
  288. bgm := new(SysCheckModelIedtypeGroupMgr)
  289. bgm.Model = T_data_model_iedtype_group{ModelId: c.Model.ModelId}
  290. groupList := bgm.List()
  291. tmpAry := []string{}
  292. if len(groupList) > 0 {
  293. for k, v := range groupList {
  294. if v == refIedtype {
  295. tmpAry = append(tmpAry, k)
  296. }
  297. }
  298. if len(tmpAry) > 0 {
  299. tmpAry = append(tmpAry, refIedtype) //包含分组编码本身
  300. refIedtype = strings.Join(tmpAry, "','")
  301. }
  302. }
  303. if c.Model.Inorout == "接收" {
  304. 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"
  305. sqlParamters = append(sqlParamters, c.Model.ModelId)
  306. desc = " from_fcda_id," //已有关联关系的端子排在前面
  307. }
  308. if c.Model.Inorout == "输出" {
  309. 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"
  310. sqlParamters = append(sqlParamters, c.Model.ModelId)
  311. }
  312. 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=? "
  313. if c.Model.FuncId > 0 {
  314. sql = sql + " and t.func_id=?"
  315. sqlParamters = append(sqlParamters, c.Model.FuncId)
  316. }
  317. if len(funcids) > 0 {
  318. sql = sql + " and t.func_id in(" + strings.Join(funcids[0], ",") + ")"
  319. }
  320. if c.Model.Svorgoose != "" {
  321. sql = sql + " and t.svorgoose=?"
  322. sqlParamters = append(sqlParamters, c.Model.Svorgoose)
  323. }
  324. if c.Model.Inorout != "" {
  325. sql = sql + " and t.inorout like ?"
  326. sqlParamters = append(sqlParamters, "%"+c.Model.Inorout+"%")
  327. }
  328. sql = sql + " order by " + desc + " t.id"
  329. if c.Model.Inorout == "输出" {
  330. sql = "select a.id,a.model_id,a.func_id,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 (" +
  331. 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"
  332. sqlParamters = append(sqlParamters, c.Model.ModelId)
  333. } else if c.Model.Inorout == "接收" {
  334. sql = "select a.id,a.model_id,a.func_id,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 (" +
  335. 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"
  336. sqlParamters = append(sqlParamters, c.Model.ModelId)
  337. }
  338. rowset := []orm.Params{}
  339. _, err := o.Raw(sql, sqlParamters).Values(&rowset)
  340. if err != nil {
  341. logger.Logger.Error(err)
  342. }
  343. return rowset, err
  344. }