GO 学习笔记例子一



// RequestPostDatarawClass 获取接收者json请求结构 数据原分类表 分类名称
type RequestPostDatarawClass struct {
    ID int64 `orm:"column(id)"`
    Classname string `json:"classname"`
}
type dataClassListClass struct {
    ID int64 `json:"id"`
    Logicalname string `json:"logicalname"` // 逻辑名称
    Physicsname string `json:"physicsname"` // 物理名称
    Category string `json:"category"` // 所属分类
    CategoryID int64 `json:"categoryid"` // 所属分类
    DataBaseID int64 `json:"databaseid"` //数据库ID
    Type string `json:"type"` // 数据源类型
    Databasename string `json:"databasename"` // 数据库名称
    From string `json:"from"` // 数据来源

}

// DataClassListResponse 传条件
type DataClassListResponse struct {
     //Title []string `json:"title"`
    Title map[ string] interface{} `json:"title"`
    Config map[ string] interface{} `json:"config"`
    Body []*dataClassListClass `json:"body"`
}

// ClassListParameteResponse 传条件搜索条件
type ClassListParameteResponse struct { //{"type":50,"table_name":"dfdd","data_origin":"1","current_page":"0","page_size":"10"}
    Type int64 `json:"type"` //所属分类
    TName string `json:"table_name"` //数据源名称
    DataOrigin int64 `json:"data_origin"` //数据源类型
    Currentpage int `json:"current_page"`
    Pagesize int `json:"page_size"`
}

// DataBaseIDResponse 数据库ID
type DataBaseIDResponse struct {
    ID int64 `json:"id"`
}

// DataTableInfo 数据表字段(获取数据表,通过数据库ID查询表,参数传id)
// @router /data-table-info [post]
func (c *RawDataclassController) DataTableInfo() {
     database := &DataBaseIDResponse{}
     err := json. Unmarshal(c.Ctx.Input.RequestBody, database)
     if err != nil {
        c. WriteJSON( nil, err)
         return
    }
     dataList, _ := rawDataLogicallayerService. GetDataTableInfoLogic(database.ID)
     listClass := make([]*dataClassListClass, len(dataList))
     for i, item := range dataList {
         d := &dataClassListClass{
            ID: item.ID,
            Logicalname: item.TableInfo,
            Physicsname: item.TName,
            DataBaseID: item.DatabaseID,
        }
        listClass[i] = d
    }
     //res.Body = listClass
    c. WriteJSON(listClass, err)
}

// DataClassList 数据源分类列表
// @router /data-class-list [post]
func (c *RawDataclassController) DataClassList() {
     req := &ClassListParameteResponse{}
     err := json. Unmarshal(c.Ctx.Input.RequestBody, req)
     res := &DataClassListResponse{}
     //currentpage := strconv.Itoa(req.Currentpage)
     //pagesize := strconv.Itoa(req.Pagesize)
     // ===============
     res.Title = map[ string] interface{}{
         "logicalname": "数据源名称",
         "physicsname": "物理名称",
         "category": "所属分类",
         "type": "数据源类型",
         "databasename": "数据库名称",
         "from": "数据来源",
    }

     var tableInfoList []*models.QReportExternalTableInfo

     tableInfoList, err = rawDataLogicallayerService. GetDataClassListLogic(req.Type, req.TName, req.Currentpage, req.Pagesize) //req.Type
     num, _ := rawDataLogicallayerService. GetDataClassListSumLogic(req.Type, req.TName)
     res.Config = map[ string] interface{}{
         "count": num,
    }
     if err != nil {
        c. WriteJSON( nil, err)
         return
    }
     listClass := make([]*dataClassListClass, len(tableInfoList))
     for i, item := range tableInfoList {
         externalDatabaseInfo, _ := rawDataLogicallayerService. GetDataBaseSelectOneLogic(item.DatabaseID)
         types, _ := rawDataLogicallayerService. GetDatarawClassTableOneLogic(item.Type) //self_rpt_datarawclass_table
         d := &dataClassListClass{
            ID: item.ID,
            Logicalname: item.TName,
            Physicsname: item.TableInfo,
            Category: types.ClassName,
            CategoryID: item.Type,
            Type: "表", //types.ClassName,//"表",// types.Class_name,
            Databasename: externalDatabaseInfo.DatabaseName, //item.DatabaseID,
            From: "无",
        }
         /*
            switch item.Type {
            case 0:
                d.Category = "表"
            case 1:
                d.Category = ""
            }*/
        listClass[i] = d
    }
     if req.DataOrigin != 2 {
         res.Body = listClass
    } else {
         res.Body = nil
    }

    c. WriteJSON(res, err)
}

// CreateClassify 创建分类
// @router /create-classify [post]
func (c *RawDataclassController) CreateClassify() {
     ctx, err := c. InitUserInfo()
     if err != nil {
        c. WriteJSON( nil, err)
         return
    }
     req := &RequestPostDatarawClass{}
     err = json. Unmarshal(c.Ctx.Input.RequestBody, req)
     if err != nil {
         //logs.Error(err)
        c. WriteJSON( nil, err)
         return
    }
     id, _ := rawDataLogicallayerService. GetCreateDataQueryLogic(req.Classname) //Logic
     if req.ID > 0 {
         _, errs := rawDataLogicallayerService. GetCreateDataUpdateLogic(req.ID, req.Classname)
         optionItem := new(optionItemClass)
         optionItem.Value = req.ID //reqid
         optionItem.Label = req.Classname
        c. WriteJSON(optionItem, errs) //"添加成功!"
         return
    }
     if id == 0 {
         ids, err := rawDataLogicallayerService. GetCreateDataRawLogic(ctx, req.Classname)
         if err == nil {
             optionItem := new(optionItemClass)
             optionItem.Value = ids
             optionItem.Label = req.Classname
            c. WriteJSON(optionItem, err) //"添加成功!"
             return
        }
    } else {
         err = errors. NewInsertRepeatError( "分类信息存在,请换个名称!")
    }

    c. WriteJSON( nil, err)
}

// ClassifyDel 删除分类
// @router /classify-del [post]
func (c *RawDataclassController) ClassifyDel() {
     req := &RequestPostDatarawClass{}
     err := json. Unmarshal(c.Ctx.Input.RequestBody, req)
     if err != nil {
         //logs.Error(err)
        c. WriteJSON( nil, err)
         return
    }
     id, errs := rawDataLogicallayerService. GetDeleteClassifyLogic(req.ID) //传ID
     //logs.Debug("err:", errs)
     if id > 0 {
         //errs = errors.NewInsertRepeatError("删除成功!")
        c. WriteJSON( "", errs)
         return
    }
    c. WriteJSON( "删除失败!", errs)
}

type optionItemClass struct {
    Value int64 `json:"value"`
    Label string `json:"label"`
}

// Classifyall 分类下拉框
// @router /classifyall [post]
func (c *RawDataclassController) Classifyall() {
     var datalist []models.QReportExternalDatarawClass
     datalist, err := rawDataLogicallayerService. GetDataQueryLogic()
     if err != nil {
        c. WriteJSON( nil, err)
         return
    }
     optionitem := make([]*optionItemClass, len(datalist))
     for i, item := range datalist {
        optionitem[i] = &optionItemClass{
            Value: item.ID,
            Label: item.ClassName,
        }
    }
    c. WriteJSON(optionitem, err)
}

// DataBaseSelect 选择数据库
// @router /data-base-select [post]
func (c *RawDataclassController) DataBaseSelect() {
     var databaselist []models.QReportExternalDatabaseInfo
     databaselist, err := rawDataLogicallayerService. GetDataBaseSelectLogic()
     if err != nil {
        c. WriteJSON( nil, err)
         return
    }
     optionitem := make([]*optionItemClass, len(databaselist))
     for i, item := range databaselist {
        optionitem[i] = &optionItemClass{
            Value: item.ID,
            Label: item.DatabaseName,
        }
    }

    c. WriteJSON(optionitem, err)
}

// DataRawUpdateRequest 传前端条件
type DataRawUpdateRequest struct {
    ID [] int64 `json:"id"`
    Type int64 `json:"type"`
}

//DatarawClassUp 数据原分类明细表 更新分类 type {"Tablename":"rpt_qdas_show_shoujizhushou_event_daily","Type":1}
// @router /dataraw-class-up [post]
func (c *RawDataclassController) DatarawClassUp() {
     //c.Ctx.WriteString("hello world")
     //return
     req := &DataRawUpdateRequest{}
     err := json. Unmarshal(c.Ctx.Input.RequestBody, req)
     if err != nil {
        c. WriteJSON( nil, err)
         return
    }
     for _, item := range req.ID {
         _, err = rawDataLogicallayerService. GetUpDatarawClassDetailTableLogic(item, req.Type) //添加
    }

     if err == nil {
        c. WriteJSON( "更新成功!", err)
         return
    }
    c. WriteJSON( "更新失败!", err)
}

//选择数据库

// DatarawClassDel 删除数据源 表
// @router /dataraw-class-del [post]
func (c *RawDataclassController) DatarawClassDel() {
     req := &models.QReportExternalTableInfo{}
     err := json. Unmarshal(c.Ctx.Input.RequestBody, req)

     if err != nil {
        c. WriteJSON( nil, err)
         return
    }
     id, errs := rawDataLogicallayerService. GetDeleteDatarawClassDetailTableLogic(req) //传ID
     if id > 0 {
        c. WriteJSON( "删除成功!", errs)
         return
    }
    c. WriteJSON( "删除失败!", errs)
}

// Dataorigin 数据源类型
// @router /dataorigin [post]
func (c *RawDataclassController) Dataorigin() {
     optionitem := []optionItemClass{
        optionItemClass{Label: "表", Value: 1},
        optionItemClass{Label: "模型", Value: 2},
    }
    c. WriteJSON(optionitem, nil)
}

type fieldAlterParameter struct {
    ID int64 `json:"id"`
    DataDaseID int64 `json:"databaseid"`
     //Label string `json:"label"`
}
type fieldAlterResponse struct {
    Title map[ string] interface{} `json:"title"`
    Table map[ string] interface{} `json:"table"`
    Body []*showField `json:"body"`
}

/*
res.Title = map[string]interface{}{
        "logicalname": "数据源名称",
        "physicsname": "物理名称",
        "category": "所属分类",
        "type": "数据源类型",
        "databasename": "数据库名称",
        "from": "数据来源",
    }
*/
type showField struct { //
    ID int64 `orm:"column(id)" json:"id"`
    FileName string `json:"logicalname"`
    FieldType string `json:"type"`
    ParamDesc string `json:"physicsname"`
}

// FieldAlter 修改表信息及字段
// @router /field-alter [post]
func (c *RawDataclassController) FieldAlter() { //{"id":1}
     parameter := &fieldAlterParameter{}
     err := json. Unmarshal(c.Ctx.Input.RequestBody, parameter)
     if err != nil {
        c. WriteJSON( nil, err)
         return
    }
     tableInfodata, _ := rawDataLogicallayerService. GetShowTableInfoOneLogic(parameter.ID)
     fieldDatalist, _ := rawDataLogicallayerService. GetShowField(parameter.ID)

     fieldAlter := &fieldAlterResponse{}
     fieldAlter.Table = map[ string] interface{}{
         "tablename": tableInfodata.TName,
         "tableinfo": tableInfodata.TableInfo,
         "type": tableInfodata.Type,
    }
     fieldAlter.Title = map[ string] interface{}{
         "fieldname": "逻辑名称",
         "paramdesc": "物理名称",
         "fieldtype": "字段类型",
    }
     listClass := make([]*showField, len(fieldDatalist))
     for i, item := range fieldDatalist {
         d := &showField{
            ID: item.ID,
            FileName: item.FieldName,
            ParamDesc: item.ParamDesc,
            FieldType: item.FieldType,
        }
        listClass[i] = d
    }
     fieldAlter.Body = listClass
    c. WriteJSON(fieldAlter, nil)
}

type fieldAlterUpRequest struct {
    Tableid int64 `json:"tableid"`
    Physicsname string `json:"physicsname"`
    Category int64 `json:"category"`
    Field []*fieldAlterUpRequestItem `json:"field"`
}

type fieldAlterUpRequestItem struct {
    ID int64 `json:"id"`
    Logicalname string `json:"logicalname"`
    Physicsname string `json:"physicsname"`
    Type string `json:"type"`
}

// FieldAlterUp 更新分类信息 及 表字段信息
// @router /field-alter-up [post]
func (c *RawDataclassController) FieldAlterUp() {
     parameter := &fieldAlterUpRequest{}
     err := json. Unmarshal(c.Ctx.Input.RequestBody, parameter)
     if err != nil {
        c. WriteJSON( nil, err)
         return
    }
     for _, item := range parameter.Field {
         //_ ,err = rawDataLogicallayerService.GetClassifyUpLogic(parameter.Tableid) //更新表字段信息 id1,types4 , parameter.Category
         _, err = rawDataLogicallayerService. GetFieldUpLogic(item.ID, item.Type, item.Physicsname) //更新字段信息用 fieldID1, fieldtype char, paramdesc 信息化
    }
     if err == nil {
        c. WriteJSON( "修改成功!", err)
         return
    }
    c. WriteJSON( "修改失败!", err)
}

// DebugInfo 前端下拉用
type DebugInfo struct {
    Value string `json:"value"`
    Label string `json:"label"`
}

// FieldType 字段类型
// @router /field-type [post]
func (c *RawDataclassController) FieldType() {
     dbgInfs := []DebugInfo{
        DebugInfo{ "int", "int"},
        DebugInfo{ "varchar", "varchar"},
        DebugInfo{ "bigint", "bigint"},
        DebugInfo{ "double", "double"},
    }
    c. WriteJSON(dbgInfs, nil)
}

// showTable 传参数
type showTable struct {
    ID int64 `orm:"column(id)" json:"id"`
    TableName string `json:"logicalname"`
    TableInfo string `json:"physicsname"`
}

// TableInfoUp 修改表信息
// @router /table-info-up [post]
func (c *RawDataclassController) TableInfoUp() {
     parameter := &fieldAlterParameter{}
     err := json. Unmarshal(c.Ctx.Input.RequestBody, parameter)
     if err != nil {
        c. WriteJSON( nil, err)
         return
    }
     tableInfoDatabase, _ := rawDataLogicallayerService. GetShowTableInfoDatabaseAllLogic(parameter.ID) //parameter.DataBaseID
     listClass := make([]*showTable, len(tableInfoDatabase))
     for i, item := range tableInfoDatabase {
         d := &showTable{
            ID: item.ID,
            TableName: item.TName,
            TableInfo: item.TableInfo,
        }
        listClass[i] = d
    }
    c. WriteJSON(listClass, nil)
}

// showTableDataRequest 传参
type showTableDataRequest struct {
    Database int64 `json:"database"`
    Table []*showTableDataRequestItem `json:"table"`
}

// showTableDataRequestItem 传参数
type showTableDataRequestItem struct {
    ID int64 `json:"id"`
    Logicalname string `json:"logicalname"`
    Physicsname string `json:"physicsname"`
    Category int64 `json:"category"`
}

// TableInfoDataUp 更新表修改信息
// @router /table-info-data-up [post]
func (c *RawDataclassController) TableInfoDataUp() {
     parameter := &showTableDataRequest{}
     err := json. Unmarshal(c.Ctx.Input.RequestBody, parameter)

     if err != nil {
        c. WriteJSON( nil, err)
         return
    }
     for _, item := range parameter.Table {
         _, err = rawDataLogicallayerService. GetTableInfoDataUpLogic(item.ID, item.Physicsname, item.Category) //添加
    }
     if err == nil {
        c. WriteJSON( "更新成功!", err)
         return
    }
    c. WriteJSON( "更新失败!", err)
}


猜你喜欢

转载自blog.csdn.net/zhaoliang831214/article/details/79930970