// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/report"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/report"
	baseModel "LAPP_ACURA_MOM_BACKEND/models/base"
	model "LAPP_ACURA_MOM_BACKEND/models/report"
	"container/list"
	"time"
)

/******************************************************************************
*
* @Description              : FilterDataInfo的默认查询条件
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-06-21 09:55:32
*
******************************************************************************/
var DefaultConditionOfFilterDataInfo = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.FilterDataInfo_Station.UrlParameterName:  grmi.NewConditionItem(meta.FilterDataInfo_Station, grmi.Equal, false),
		meta.FilterDataInfo_Ts.UrlParameterName:       grmi.NewConditionItem(meta.FilterDataInfo_Ts, grmi.Approximate, false),
		meta.FilterDataInfo_Address.UrlParameterName:  grmi.NewConditionItem(meta.FilterDataInfo_Address, grmi.Equal, false),
		meta.FilterDataInfo_Value.UrlParameterName:    grmi.NewConditionItem(meta.FilterDataInfo_Value, grmi.Equal, false),
		meta.FilterDataInfo_Duration.UrlParameterName: grmi.NewConditionItem(meta.FilterDataInfo_Duration, grmi.Equal, false),
		meta.FilterDataInfo_DescCN.UrlParameterName:   grmi.NewConditionItem(meta.FilterDataInfo_DescCN, grmi.Equal, false),
		meta.FilterDataInfo_PartNo.UrlParameterName:   grmi.NewConditionItem(meta.FilterDataInfo_PartNo, grmi.Equal, false),
	},
	nil,
)

/******************************************************************************
*
* @Description              : FilterDataInfo的默认分页查询条件
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-06-21 09:55:32
*
******************************************************************************/
var DefaultConditionOfFilterDataInfoAndPaging = grmi.NewCondition(
	DefaultConditionOfFilterDataInfo.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : FilterDataInfoServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : FilterDataInfo的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-06-21 09:55:32
*
******************************************************************************/
type FilterDataInfoServiceImplement struct {
}

/******************************************************************************
*
* @Function Name            : NewFilterDataInfoDAOImplement
*-----------------------------------------------------------------------------
*
* @Description              : 创建一个FilterDataInfoDAOImplement实例
*
* @Return Value             : FilterDataInfoDAOImplement实例
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-06-21 09:55:32
*
******************************************************************************/
func NewFilterDataInfoServiceImplement() *FilterDataInfoServiceImplement {
	return &FilterDataInfoServiceImplement{}
}

/******************************************************************************
*
* @Reference LAPP_ACURA_MOM_BACKEND/services/report/FilterDataInfoService.InsertOne
*
******************************************************************************/
func (impl *FilterDataInfoServiceImplement) InsertOne(entity *model.FilterDataInfo) error {

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewFilterDataInfoDAO(session)
	record, err := dao.SelectOne()
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
*
* @Reference LAPP_ACURA_MOM_BACKEND/services/report/FilterDataInfoService.DeleteOne
*
******************************************************************************/
func (impl *FilterDataInfoServiceImplement) DeleteOne() error {
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewFilterDataInfoDAO(session)
	record, err := dao.SelectOne()
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne()
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
*
* @Reference LAPP_ACURA_MOM_BACKEND/services/report/FilterDataInfoService.SelectOne
*
******************************************************************************/
func (impl *FilterDataInfoServiceImplement) SelectOne() (*model.FilterDataInfo, error) {
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewFilterDataInfoDAO(session)
	result, err := dao.SelectOne()
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
*
* @Reference LAPP_ACURA_MOM_BACKEND/services/report/FilterDataInfoService.UpdateOne
*
******************************************************************************/
func (impl *FilterDataInfoServiceImplement) UpdateOne(entity *model.FilterDataInfo) error {
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewFilterDataInfoDAO(session)
	record, err := dao.SelectOne()
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
*
* @Reference LAPP_ACURA_MOM_BACKEND/services/report/FilterDataInfoService.Insert
*
******************************************************************************/
func (impl *FilterDataInfoServiceImplement) Insert(entities *[]model.FilterDataInfo) error {
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewFilterDataInfoDAO(session)

	for range *entities {
		record, err := dao.SelectOne()
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

	err := dao.Insert(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
*
* @Reference LAPP_ACURA_MOM_BACKEND/services/report/FilterDataInfoService.Delete
*
******************************************************************************/
func (impl *FilterDataInfoServiceImplement) Delete(entities *[]model.FilterDataInfo) error {

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewFilterDataInfoDAO(session)

	for range *entities {
		record, err := dao.SelectOne()
		if err != nil {
			return err
		}
		if record == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
	}

	err := dao.Delete(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
*
* @Reference LAPP_ACURA_MOM_BACKEND/services/report/FilterDataInfoService.Select
*
******************************************************************************/
func (impl *FilterDataInfoServiceImplement) Select(urlParameters map[string]string) (interface{}, error) {
	condition := DefaultConditionOfFilterDataInfoAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewFilterDataInfoDAO(session)
	if condition.Fill(urlParameters) {
		result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	} else {
		result, err := dao.Select(predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	}
}

/******************************************************************************
*
* @Reference LAPP_ACURA_MOM_BACKEND/services/report/FilterDataInfoService.Update
*
******************************************************************************/
func (impl *FilterDataInfoServiceImplement) Update(entities *[]model.FilterDataInfo) error {
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewFilterDataInfoDAO(session)

	for range *entities {
		record, err := dao.SelectOne()
		if err != nil {
			return err
		}
		if record == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
	}

	err := dao.Update(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
*
* @Reference LAPP_ACURA_MOM_BACKEND/services/report/FilterDataInfoService.Select
*
******************************************************************************/
//func (impl *FilterDataInfoServiceImplement) GetDisplayData() (result []model.AWMDisplay, err error) {
//	user := "report"
//	result = make([]model.AWMDisplay, 0)
//	slaves := db.Eloquent.Slaves()
//	if len(slaves) < 3 {
//		return result, errors.New("未检测到数据源数据引擎")
//	}
//	engine := slaves[2]
//	session := engine.NewSession()
//	defer session.Close()
//	localSession := db.Eloquent.Master().NewSession()
//	defer localSession.Close()
//	displayDao := dal.NewDisplayDAO(localSession, user)
//	displayParaDao := dal.NewDisplayParamDAO(localSession, user)
//	displayLi, err := displayDao.Select([]grmi.Predicate{meta.Display_Source.NewPredicate(grmi.Equal, model.DISPLAY_SOURCE_ARC)}, []grmi.Field{meta.Display_DPCtrlPara1})
//	if err != nil {
//		return nil, err
//	}
//	dao := dal.NewFilterDataInfoDAO(session)
//	workPlaceDao := baseDal.NewWorkPlaceDAO(localSession, baseModel.PlantNr, user)
//	calendarHeadDao := baseDal.NewWorkCalendarDAO(localSession, baseModel.PlantNr, user)
//	calendarLstDao := baseDal.NewWorkCalendarLstDAO(localSession, baseModel.PlantNr, user)
//	weekModelHeadDao := baseDal.NewWeekModelDAO(localSession, baseModel.PlantNr, user)
//	weekModelLstDao := baseDal.NewWeekModelLstDAO(localSession, baseModel.PlantNr, user)
//	dayModelDao := baseDal.NewDayModelDAO(localSession, baseModel.PlantNr, user)
//	workShiftDao := baseDal.NewWorkShiftDAO(localSession, baseModel.PlantNr, user)
//	todayString := time.Now().Format(grmi.DateOutFormat)
//	today, _ := time.ParseInLocation(grmi.DateOutFormat, time.Now().Format(grmi.DateOutFormat), utils.TimezoneLocation)
//	yesterday, err := time.ParseInLocation(grmi.DateOutFormat, time.Now().AddDate(0, 0, -1).Format(grmi.DateOutFormat), utils.TimezoneLocation)
//	if err != nil {
//		return nil, err
//	}
//	todayBegin, err := time.ParseInLocation(grmi.DateOutFormat, time.Now().Format(grmi.DateOutFormat), utils.TimezoneLocation)
//	if err != nil {
//		return nil, err
//	}
//	yesterdayBegin, err := time.ParseInLocation(grmi.DateOutFormat, time.Now().AddDate(0, 0, -1).Format(grmi.DateOutFormat), utils.TimezoneLocation)
//	if err != nil {
//		return nil, err
//	}
//	//todayBegin, err := time.ParseInLocation(grmi.DateOutFormat, "2021-06-12", utils.TimezoneLocation)
//	//if err != nil {
//	//	return nil, err
//	//}
//	//todayString := "2021-06-12"
//	//today, _ := time.ParseInLocation(grmi.DateOutFormat, "2021-06-12", utils.TimezoneLocation)
//
//	//today := time.Now().Format(grmi.DateOutFormat)
//	for _, display := range displayLi {
//		paramLi, err := displayParaDao.Select([]grmi.Predicate{meta.DisplayParam_DisplayId.NewPredicate(grmi.Equal, display.DisplayId)}, nil)
//		if err != nil {
//			return nil, err
//		}
//		if len(paramLi) < 5 {
//			return nil, grmi.NewBusinessError("配置不足")
//		}
//		var workPlaceNr int
//		var descCN string
//		var standardValue float64
//		var statisticField string
//		var uom string
//		var cate string
//		var inputAvg float64
//		var max float64
//		var min float64
//		var startTime string
//		var endTime string
//		var muTitle string
//		var muUom string
//		for _, param := range paramLi {
//			switch param.ParamName {
//			case "WorkPlaceId":
//				workPlaceNr = param.CtrlPara1
//			case "DescCN":
//				descCN = param.CtrlPara5
//			case "StandardValue":
//				standardValue = param.CtrlPara3
//			case "StatisticField":
//				statisticField = param.CtrlPara5
//			case "Uom":
//				uom = param.CtrlPara5
//			case "Cate":
//				cate = param.CtrlPara5
//			case "InputAvg":
//				inputAvg = param.CtrlPara3
//			case "Max":
//				max = param.CtrlPara3
//			case "Start":
//				startTime = param.CtrlPara5
//			case "End":
//				endTime = param.CtrlPara5
//			case "MuTitle":
//				muTitle = param.CtrlPara5
//			case "MuUom":
//				muUom = param.CtrlPara5
//			case "Min":
//				min = param.CtrlPara3
//			}
//		}
//		workPlace, err := workPlaceDao.SelectOne(workPlaceNr)
//		if err != nil {
//			return nil, err
//		}
//		if workPlace == nil {
//			return nil, grmi.NewBusinessError("该工位不存在")
//		}
//		// 获取今天和昨天的日模型
//		var dayModelNr int
//		var yesterdayModelNr int
//
//		if workPlace.WorkCalendarNr != 0 {
//			workCalendar, err := calendarHeadDao.SelectOne(workPlace.WorkCalendarNr)
//			if err != nil {
//				return nil, err
//			}
//			if workCalendar == nil {
//				return nil, grmi.NewBusinessError("不存在工位对应的工作日历")
//			}
//			calendarLstLi, err := calendarLstDao.Select([]grmi.Predicate{baseMeta.WorkCalendarLst_WorkCalendarNr.NewPredicate(grmi.Equal, workCalendar.WorkCalendarNr)}, nil)
//			if err != nil {
//				return nil, err
//			}
//			for _, calendar := range calendarLstLi {
//				if calendar.WorkDate.Restore().Equal(today) {
//					dayModelNr = calendar.DayModelNr
//				} else if calendar.WorkDate.Restore().Equal(yesterday) {
//					yesterdayModelNr = calendar.DayModelNr
//				}
//				if dayModelNr != 0 && yesterdayModelNr != 0 {
//					break
//				}
//			}
//		}
//		var weekModelLstLi []baseModel.WeekModelLst
//
//		if dayModelNr == 0 {
//			weekModel, err := weekModelHeadDao.SelectOne(workPlace.WeekModelNr)
//			if err != nil {
//				return nil, err
//			}
//			if weekModel == nil {
//				return nil, grmi.NewBusinessError("不存在工位对应的周模型")
//			}
//			weekModelLstLi, err = weekModelLstDao.Select([]grmi.Predicate{baseMeta.WeekModelLst_WeekModelNr.NewPredicate(grmi.Equal, weekModel.WeekModelNr)}, []grmi.Field{baseMeta.WeekModelLst_WorkDate})
//			weekDay := today.Weekday()
//			for _, weekModelLst := range weekModelLstLi {
//				if weekDay == weekModelLst.WorkDate.Restore().Weekday() {
//					dayModelNr = weekModelLst.DayModelNr
//					break
//				}
//			}
//		}
//		if yesterdayModelNr == 0 {
//			weekModel, err := weekModelHeadDao.SelectOne(workPlace.WeekModelNr)
//			if err != nil {
//				return nil, err
//			}
//			if weekModel == nil {
//				return nil, grmi.NewBusinessError("不存在工位对应的周模型")
//			}
//			if weekModelLstLi == nil {
//				weekModelLstLi, err = weekModelLstDao.Select([]grmi.Predicate{baseMeta.WeekModelLst_WeekModelNr.NewPredicate(grmi.Equal, weekModel.WeekModelNr)}, []grmi.Field{baseMeta.WeekModelLst_WorkDate})
//			}
//			weekDay := yesterday.Weekday()
//			for _, weekModelLst := range weekModelLstLi {
//				if weekDay == weekModelLst.WorkDate.Restore().Weekday() {
//					yesterdayModelNr = weekModelLst.DayModelNr
//					break
//				}
//			}
//		}
//		if dayModelNr == 0 || yesterdayModelNr == 0 {
//			return nil, grmi.NewBusinessError("未获取到该工位的日模型主数据")
//		}
//		dayModel, err := dayModelDao.SelectOne(dayModelNr)
//		if err != nil {
//			return nil, err
//		}
//		if dayModel == nil {
//			return nil, grmi.NewBusinessError("未获取到该工位的今天日模型主数据")
//		}
//		yesterdayModel, err := dayModelDao.SelectOne(yesterdayModelNr)
//		if err != nil {
//			return nil, err
//		}
//		if yesterdayModel == nil {
//			return nil, grmi.NewBusinessError("未获取到该工位的昨天日模型主数据")
//		}
//		var yesterdayEnd int
//		var yesterdayStart int
//		var yesterdayLength int
//		var start time.Time
//		var end time.Time
//
//		// 求取昨天的开始 结束时间和工作时长
//		if yesterdayModel.WorkShiftToggle {
//			if yesterdayModel.S1WorkShiftNr == 0 {
//				return nil, grmi.NewBusinessError("绑定的日模型错误")
//			}
//
//			s1WorkShift, err := workShiftDao.SelectOne(yesterdayModel.S1WorkShiftNr)
//			if err != nil {
//				return nil, err
//			}
//			if s1WorkShift == nil {
//				return nil, grmi.NewBusinessError("绑定的日模型错误, 人员班组不存在")
//			}
//			yesterdayLength += s1WorkShift.SLen
//			yesterdayStart = s1WorkShift.SBeg
//			yesterdayEnd = s1WorkShift.SEnd
//			if yesterdayModel.S2WorkShiftNr != 0 {
//				s2WorkShift, err := workShiftDao.SelectOne(yesterdayModel.S2WorkShiftNr)
//				if err != nil {
//					return nil, err
//				}
//				if s2WorkShift == nil {
//					return nil, grmi.NewBusinessError("绑定的日模型错误, 人员班组不存在")
//				}
//				yesterdayEnd = s2WorkShift.SEnd
//				yesterdayLength += s2WorkShift.SLen
//			}
//			if yesterdayModel.S3WorkShiftNr != 0 {
//				s3WorkShift, err := workShiftDao.SelectOne(yesterdayModel.S3WorkShiftNr)
//				if err != nil {
//					return nil, err
//				}
//				if s3WorkShift == nil {
//					return nil, grmi.NewBusinessError("绑定的日模型错误, 人员班组不存在")
//				}
//				yesterdayEnd = s3WorkShift.SEnd
//				yesterdayLength += s3WorkShift.SLen
//			}
//		} else {
//			yesterdayStart = yesterdayModel.S1Beg
//			yesterdayEnd = yesterdayModel.S1End
//			yesterdayLength += yesterdayModel.S1Len
//			if yesterdayModel.S2End != 0 {
//				yesterdayEnd = yesterdayModel.S2End
//				yesterdayLength += yesterdayModel.S2Len
//			}
//			if yesterdayModel.S3End != 0 {
//				yesterdayEnd = yesterdayModel.S3End
//				yesterdayLength += yesterdayModel.S3Len
//			}
//		}
//		yesterdayStartStr := yesterdayBegin.Add(time.Duration(yesterdayStart) * time.Second).Format(grmi.DateTimeOutFormat)
//		yesterdayEndStr := yesterdayBegin.Add(time.Duration(yesterdayEnd) * time.Second).Format(grmi.DateTimeOutFormat)
//
//		// 求取今天的开始 结束时间
//		var startInt int
//		if dayModel.WorkShiftToggle {
//			if dayModel.S1WorkShiftNr == 0 {
//				return nil, grmi.NewBusinessError("绑定的日模型错误")
//			}
//
//			s1WorkShift, err := workShiftDao.SelectOne(dayModel.S1WorkShiftNr)
//			if err != nil {
//				return nil, err
//			}
//			if s1WorkShift == nil {
//				return nil, grmi.NewBusinessError("绑定的日模型错误, 人员班组不存在")
//			}
//			dayModel.S1WorkShift = *s1WorkShift
//			startInt = s1WorkShift.SBeg
//			if dayModel.S2WorkShiftNr != 0 {
//				s2WorkShift, err := workShiftDao.SelectOne(dayModel.S2WorkShiftNr)
//				if err != nil {
//					return nil, err
//				}
//				if s2WorkShift == nil {
//					return nil, grmi.NewBusinessError("绑定的日模型错误, 人员班组不存在")
//				}
//				dayModel.S2WorkShift = *s2WorkShift
//			}
//			if dayModel.S3WorkShiftNr != 0 {
//				s3WorkShift, err := workShiftDao.SelectOne(dayModel.S3WorkShiftNr)
//				if err != nil {
//					return nil, err
//				}
//				if s3WorkShift == nil {
//					return nil, grmi.NewBusinessError("绑定的日模型错误, 人员班组不存在")
//				}
//				dayModel.S3WorkShift = *s3WorkShift
//			}
//		} else {
//			startInt = dayModel.S1Beg
//		}
//		startDayModel := todayBegin.Add(time.Duration(startInt) * time.Second)
//
//		if endTime == "" {
//			endTime = time.Now().Format(grmi.DateTimeOutFormat)
//			endTime = endTime[11:len(endTime)]
//		}
//		if startTime != "" {
//			startTimeStr := todayString + " " + startTime
//			start, err = time.ParseInLocation(grmi.DateTimeOutFormat, startTimeStr, utils.TimezoneLocation)
//			if err != nil {
//				return nil, err
//			}
//		} else {
//			start = startDayModel
//		}
//		endTimeStr := todayString + " " + endTime
//		end, err = time.ParseInLocation(grmi.DateTimeOutFormat, endTimeStr, utils.TimezoneLocation)
//		if err != nil {
//			return nil, err
//		}
//		//dayModelList := impl.InitDayModelList(dayModel, today)
//		//duration := impl.GetValidDuration(dayModelList, start, end)
//		startStr := start.Format(grmi.DateTimeOutFormat)
//		endStr := end.Format(grmi.DateTimeOutFormat)
//		var displayData model.AWMDisplay
//		arcLi, err := dao.SelectDisplayData(descCN, startStr, endStr)
//		if err != nil {
//			return result, err
//		}
//
//		yesterdayArcLi, err := dao.SelectDisplayData(descCN, yesterdayStartStr, yesterdayEndStr)
//		if err != nil {
//			return result, err
//		}
//		// 计算昨天的mu
//		var mu float64
//		var yesterdaySum float64
//		yesterdayCapacity := 0
//		for _, dataInfo := range yesterdayArcLi {
//			targetValue := reflect.ValueOf(dataInfo).FieldByName(statisticField).String()
//			target, err := strconv.ParseFloat(targetValue, 64)
//			if err != nil {
//				return result, err
//			}
//			if max != 0 && target > max {
//				continue
//			}
//			if min != 0 && target < min {
//				continue
//			}
//			yesterdayCapacity++
//			yesterdaySum += target
//		}
//		yesterdayAvg := yesterdaySum / float64(yesterdayCapacity)
//		if yesterdayCapacity == 0 {
//			mu = 0
//		} else {
//			mu = float64(yesterdayCapacity) * yesterdayAvg / float64(yesterdayLength)
//		}
//		//mu, err = strconv.ParseFloat(fmt.Sprintf("%.4f", mu), 64)
//		//if err != nil {
//		//	return result, err
//		//}
//		// 处理今天的数据
//		displayArcLi := make([]model.FilterDataInfo, 0)
//		var sum float64
//		for _, dataInfo := range arcLi {
//			targetValue := reflect.ValueOf(dataInfo).FieldByName(statisticField).String()
//			target, err := strconv.ParseFloat(targetValue, 64)
//			if err != nil {
//				return result, err
//			}
//			if max != 0 && target > max {
//				continue
//			}
//			if min != 0 && target < min {
//				continue
//			}
//			displayArcLi = append(displayArcLi, dataInfo)
//			sum += target
//		}
//		var avg float64
//
//		capacity := len(displayArcLi)
//		if capacity == 0 {
//			avg = 0
//		} else {
//			avg = sum / float64(capacity)
//
//		}
//		avg, err = strconv.ParseFloat(fmt.Sprintf("%.2f", avg), 64)
//		if err != nil {
//			return result, err
//		}
//
//		displayData.MuTitle = muTitle
//		displayData.MuUom = muUom
//		displayData.Capacity = capacity
//		displayData.MU = fmt.Sprintf("%.2f", mu*100) + "%"
//		displayData.Data = displayArcLi
//		displayData.Start = startStr
//		displayData.End = endStr
//		displayData.Title = display.Title
//		displayData.Standard = standardValue
//		if inputAvg != 0 {
//			displayData.Avg = inputAvg
//		} else {
//			displayData.Avg = avg
//		}
//		displayData.Uom = uom
//		displayData.UseField = meta.FilterDataInfo.TableName + "-" + statisticField
//		displayData.Cate = cate
//		result = append(result, displayData)
//	}
//
//	return result, nil
//}

func (impl *FilterDataInfoServiceImplement) InitDayModelList(dayModel *baseModel.DayModel, date time.Time) *list.List {
	dayModelList := list.New()
	dayModelList = dayModelList.Init()
	var element model.ListElement
	if dayModel.WorkShiftToggle {
		s1WorkShift := dayModel.S1WorkShift
		s2WorkShift := dayModel.S2WorkShift
		s3WorkShift := dayModel.S3WorkShift
		element.Start = date.Add(time.Duration(s1WorkShift.SBeg) * time.Second)
		if s1WorkShift.SB1Beg != 0 {
			var end time.Time
			element.End = date.Add(time.Duration(s1WorkShift.SB1Beg) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
			end = date.Add(time.Duration(s1WorkShift.SB1End) * time.Second)
			if s1WorkShift.SB2Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s1WorkShift.SB2Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s1WorkShift.SB2End) * time.Second)
			}
			if s1WorkShift.SB3Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s1WorkShift.SB3Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s1WorkShift.SB3End) * time.Second)
			}
			if s1WorkShift.SB4Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s1WorkShift.SB4Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s1WorkShift.SB4End) * time.Second)
			}
			if s1WorkShift.SB5Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s1WorkShift.SB5Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s1WorkShift.SB5End) * time.Second)
			}
			element.Start = end
			element.End = date.Add(time.Duration(s1WorkShift.SEnd) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
		} else {
			element.End = date.Add(time.Duration(s1WorkShift.SEnd) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
		}
		if s2WorkShift.SB1Beg != 0 {
			var end time.Time
			element.End = date.Add(time.Duration(s2WorkShift.SB1Beg) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
			end = date.Add(time.Duration(s2WorkShift.SB1End) * time.Second)
			if s2WorkShift.SB2Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s2WorkShift.SB2Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s2WorkShift.SB2End) * time.Second)
			}
			if s2WorkShift.SB3Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s2WorkShift.SB3Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s2WorkShift.SB3End) * time.Second)
			}
			if s2WorkShift.SB4Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s2WorkShift.SB4Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s2WorkShift.SB4End) * time.Second)
			}
			if s2WorkShift.SB5Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s2WorkShift.SB5Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s2WorkShift.SB5End) * time.Second)
			}
			element.Start = end
			element.End = date.Add(time.Duration(s2WorkShift.SEnd) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
		} else {
			element.End = date.Add(time.Duration(s2WorkShift.SEnd) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
		}
		if s3WorkShift.SB1Beg != 0 {
			var end time.Time
			element.End = date.Add(time.Duration(s3WorkShift.SB1Beg) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
			end = date.Add(time.Duration(s3WorkShift.SB1End) * time.Second)
			if s3WorkShift.SB2Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s3WorkShift.SB2Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s3WorkShift.SB2End) * time.Second)
			}
			if s3WorkShift.SB3Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s3WorkShift.SB3Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s3WorkShift.SB3End) * time.Second)
			}
			if s3WorkShift.SB4Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s3WorkShift.SB4Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s3WorkShift.SB4End) * time.Second)
			}
			if s3WorkShift.SB5Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s3WorkShift.SB5Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s3WorkShift.SB5End) * time.Second)
			}
			element.Start = end
			element.End = date.Add(time.Duration(s3WorkShift.SEnd) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
		} else {
			element.End = date.Add(time.Duration(s3WorkShift.SEnd) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
		}
	} else {

		element.Start = date.Add(time.Duration(dayModel.S1Beg) * time.Second)
		if dayModel.S1B1Beg != 0 {
			var end time.Time
			element.End = date.Add(time.Duration(dayModel.S1B1Beg) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
			end = date.Add(time.Duration(dayModel.S1B1End) * time.Second)
			if dayModel.S1B2Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S1B2Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S1B2End) * time.Second)
			}
			if dayModel.S1B3Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S1B3Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S1B3End) * time.Second)
			}
			if dayModel.S1B4Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S1B4Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S1B4End) * time.Second)
			}
			if dayModel.S1B5Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S1B5Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S1B5End) * time.Second)
			}
			element.Start = end
			element.End = date.Add(time.Duration(dayModel.S1End) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
		} else {
			element.End = date.Add(time.Duration(dayModel.S1End) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
		}

		element.Start = date.Add(time.Duration(dayModel.S2Beg) * time.Second)
		if dayModel.S2B1Beg != 0 {
			var end time.Time
			element.End = date.Add(time.Duration(dayModel.S2B1Beg) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
			end = date.Add(time.Duration(dayModel.S2B1End) * time.Second)
			if dayModel.S2B2Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S2B2Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S2B2End) * time.Second)
			}
			if dayModel.S2B3Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S2B3Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S2B3End) * time.Second)
			}
			if dayModel.S2B4Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S2B4Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S2B4End) * time.Second)
			}
			if dayModel.S2B5Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S2B5Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S2B5End) * time.Second)
			}
			element.Start = end
			element.End = date.Add(time.Duration(dayModel.S2End) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
		} else {
			element.End = date.Add(time.Duration(dayModel.S2End) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
		}
		element.Start = date.Add(time.Duration(dayModel.S3Beg) * time.Second)
		if dayModel.S3B1Beg != 0 {
			var end time.Time
			element.End = date.Add(time.Duration(dayModel.S3B1Beg) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
			end = date.Add(time.Duration(dayModel.S3B1End) * time.Second)
			if dayModel.S3B2Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S3B2Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S3B2End) * time.Second)
			}
			if dayModel.S3B3Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S3B3Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S3B3End) * time.Second)
			}
			if dayModel.S3B4Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S3B4Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S3B4End) * time.Second)
			}
			if dayModel.S3B5Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S3B5Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S3B5End) * time.Second)
			}
			element.Start = end
			element.End = date.Add(time.Duration(dayModel.S3End) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
		} else {
			element.End = date.Add(time.Duration(dayModel.S3End) * time.Second)
			element.Duration = element.End.Sub(element.Start)
			dayModelList.PushBack(element)
		}
	}
	return dayModelList
}

func (impl FilterDataInfoServiceImplement) GetValidDuration(dayModelList *list.List, start time.Time, end time.Time) time.Duration {
	var duration time.Duration
	nums := dayModelList.Len()
	var element *list.Element
	var elementValue model.ListElement
	for i := 0; i < nums; i++ {
		if element == nil {
			element = dayModelList.Front()
		} else {
			element = element.Next()
		}
		elementValue = element.Value.(model.ListElement)
		if end.Unix() <= elementValue.Start.Unix() {
			return duration
		} else if end.Unix() <= elementValue.End.Unix() {
			if start.Unix() <= elementValue.Start.Unix() {
				return duration + end.Sub(elementValue.Start)
			} else {
				return duration + end.Sub(start)
			}
		} else {
			if start.Unix() <= elementValue.Start.Unix() {
				duration += elementValue.End.Sub(elementValue.Start)
			} else {
				duration += elementValue.End.Sub(start)
			}
		}
	}
	return duration
}
