package supply_analysis

import (
	"eta/eta_mobile/models/data_manage/supply_analysis"
	"eta/eta_mobile/models/system"
	"eta/eta_mobile/utils"
	"time"
)

// IsVarietySuperAdmin 判断是否供应分析的品种超管
func IsVarietySuperAdmin(sysUser *system.Admin) (isSuperAdmin bool) {
	//ficc管理员和超管有权限创建和管理品种
	if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
		isSuperAdmin = true
	}
	return
}

// HasVarietyPlantPermission 判断是否供应分析的品种超管
func HasVarietyPlantPermission(sysUser *system.Admin, varietyId int) (hasPermission bool, err error) {
	hasPermission = IsVarietySuperAdmin(sysUser)
	if hasPermission {
		return
	}

	tmp, err := supply_analysis.GetVarietyPermissionByVarietyIdAndUserId(varietyId, sysUser.AdminId)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	if tmp != nil { //找到记录那就代表有操作权限
		hasPermission = true
	}
	return
}

// CalculateByDay 计算日度减产量
func CalculateByDay(varietyInfo *supply_analysis.Variety) (dataMap map[time.Time]float64, startDate, lastDate time.Time, err error) {
	// 获取所有的装置
	plantList, err := supply_analysis.GetAllVarietyPlantByVarietyId(varietyInfo.VarietyId)
	if err != nil {
		return
	}

	// 产生的数据的结束日期
	{
		currDate := time.Now()
		currHour := currDate.Hour()
		if currHour < 22 {
			lastDate, _ = time.ParseInLocation(utils.FormatDate, currDate.Format(utils.FormatDate), time.Local)
		} else {
			lastDate, _ = time.ParseInLocation(utils.FormatDate, currDate.AddDate(0, 0, 1).Format(utils.FormatDate), time.Local)
		}

		lastDate = lastDate.AddDate(0, 0, 365) //业务需要计算的数据日期往当前日期后面加上配置的日期
	}

	// 维修数据的开始日期和结束日期
	//所有设备在该日期的减产数
	maintenanceDataMap := make(map[time.Time][]float64)

	for _, plantInfo := range plantList {
		if plantInfo.IsStop == 1 { // 如果是停产了，那么结束日期就是到 产生的数据的结束日期
			plantInfo.ResumptionDate = lastDate.Format(utils.FormatDate)
		}
		// 数据不全
		if plantInfo.MaintenanceDate == `` || plantInfo.ResumptionDate == `` {
			continue
		}
		// 日均产量减量为0，说明数据不全，不参与计算
		if plantInfo.AverageDailyCapacityReduction == 0 {
			continue
		}
		// 开始检修日期
		maintenanceDate, _ := time.ParseInLocation(utils.FormatDate, plantInfo.MaintenanceDate, time.Local)
		if startDate.IsZero() || maintenanceDate.Before(startDate) {
			startDate = maintenanceDate
		}

		// 结束检修日期
		resumptionDate, _ := time.ParseInLocation(utils.FormatDate, plantInfo.ResumptionDate, time.Local)

		for tmpDate := maintenanceDate; !tmpDate.After(resumptionDate); tmpDate = tmpDate.AddDate(0, 0, 1) {
			dataList, ok := maintenanceDataMap[tmpDate]
			if !ok {
				dataList = make([]float64, 0)
			}
			dataList = append(dataList, plantInfo.AverageDailyCapacityReduction)
			maintenanceDataMap[tmpDate] = dataList
		}
	}

	// 一个有用的装置都没有，那么就不需要计算了
	if len(maintenanceDataMap) <= 0 {
		return
	}

	// 计算出数据
	dataMap = make(map[time.Time]float64)
	for tmpDate := startDate; !tmpDate.After(lastDate); tmpDate = tmpDate.AddDate(0, 0, 1) {
		var calculateVal float64
		tmpDataList, ok := maintenanceDataMap[tmpDate]
		// 如果没有维修数据，那么退出当前循环，进入下一个循环
		if !ok {
			calculateVal = 0
		} else {
			for _, tmpVal := range tmpDataList {
				calculateVal += tmpVal
			}
		}

		dataMap[tmpDate] = calculateVal
	}

	return
}
