123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- package bo
- import (
- "errors"
- "fmt"
- "log"
- "os"
- "scd_check_tools/logger"
- "scd_check_tools/models/enum"
- "scd_check_tools/tools"
- "strconv"
- "strings"
- "github.com/astaxie/beego/orm"
- )
- //数据附件清理模型
- type T_data_attachment struct {
- Id int `orm:"pk;auto"`
- Name string
- Skeepday int
- Tablename string
- Colname string
- Filterwhere string
- Memo string
- Ct string
- Cr string
- }
- //附件模型
- type T_sys_attachment struct {
- Id int32 `orm:"pk"` // INT NOT NULL AUTO_INCREMENT COMMENT '主键' ,
- CreatedBy string //INT DEFAULT 0 COMMENT '创建人' ,
- CreatedTime string //DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间' ,
- StationId int32 //INT NOT NULL COMMENT '所属站' ,
- ScdId int64
- IedName string
- IedDesc string
- FileName string //VARCHAR(100) NOT NULL COMMENT '文件名称' ,
- FileSize string //VARCHAR(20) COMMENT '文件大小' ,
- SavePath string //VARCHAR(200) COMMENT '保存路径' ,
- FileType string //VARCHAR(10) COMMENT '文件类型' ,
- FileSuffix string //VARCHAR(10) COMMENT '文件后缀' ,
- CrcSign string //VARCHAR(1) COMMENT '认证标记' ,
- //文档管控模式 0 非管控 1管控
- CheckFlag int
- Remark string //VARCHAR(200) COMMENT '备注' ,
- }
- type T_sys_attachment_download struct {
- Id int
- AttachmentId int
- DownloadUser int
- DownloadDate string
- DownloadDesc string
- }
- //数据附件清理对象
- type AttachmentClearMgr struct {
- Model T_data_attachment
- DeviceBaseModel
- }
- //附件管理对象
- type AttachmentMgr struct {
- Model T_sys_attachment
- DeviceBaseModel
- }
- func init() {
- orm.RegisterModel(new(T_data_attachment))
- orm.RegisterModel(new(T_sys_attachment))
- orm.RegisterModel(new(T_sys_attachment_download))
- }
- func (c *AttachmentClearMgr) Save() (err error) {
- db := orm.NewOrm()
- c.Model.Cr = c.GetUserId() // tools.IsEmpty(c.Userinfo["userid"])
- c.Model.Ct = tools.NowTime()
- if c.Model.Id == 0 {
- _, err = db.Insert(&c.Model)
- } else {
- _, err = db.Update(&c.Model)
- }
- dblog := new(SystemLog)
- dblog.SetUserInfo(c.GetUserInfo())
- dblog.Audittype = enum.AuditType_admin_system_attachment
- dblog.Logtype = enum.LogType_Insert
- dblog.Eventtype = enum.OptEventType_Bus
- dblog.Eventlevel = enum.OptEventLevel_Low
- if err != nil {
- logger.Logger.Error(err)
- dblog.Description = fmt.Sprintf("保存清理名单%s失败:%s", c.Model.Name, err.Error())
- dblog.Fail2()
- } else {
- dblog.Description = fmt.Sprintf("保存清理名单%s成功", c.Model.Name)
- dblog.Success2()
- }
- return err
- }
- func (c *AttachmentClearMgr) One() (T_data_attachment, error) {
- o := orm.NewOrm()
- err := o.Read(&c.Model)
- if err != nil {
- log.Println(err)
- }
- return c.Model, err
- }
- func (c *AttachmentClearMgr) List() ([]T_data_attachment, int, error) {
- o := orm.NewOrm()
- t := o.QueryTable("t_data_attachment")
- r := []T_data_attachment{}
- dblog := new(SystemLog)
- dblog.SetUserInfo(c.GetUserInfo())
- dblog.Audittype = enum.AuditType_admin_system_attachment
- dblog.Logtype = enum.LogType_Query
- dblog.Eventtype = enum.OptEventType_Bus
- dblog.Eventlevel = enum.OptEventLevel_Low
- dblog.Description = "列表查询"
- _, err := t.All(&r)
- if err != nil {
- logger.Logger.Error(err)
- dblog.Fail2()
- } else {
- dblog.Success2()
- }
- return r, len(r), err
- }
- func (c *AttachmentClearMgr) Delete() (err error) {
- db := orm.NewOrm()
- if c.Model.Id == 0 {
- return errors.New("无效的ID")
- } else {
- db.Read(&c.Model)
- _, err = db.Delete(&c.Model)
- }
- dblog := new(SystemLog)
- dblog.SetUserInfo(c.GetUserInfo())
- dblog.Audittype = enum.AuditType_admin_system_attachment
- dblog.Logtype = enum.LogType_Delete
- dblog.Eventtype = enum.OptEventType_Bus
- dblog.Eventlevel = enum.OptEventLevel_Low
- if err != nil {
- logger.Logger.Error(err)
- dblog.Description = fmt.Sprintf("删除清理名单%s失败:%s", c.Model.Name, err.Error())
- dblog.Fail2()
- } else {
- dblog.Description = fmt.Sprintf("删除清理名单%s成功", c.Model.Name)
- dblog.Success2()
- }
- return err
- }
- func (c *AttachmentMgr) TypeStat(param map[string]interface{}) (map[string]interface{}, error) {
- var result = map[string]interface{}{}
- result["data"] = []orm.Params{}
- constCode := new(Global)
- list, _ := constCode.GetChildrenGlobalCode("file_types")
- result["comments"] = []orm.Params{}
- rowset := []orm.Params{}
- uaObj := new(UserAreaRelationObject)
- uaObj.SetUserInfo(c.GetUserInfo())
- areaFilerWhere := uaObj.MakeAreaFilerIds("t.station_id", "and")
- db := orm.NewOrm()
- for _, row := range list {
- typecode := tools.IsEmpty(row["code"])
- if typecode == "file_all" {
- continue
- }
- result["comments"] = append(result["comments"].([]orm.Params), row)
- sql := ""
- sqlParams := []interface{}{}
- if typecode == "file_a_scd" {
- sql = "select " + tools.IsEmpty(row["id"]) + " file_type,count(0) cnt from t_scd_scl t where enable=1 "
- station_id := tools.IsEmpty(param["station_id"])
- if station_id != "" {
- sql = sql + " and t.station_id=?"
- sqlParams = append(sqlParams, station_id)
- } else {
- if areaFilerWhere != "" {
- sql = sql + areaFilerWhere
- }
- }
- } else {
- sql = "select t.file_type,count(0) cnt from global_const_code a left join t_sys_attachment t on a.id=t.file_type "
- station_id := tools.IsEmpty(param["station_id"])
- if station_id != "" {
- sql = sql + " and t.station_id=?"
- sqlParams = append(sqlParams, station_id)
- } else {
- if areaFilerWhere != "" {
- sql = sql + areaFilerWhere
- }
- }
- sqlParams = append(sqlParams, tools.IsEmpty(row["id"]))
- sql = sql + " where a.parentcode='file_types' and a.id=? GROUP BY a.id"
- }
- tmprowset := []orm.Params{}
- _, err := db.Raw(sql, sqlParams).Values(&tmprowset)
- if err != nil {
- log.Println(err, fmt.Sprintf("SQL:%s 参数:%+v", sql, sqlParams))
- } else {
- rowset = append(rowset, tmprowset...)
- }
- }
- result["data"] = rowset
- return result, nil
- }
- func (c *AttachmentMgr) Save() (id int32, err error) {
- db := orm.NewOrm()
- c.Model.CreatedBy = c.GetUserId()
- c.Model.CreatedTime = tools.NowTime()
- dblog := new(SystemLog)
- dblog.SetUserInfo(c.GetUserInfo())
- dblog.Audittype = enum.AuditType_admin_system_attachment
- dblog.Logtype = enum.LogType_Insert
- dblog.Eventtype = enum.OptEventType_Bus
- dblog.Eventlevel = enum.OptEventLevel_Hight
- newid := int64(0)
- if c.Model.Id == 0 {
- newid, err = db.Insert(&c.Model)
- } else {
- _, err = db.Update(&c.Model)
- newid = int64(c.Model.Id)
- }
- if err != nil {
- logger.Logger.Error(err, fmt.Sprintf("原始数据:%+v", c.Model))
- dblog.Description = fmt.Sprintf("上传附件%s失败:%s", c.Model.FileName, err.Error())
- dblog.Fail2()
- } else {
- dblog.Description = fmt.Sprintf("上传附件%s成功", c.Model.FileName)
- dblog.Success2()
- }
- return int32(newid), err
- }
- func (c *AttachmentMgr) One() (T_sys_attachment, error) {
- o := orm.NewOrm()
- err := o.Read(&c.Model)
- if err != nil {
- logger.Logger.Error(err)
- }
- return c.Model, err
- }
- //查询文档列表
- //参数:
- //station_id:所属变电站ID int32
- //iedname:指定IED装置名称
- //filename:指定文件名称。模糊匹配
- //filetype:指定文件分类ID.
- //isCheckIn:是否管控文档。1(是)|0(否)
- func (c *AttachmentMgr) List(scd_id string, station_id int32, iedname, filename, filetype, isCheckIn string, pageno, pagesize int32) ([]orm.Params, int, error) {
- o := orm.NewOrm()
- sqlParamters := []interface{}{}
- sql := "select t.*,s1.version,s1.is_parse,u.name username,c.name type_name from t_sys_attachment t left join t_scd_scl s1 on t.scd_id=s1.id left join t_data_user u on t.created_by=u.id left join global_const_code c on t.file_type=c.id and c.parentcode='file_types' where t.station_id=? "
- sqlParamters = append(sqlParamters, station_id)
- if iedname != "" {
- sql += " and t.ied_name=?"
- sqlParamters = append(sqlParamters, iedname)
- }
- if filename != "" {
- sql += " and t.file_name like ?"
- sqlParamters = append(sqlParamters, "%"+filename+"%")
- }
- if filetype != "" {
- sql += " and t.file_type=?"
- sqlParamters = append(sqlParamters, filetype)
- }
- if isCheckIn == "1" {
- sql += " and t.check_flag=1"
- } else if isCheckIn == "0" {
- sql += " and t.check_flag=0"
- } else if isCheckIn == "2" {
- //获取裁剪文件,仅支持ccd\icd\cid
- sql += " and t.check_flag=2"
- }
- limit := fmt.Sprintf(" order by t.file_name desc limit %d,%d", (pageno-1)*pagesize, pagesize)
- dblog := new(SystemLog)
- dblog.SetUserInfo(c.GetUserInfo())
- dblog.Audittype = enum.AuditType_admin_system_attachment
- dblog.Logtype = enum.LogType_Query
- dblog.Eventtype = enum.OptEventType_Bus
- dblog.Eventlevel = enum.OptEventLevel_Low
- r := []orm.Params{}
- _, err := o.Raw(sql+limit, sqlParamters).Values(&r)
- sqllog := fmt.Sprintf("SQL:%s 参数:%+v", sql+limit, sqlParamters)
- if err != nil {
- logger.Logger.Error(err, sqllog)
- dblog.Description = sqllog
- dblog.Fail2()
- return nil, 0, err
- }
- dblog.Description = sqllog
- dblog.Success2()
- total := []orm.Params{}
- totalSql := strings.Replace(sql, "t.*,u.name username,c.name type_name", "count(1) cnt", 1)
- sqllog = fmt.Sprintf("SQL:%s 参数:%+v", totalSql, sqlParamters)
- _, err = o.Raw(totalSql, sqlParamters).Values(&total)
- if err != nil {
- logger.Logger.Error(err, sqllog)
- return nil, 0, err
- }
- totalCnt := 0
- if len(total) > 0 {
- totalCnt, _ = strconv.Atoi(tools.IsEmpty(total[0]["cnt"]))
- }
- return r, totalCnt, err
- }
- func (c *AttachmentMgr) Delete(isall ...bool) (err error) {
- db := orm.NewOrm()
- if c.Model.Id == 0 {
- return errors.New("无效的ID")
- } else {
- db.Read(&c.Model)
- //判断是否是scd
- if c.Model.ScdId > 0 && c.Model.FileSuffix == "scd" {
- scd := new(ScdMgr)
- scd.SetUserInfo(c.GetUserInfo())
- delAll := true
- if len(isall) > 0 {
- delAll = isall[0]
- }
- err = scd.DeleteScd(tools.IsEmpty(c.Model.ScdId), delAll)
- } else {
- _, err = db.Delete(&c.Model)
- filepath := c.Model.SavePath
- if filepath != "" {
- if filepath[0:1] != "." {
- filepath = "." + filepath
- }
- ferr := os.Remove(filepath)
- if ferr != nil {
- logger.Logger.Error(ferr)
- }
- }
- }
- }
- dblog := new(SystemLog)
- dblog.SetUserInfo(c.GetUserInfo())
- dblog.Audittype = enum.AuditType_admin_system_attachment
- dblog.Logtype = enum.LogType_Delete
- dblog.Eventtype = enum.OptEventType_System
- dblog.Eventlevel = enum.OptEventLevel_Hight
- if err != nil {
- logger.Logger.Error(err)
- dblog.Description = fmt.Sprintf("删除附件%s失败:%s", c.Model.FileName, err.Error())
- dblog.Fail2()
- } else {
- dblog.Description = fmt.Sprintf("删除附件%s成功", c.Model.FileName)
- dblog.Success2()
- }
- return err
- }
- //获取指定装置及类型的文件信息
- func (c *AttachmentMgr) GetInfoByIed(scdid int64, iedname, iedtype string) (T_sys_attachment, error) {
- result := T_sys_attachment{}
- db := orm.NewOrm()
- rowset := []orm.Params{}
- db.Raw("select * from t_sys_attachment where scd_id=? and ied_name=? and file_suffix=?", scdid, iedname, iedtype).Values(&rowset)
- if len(rowset) > 0 {
- id, _ := strconv.Atoi(tools.IsEmpty(rowset[0]["id"]))
- result.Id = int32(id)
- result.SavePath = tools.IsEmpty(rowset[0]["save_path"])
- }
- return result, nil
- }
- //开始分析指定的scd文件
- func (c *AttachmentMgr) StartScdTempParse(stationid int, scdname, scdpath string) error {
- if stationid == 0 {
- return errors.New("无效的变站电编号")
- }
- if scdname == "" || scdpath == "" {
- return errors.New("无效的SCD文件名称或路径")
- }
- dblog := new(SystemLog)
- dblog.SetUserInfo(c.GetUserInfo())
- dblog.Audittype = enum.AuditType_scd_resetparse
- dblog.Logtype = enum.LogType_Update
- dblog.Eventtype = enum.OptEventType_System
- dblog.Eventlevel = enum.OptEventLevel_Hight
- sql := "update t_sys_attachment set scd_id=-1 where station_id=? and file_name=? and save_path=?"
- sqllog := fmt.Sprintf("SQL:%s 参数:%d,%s,%s", sql, stationid, scdname, scdpath)
- _, err := orm.NewOrm().Raw(sql, stationid, scdname, scdpath).Exec()
- if err != nil {
- logger.Logger.Error(err)
- dblog.Description = sqllog
- dblog.Fail2()
- } else {
- dblog.Description = sqllog
- dblog.Success2()
- }
- return err
- }
- //更新指定变电站的最新管控文件的scdid
- func (c *AttachmentMgr) UpdateScdID(stationid int, scdname, scdpath string, scdid int64) {
- db := orm.NewOrm()
- if scdpath != "" && scdpath[0:1] == "." {
- scdpath = scdpath[1:]
- }
- _, err := db.Raw("update t_sys_attachment set scd_id=? where station_id=? and scd_id<=0 and file_name=? and save_path=?", scdid, stationid, scdname, scdpath).Exec()
- if err != nil {
- log.Println(err)
- }
- _, err = db.Raw("update t_sys_attachment t set scd_id=? where file_suffix!='scd' and station_id=? and scd_id<=0", scdid, stationid).Exec()
- if err != nil {
- log.Println(err)
- }
- }
- //清理指定变电站已上传但不使用的指定类型文件
- //以避免重复上传时,出现无效文件
- func (c *AttachmentMgr) ClearDityFile(stationid int32, fileType string) {
- db := orm.NewOrm()
- rowset := []orm.Params{}
- db.Raw("select save_path from t_sys_attachment where station_id=? and file_type=? and scd_id=-1 and check_flag=1", stationid, fileType).Values(&rowset)
- db.Raw("delete from t_sys_attachment where station_id=? and file_type=? and scd_id=-1 and check_flag=1", stationid, fileType).Exec()
- if rowset != nil && len(rowset) > 0 {
- for _, row := range rowset {
- fp := tools.IsEmpty(row["save_path"])
- if fp == "" {
- continue
- }
- if fp[0:1] != "." {
- fp = "." + fp
- }
- os.Remove(fp)
- }
- }
- }
- //清理指定变电站文件
- func (c *AttachmentMgr) ClearAllFile(stationid int32) {
- db := orm.NewOrm()
- rowset := []orm.Params{}
- db.Raw("select save_path from t_sys_attachment where station_id=? ", stationid).Values(&rowset)
- db.Raw("delete from t_sys_attachment where station_id=? ", stationid).Exec()
- if rowset != nil && len(rowset) > 0 {
- for _, row := range rowset {
- fp := tools.IsEmpty(row["save_path"])
- if fp == "" {
- continue
- }
- if fp[0:1] != "." {
- fp = "." + fp
- }
- os.Remove(fp)
- }
- }
- }
- func (c *AttachmentMgr) DownloadRec(attamentid int, desc string) error {
- o := orm.NewOrm()
- dwn := T_sys_attachment_download{}
- dwn.AttachmentId = attamentid
- dwn.DownloadDate = tools.NowTime()
- dwn.DownloadUser, _ = strconv.Atoi(c.GetUserId())
- dwn.DownloadDesc = desc
- _, err := o.Insert(&dwn)
- return err
- }
|