123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599 |
- package data_manage
- import (
- "eta/eta_api/models"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "github.com/tealeg/xlsx"
- "os"
- "path/filepath"
- "strings"
- "time"
- )
- // CCFClassify
- // @Title CCF化纤分类
- // @Description CCF化纤分类接口
- // @Success 200 {object} data_manage.BaseFromCCFClassifyItem
- // @router /ccf/classify [get]
- func (this *EdbInfoController) CCFClassify() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- classifies, e := data_manage.GetBaseFromCCFClassify()
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取CCF化纤分类失败, Err: " + e.Error()
- return
- }
- classifyOb := new(data_manage.BaseFromCCFClassify)
- resp := make([]*data_manage.BaseFromCCFClassifyItem, 0)
- parentMap := make(map[int][]*data_manage.BaseFromCCFClassifyItem)
- for _, v := range classifies {
- t := classifyOb.Format2Item(v)
- if v.ParentId == 0 {
- resp = append(resp, t)
- }
- if v.ParentId > 0 {
- if parentMap[v.ParentId] == nil {
- parentMap[v.ParentId] = make([]*data_manage.BaseFromCCFClassifyItem, 0)
- }
- parentMap[v.ParentId] = append(parentMap[v.ParentId], t)
- }
- }
- for _, v := range resp {
- v.Child = parentMap[v.ClassifyId]
- }
- br.Data = resp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // CCFIndexData
- // @Title 获取CCF化纤数据
- // @Description 获取CCF化纤接口
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param ClassifyId query string true "分类id"
- // @Success 200 {object} data_manage.LzFrequency
- // @router /ccf/index/data [get]
- func (this *EdbInfoController) CCFIndexData() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- classifyId, _ := this.GetInt("ClassifyId")
- if classifyId < 0 {
- br.Msg = "请选择分类"
- br.ErrMsg = "请选择分类"
- return
- }
- // 获取指标
- var condition string
- var pars []interface{}
- if classifyId >= 0 {
- condition += ` AND classify_id=? `
- pars = append(pars, classifyId)
- }
- indexes, err := data_manage.GetCCFIndex(condition, pars)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- indexCodes := make([]string, 0)
- for _, v := range indexes {
- indexCodes = append(indexCodes, v.IndexCode)
- }
- indexCounts, e := data_manage.GetCCFIndexDataCountGroup(indexCodes)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取指标数据总量失败, Err:" + err.Error()
- return
- }
- countMap := make(map[string]int)
- for _, v := range indexCounts {
- countMap[v.IndexCode] = v.Count
- }
- resultList := make([]*data_manage.BaseFromCCFIndexList, 0)
- for _, v := range indexes {
- product := new(data_manage.BaseFromCCFIndexList)
- product.BaseFromCcfIndexId = v.BaseFromCcfIndexId
- product.ClassifyId = v.ClassifyId
- product.Unit = v.Unit
- product.IndexCode = v.IndexCode
- product.IndexName = v.IndexName
- product.Frequency = v.Frequency
- product.CreateTime = v.CreateTime
- product.ModifyTime = v.ModifyTime
- total := countMap[v.IndexCode]
- page := paging.GetPaging(currentIndex, pageSize, total)
- dataList, e := data_manage.GetCCFIndexData(v.IndexCode, startSize, pageSize)
- if e != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取指标数据失败,Err:" + e.Error()
- return
- }
- if dataList == nil {
- dataList = make([]*data_manage.BaseFromCCFData, 0)
- }
- product.DataList = dataList
- product.Paging = page
- resultList = append(resultList, product)
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resultList
- }
- // CCFSearchList
- // @Title CCF模糊搜索
- // @Description CCF模糊搜索
- // @Param Keyword query string ture "关键字搜索"
- // @Success 200 {object} data_manage.BaseFromCCFIndexSearchItem
- // @router /ccf/search_list [get]
- func (this *EdbInfoController) CCFSearchList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- list := make([]*data_manage.BaseFromCCFIndexSearchItem, 0)
- var err error
- // 关键字
- keyword := this.GetString("Keyword")
- if keyword != "" {
- keyWordArr := strings.Split(keyword, " ")
- if len(keyWordArr) > 0 {
- condition := ""
- for _, v := range keyWordArr {
- condition += ` AND CONCAT(index_name,index_code) LIKE '%` + v + `%'`
- }
- list, err = data_manage.GetCCFItemList(condition)
- if err != nil {
- br.ErrMsg = "获取失败,Err:" + err.Error()
- br.Msg = "获取失败"
- return
- }
- }
- } else {
- list, err = data_manage.GetCCFItemList("")
- if err != nil {
- br.ErrMsg = "获取失败,Err:" + err.Error()
- br.Msg = "获取失败"
- return
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = list
- }
- // CCFSingleData
- // @Title 获取CCF数据
- // @Description 获取CCF单条数据接口
- // @Param IndexCode query string true "指标唯一编码"
- // @Success 200 {object} models.BaseResponse
- // @router /ccf/single_data [get]
- func (this *EdbInfoController) CCFSingleData() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- indexCode := this.GetString("IndexCode")
- indexInfo, err := data_manage.GetBaseFromCCFIndexByIndexCode(indexCode)
- if err != nil {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
- return
- }
- dataTmpList, err := data_manage.GetCCFIndexDataByCode(indexCode)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- var ret data_manage.CCFSingleDataResp
- var dataList []*data_manage.CCFSingleData
- ret.ClassifyId = indexInfo.ClassifyId
- ret.BaseFromCcfIndexId = indexInfo.BaseFromCcfIndexId
- ret.IndexCode = indexInfo.IndexCode
- ret.IndexName = indexInfo.IndexName
- ret.Frequency = indexInfo.Frequency
- ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
- ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
- ret.Unit = indexInfo.Unit
- for _, v := range dataTmpList {
- tmp := &data_manage.CCFSingleData{
- Value: v.Value,
- DataTime: v.DataTime,
- }
- dataList = append(dataList, tmp)
- }
- ret.Data = dataList
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = ret
- }
- // ExportCCFList
- // @Title 导出CCF数据
- // @Description 导出CCF数据
- // @Param IndexName query string false "名称关键词"
- // @Param IndexCode query string false "指标唯一编码"
- // @Param ClassifyId query string true "分类"
- // @Success 200 导出成功
- // @router /ccf/export [get]
- func (this *EdbInfoController) ExportCCFList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- indexCode := this.GetString("IndexCode") //指标唯一编码
- classifyId, _ := this.GetInt("ClassifyId")
- dir, _ := os.Executable()
- exPath := filepath.Dir(dir)
- downFile := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
- xlsxFile := xlsx.NewFile()
- var pars []interface{}
- condition := ""
- if classifyId > 0 {
- //获取指标
- condition += " AND classify_id=?"
- pars = append(pars, classifyId)
- }
- if indexCode != "" {
- //获取指标
- condition += " AND index_code=?"
- pars = append(pars, indexCode)
- }
- indexList, err := data_manage.GetCCFIndex(condition, pars)
- if err != nil {
- fmt.Println("获取数据失败,Err:" + err.Error())
- return
- }
- if len(indexList) <= 0 {
- fmt.Println("indexList 为空")
- br.Ret = 200
- br.Success = true
- br.Msg = "success"
- return
- }
- sheetNew := new(xlsx.Sheet)
- sheetNew, err = xlsxFile.AddSheet("CCF化纤")
- //sheetNew.SetColWidth()
- //获取指标数据
- windRow := sheetNew.AddRow()
- secNameRow := sheetNew.AddRow()
- indexCodeRow := sheetNew.AddRow()
- frequencyRow := sheetNew.AddRow()
- unitRow := sheetNew.AddRow()
- lastModifyDateRow := sheetNew.AddRow()
- //获取分类下指标最大数据量
- var dataMax int
- setRowIndex := 6
- indexCodeList := make([]string, 0)
- for _, v := range indexList {
- indexCodeList = append(indexCodeList, v.IndexCode)
- }
- dataListMap := make(map[string][]*data_manage.BaseFromCCFData)
- if len(indexList) > 0 {
- allDataList, e := data_manage.GetCCFIndexDataByCodes(indexCodeList)
- if e != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + e.Error()
- return
- }
- for _, v := range allDataList {
- dataListMap[v.IndexCode] = append(dataListMap[v.IndexCode], v)
- }
- for _, v := range dataListMap {
- if len(v) > dataMax {
- dataMax = len(v)
- }
- }
- }
- for k, sv := range indexList {
- //获取数据
- dataList, ok := dataListMap[sv.IndexCode]
- if !ok {
- continue
- }
- if len(dataList) > 0 {
- windRow.AddCell().SetValue("CCF化纤")
- secNameRow.AddCell().SetValue("指标名称")
- indexCodeRow.AddCell().SetValue("指标ID")
- frequencyRow.AddCell().SetValue("频率")
- unitRow.AddCell().SetValue("单位")
- lastModifyDateRow.AddCell().SetValue("更新时间")
- secNameRow.AddCell().SetValue(sv.IndexName)
- indexCodeRow.AddCell().SetValue(sv.IndexCode)
- frequencyRow.AddCell().SetValue(sv.Frequency)
- unitRow.AddCell().SetValue(sv.Unit)
- lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
- windRow.AddCell()
- windRow.AddCell()
- secNameRow.AddCell()
- indexCodeRow.AddCell()
- frequencyRow.AddCell()
- unitRow.AddCell()
- lastModifyDateRow.AddCell()
- minCol := k * 3
- sheetNew.SetColWidth(minCol, minCol, 15)
- if len(dataList) <= 0 {
- for n := 0; n < dataMax; n++ {
- rowIndex := setRowIndex + n
- row := sheetNew.Row(rowIndex)
- row.AddCell()
- row.AddCell()
- row.AddCell()
- }
- } else {
- endRowIndex := 0
- for rk, dv := range dataList {
- rowIndex := setRowIndex + rk
- row := sheetNew.Row(rowIndex)
- displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
- displayDateCell := row.AddCell()
- style := new(xlsx.Style)
- style.ApplyAlignment = true
- style.Alignment.WrapText = true
- displayDateCell.SetStyle(style)
- displayDateCell.SetDate(displayDate)
- row.AddCell().SetValue(dv.Value)
- row.AddCell()
- endRowIndex = rowIndex
- }
- if len(dataList) < dataMax {
- dataLen := dataMax - len(dataList)
- for n := 0; n < dataLen; n++ {
- rowIndex := (endRowIndex + 1) + n
- row := sheetNew.Row(rowIndex)
- row.AddCell()
- row.AddCell()
- row.AddCell()
- }
- }
- }
- }
- }
- err = xlsxFile.Save(downFile)
- if err != nil {
- //有指标无数据时先导出一遍空表
- sheet, err := xlsxFile.AddSheet("无数据")
- if err != nil {
- br.Msg = "新增Sheet失败"
- br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
- return
- }
- rowSecName := sheet.AddRow()
- celSecName := rowSecName.AddCell()
- celSecName.SetValue("")
- err = xlsxFile.Save(downFile)
- if err != nil {
- br.Msg = "保存文件失败"
- br.ErrMsg = "保存文件失败"
- return
- }
- }
- fileName := `CCF化纤信息`
- //if len(indexList) > 0 {
- // fileName = indexList[0].IndexName
- //}
- fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
- this.Ctx.Output.Download(downFile, fileName)
- defer func() {
- os.Remove(downFile)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "success"
- }
- // CCFStockClassify
- // @Title CCF化纤信息-装置分类
- // @Description CCF化纤信息-装置分类
- // @Success 200 {object} data_manage.CCFStockClassify
- // @router /ccf/stock/classify [get]
- func (this *EdbInfoController) CCFStockClassify() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- classifyOb := new(data_manage.CCFStockClassify)
- items, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "ccf_stock_classify_id ASC")
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取CCF化纤分类列表失败, Err: " + e.Error()
- return
- }
- resp := make([]*data_manage.CCFStockClassifyItem, 0)
- for _, v := range items {
- resp = append(resp, v.Format2Item())
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // CCFStockTable
- // @Title CCF化纤信息-装置表格详情
- // @Description CCF化纤信息-装置表格详情
- // @Param ClassifyId query int true "分类ID"
- // @Param TableDate query string false "表格日期"
- // @Success 200 {object} data_manage.CCFStockExcel
- // @router /ccf/stock/table [get]
- func (this *EdbInfoController) CCFStockTable() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- classifyId, _ := this.GetInt("ClassifyId", 0)
- tableDate := this.GetString("TableDate")
- if tableDate != "" {
- _, e := time.ParseInLocation(utils.FormatDate, tableDate, time.Local)
- if e != nil {
- br.Msg = "日期格式有误"
- return
- }
- }
- excelOb := new(data_manage.CCFStockExcel)
- cond := ``
- pars := make([]interface{}, 0)
- if classifyId > 0 {
- cond += fmt.Sprintf(` AND %s = ?`, excelOb.Cols().ClassifyId)
- pars = append(pars, classifyId)
- }
- if tableDate != "" {
- cond += fmt.Sprintf(` AND %s = ?`, excelOb.Cols().ExcelDate)
- pars = append(pars, tableDate)
- }
- // 若无tableDate默认取最近的有数据的表格
- item, e := excelOb.GetItemByCondition(cond, pars, fmt.Sprintf("%s DESC", excelOb.Cols().ExcelDate))
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Ret = 200
- br.Success = true
- br.Msg = "该日期暂无数据"
- br.Data = data_manage.CCFStockExcel{}
- return
- }
- br.Msg = "获取失败"
- br.ErrMsg = "获取CCF化纤装置表格失败, Err: " + e.Error()
- return
- }
- resp := item.Format2Item()
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
|