123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675 |
- package supply_analysis
- import (
- "encoding/json"
- "eta/eta_api/controllers"
- "eta/eta_api/models"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/data_manage/request"
- "eta/eta_api/models/data_manage/response"
- "eta/eta_api/models/data_manage/supply_analysis"
- "eta/eta_api/services/data"
- supply_analysisServ "eta/eta_api/services/data/supply_analysis"
- "eta/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strconv"
- "strings"
- "time"
- )
- // VarietyController 期货指标分类
- type VarietyController struct {
- controllers.BaseAuthController
- }
- // List
- // @Title 存量装置图表分类列表
- // @Description 存量装置图表分类列表接口
- // @Param IsAnalyse query bool true "是否有权限的,true、false"
- // @Param Keyword query string true "关键字搜索"
- // @Success 200 {object} response.VarietyListResp
- // @router /variety/list [get]
- func (this *VarietyController) List() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- isAnalyse, _ := this.GetBool("IsAnalyse")
- keyword := this.GetString("Keyword")
- 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)
- var condition string
- var pars []interface{}
- if keyword != `` {
- condition += ` AND (a.variety_name like ? OR c.edb_code like ? ) `
- pars = utils.GetLikeKeywordPars(pars, keyword, 2)
- }
- // 是否超管
- isSuperAdmin := supply_analysisServ.IsVarietySuperAdmin(this.SysUser)
- varietyObj := supply_analysis.Variety{}
- var total int
- var list []*supply_analysis.VarietyItem
- var err error
- if isSuperAdmin || !isAnalyse {
- total, list, err = varietyObj.GetListBySuperAdminPage(condition, pars, startSize, pageSize)
- } else {
- if isAnalyse {
- condition += ` AND b.sys_user_id = ? `
- pars = append(pars, this.SysUser.AdminId)
- }
- total, list, err = varietyObj.GetListByPage(condition, pars, startSize, pageSize)
- }
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- for k, v := range list {
- button := supply_analysis.VarietyButton{}
- if isSuperAdmin {
- button.Edit = true
- button.Delete = true
- button.Analyse = true
- } else {
- if v.PermissionUserId != `` {
- tmpUserIdStrList := strings.Split(v.PermissionUserId, ",")
- if utils.InArrayByStr(tmpUserIdStrList, strconv.Itoa(this.SysUser.AdminId)) {
- button.Analyse = true
- }
- }
- }
- v.Button = button
- list[k] = v
- }
- resp := response.VarietyListResp{
- AddButton: isSuperAdmin,
- Paging: paging.GetPaging(currentIndex, pageSize, total),
- List: list,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Add
- // @Title 新增品种
- // @Description 新增品种接口
- // @Param request body request.AddVarietyReq true "type json string"
- // @Success 200 Ret=200 添加成功
- // @router /variety/add [post]
- func (this *VarietyController) Add() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.AddVarietyReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- isSuperAdmin := supply_analysisServ.IsVarietySuperAdmin(this.SysUser)
- if !isSuperAdmin {
- br.Msg = "无操作权限"
- br.IsSendEmail = false
- return
- }
- if req.VarietyName == "" {
- br.Msg = "请输入品种名称"
- br.IsSendEmail = false
- return
- }
- varietyName := utils.TrimStr(req.VarietyName)
- item, err := supply_analysis.GetVarietyByName(varietyName)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "添加失败"
- br.ErrMsg = "添加失败,Err:" + err.Error()
- return
- }
- if item != nil {
- br.Msg = "添加失败,品种名称不能重复"
- br.IsSendEmail = false
- return
- }
- variety := &supply_analysis.Variety{
- VarietyId: 0,
- VarietyName: varietyName,
- LastUpdateSysUserId: this.SysUser.AdminId,
- LastUpdateSysUserRealName: this.SysUser.RealName,
- ProductionDay: 365,
- SysUserId: this.SysUser.AdminId,
- SysUserRealName: this.SysUser.RealName,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- err = supply_analysis.CreateVariety(variety, req.AdminIdList)
- if err != nil {
- br.Msg = "添加品种失败"
- br.ErrMsg = "添加品种失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Msg = "添加成功"
- br.IsAddLog = true
- br.Success = true
- }
- // Edit
- // @Title 编辑品种接口
- // @Description 编辑品种接口
- // @Param request body request.EditVarietyReq true "type json string"
- // @Success 200 Ret=200 修改成功
- // @router /variety/edit [post]
- func (this *VarietyController) Edit() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.EditVarietyReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- isSuperAdmin := supply_analysisServ.IsVarietySuperAdmin(this.SysUser)
- if !isSuperAdmin {
- br.Msg = "无操作权限"
- br.IsSendEmail = false
- return
- }
- if req.VarietyId <= 0 {
- br.Msg = "请选择品种"
- br.IsSendEmail = false
- return
- }
- if req.VarietyName == "" {
- br.Msg = "请输入品种名称"
- br.IsSendEmail = false
- return
- }
- varietyName := utils.TrimStr(req.VarietyName)
- item, err := supply_analysis.GetVarietyByName(varietyName)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "添加失败"
- br.ErrMsg = "添加失败,Err:" + err.Error()
- return
- }
- if item != nil && item.VarietyId != req.VarietyId {
- br.Msg = "添加失败,品种名称不能重复"
- br.IsSendEmail = false
- return
- }
- // 获取品种详情
- varietyInfo, err := supply_analysis.GetVarietyById(req.VarietyId)
- if err != nil {
- br.Msg = "查询品种失败"
- br.ErrMsg = "查询品种失败;ERR:" + err.Error()
- return
- }
- // 编辑
- varietyInfo.VarietyName = varietyName
- varietyInfo.LastUpdateSysUserId = this.SysUser.AdminId
- varietyInfo.LastUpdateSysUserRealName = this.SysUser.RealName
- varietyInfo.ModifyTime = time.Now()
- err = supply_analysis.EditVariety(varietyInfo, req.AdminIdList)
- if err != nil {
- br.Msg = "修改品种失败"
- br.ErrMsg = "修改品种失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Msg = "修改成功"
- br.IsAddLog = true
- br.Success = true
- }
- // DeleteCheck
- // @Title 删除品种检测接口
- // @Description 删除品种检测接口
- // @Param request body request.DelVarietyReq true "type json string"
- // @Success 200 Ret=200 检测成功
- // @router /variety/delete/check [post]
- func (this *VarietyController) DeleteCheck() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.DelVarietyReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.VarietyId <= 0 {
- br.Msg = "请选择品种"
- br.IsSendEmail = false
- return
- }
- var deleteStatus int
- var tipsMsg string
- //判断该品种是否生成指标
- count, err := supply_analysis.GetCountVarietyEdbInfoByVarietyId(req.VarietyId)
- if err != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "查询品种下是否含有指标失败,Err:" + err.Error()
- return
- }
- // 是否超管
- isSuperAdmin := supply_analysisServ.IsVarietySuperAdmin(this.SysUser)
- if !isSuperAdmin {
- deleteStatus = 1
- tipsMsg = "无删除权限"
- } else {
- if count > 0 {
- deleteStatus = 1
- tipsMsg = "该品种已在指标库生成指标,不可删除"
- }
- }
- resp := response.VarietyDeleteCheckResp{
- DeleteStatus: deleteStatus,
- TipsMsg: tipsMsg,
- }
- br.Ret = 200
- br.Msg = "检测成功"
- br.Success = true
- br.Data = resp
- }
- // Delete
- // @Title 删除存量装置图表分类/图表
- // @Description 删除存量装置图表分类/图表接口
- // @Param request body data_manage.DeleteChartClassifyReq true "type json string"
- // @Success 200 Ret=200 删除成功
- // @router /variety/delete [post]
- func (this *VarietyController) Delete() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.DelVarietyReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.VarietyId <= 0 {
- br.Msg = "请选择品种"
- br.IsSendEmail = false
- return
- }
- //判断该品种是否生成指标
- count, err := supply_analysis.GetCountVarietyEdbInfoByVarietyId(req.VarietyId)
- if err != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "查询品种下是否含有指标失败,Err:" + err.Error()
- return
- }
- // 是否超管
- isSuperAdmin := supply_analysisServ.IsVarietySuperAdmin(this.SysUser)
- if !isSuperAdmin {
- br.Msg = `无删除权限`
- br.IsSendEmail = false
- return
- }
- if count > 0 {
- br.Msg = `该品种已在指标库生成指标,不可删除`
- br.IsSendEmail = false
- return
- }
- varietyInfo, err := supply_analysis.GetVarietyById(req.VarietyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "该品种不存在或已删除"
- br.IsSendEmail = false
- } else {
- br.Msg = "删除失败"
- br.ErrMsg = "查找品种失败,ERR:" + err.Error()
- }
- return
- }
- err = varietyInfo.Delete()
- if err != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "查找品种失败,ERR:" + err.Error()
- return
- }
- br.Ret = 200
- br.Msg = "删除成功"
- br.Success = true
- br.IsAddLog = true
- }
- // Detail
- // @Title 存量装置图表分类列表
- // @Description 存量装置图表分类列表接口
- // @Param VarietyId query int true "品种id"
- // @Success 200 {object} response.VarietyListResp
- // @router /variety/detail [get]
- func (this *VarietyController) Detail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- varietyId, _ := this.GetInt("VarietyId")
- if varietyId < 0 {
- br.Msg = "不存在该品种或该品种已被删除"
- br.ErrMsg = "不存在该品种或该品种已被删除"
- br.IsSendEmail = false
- }
- // 获取品种详情
- varietyInfo, err := supply_analysis.GetVarietyById(varietyId)
- if err != nil {
- br.Msg = "查询品种失败"
- br.ErrMsg = "查询品种失败;ERR:" + err.Error()
- if err.Error() != utils.ErrNoRow() {
- br.Msg = "不存在该品种或该品种已被删除"
- br.ErrMsg = "不存在该品种或该品种已被删除"
- br.IsSendEmail = false
- }
- return
- }
- if varietyInfo == nil {
- br.Msg = "不存在该品种或该品种已被删除"
- br.ErrMsg = "不存在该品种或该品种已被删除"
- br.IsSendEmail = false
- return
- }
- resp := supply_analysis.VarietyItem{
- VarietyId: varietyInfo.VarietyId,
- VarietyName: varietyInfo.VarietyName,
- ProductionDay: varietyInfo.ProductionDay,
- LastUpdateSysUserId: varietyInfo.LastUpdateSysUserId,
- LastUpdateSysUserRealName: varietyInfo.LastUpdateSysUserRealName,
- PermissionUserId: ``,
- ModifyTime: varietyInfo.ModifyTime.Format(utils.FormatDateTime),
- CreateTime: varietyInfo.CreateTime.Format(utils.FormatDateTime),
- //Button: supply_analysis.VarietyButton{},
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Analysis
- // @Title 品种分析
- // @Description 品种分析接口
- // @Param request body request.AnalysisReq true "type json string"
- // @Success 200 Ret=200 检测成功
- // @router /variety/analysis [post]
- func (this *VarietyController) Analysis() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.AnalysisReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.VarietyId <= 0 {
- br.Msg = "请选择品种"
- br.IsSendEmail = false
- return
- }
- //加入缓存机制,避免创建同一时间,多人去分析 start
- redisKey := fmt.Sprint("supply_analysis:variety:", req.VarietyId)
- isExist := utils.Rc.IsExist(redisKey)
- if isExist {
- br.Msg = "品种正在分析中,请等待"
- br.IsSendEmail = false
- return
- } else {
- //设置3分钟缓存
- utils.Rc.SetNX(redisKey, 1, time.Second*300)
- defer func() {
- utils.Rc.Delete(redisKey)
- }()
- }
- //加入缓存机制,避免创建同一时间,多人去分析 end
- // 获取品种详情
- varietyInfo, err := supply_analysis.GetVarietyById(req.VarietyId)
- if err != nil {
- br.Msg = "查询品种失败"
- br.ErrMsg = "查询品种失败;ERR:" + err.Error()
- return
- }
- //判断该品种是否生成指标
- list, err := supply_analysis.GetVarietyEdbListByVarietyId(req.VarietyId)
- if err != nil {
- br.Msg = "分析失败"
- br.ErrMsg = "分析失败,Err:" + err.Error()
- return
- }
- // 如果没有生成过指标,那么就先生成指标
- if len(list) <= 0 {
- //randStr := utils.GetRandDigit(4)
- //prefixCode := "HZ_V"
- //suffixCode := time.Now().Format("060102") + randStr
- //`source` int(9) DEFAULT NULL COMMENT '来源,1:影响周度产量;2:周度产量变动;3:影响月度产量;4:月度产量变动',
- list := []*supply_analysis.VarietyEdbInfo{
- {
- //VarietyEdbId: 0,
- VarietyId: req.VarietyId,
- EdbName: varietyInfo.VarietyName + "影响周度产量",
- Frequency: "日度",
- EdbInfoId: 0,
- //EdbCode: fmt.Sprint(prefixCode, 1, suffixCode),
- Source: 1,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- },
- {
- //VarietyEdbId: 0,
- VarietyId: req.VarietyId,
- EdbName: varietyInfo.VarietyName + "周度产量变动",
- Frequency: "日度",
- EdbInfoId: 0,
- //EdbCode: fmt.Sprint(prefixCode, 2, suffixCode),
- Source: 2,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- },
- {
- //VarietyEdbId: 0,
- VarietyId: req.VarietyId,
- EdbName: varietyInfo.VarietyName + "影响月度产量",
- Frequency: "月度",
- EdbInfoId: 0,
- //EdbCode: fmt.Sprint(prefixCode, 3, suffixCode),
- Source: 3,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- },
- {
- //VarietyEdbId: 0,
- VarietyId: req.VarietyId,
- EdbName: varietyInfo.VarietyName + "月度产量变动",
- Frequency: "月度",
- EdbInfoId: 0,
- //EdbCode: fmt.Sprint(prefixCode, 4, suffixCode),
- Source: 4,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- },
- }
- err = supply_analysis.BatchCreateVarietyEdbInfo(list)
- if err != nil {
- br.Msg = `分析失败`
- br.ErrMsg = `分析失败,生成指标失败,ERR:` + err.Error()
- return
- }
- }
- tmpMap := map[string]interface{}{
- "VarietyId": req.VarietyId,
- "AdminId": this.SysUser.AdminId,
- "AdminName": this.SysUser.RealName,
- }
- reqJson, err := json.Marshal(tmpMap)
- // 开始分析
- _, err = supply_analysisServ.Calculate(string(reqJson))
- if err != nil {
- br.Msg = `分析失败`
- br.ErrMsg = `分析失败,ERR:` + err.Error()
- return
- }
- // 更新ETA指标库信息
- {
- if len(list) > 0 {
- for _, v := range list {
- if v.EdbInfoId > 0 {
- edbInfo, err := data_manage.GetEdbInfoById(v.EdbInfoId)
- if err != nil {
- continue
- }
- // 刷新指标数据
- go data.RefreshEdbData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, edbInfo.StartDate)
- }
- }
- }
- }
- // 变更品种的更新时间
- {
- varietyInfo.ModifyTime = time.Now()
- varietyInfo.LastUpdateSysUserId = this.SysUser.AdminId
- varietyInfo.LastUpdateSysUserRealName = this.SysUser.RealName
- go varietyInfo.Update([]string{"ModifyTime", "LastUpdateSysUserId", "LastUpdateSysUserRealName"})
- }
- br.Ret = 200
- br.Msg = "分析成功"
- br.Success = true
- }
- // ModifyProductionDay
- // @Title 编辑品种的生产天数接口
- // @Description 编辑品种的生产天数接口
- // @Param request body request.ModifyProductionDayReq true "type json string"
- // @Success 200 Ret=200 修改成功
- // @router /variety/production_day/modify [post]
- func (this *VarietyController) ModifyProductionDay() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req request.ModifyProductionDayReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- var hasPermission bool //是否有操作权限
- // 是否有装置的操作权限
- hasPermission, err = supply_analysisServ.HasVarietyPlantPermission(this.SysUser, req.VarietyId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败"
- br.ErrMsg = "查询是否有装置的操作权限失败;ERR:" + err.Error()
- return
- }
- if !hasPermission {
- br.Msg = "无操作权限"
- br.ErrMsg = "无操作权限"
- return
- }
- if req.VarietyId <= 0 {
- br.Msg = "请选择品种"
- br.IsSendEmail = false
- return
- }
- if req.Day <= 0 {
- br.Msg = "生产天数不能小于等于0"
- br.IsSendEmail = false
- return
- }
- // 获取品种详情
- varietyInfo, err := supply_analysis.GetVarietyById(req.VarietyId)
- if err != nil {
- br.Msg = "查询品种失败"
- br.ErrMsg = "查询品种失败;ERR:" + err.Error()
- return
- }
- // 编辑
- varietyInfo.ProductionDay = req.Day
- varietyInfo.LastUpdateSysUserId = this.SysUser.AdminId
- varietyInfo.LastUpdateSysUserRealName = this.SysUser.RealName
- varietyInfo.ModifyTime = time.Now()
- err = varietyInfo.Update([]string{"ProductionDay", "LastUpdateSysUserId", "LastUpdateSysUserRealName", "ModifyTime"})
- if err != nil {
- br.Msg = "修改生产天数失败"
- br.ErrMsg = "修改生产天数失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Msg = "修改成功"
- br.IsAddLog = true
- br.Success = true
- }
|