check_sys_model.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. package bo
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "errors"
  6. "fmt"
  7. "scd_check_tools/arrayex"
  8. "scd_check_tools/logger"
  9. "scd_check_tools/models/enum"
  10. "scd_check_tools/tools"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "github.com/astaxie/beego/orm"
  15. )
  16. //系统内置模型
  17. type T_data_model_defualt struct {
  18. Id int `orm:"pk"`
  19. ModelName string // '模型名称' ,
  20. AreaType int // '所属间隔类型ID' ,
  21. VolId int // '所属电压等级ID' ,
  22. LineLinkStyle int // '接线方式' ,
  23. IedTypes string // '包含的装置类型' ,
  24. RelationJson string // '关系定义内容' ,
  25. IsSys int //是否内置模型。1 系统内置模型 2 任务检测模型
  26. FromModelId int //非系统内置模型时的参照模型ID。is_sys为2时有效
  27. Cr int // '创建人' ,
  28. Ct string `orm:"-"` // '创建时间' ,
  29. Ur int // '更新人' ,
  30. Ut string `orm:"-"` // '更新时间'
  31. }
  32. type ModelNodeStruct struct {
  33. XMLName xml.Name `xml:"flow"`
  34. Nodes []*INodeStruct `xml:"nodes"`
  35. Edges []*IEdges `xml:"edges"`
  36. }
  37. type INodeStruct struct {
  38. Id string `xml:"id"`
  39. Type string `xml:"type"`
  40. X string `xml:"x"`
  41. Y string `xml:"y"`
  42. Text *IText `xml:"text"`
  43. Properties *Iproperties `xml:"properties"`
  44. }
  45. type IText struct {
  46. X string `xml:"x"`
  47. Y string `xml:"y"`
  48. Value string `xml:"value"`
  49. }
  50. type Iproperties struct {
  51. IedType string `xml:"ied_type"`
  52. }
  53. type IEdges struct {
  54. Id string `xml:"id"`
  55. Type string `xml:"type"`
  56. SourceNodeId string `xml:"sourceNodeId"`
  57. TargetNodeId string `xml:"targetNodeId"`
  58. Properties *ILineProperties `xml:"properties"`
  59. }
  60. type ILineProperties struct {
  61. Issv string `xml:"issv"` // SV|GOOSE
  62. }
  63. //内置检测模型管理对象
  64. type SysCheckModelMgr struct {
  65. Model T_data_model_defualt
  66. DeviceBaseModel
  67. }
  68. var sysCheckModelDesc = "内置检测模型"
  69. var SysModelList = sync.Map{}
  70. func init() {
  71. orm.RegisterModel(new(T_data_model_defualt))
  72. }
  73. //判断名称是否已存在
  74. func (c *SysCheckModelMgr) ExistName(name string) (has bool, id int) {
  75. db := orm.NewOrm()
  76. rows := []orm.Params{}
  77. _, err := db.Raw("select id from t_data_model_defualt where model_name=?", name).Values(&rows)
  78. if err != nil {
  79. logger.Logger.Error(err)
  80. return true, 0
  81. }
  82. if len(rows) > 0 {
  83. id, _ = strconv.Atoi(tools.IsEmpty(rows[0]["id"]))
  84. return true, id
  85. }
  86. return false, 0
  87. }
  88. //保存检测模型信息
  89. func (c *SysCheckModelMgr) Save() (model T_data_model_defualt, err error) {
  90. dblog := new(SystemLog)
  91. dblog.SetUserInfo(c.GetUserInfo())
  92. dblog.Audittype = enum.AuditType_check_model
  93. dblog.Logtype = enum.LogType_Insert
  94. dblog.Eventtype = enum.OptEventType_Bus
  95. dblog.Eventlevel = enum.OptEventLevel_Hight
  96. db := orm.NewOrm()
  97. if c.Model.Id == 0 {
  98. h, _ := c.ExistName(c.Model.ModelName)
  99. if h {
  100. return c.Model, errors.New("模型名称已存在")
  101. }
  102. if c.Model.IsSys == 0 {
  103. c.Model.IsSys = 1
  104. }
  105. c.Model.Cr, _ = strconv.Atoi(c.GetUserId())
  106. //处理组合装置
  107. if c.Model.RelationJson != "" {
  108. modelJson := map[string]interface{}{}
  109. err = json.Unmarshal([]byte(c.Model.RelationJson), &modelJson)
  110. if err != nil {
  111. logger.Logger.Error(err)
  112. dblog.Description = fmt.Sprintf("保存%s信息失败:%s,操作数据:%+v", sysCheckModelDesc, err.Error(), c.Model)
  113. dblog.Fail2()
  114. return c.Model, err
  115. }
  116. if modelJson["nodes"] != nil {
  117. nodes := modelJson["nodes"].([]interface{})
  118. iedtypes := []string{}
  119. for _, ritem := range nodes {
  120. ritem2 := ritem.(map[string]interface{})
  121. nodetype := tools.IsEmpty(ritem2["type"])
  122. if nodetype == "my-group" {
  123. properties := ritem2["properties"].(map[string]interface{})
  124. tmp := tools.IsEmpty(properties["ied_type"])
  125. if arrayex.IndexOf(iedtypes, tmp) > -1 {
  126. //装置类型重复了
  127. return c.Model, errors.New("装置类型" + tmp + "不能重复引用")
  128. }
  129. iedtypes = append(iedtypes, tmp)
  130. continue
  131. }
  132. properties := ritem2["properties"].(map[string]interface{})
  133. tmp := tools.IsEmpty(properties["ied_type"])
  134. if arrayex.IndexOf(iedtypes, tmp) > -1 {
  135. //装置类型重复了
  136. return c.Model, errors.New("装置类型" + tmp + "不能重复引用")
  137. }
  138. iedtypes = append(iedtypes, tmp)
  139. }
  140. c.Model.IedTypes = strings.Join(iedtypes, ",")
  141. }
  142. }
  143. newid, err2 := db.Insert(&c.Model)
  144. if err2 == nil {
  145. c.Model.Id = int(newid)
  146. //添加接线方式与模型关系
  147. lm := new(LinkStyleModelMgr)
  148. lm.Model.LinkstyleId = c.Model.LineLinkStyle
  149. lm.Model.ModelId = int(newid)
  150. lm.SetUserInfo(c.GetUserInfo())
  151. lm.Save()
  152. }
  153. if err2 != nil {
  154. err = err2
  155. }
  156. } else {
  157. tmpObj, err := c.One()
  158. if err != nil {
  159. return c.Model, err
  160. }
  161. if c.Model.ModelName != "" {
  162. h, oldid := c.ExistName(c.Model.ModelName)
  163. if h && c.Model.Id != oldid {
  164. return c.Model, errors.New("模型名称已存在")
  165. }
  166. } else {
  167. c.Model.ModelName = tmpObj.ModelName
  168. }
  169. if c.Model.AreaType == 0 {
  170. c.Model.AreaType = tmpObj.AreaType
  171. }
  172. if c.Model.IedTypes == "" {
  173. c.Model.IedTypes = tmpObj.IedTypes
  174. }
  175. if c.Model.IsSys == 0 {
  176. c.Model.IsSys = tmpObj.IsSys
  177. }
  178. if c.Model.LineLinkStyle == 0 {
  179. c.Model.LineLinkStyle = tmpObj.LineLinkStyle
  180. } else if c.Model.LineLinkStyle != tmpObj.LineLinkStyle {
  181. //更新接线方式与模型关系
  182. lm := new(LinkStyleModelMgr)
  183. lm.Model.LinkstyleId = tmpObj.LineLinkStyle
  184. lm.Model.ModelId = c.Model.Id
  185. lm.SetUserInfo(c.GetUserInfo())
  186. lm.Delete() //清除原来的关系
  187. lm.Model.LinkstyleId = c.Model.LineLinkStyle
  188. lm.Save()
  189. }
  190. if c.Model.VolId == 0 {
  191. c.Model.VolId = tmpObj.VolId
  192. }
  193. if c.Model.IsSys == 0 {
  194. c.Model.IsSys = tmpObj.IsSys
  195. }
  196. if c.Model.FromModelId == 0 {
  197. c.Model.FromModelId = tmpObj.FromModelId
  198. }
  199. if c.Model.RelationJson != "" {
  200. // 解析出模型需要的装置类型
  201. modelJson := map[string]interface{}{}
  202. err = json.Unmarshal([]byte(c.Model.RelationJson), &modelJson)
  203. /* <nodes>
  204. <id>5d787390-ebbb-4e97-93d2-87f6a63f7c44</id>
  205. <type>rect</type>
  206. <x>417</x>
  207. <y>202</y>
  208. <properties>
  209. <ied_type>PM</ied_type>
  210. </properties>
  211. <text>
  212. <x>417</x>
  213. <y>202</y>
  214. <value>母线保护</value>
  215. </text>
  216. </nodes>*/
  217. /*nodes := new(ModelNodeStruct)
  218. err = xml.Unmarshal([]byte(`<flow>`+c.Model.RelationJson+`</flow>`), &nodes)
  219. */
  220. if err != nil {
  221. logger.Logger.Error(err)
  222. dblog.Description = fmt.Sprintf("保存%s信息失败:%s,操作数据:%+v", sysCheckModelDesc, err.Error(), c.Model)
  223. dblog.Fail2()
  224. return c.Model, err
  225. }
  226. if modelJson["nodes"] != nil {
  227. nodes := modelJson["nodes"].([]interface{})
  228. iedtypes := []string{}
  229. //groupIedTypes := map[string]int{}
  230. for _, ritem := range nodes {
  231. ritem2 := ritem.(map[string]interface{})
  232. nodetype := tools.IsEmpty(ritem2["type"])
  233. if nodetype == "my-group" {
  234. //装置分组
  235. properties := ritem2["properties"].(map[string]interface{})
  236. tmp := tools.IsEmpty(properties["ied_type"])
  237. if arrayex.IndexOf(iedtypes, tmp) > -1 {
  238. //装置类型重复了
  239. return c.Model, errors.New("装置类型" + tmp + "不能重复引用")
  240. }
  241. iedtypes = append(iedtypes, tmp)
  242. continue
  243. }
  244. properties := ritem2["properties"].(map[string]interface{})
  245. tmp := tools.IsEmpty(properties["ied_type"])
  246. if arrayex.IndexOf(iedtypes, tmp) > -1 {
  247. //装置类型重复了
  248. return c.Model, errors.New("装置类型" + tmp + "不能重复引用")
  249. }
  250. iedtypes = append(iedtypes, tmp)
  251. }
  252. c.Model.IedTypes = strings.Join(iedtypes, ",")
  253. }
  254. m2 := new(SysCheckModelIedRelationMgr)
  255. m2.SetUserInfo(c.GetUserInfo())
  256. m2.Model.ModelId = c.Model.Id
  257. err = m2.Save(modelJson) //先执行这
  258. } else {
  259. c.Model.RelationJson = tmpObj.RelationJson
  260. }
  261. if err == nil {
  262. c.Model.Cr = tmpObj.Cr
  263. c.Model.Ur, _ = strconv.Atoi(c.GetUserId())
  264. _, err = db.Update(&c.Model)
  265. if err != nil {
  266. //对比装置类型是否有变动.如果有减少则,需要同步删除减少装置类型的相关数据
  267. oldTypes := strings.Split(tmpObj.IedTypes, ",")
  268. newTypes := "," + c.Model.IedTypes + ","
  269. for _, item := range oldTypes {
  270. if strings.Index(newTypes, ","+item+",") == -1 {
  271. //当前装置类型item被删除:删除该装置类型原来的功能定义等
  272. m1 := new(SysCheckModelIedFuncMgr)
  273. m1.Model = T_data_model_func_def{ModelId: c.Model.Id, IedType: item}
  274. m1.Delete()
  275. }
  276. }
  277. }
  278. }
  279. }
  280. if err != nil {
  281. logger.Logger.Error(err)
  282. dblog.Description = fmt.Sprintf("保存%s信息失败:%s,操作数据:%+v", sysCheckModelDesc, err.Error(), c.Model)
  283. dblog.Fail2()
  284. } else {
  285. dblog.Description = fmt.Sprintf("保存%s信息成功,操作数据:%+v", sysCheckModelDesc, c.Model)
  286. dblog.Success2()
  287. //保存装置分组
  288. bgm := new(SysCheckModelIedtypeGroupMgr)
  289. bgm.Model = T_data_model_iedtype_group{ModelId: c.Model.Id}
  290. bgm.Save(c.Model.RelationJson)
  291. }
  292. return c.Model, err
  293. }
  294. func (c *SysCheckModelMgr) One() (T_data_model_defualt, error) {
  295. if c.Model.Id == 0 {
  296. return c.Model, errors.New("未指定id")
  297. }
  298. o := orm.NewOrm()
  299. tmpMod := T_data_model_defualt{Id: c.Model.Id}
  300. err := o.Read(&tmpMod)
  301. if err != nil {
  302. logger.Logger.Error(err)
  303. }
  304. return tmpMod, err
  305. }
  306. //更新指定模型中的指定装置类型编码
  307. func (c *SysCheckModelMgr) UpdateIedType(oldiedtype, newiedtype string) error {
  308. c.Model, _ = c.One()
  309. dblog := new(SystemLog)
  310. dblog.SetUserInfo(c.GetUserInfo())
  311. dblog.Audittype = enum.AuditType_check_model
  312. dblog.Logtype = enum.LogType_Delete
  313. dblog.Eventtype = enum.OptEventType_Bus
  314. dblog.Eventlevel = enum.OptEventLevel_Hight
  315. /*
  316. //判断修改的是否是分组的自定义装置类型
  317. groupMapping := new(SysCheckModelIedtypeGroupMgr)
  318. groupMapping.Model = T_data_model_iedtype_group{ModelId: c.Model.Id}
  319. rowset := groupMapping.List()
  320. for _, g := range rowset {
  321. if g == oldiedtype {
  322. db := orm.NewOrm()
  323. db.Raw("update t_data_model_relation_def set from_ied_code=? where model_id=? and from_ied_code=?", newiedtype, c.Model.Id, oldiedtype).Exec()
  324. db.Raw("update t_data_model_relation_def set to_ied_code=? where model_id=? and to_ied_code=?", newiedtype, c.Model.Id, oldiedtype).Exec()
  325. db.Raw("update t_data_model_iedtype_group set ied_type=? where model_id=? and ied_type=?", newiedtype, c.Model.Id, oldiedtype).Exec()
  326. db.Raw("update t_data_model_iedtype_group set children_type=? where model_id=? and children_type=?", newiedtype, c.Model.Id, oldiedtype).Exec()
  327. dblog.Description = fmt.Sprintf("设置%s%s装置类型编码(%s->%s)成功", sysCheckModelDesc, c.Model.ModelName, oldiedtype, newiedtype)
  328. dblog.Success2()
  329. return nil
  330. }
  331. }
  332. */
  333. mapping := new(SysCheckModelIedtypeMappingMgr)
  334. mapping.Model = T_data_model_iedtype_mapping{ModelId: c.Model.Id}
  335. mapping.Model.IedType = oldiedtype
  336. mapping.Model.MappingIedType = newiedtype
  337. err := mapping.Save()
  338. if err != nil {
  339. dblog.Description = fmt.Sprintf("设置%s%s装置类型编码(%s->%s)失败:%s", sysCheckModelDesc, c.Model.ModelName, oldiedtype, newiedtype, err.Error())
  340. dblog.Fail2()
  341. } else {
  342. dblog.Description = fmt.Sprintf("设置%s%s装置类型编码(%s->%s)成功", sysCheckModelDesc, c.Model.ModelName, oldiedtype, newiedtype)
  343. dblog.Success2()
  344. }
  345. return err
  346. }
  347. //获取指定装置类型的自定义类型编码
  348. func (c *SysCheckModelMgr) GetIedtypeMapping(iedtype string) string {
  349. mapping := new(SysCheckModelIedtypeMappingMgr)
  350. mapping.Model = T_data_model_iedtype_mapping{ModelId: c.Model.Id}
  351. mapping.Model.IedType = iedtype
  352. re := mapping.List()
  353. if len(re) == 0 {
  354. return ""
  355. }
  356. return re[iedtype]
  357. }
  358. //根据model中指定的id删除
  359. func (c *SysCheckModelMgr) Delete() (err error) {
  360. dblog := new(SystemLog)
  361. dblog.SetUserInfo(c.GetUserInfo())
  362. dblog.Audittype = enum.AuditType_check_model
  363. dblog.Logtype = enum.LogType_Delete
  364. dblog.Eventtype = enum.OptEventType_Bus
  365. dblog.Eventlevel = enum.OptEventLevel_Hight
  366. db := orm.NewOrm()
  367. if c.Model.Id > 0 {
  368. db.Read(&c.Model)
  369. _, err = db.Delete(&c.Model)
  370. }
  371. if err != nil {
  372. logger.Logger.Error(err)
  373. dblog.Description = fmt.Sprintf("删除%s%s失败:%s", sysCheckModelDesc, c.Model.ModelName, err.Error())
  374. dblog.Fail2()
  375. } else {
  376. SysModelList.Delete(c.Model.Id)
  377. lsm := new(LinkStyleModelMgr)
  378. lsm.SetUserInfo(c.GetUserInfo())
  379. lsm.Model.ModelId = c.Model.Id
  380. lsm.Delete()
  381. m2 := new(SysCheckModelIedRelationMgr)
  382. m2.SetUserInfo(c.GetUserInfo())
  383. m2.Model.ModelId = c.Model.Id
  384. m2.Delete()
  385. m3 := new(SysCheckModelIedFuncMgr)
  386. m3.Model = T_data_model_func_def{ModelId: c.Model.Id}
  387. m3.Delete()
  388. bgm := new(SysCheckModelIedtypeGroupMgr)
  389. bgm.Model = T_data_model_iedtype_group{ModelId: c.Model.Id}
  390. bgm.Delete()
  391. obj1 := new(SysCheckModelIedtypeMappingMgr)
  392. obj1.Model = T_data_model_iedtype_mapping{ModelId: c.Model.Id}
  393. obj1.Delete()
  394. dblog.Description = fmt.Sprintf("删除%s%s成功", sysCheckModelDesc, c.Model.ModelName)
  395. dblog.Success2()
  396. }
  397. return err
  398. }
  399. //复制模型
  400. func (c *SysCheckModelMgr) Copy(modelid int) (error, int) {
  401. dblog := new(SystemLog)
  402. dblog.SetUserInfo(c.GetUserInfo())
  403. dblog.Audittype = enum.AuditType_check_model
  404. dblog.Logtype = enum.LogType_Query
  405. dblog.Eventtype = enum.OptEventType_Bus
  406. dblog.Eventlevel = enum.OptEventLevel_Low
  407. o := orm.NewOrm()
  408. tmpM := T_data_model_defualt{Id: modelid}
  409. err := o.Read(&tmpM)
  410. if err != nil {
  411. logger.Logger.Error(err)
  412. return err, 0
  413. }
  414. if c.Model.ModelName != "" {
  415. if c.Model.IsSys == 2 {
  416. tmpM.ModelName = c.Model.ModelName + tmpM.ModelName
  417. } else {
  418. tmpM.ModelName = c.Model.ModelName
  419. }
  420. } else {
  421. tmpM.ModelName = "[检测模型]" + tmpM.ModelName
  422. }
  423. if c.Model.VolId > 0 {
  424. tmpM.VolId = c.Model.VolId
  425. }
  426. if c.Model.LineLinkStyle > 0 {
  427. tmpM.LineLinkStyle = c.Model.LineLinkStyle
  428. }
  429. if c.Model.IsSys > 0 {
  430. tmpM.IsSys = c.Model.IsSys
  431. } else {
  432. tmpM.IsSys = 2
  433. }
  434. sql := "insert into t_data_model_defualt(model_name,area_type,vol_id,line_link_style,ied_types,relation_json,is_sys,from_model_id)values(?,?,?,?,?,?,?,?)"
  435. r, err := o.Raw(sql, []interface{}{tmpM.ModelName, tmpM.AreaType, tmpM.VolId, tmpM.LineLinkStyle, tmpM.IedTypes, tmpM.RelationJson, tmpM.IsSys, modelid}).Exec()
  436. if err != nil {
  437. logger.Logger.Error(err)
  438. dblog.Description = fmt.Sprintf("复制%s%s失败:%s", sysCheckModelDesc, c.Model.ModelName, err.Error())
  439. dblog.Fail2()
  440. return err, 0
  441. } else {
  442. m2 := new(SysCheckModelIedRelationMgr)
  443. m2.SetUserInfo(c.GetUserInfo())
  444. m2.Model = T_data_model_relation_def{ModelId: modelid}
  445. newid, _ := r.LastInsertId()
  446. err, _ = m2.Copy(int(newid))
  447. if err != nil {
  448. c.Model.Id = int(newid)
  449. c.Delete()
  450. dblog.Description = fmt.Sprintf("复制%s%s失败:%s", sysCheckModelDesc, c.Model.ModelName, err.Error())
  451. dblog.Fail2()
  452. return err, 0
  453. }
  454. //复制装置类型分组数据
  455. bgm := new(SysCheckModelIedtypeGroupMgr)
  456. bgm.Model = T_data_model_iedtype_group{ModelId: modelid}
  457. bgm.Copy(int(newid))
  458. //复制装置类型自定义信息
  459. obj1 := new(SysCheckModelIedtypeMappingMgr)
  460. obj1.Model = T_data_model_iedtype_mapping{ModelId: modelid}
  461. obj1.Copy(int(newid))
  462. //复制功能及端子信息
  463. m3 := new(SysCheckModelIedFuncMgr)
  464. m3.SetUserInfo(c.GetUserInfo())
  465. err = m3.Copy(tmpM.Id, int(newid))
  466. if err != nil {
  467. c.Model.Id = int(newid)
  468. c.Delete()
  469. m2.Model.ModelId = int(newid)
  470. m2.Delete()
  471. dblog.Description = fmt.Sprintf("复制%s%s失败:%s", sysCheckModelDesc, c.Model.ModelName, err.Error())
  472. dblog.Fail2()
  473. return err, 0
  474. }
  475. dblog.Description = fmt.Sprintf("复制%s%s成功", sysCheckModelDesc, c.Model.ModelName)
  476. dblog.Success2()
  477. return nil, int(newid)
  478. }
  479. }
  480. //获取指定电压等级下的所有模型
  481. func (c *SysCheckModelMgr) GetModelsByVolid(is_sys ...int) ([]orm.Params, error) {
  482. o := orm.NewOrm()
  483. s := 1
  484. if len(is_sys) > 0 {
  485. s = is_sys[0]
  486. }
  487. sqlParamters := []interface{}{s}
  488. sql := "select t.id, t.model_name,t.area_type,c.code area_type_code,c.name area_type_name ,t.ied_types,t.is_sys ,c1.`code` vol_code,c1.`name` vol_name from t_data_model_defualt t INNER JOIN global_const_code c on c.id=t.area_type and c.parentcode='area_type' INNER JOIN global_const_code c1 on t.vol_id=c1.id and c1.parentcode='voltage_level' where t.is_sys=? "
  489. rowset := []orm.Params{}
  490. _, err := o.Raw(sql, sqlParamters).Values(&rowset)
  491. if err != nil {
  492. logger.Logger.Error(err)
  493. }
  494. return rowset, err
  495. }
  496. //根据model中的指定过滤属性条件查询列表
  497. func (c *SysCheckModelMgr) List(pageno, pagesize int) ([]orm.Params, int, error) {
  498. dblog := new(SystemLog)
  499. dblog.SetUserInfo(c.GetUserInfo())
  500. dblog.Audittype = enum.AuditType_check_model
  501. dblog.Logtype = enum.LogType_Query
  502. dblog.Eventtype = enum.OptEventType_Bus
  503. dblog.Eventlevel = enum.OptEventLevel_Low
  504. o := orm.NewOrm()
  505. sqlParamters := []interface{}{}
  506. sql := "select t.*,t1.name area_type_name,s1.name line_link_style_name,v1.name voltage_level_name from t_data_model_defualt t left join global_const_code t1 on t.area_type=t1.id and t1.parentcode='area_type' left join global_const_code v1 on t.vol_id=v1.id and v1.parentcode='voltage_level' left join t_data_link_style s1 on t.line_link_style=s1.id where 1=1 "
  507. if c.Model.Id > 0 {
  508. sql += " and t.id=?"
  509. sqlParamters = append(sqlParamters, c.Model.Id)
  510. }
  511. if c.Model.VolId > 0 {
  512. sql += " and t.vol_id=?"
  513. sqlParamters = append(sqlParamters, c.Model.VolId)
  514. }
  515. if c.Model.LineLinkStyle > 0 {
  516. sql += " and t.line_link_style=?"
  517. sqlParamters = append(sqlParamters, c.Model.LineLinkStyle)
  518. }
  519. if c.Model.AreaType > 0 {
  520. sql += " and t.area_type=?"
  521. sqlParamters = append(sqlParamters, c.Model.AreaType)
  522. }
  523. if c.Model.ModelName != "" {
  524. sql += " and t.model_name like ?"
  525. sqlParamters = append(sqlParamters, "%"+c.Model.ModelName+"%")
  526. }
  527. if c.Model.IsSys > 0 {
  528. sql += " and t.is_sys = ?"
  529. sqlParamters = append(sqlParamters, c.Model.IsSys)
  530. }
  531. limit := fmt.Sprintf(" order by t.ct desc limit %d,%d", (pageno-1)*pagesize, pagesize)
  532. r := []orm.Params{}
  533. _, err := o.Raw(sql+limit, sqlParamters).Values(&r)
  534. dblog.Description = fmt.Sprintf("SQL:%s 参数:%+v", sql+limit, sqlParamters)
  535. if err != nil {
  536. logger.Logger.Error(err, dblog.Description)
  537. dblog.Fail2()
  538. return nil, 0, err
  539. }
  540. dblog.Success2()
  541. total := []orm.Params{}
  542. _, err = o.Raw(strings.Replace(sql, "t.*,t1.name area_type_name,s1.name line_link_style_name,v1.name voltage_level_name", "count(1) cnt", 1), sqlParamters).Values(&total)
  543. if err != nil {
  544. logger.Logger.Error(err)
  545. return nil, 0, err
  546. }
  547. totalCnt := 0
  548. if len(total) > 0 {
  549. totalCnt, _ = strconv.Atoi(tools.IsEmpty(total[0]["cnt"]))
  550. }
  551. return r, totalCnt, err
  552. }