package controllers

import (
	"encoding/json"
	"eta/eta_index_lib/logic"
	"eta/eta_index_lib/models"
	"eta/eta_index_lib/utils"
	"fmt"
	"github.com/mozillazg/go-pinyin"
	"strconv"
	"time"
)

type MtjhDataController struct {
	BaseAuthController
}

// @Title 煤炭江湖数据
// @Description 刷新煤炭江湖数据接口
// @Param	request	body models.CoalMineDataReq true "type json string"
// @Success 200 {object} models.EdbClassify
// @router /data [post]
func (this *MtjhDataController) Mtjh() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.CoalMineDataReq

	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	var area string
	var port string
	var description string

	var dateMap = make(map[int]string)
	var varietyMap = make(map[int]string)
	var codeMap = make(map[string]string)
	var indexMap = make(map[string]string)
	var codeCompanyMap = make(map[string]string)
	var indexCompanyMap = make(map[string]string)
	var items []*models.BaseFromMtjhMapping
	var itemsIndex []*models.BaseFromMtjhIndex
	codeList, err := models.GetBaseFromMtjhMapping()
	if err != nil && err.Error() != utils.ErrNoRow() {
		utils.FileLog.Info("获取煤炭指标失败：", err)
		return
	}

	if len(codeList) > 0 {
		for _, v := range codeList {
			codeMap[v.IndexName] = v.IndexCode
		}
	}

	codeCompanyList, err := models.GetBaseFromMtjhMapping()
	if err != nil && err.Error() != utils.ErrNoRow() {
		utils.FileLog.Info("获取煤炭公司指标失败：", err)
		return
	}
	if len(codeCompanyList) > 0 {
		for _, v := range codeCompanyList {
			codeCompanyMap[v.IndexName] = v.IndexCode
		}
	}

	indexList, err := models.GetBaseFromMtjhIndex()
	if err != nil && err.Error() != utils.ErrNoRow() {
		utils.FileLog.Info("获取煤炭公司指标失败：", err)
		return
	}
	if len(indexList) > 0 {
		for _, v := range indexList {
			indexMap[v.IndexName+v.DataTime] = v.DealValue
		}
	}

	indexCompanyList, err := models.GetBaseFromCoalmineCompanyIndex()
	if err != nil && err.Error() != utils.ErrNoRow() {
		utils.FileLog.Info("获取煤炭公司指标失败：", err)
		return
	}
	if len(indexCompanyList) > 0 {
		for _, v := range indexCompanyList {
			indexCompanyMap[v.IndexName+v.DataTime] = v.DealValue
		}
	}

	for _, sheet := range req.SheetData {
		//遍历行读取
		maxRow := sheet.MaxRow
		for i := 1; i < maxRow; i++ {
			//获取样本情况
			row := sheet.Rows[i]
			cells := row.Cells

			// 获取日期
			if i == 1 {
				var date string
				for k, cell := range cells {
					if k > 2 {
						text := cell.Value
						if text != "" {
							stamp, _ := time.ParseInLocation("01-02-06", text, time.Now().Location())
							date = stamp.Format(utils.FormatDate)
						}
						dateMap[k] = date
					}
				}
			}

			// 获取品种
			if i == 2 {
				for k, cell := range cells {
					if k > 2 {
						text := cell.Value
						if text != "" {
							varietyMap[k] = text
						}
					}
				}
			}

			//for i := 0 ;i < len(cells);i ++ {
			//	fmt.Println("k:",i)
			//	fmt.Println("dateMap:",dateMap[i])
			//	fmt.Println("varietyMap:",varietyMap[i])
			//}
			if i > 2 {
				for k, cell := range cells {
					text := cell.Value
					if text != "" {
						if k == 1 {
							area = text
						}
						if k == 2 {
							port = text

							for j := 0; j < 3; j++ {
								item := new(models.BaseFromMtjhMapping)
								switch j {
								case 0:
									description = "动力煤"
								case 1:
									description = "焦煤"
								case 2:
									description = "合计"
								}
								itemName := area + port + description

								//取处理后公司名首字母缩写
								var strResult string

								a := pinyin.NewArgs()
								pys := [][]string{}
								for _, r := range itemName {
									py := pinyin.SinglePinyin(r, a)
									if len(py) > 0 {
										pys = append(pys, py)
									} else {
										if r != '-' && r != '（' && r != '）' {
											pys = append(pys, []string{string(r)})
										}
									}
								}
								for i := 0; i < len(pys); i++ {
									if len(pys[i]) != 0 {
										str := pys[i][0]
										pi := str[0:1]
										strResult += pi
									}
								}

								strResult = "mtjh" + strResult

								item.IndexCode = strResult
								item.Area = area
								item.Port = port
								item.Variety = description
								item.Unit = "万吨"
								item.Frequency = "周度"
								item.IndexName = itemName

								item.CreateTime = time.Now()
								items = append(items, item)
							}
						}
						if k > 2 {
							item := new(models.BaseFromMtjhIndex)
							item.IndexName = area + port + varietyMap[k]
							item.IndexCode = codeMap[item.IndexName]
							item.DealValue = text
							item.DataTime = dateMap[k]
							item.Area = area
							item.Port = port
							item.Variety = varietyMap[k]
							item.Unit = "万吨"
							item.Frequency = "周度"
							item.ModifyTime = time.Now()
							item.CreateTime = time.Now()
							itemsIndex = append(itemsIndex, item)
						}
					}
				}
			}
		}
	}

	//添加数据到数据库
	mappingAddList := make([]*models.BaseFromMtjhMapping, 0)
	for _, v := range items {
		if codeMap[v.IndexName] == "" {
			codeMap[v.IndexName] = v.IndexCode
			mappingAddList = append(mappingAddList, v)
		}
	}
	if len(mappingAddList) > 0 {
		newId, err := models.AddBaseFromMtjhMappingMuti(mappingAddList)
		if err != nil {
			fmt.Println("添加指标名称错误", err.Error())
		} else {
			fmt.Println("添加指标名称成功", newId)
		}
	}

	fmt.Println("指标操作完成")

	indexAddList := make([]*models.BaseFromMtjhIndex, 0)
	for _, v := range itemsIndex {
		v.IndexCode = codeMap[v.IndexName]
		if indexMap[v.IndexName+v.DataTime] == "" && v.DealValue != "" {
			indexAddList = append(indexAddList, v)
		} else {
			if indexMap[v.IndexName+v.DataTime] != v.DealValue && v.DealValue != "" {
				err = models.UpdateBaseFromMtjhIndex(v)
				if err != nil {
					fmt.Println("修改数据错误错误", err)
					return
				}
			}
		}
	}
	if len(indexAddList) > 0 {
		newId, err := models.AddBaseFromMtjhIndexMuti(indexAddList)
		if err != nil {
			fmt.Println("添加指标名称错误", err.Error())
		} else {
			fmt.Println("添加指标名称成功", newId)
		}
	}
	fmt.Println("数据操作完成")

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	return
}

// @Title 新增煤炭网指标接口
// @Description 新增煤炭网指标接口
// @Success 200 {object} models.AddEdbInfoReq
// @router /add [post]
func (this *MtjhDataController) Add() {
	br := new(models.BaseResponse).Init()
	var cacheKey string
	defer func() {
		utils.Rc.Delete(cacheKey)
		this.Data["json"] = br
		this.ServeJSON()
	}()
	source := utils.DATA_SOURCE_MTJH
	var req models.AddEdbInfoReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.EdbCode == "" {
		br.Msg = "请输入指标编码！"
		br.ErrMsg = "请输入指标编码，指标编码为空"
		return
	}
	cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.EdbCode
	if !utils.Rc.IsExist(cacheKey) {
		utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
		err = models.AddEdbDataFromMtjh(req.EdbCode)
		if err != nil {
			br.Msg = "获取指标信息失败！"
			br.ErrMsg = "获取指标信息失败 AddEdbDataFromMtjh，Err:" + err.Error()
			return
		}
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
	} else {
		br.Ret = 501
		br.Success = true
		br.Msg = "系统处理中，请稍后重试"
	}
}

// @Title 刷新中国煤炭市场网指标接口
// @Description 刷新中国煤炭市场网指标接口
// @Success 200 {object} models.RefreshEdbInfoReq
// @router /refresh [post]
func (this *MtjhDataController) Refresh() {
	br := new(models.BaseResponse).Init()
	var cacheKey string
	defer func() {
		utils.Rc.Delete(cacheKey)
		this.Data["json"] = br
		this.ServeJSON()
	}()
	source := utils.DATA_SOURCE_MTJH
	var req models.RefreshEdbInfoReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.EdbCode == "" {
		br.Msg = "请输入指标编码！"
		br.ErrMsg = "请输入指标编码，指标编码为空"
		return
	}
	if req.EdbInfoId <= 0 {
		br.Msg = "请输入指标ID！"
		br.ErrMsg = "请输入指标ID"
		return
	}

	cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
	if utils.Rc.IsExist(cacheKey) {
		br.Ret = 501
		br.Success = true
		br.Msg = "系统处理中，请稍后重试"
		return
	}

	utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
	// 获取指标详情
	edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
	if err != nil {
		br.Msg = "指标不存在！"
		br.ErrMsg = "指标不存在"
		return
	}

	err = models.RefreshEdbDataFromMtjh(req.EdbInfoId, req.EdbCode, req.StartDate)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "刷新指标信息失败！"
		br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromCoal，Err:" + err.Error()
		return
	}
	// 更新指标最大最小值
	err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = err.Error()
		return
	}

	// 更新ES
	go logic.UpdateEs(edbInfo.EdbInfoId)

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}
