package data_manage

import (
	"eta/eta_api/models"
	"eta/eta_api/models/data_manage"
	"eta/eta_api/services/data"
	"eta/eta_api/utils"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/paging"
	"time"
)

// @Title 指标一键刷新-统计
// @Description 指标一键刷新-统计接口
// @Success 200 {object} data_manage.UpdatesStatisticalResp
// @router /edb_info/updates/statistical [get]
func (this *EdbInfoController) EdbInfoUpdatesStatistical() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	var condition string
	var pars []interface{}

	condition += ` AND  is_update=? AND edb_info_type = ?`
	pars = append(pars, 2, 0)
	updateList, _ := data_manage.GetUpdatesStatistical(condition, pars)

	resp := new(data_manage.UpdatesStatisticalResp)
	for _, v := range updateList {
		if v.Source == utils.DATA_SOURCE_THS {
			resp.ThsUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_WIND {
			resp.WindUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_PB {
			resp.PbUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_PB_FINANCE {
			resp.PbFinanceUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_LZ {
			resp.LzUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_YS {
			resp.SmmUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_GL {
			resp.MysteelUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_MANUAL {
			resp.ManualUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_ZZ {
			resp.ZzUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_DL {
			resp.DlUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_SH {
			resp.ShUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_CFFEX {
			resp.CffexUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_SHFE {
			resp.ShfeUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_GIE {
			resp.GieUpdate = v.Total
		}
		//if v.Source == utils.DATA_SOURCE_CALCULATE ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_LJZZY ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_TBZ ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_TCZ ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_HBZ ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_HCZ ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_BP ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_TIME_SHIFT ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_ZJPJ ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_LJZTBPJ ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_CJJX ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_NHCC {
		//	resp.CalculateUpdate += v.Total
		//}
		if v.EdbType == 2 {
			resp.CalculateUpdate += v.Total
		}
		if v.Source == utils.DATA_SOURCE_LT {
			resp.LtUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_COAL {
			resp.CoalUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_GOOGLE_TRAVEL {
			resp.GoogleTravelUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_EIA_STEO {
			resp.EiaSteoUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_COM_TRADE {
			resp.UNUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_SCI {
			resp.SciUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_BAIINFO {
			resp.BaiinfoUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_NATIONAL_STATISTICS {
			resp.NationalStatisticsUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_FUBAO {
			resp.FubaoUpdate = v.Total
		}
	}

	var notUpdateCondition string
	var notUpdatePars []interface{}
	notUpdateCondition += ` AND  is_update=? AND edb_info_type = ? `
	notUpdatePars = append(notUpdatePars, 1, 0)
	notUpdateList, _ := data_manage.GetUpdatesStatistical(notUpdateCondition, notUpdatePars)

	for _, v := range notUpdateList {
		if v.Source == utils.DATA_SOURCE_THS {
			resp.ThsNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_WIND {
			resp.WindNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_PB {
			resp.PbNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_PB_FINANCE {
			resp.PbFinanceNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_LZ {
			resp.LzNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_YS {
			resp.SmmNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_GL {
			resp.MysteelNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_MANUAL {
			resp.ManualNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_ZZ {
			resp.ZzNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_DL {
			resp.DlNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_SH {
			resp.ShNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_CFFEX {
			resp.CffexNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_SHFE {
			resp.ShfeNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_GIE {
			resp.GieNotUpdate = v.Total
		}
		//if v.Source == utils.DATA_SOURCE_CALCULATE ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_LJZZY ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_TBZ ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_TCZ ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_HBZ ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_HCZ ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_BP ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_TIME_SHIFT ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_ZJPJ ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_LJZTBPJ ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_CJJX ||
		//	v.Source == utils.DATA_SOURCE_CALCULATE_NHCC {
		//	resp.CalculateNotUpdate += v.Total
		//}

		if v.EdbType == 2 {
			resp.CalculateNotUpdate += v.Total
		}
		if v.Source == utils.DATA_SOURCE_LT {
			resp.LtNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_COAL {
			resp.CoalNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_GOOGLE_TRAVEL {
			resp.GoogleTravelNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_EIA_STEO {
			resp.EiaSteoNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_COM_TRADE {
			resp.UNNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_SCI {
			resp.SciNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_BAIINFO {
			resp.BaiinfoNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_NATIONAL_STATISTICS {
			resp.NationalStatisticsNotUpdate = v.Total
		}
		if v.Source == utils.DATA_SOURCE_FUBAO {
			resp.FubaoNotUpdate = v.Total
		}
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 指标一键刷新->列表接口
// @Description 指标一键刷新->列表接口
// @Param   Status   query   int  true       "1:未更新，2：已更新"
// @Param   Source   query   int  true       "来源:0:全部，1:同花顺,2:wind,3:彭博,9:手工指标,10:隆众,11:有色，99:计算指标"
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码，从1开始"
// @Success Ret=200 刷新成功
// @router /edb_info/updates/list [get]
func (this *EdbInfoController) EdbInfoUpdatesList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	status, _ := this.GetInt("Status")
	source, _ := this.GetInt("Source")
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")

	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = paging.StartIndex(currentIndex, pageSize)

	var condition string
	var pars []interface{}

	condition += ` AND edb_info_type = ? `
	pars = append(pars, 0)

	if status > 0 {
		condition += ` AND  is_update=?`
		pars = append(pars, status)
	}

	if source == 99 {
		condition += ` AND  edb_type=?  `
		pars = append(pars, 2)
	}

	if source > 0 && source != 99 {
		condition += ` AND  source=?`
		pars = append(pars, source)
	}

	condition += ` ORDER BY modify_time ASC `
	list, err := data_manage.GetUpdatesList(condition, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	total, err := data_manage.GetUpdatesListTotal(condition, pars)
	if err != nil {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取更新数据总数失败，Err:" + err.Error()
		return
	}
	page := paging.GetPaging(currentIndex, pageSize, total)
	resp := new(data_manage.UpdatesListResp)
	resp.List = list
	resp.Total = total
	resp.Paging = page
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// EdbInfoUpdates
// @Title 指标一键刷新接口
// @Description 指标一键刷新接口
// @Success Ret=200 更新成功
// @router /edb_info/updates [post]
func (this *EdbInfoController) EdbInfoUpdates() {
	br := new(models.BaseResponse).Init()
	setNxKey := "EDB_INFO_UPDATES"
	deleteCache := true
	defer func() {
		if deleteCache {
			utils.Rc.Delete(setNxKey)
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	if !utils.Rc.SetNX(setNxKey, 1, 30*time.Minute) {
		deleteCache = false
		br.Msg = "系统处理中，请勿重复操作！"
		br.IsSendEmail = false
		return
	}

	list, err := data_manage.GetAllUpdatesList()
	if err != nil {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	for _, v := range list {
		err = data.EdbInfoRefresh(v, false)
		if err != nil {
			br.Msg = "刷新失败"
			br.ErrMsg = "刷新指标失败，指标id：" + fmt.Sprint(v.EdbInfoId) + ", Err:" + err.Error()
			return
		}
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "更新成功"
}

// @Title 指标一键刷新接口
// @Description 指标一键刷新接口
// @Success Ret=200 更新成功
// @router /edb_info/updates/check [post]
func (this *EdbInfoController) EdbInfoUpdatesCheck() {
	br := new(models.BaseResponse).Init()
	setNxKey := "EDB_INFO_UPDATES"
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	status := 0
	if utils.Rc.IsExist(setNxKey) {
		status = 1
	} else {
		status = 2
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "检测成功"
	br.Data = status
}
