package implments

import (
	baseDal "LAPP_ACURA_MOM_BACKEND/dao/base"
	jitDal "LAPP_ACURA_MOM_BACKEND/dao/jit"
	meDal "LAPP_ACURA_MOM_BACKEND/dao/me"
	omDal "LAPP_ACURA_MOM_BACKEND/dao/om"
	dal "LAPP_ACURA_MOM_BACKEND/dao/pln"
	qmDal "LAPP_ACURA_MOM_BACKEND/dao/qm"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	baseMeta "LAPP_ACURA_MOM_BACKEND/meta/base"
	meta "LAPP_ACURA_MOM_BACKEND/meta/pln"
	qmMeta "LAPP_ACURA_MOM_BACKEND/meta/qm"
	baseModel "LAPP_ACURA_MOM_BACKEND/models/base"
	jitModel "LAPP_ACURA_MOM_BACKEND/models/jit"
	meModel "LAPP_ACURA_MOM_BACKEND/models/me"
	model "LAPP_ACURA_MOM_BACKEND/models/pln"
	"LAPP_ACURA_MOM_BACKEND/utils"
	"LAPP_ACURA_MOM_BACKEND/web/middleware/glog"
	"container/list"
	"errors"
	"strconv"
	"sync"
	"time"
)

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderService.Select
 *
 ******************************************************************************/
func (impl *CustOrderServiceImplement) Dashboard(user *global.User, workLineId string) (interface{}, error) {
	grmi.Log(user, "/services/pln/implments/CustOrder.service.impl.go", "SelectCustOrder", "查询CustOrder")

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

	engineSlaves := db.Eloquent.Slaves()
	sessionSlave := engineSlaves[1].NewSession()
	//sessionSlave := engine.NewSession()
	//defer sessionSlave.Close()

	daoWorkLine := baseDal.NewWorkLineDAO(session, user.PlantNr, user.UserId)
	daoDayModel := baseDal.NewDayModelDAO(session, user.PlantNr, user.UserId)
	//daoWeekModel := baseDal.NewWeekModelLstDAO(session, user.PlantNr, user.UserId)
	daoWeekModelLst := baseDal.NewWeekModelLstDAO(session, user.PlantNr, user.UserId)
	//daoWorkCalendar := baseDal.NewWorkCalendarDAO(session, user.PlantNr, user.UserId)
	daoWorkCalendarLst := baseDal.NewWorkCalendarLstDAO(session, user.PlantNr, user.UserId)
	daoWorkShift := baseDal.NewWorkShiftDAO(session, user.PlantNr, user.UserId)
	dashlisDao := baseDal.NewRPDashBoardLstDAO(sessionSlave, user.PlantNr, user.UserId)
	dashlist, err := dashlisDao.Select([]grmi.Predicate{baseMeta.RPDashBoardLst_DashboardId.NewPredicate(grmi.Equal, workLineId)}, nil)
	if err != nil {
		return nil, err
	}
	var personNum int      //当班人数
	var timeBeat float64   //节拍
	var days int           //展示天数
	var ippmTarge int      //IPPM
	var jPHPercent float64 //IPPM
	for _, v := range dashlist {
		if v.ParamName == "TimeBeat" {
			timeBeat = utils.ValueToFloat(v.CharParam1, 0.0)
		}
		if v.ParamName == "PersonNum" {
			personNum = utils.ValueToInt(v.CharParam1, 0)
		}
		if v.ParamName == "Days" {
			days = utils.ValueToInt(v.CharParam1, 0)
		}
		if v.ParamName == "IppmTarge" {
			ippmTarge = utils.ValueToInt(v.CharParam1, 0)
		}
		if v.ParamName == "JPHPercent" {
			jPHPercent = utils.ValueToFloat(v.CharParam1, 0.0)
		}

	}
	if personNum <= 0 {
		return nil, errors.New("当班人数不能为空!")
	}
	if timeBeat <= 0 {
		return nil, errors.New("节拍不能为空!")
	}
	if days <= 0 {
		return nil, errors.New("日期长度不能为空!")
	}
	// 获取产线今天的时间模型
	workLine, err := daoWorkLine.SelectOne(workLineId)
	if err != nil {
		return nil, grmi.NewBusinessError("查询产线失败, error:" + err.Error())
	}
	if workLine == nil {
		return nil, grmi.NewBusinessError("产线不存在")
	}
	var dayModelNr int
	// 优先使用产线工作日历
	if workLine.WorkCalendarNr != 0 {
		workCalendarLstLi, err := daoWorkCalendarLst.Select([]grmi.Predicate{baseMeta.WorkCalendarLst_WorkCalendarNr.NewPredicate(grmi.Equal, workLine.WorkCalendarNr)}, []grmi.Field{baseMeta.WorkCalendarLst_WorkDate})
		if err != nil {
			return nil, grmi.NewBusinessError("查询产线工作日历子项失败, error:" + err.Error())
		}
		for _, workCalendarLst := range workCalendarLstLi {
			if workCalendarLst.WorkDate == grmi.Date(time.Now()) {
				dayModelNr = workCalendarLst.DayModelNr
				break
			}
		}
	}
	// 如果产线工作日不存在当天 则使用周模型
	if dayModelNr == 0 {
		weekModelLstLi, err := daoWeekModelLst.Select([]grmi.Predicate{baseMeta.WeekModelLst_WeekModelNr.NewPredicate(grmi.Equal, workLine.WeekModelNr)}, []grmi.Field{baseMeta.WeekModelLst_WorkDate})
		if err != nil {
			return nil, grmi.NewBusinessError("查询周模型子项失败, error:" + err.Error())
		}
		weeKDay := time.Now().Weekday()
		for _, weekModelLst := range weekModelLstLi {
			if weekModelLst.WorkDate.Restore().Weekday() == weeKDay {
				dayModelNr = weekModelLst.DayModelNr
				break
			}
		}
	}
	if dayModelNr == 0 {
		return nil, grmi.NewBusinessError("未获取到匹配今天的时间模型")
	}
	dayModel, err := daoDayModel.SelectOne(dayModelNr)
	if err != nil {
		return nil, grmi.NewBusinessError("查询日模型失败, error:" + err.Error())
	}
	if dayModel == nil {
		return nil, grmi.NewBusinessError("关联的日模型不存在")
	}
	if dayModel.WorkShiftToggle {
		if dayModel.S1WorkShiftNr == 0 {
			return nil, grmi.NewBusinessError("绑定的日模型错误")
		}

		s1WorkShift, err := daoWorkShift.SelectOne(dayModel.S1WorkShiftNr)
		if err != nil {
			return nil, err
		}
		if s1WorkShift != nil {
			dayModel.S1WorkShift = *s1WorkShift

		}

		if dayModel.S2WorkShiftNr != 0 {
			s2WorkShift, err := daoWorkShift.SelectOne(dayModel.S2WorkShiftNr)
			if err != nil {
				return nil, err
			}
			if s2WorkShift != nil {
				dayModel.S2WorkShift = *s2WorkShift

			}

		}
		if dayModel.S3WorkShiftNr != 0 {
			s3WorkShift, err := daoWorkShift.SelectOne(dayModel.S3WorkShiftNr)
			if err != nil {
				return nil, err
			}
			if s3WorkShift != nil {
				dayModel.S3WorkShift = *s3WorkShift

			}

		}
	}

	DashboardData := model.DashboardPic{}
	//当班人数
	DashboardData.TemPoPerson = personNum
	DashboardData.TeamTemPo = utils.ValueToFloat(personNum, 0.0)
	//节拍
	DashboardData.CurrentTempo = timeBeat
	DashboardData.IppmTarge = ippmTarge
	DashboardData.DateTimeNow = utils.TimeFormat(time.Now(), "yyyy-MM-dd HH:00")

	var WorkShift int
	_ = WorkShift
	//构建开始时间和结束时间
	startDayTime := utils.GetZeroTime(time.Now())
	//判断当前时间，决定取时间模型
	//nowUnix := int(time.Now().Unix() - startDayTime.Unix())
	//if nowUnix < dayModel.S1Beg {
	//	startDayTime = startDayTime.AddDate(0, 0, -1)
	//	nowUnix += 86400
	//}
	now := time.Now()
	var startTime, endTime time.Time
	var usePreDay, notUseToday bool
	if dayModel.WorkShiftToggle {

		startTime = startDayTime.Add(time.Duration(dayModel.S1WorkShift.SBeg) * time.Second)
		//endTime = startDayTime.Add(time.Duration(dayModel.S1WorkShift.SEnd) * time.Second)
		//if dayModel.S2WorkShiftNr != 0 {
		//	endTime = startDayTime.Add(time.Duration(dayModel.S2WorkShift.SEnd) * time.Second)
		//}
		//if dayModel.S3WorkShiftNr != 0 {
		//	endTime = startDayTime.Add(time.Duration(dayModel.S3WorkShift.SEnd) * time.Second)
		//}
	} else {
		startTime = startDayTime.Add(time.Duration(dayModel.S1Beg) * time.Second)
		endTime = startDayTime.Add(time.Duration(dayModel.S1End) * time.Second)
	}
	if now.Unix() < startTime.Unix() {
		notUseToday = true
	}
	if notUseToday {
		preDayStartTime := startDayTime.AddDate(0, 0, -1)
		if dayModel.WorkShiftToggle {
			startTime = preDayStartTime.Add(time.Duration(dayModel.S1WorkShift.SBeg) * time.Second)
			endTime = preDayStartTime.Add(time.Duration(dayModel.S1WorkShift.SEnd) * time.Second)
			if dayModel.S2WorkShiftNr != 0 {
				endTime = preDayStartTime.Add(time.Duration(dayModel.S2WorkShift.SEnd) * time.Second)
			}
			if dayModel.S3WorkShiftNr != 0 {
				endTime = preDayStartTime.Add(time.Duration(dayModel.S3WorkShift.SEnd) * time.Second)
			}
		} else {
			startTime = preDayStartTime.Add(time.Duration(dayModel.S1Beg) * time.Second)
			endTime = preDayStartTime.Add(time.Duration(dayModel.S1End) * time.Second)
			if dayModel.S2Toggle {
				endTime = preDayStartTime.Add(time.Duration(dayModel.S2End) * time.Second)

			}
			if dayModel.S3Toggle {
				endTime = preDayStartTime.Add(time.Duration(dayModel.S3End) * time.Second)
			}
		}
		if now.Unix() >= startTime.Unix() && now.Unix() <= endTime.Unix() {
			usePreDay = true
		}
	}
	if now.Unix() > endTime.Unix() {
		notUseToday = true
	}
	glog.InfoExtln("DashboardData", "notUseToday", notUseToday)
	glog.InfoExtln("DashboardData", "usePreDay", usePreDay)
	if notUseToday && !usePreDay {
		return DashboardData, nil
	}
	var useDayStartTime time.Time
	if usePreDay {
		useDayStartTime = startDayTime.AddDate(0, 0, -1)
	} else {
		useDayStartTime = startDayTime
	}

	glog.InfoExtln("DashboardData", "WorkShift", WorkShift)
	glog.InfoExtln("DashboardData", "startTime", startTime)
	glog.InfoExtln("DashboardData", "endTime", endTime)
	if startTime.Unix() > endTime.Unix() {
		return nil, errors.New("日模型开始时间大于结束时间")
	}

	//dayModelStartHour := startDayTime.Add(time.Duration(dayModelStartTime) * time.Second)
	//dayModelEndHour := startDayTime.Add(time.Duration(dayModelEndTime) * time.Second)

	//glog.InfoExtln("DashboardData","dayModelStartHour",dayModelStartHour)
	//glog.InfoExtln("DashboardData","dayModelEndHour",dayModelEndHour)
	//
	//nowEndHour := time.Now()
	glog.InfoExtln("DashboardData", "startTime", startTime)
	glog.InfoExtln("DashboardData", "now", now)
	DayModelHours, err := utils.GetHoursData(startTime, now)

	//_ = DayModelHours
	if err != nil {
		return nil, err
	}

	var err1, err2, err3, err4, err5, err6 error
	var wg sync.WaitGroup
	wg.Add(7)
	//图例001
	go func(wg *sync.WaitGroup) {
		DashboardData.DashboardDataPic1, err1 = impl.SelectPic1(user, dayModel, workLineId, startTime, endTime, useDayStartTime)
		wg.Done() // 操作完成，减少一个计数
	}(&wg)
	//图例002
	go func(wg *sync.WaitGroup) {
		DashboardData.DashboardDataPic2, err2 = impl.SelectDefectNumber(user, days, workLineId)
		wg.Done() // 操作完成，减少一个计数
	}(&wg)
	//图例003
	go func(wg *sync.WaitGroup) {
		DashboardData.DashboardDataPic3, err3 = impl.SelectPic3(user, DayModelHours, dayModel, jPHPercent, startTime, now, timeBeat, personNum, workLineId, useDayStartTime)
		wg.Done() // 操作完成，减少一个计数
	}(&wg)
	//图例004
	go func(wg *sync.WaitGroup) {
		DashboardData.DashboardDataPic4, err4 = impl.CountTarget(user, dayModel, WorkShift, startTime, endTime, workLineId)
		wg.Done() // 操作完成，减少一个计数
	}(&wg)
	//计算当前节拍,及其平均
	go func(wg *sync.WaitGroup) {
		DashboardData.TeamTemPo, DashboardData.PerHourTemPo, err5 = impl.SelectTeamTemPo(user, dayModel, jPHPercent, personNum, startTime, endTime, WorkShift, workLineId, useDayStartTime)
		wg.Done() // 操作完成，减少一个计数
	}(&wg)
	go func(wg *sync.WaitGroup) {
		DashboardData.DashboardDataStatus, err6 = impl.SelectAndonInfo(user)
		wg.Done() // 操作完成，减少一个计数
	}(&wg)
	go func(wg *sync.WaitGroup) {
		DashboardData.DefectPackOrder, err6 = impl.SelectDefectPackOrder(user, workLineId)
		wg.Done() // 操作完成，减少一个计数
	}(&wg)

	wg.Wait()
	if err1 != nil {
		return nil, err1
	}
	if err2 != nil {
		return nil, err2
	}
	if err3 != nil {
		return nil, err3
	}

	if err4 != nil {
		return nil, err4
	}

	if err5 != nil {
		return nil, err5
	}

	if err6 != nil {
		return nil, err6
	}

	return DashboardData, err

}

// InitDayModelList 生成日模型时间双向连表
func (impl *CustOrderServiceImplement) 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)
			}
			if s1WorkShift.SB6Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s1WorkShift.SB6Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s1WorkShift.SB6End) * time.Second)
			}
			if s1WorkShift.SB7Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s1WorkShift.SB7Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s1WorkShift.SB7End) * time.Second)
			}
			if s1WorkShift.SB8Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s1WorkShift.SB8Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s1WorkShift.SB8End) * time.Second)
			}
			if s1WorkShift.SB9Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s1WorkShift.SB9Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s1WorkShift.SB9End) * time.Second)
			}
			if s1WorkShift.SB10Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s1WorkShift.SB10Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s1WorkShift.SB10End) * 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)
			}
			if s2WorkShift.SB6Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s2WorkShift.SB6Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s2WorkShift.SB6End) * time.Second)
			}
			if s2WorkShift.SB7Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s2WorkShift.SB7Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s2WorkShift.SB7End) * time.Second)
			}
			if s2WorkShift.SB8Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s2WorkShift.SB8Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s2WorkShift.SB8End) * time.Second)
			}
			if s2WorkShift.SB9Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s2WorkShift.SB9Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s2WorkShift.SB9End) * time.Second)
			}
			if s2WorkShift.SB10Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s2WorkShift.SB10Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s2WorkShift.SB10End) * 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)
			}
			if s3WorkShift.SB6Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s3WorkShift.SB6Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s3WorkShift.SB6End) * time.Second)
			}
			if s3WorkShift.SB7Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s3WorkShift.SB7Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s3WorkShift.SB7End) * time.Second)
			}
			if s3WorkShift.SB8Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s3WorkShift.SB8Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s3WorkShift.SB8End) * time.Second)
			}
			if s3WorkShift.SB9Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s3WorkShift.SB9Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s3WorkShift.SB9End) * time.Second)
			}
			if s3WorkShift.SB10Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(s3WorkShift.SB10Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(s3WorkShift.SB10End) * 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)
			}
			if dayModel.S1B6Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S1B6Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S1B6End) * time.Second)
			}
			if dayModel.S1B7Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S1B7Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S1B7End) * time.Second)
			}
			if dayModel.S1B8Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S1B8Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S1B8End) * time.Second)
			}
			if dayModel.S1B9Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S1B9Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S1B9End) * time.Second)
			}
			if dayModel.S1B10Beg != 0 {
				element.Start = end
				element.End = date.Add(time.Duration(dayModel.S1B10Beg) * time.Second)
				element.Duration = element.End.Sub(element.Start)
				dayModelList.PushBack(element)
				end = date.Add(time.Duration(dayModel.S1B10End) * 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)
		}
		if dayModel.S2Toggle {
			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)
				}
				if dayModel.S2B6Beg != 0 {
					element.Start = end
					element.End = date.Add(time.Duration(dayModel.S2B6Beg) * time.Second)
					element.Duration = element.End.Sub(element.Start)
					dayModelList.PushBack(element)
					end = date.Add(time.Duration(dayModel.S2B6End) * time.Second)
				}
				if dayModel.S2B7Beg != 0 {
					element.Start = end
					element.End = date.Add(time.Duration(dayModel.S2B7Beg) * time.Second)
					element.Duration = element.End.Sub(element.Start)
					dayModelList.PushBack(element)
					end = date.Add(time.Duration(dayModel.S2B7End) * time.Second)
				}
				if dayModel.S2B8Beg != 0 {
					element.Start = end
					element.End = date.Add(time.Duration(dayModel.S2B8Beg) * time.Second)
					element.Duration = element.End.Sub(element.Start)
					dayModelList.PushBack(element)
					end = date.Add(time.Duration(dayModel.S2B8End) * time.Second)
				}
				if dayModel.S2B9Beg != 0 {
					element.Start = end
					element.End = date.Add(time.Duration(dayModel.S2B9Beg) * time.Second)
					element.Duration = element.End.Sub(element.Start)
					dayModelList.PushBack(element)
					end = date.Add(time.Duration(dayModel.S2B9End) * time.Second)
				}
				if dayModel.S2B10Beg != 0 {
					element.Start = end
					element.End = date.Add(time.Duration(dayModel.S2B10Beg) * time.Second)
					element.Duration = element.End.Sub(element.Start)
					dayModelList.PushBack(element)
					end = date.Add(time.Duration(dayModel.S2B10End) * 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)
			}
		}
		if dayModel.S3Toggle {
			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)
				}
				if dayModel.S3B6Beg != 0 {
					element.Start = end
					element.End = date.Add(time.Duration(dayModel.S3B6Beg) * time.Second)
					element.Duration = element.End.Sub(element.Start)
					dayModelList.PushBack(element)
					end = date.Add(time.Duration(dayModel.S3B6End) * time.Second)
				}
				if dayModel.S3B7Beg != 0 {
					element.Start = end
					element.End = date.Add(time.Duration(dayModel.S3B7Beg) * time.Second)
					element.Duration = element.End.Sub(element.Start)
					dayModelList.PushBack(element)
					end = date.Add(time.Duration(dayModel.S3B7End) * time.Second)
				}
				if dayModel.S3B8Beg != 0 {
					element.Start = end
					element.End = date.Add(time.Duration(dayModel.S3B8Beg) * time.Second)
					element.Duration = element.End.Sub(element.Start)
					dayModelList.PushBack(element)
					end = date.Add(time.Duration(dayModel.S3B8End) * time.Second)
				}
				if dayModel.S3B9Beg != 0 {
					element.Start = end
					element.End = date.Add(time.Duration(dayModel.S3B9Beg) * time.Second)
					element.Duration = element.End.Sub(element.Start)
					dayModelList.PushBack(element)
					end = date.Add(time.Duration(dayModel.S3B9End) * time.Second)
				}
				if dayModel.S3B10Beg != 0 {
					element.Start = end
					element.End = date.Add(time.Duration(dayModel.S3B10Beg) * time.Second)
					element.Duration = element.End.Sub(element.Start)
					dayModelList.PushBack(element)
					end = date.Add(time.Duration(dayModel.S3B10End) * 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
}

// SelectDefectNumber dashboard右上角 查询缺陷数据
func (impl *CustOrderServiceImplement) SelectDefectNumber(user *global.User, days int, workLineId string) ([]model.DefectItem, error) {
	result := make([]model.DefectItem, 0, days)
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	defectRecordDao := qmDal.NewDefectRecordDAO(session, user.PlantNr, user.UserId)
	repairInfoDao := baseDal.NewRepairInfoDAO(session, user.UserId)
	custOrderDao := dal.NewCustOrderDAO(session, user.PlantNr, user.UserId)
	serialOrderDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	productFamilyRelateDao := meDal.NewProductFamilyRelateDAO(session, user.PlantNr, user.UserId)
	productFamilyMap := make(map[string]int)
	now := time.Now()
	custOrderMap := make(map[string]*model.CustOrder)
	for i := days - 1; i >= 0; i-- {
		var defectItem model.DefectItem
		day := now.AddDate(0, 0, -1*i).Format(grmi.DateOutFormat)
		start := day + " " + "00:00:00"
		end := day + " " + "23:59:59"
		defectRecordLi, err := defectRecordDao.Select([]grmi.Predicate{
			qmMeta.DefectRecord_CreateTime.NewPredicate(grmi.GreaterOrEqual, start),
			qmMeta.DefectRecord_CreateTime.NewPredicate(grmi.LessOrEqual, end),
		}, []grmi.Field{qmMeta.DefectRecord_SerialOrderId})
		if err != nil {
			return nil, grmi.NewBusinessError("查询缺陷记录失败，错误:" + err.Error())
		}
		serialOrderMap := make(map[string]interface{})
		for _, defectRecord := range defectRecordLi {
			_, exist := serialOrderMap[defectRecord.SerialOrderId]
			if !exist {
				serialOrderMap[defectRecord.SerialOrderId] = nil
			}
		}
		repairInfoLi, err := repairInfoDao.Select([]grmi.Predicate{
			baseMeta.RepairInfo_CreateTime.NewPredicate(grmi.GreaterOrEqual, start),
			baseMeta.RepairInfo_CreateTime.NewPredicate(grmi.LessOrEqual, end),
		}, []grmi.Field{baseMeta.RepairInfo_SerialOrderId})
		if err != nil {
			return nil, grmi.NewBusinessError("查询返修记录失败, 错误:" + err.Error())
		}
		for _, repairInfo := range repairInfoLi {
			_, exist := serialOrderMap[repairInfo.SerialOrderId]
			if !exist {
				serialOrderMap[repairInfo.SerialOrderId] = nil
			}
		}
		// 判断缺陷的这些工单归属的
		for serialOrderId := range serialOrderMap {
			serialOrder, err := serialOrderDao.SelectOne(serialOrderId)
			if err != nil {
				return nil, grmi.NewBusinessError("查询工单数据失败, 错误:" + err.Error())
			}
			if serialOrder == nil {
				delete(serialOrderMap, serialOrderId)
				continue
			}
			if serialOrder.CustOrderId == "" {
				delete(serialOrderMap, serialOrderId)
				continue
			}
			if serialOrder.OrderType == baseModel.ORDER_TYPE_PRE {
				delete(serialOrderMap, serialOrderId)
				continue
			}
			custOrder, exist := custOrderMap[serialOrder.CustOrderId]
			if !exist {
				custOrder, err = custOrderDao.SelectOne(serialOrder.CustOrderId)
				if err != nil {
					return nil, grmi.NewBusinessError("查询客户订单失败, 错误:" + err.Error())
				}
				if custOrder == nil {
					delete(serialOrderMap, serialOrderId)
					continue
				}
				custOrderMap[serialOrder.CustOrderId] = custOrder
			}
			if custOrder.WorkLineId != workLineId {
				delete(serialOrderMap, serialOrderId)
			}
		}
		defectNumber := len(serialOrderMap)
		custOrderLi, err := custOrderDao.JoinSelect([]grmi.Predicate{
			meta.CustOrder_PlanStartTime.NewPredicate(grmi.GreaterOrEqual, start),
			meta.CustOrder_PlanStartTime.NewPredicate(grmi.LessThen, end),
		}, []grmi.Field{meta.CustOrder_PlanStartTime})
		if err != nil {
			return nil, grmi.NewBusinessError("查询客户订单失败, 错误:" + err.Error())
		}
		var planQty int
		for _, custOrder := range custOrderLi {
			packNum, exist := productFamilyMap[custOrder.ProductFamilyId]
			if !exist {
				productLi, err := productFamilyRelateDao.SelectProductByFamilyId(custOrder.ProjectId, custOrder.ProductFamilyId)
				if err != nil {
					return nil, grmi.NewBusinessError("查询派生数据失败, 错误:" + err.Error())
				}
				packNum = len(productLi)
				productFamilyMap[custOrder.ProductFamilyId] = packNum
			}
			planQty += packNum * (custOrder.OrderQty.PlanQty - custOrder.OrderQty.CancelQty)
		}
		var percent int64
		if planQty == 0 {
			percent = 0
		} else {
			percent = int64(float64(defectNumber) / float64(planQty) * 1000000)
		}
		defectItem.Number = percent
		defectItem.Date = day
		result = append(result, defectItem)
	}
	return result, nil
}

func (impl *CustOrderServiceImplement) CountTarget(user *global.User, dayModel *baseModel.DayModel, WorkShift int, startDayTime time.Time, endDayTime time.Time, workLineId string) (model.TargetItem, error) {
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	result := model.TargetItem{}
	custOrderDao := dal.NewCustOrderDAO(session, user.PlantNr, user.UserId)
	packDao := jitDal.NewPackOrderDAO(session, user.PlantNr, user.UserId)
	workLineDao := baseDal.NewWorkLineDAO(session, user.PlantNr, user.UserId)
	workLineLi, err := workLineDao.Select([]grmi.Predicate{baseMeta.WorkLine_GroupLineId.NewPredicate(grmi.Equal, workLineId)}, nil)
	if err != nil {
		return result, grmi.NewBusinessError("查询工厂产线数据失败, error:" + err.Error())
	}
	if len(workLineLi) == 0 {
		return result, grmi.NewBusinessError("未查询到实体产线")
	}
	var workLineIds []string
	for _, v := range workLineLi {
		workLineIds = append(workLineIds, v.WorkLineid)
	}
	custOrderLi, err := custOrderDao.JoinSelect([]grmi.Predicate{
		meta.CustOrder_WorkLineId.NewPredicate(grmi.Equal, workLineId),
		meta.CustOrder_PlanStartTime.NewPredicate(grmi.GreaterOrEqual, utils.TimeFormat(startDayTime, "yyyy-MM-dd HH:mm:ss")),
		meta.CustOrder_PlanStartTime.NewPredicate(grmi.LessThen, utils.TimeFormat(endDayTime, "yyyy-MM-dd HH:mm:ss")),
	}, []grmi.Field{meta.CustOrder_PlanStartTime})
	if err != nil {
		return result, err
	}
	//计算计划数量
	for _, custOrderData := range custOrderLi {
		result.Target += custOrderData.OrderQty.PlanQty - custOrderData.OrderQty.CancelQty

	}
	packList, err := packDao.SelectUserClosePackOrder(utils.TimeFormat(startDayTime, "yyyy-MM-dd HH:mm:ss"), utils.TimeFormat(endDayTime, "yyyy-MM-dd HH:mm:ss"), workLineIds)
	if err != nil {
		return result, err
	}
	result.Current = len(packList)
	return result, nil
}

//计算当班节拍数量
func (impl *CustOrderServiceImplement) SelectTeamTemPo(user *global.User, dayModel *baseModel.DayModel, jPHPercent float64, num int, startDayTime time.Time, endDayTime time.Time, WorkShift int, workLineId string, dayStartTime time.Time) (TeamTemPoNumber float64, PerTeamTemPoNumber float64, err error) {
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	packOrderDao := jitDal.NewPackOrderDAO(session, user.PlantNr, user.UserId)
	workLineDao := baseDal.NewWorkLineDAO(session, user.PlantNr, user.UserId)
	workLineLi, err := workLineDao.Select([]grmi.Predicate{baseMeta.WorkLine_GroupLineId.NewPredicate(grmi.Equal, workLineId)}, nil)
	if err != nil {
		return 0, 0, grmi.NewBusinessError("查询工厂产线数据失败, error:" + err.Error())
	}
	if len(workLineLi) == 0 {
		return 0, 0, grmi.NewBusinessError("未查询到实体产线")
	}
	var workLineIds []string
	for _, v := range workLineLi {
		workLineIds = append(workLineIds, v.WorkLineid)
	}
	//now := time.Now()
	//todayStart := now.Format(grmi.DateOutFormat) + " 00:00:00"
	//todayStartTime, _ := time.ParseInLocation(grmi.DateTimeOutFormat, todayStart, utils.TimezoneLocation)

	timeModelList := impl.InitDayModelList(dayModel, dayStartTime)
	packData, err := packOrderDao.SelectUserClosePackOrder(utils.TimeFormat(startDayTime, "yyyy-MM-dd HH:mm:ss"), utils.TimeFormat(endDayTime, "yyyy-MM-dd HH:mm:ss"), workLineIds)
	if err != nil {
		return 0, 0, err
	}
	countPack := len(packData)
	packTimes := impl.GetUsefulTime(startDayTime, time.Now(), timeModelList)
	if countPack > 0 {
		TeamTemPoNumber = utils.Decimal(float64(packTimes)/float64(countPack), 2)
		packHours := float64(packTimes) / float64(3600)
		if packHours > 0 {
			PerTeamTemPoNumber = utils.Decimal(float64(countPack)*jPHPercent/float64(num)/float64(packHours), 3)
		} else {
			PerTeamTemPoNumber = 0
		}
	} else {
		TeamTemPoNumber = 0
		PerTeamTemPoNumber = 0
	}
	return TeamTemPoNumber, PerTeamTemPoNumber, nil

	return 0, 0, nil
}

//图例1
func (impl *CustOrderServiceImplement) SelectPic1(user *global.User, dayModel *baseModel.DayModel, workLineId string, startTime, endTime, dayStartTime time.Time) (result model.CutLine1, err error) {
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	custOrderDao := dal.NewCustOrderDAO(session, user.PlantNr, user.UserId)
	packOrderDao := jitDal.NewPackOrderDAO(session, user.PlantNr, user.UserId)
	productFamilyDao := meDal.NewProductFamilyDAO(session, user.PlantNr, user.UserId)
	productFamilyRelateDao := meDal.NewProductFamilyRelateDAO(session, user.PlantNr, user.UserId)
	productFamilyMap := make(map[string]*meModel.ProductFamily)
	productFamilyRelateMap := make(map[string]map[string]interface{})
	productFamilyMeterMap := make(map[string]float64)
	//now := time.Now()
	//now, _ := time.ParseInLocation(grmi.DateTimeOutFormat, "2022-05-18 23:00:00", utils.TimezoneLocation)
	//endTime := now.Format(grmi.DateOutFormat) + " 23:59:59"
	//todayStart := now.Format(grmi.DateOutFormat) + " 00:00:00"
	//todayStartTime, _ := time.ParseInLocation(grmi.DateTimeOutFormat, todayStart, utils.TimezoneLocation)

	// 查询正在正在生产的客户订单
	custOrderLi, err := custOrderDao.SelectRunningCustOrder(workLineId)
	if err != nil {
		return result, grmi.NewBusinessError("查询正在生产的客户订单失败, 错误:" + err.Error())
	}
	var currentMeter int
	if len(custOrderLi) == 0 {
		currentMeter = 0
	} else {
		custOrder := custOrderLi[0]
		productFamily, err := productFamilyDao.SelectOne(custOrder.ProductFamilyId)
		if err != nil {
			return result, grmi.NewBusinessError("查询派生数据失败, 错误:" + err.Error())
		}
		if productFamily == nil {
			return result, grmi.NewBusinessError("客户订单派生不存在, 派生ID:" + custOrder.ProductFamilyId)
		}
		if productFamily.Jph == 0 {
			return result, grmi.NewBusinessError("派生JPH为0 派生ID:" + custOrder.ProductFamilyId)
		}
		currentMeter = int(float64(3600) / productFamily.Jph)
		productFamilyMeterMap[custOrder.ProductFamilyId] = float64(3600) / productFamily.Jph
		productFamilyMap[custOrder.ProductFamilyId] = productFamily
		productLi, err := productFamilyRelateDao.SelectProductByFamilyId(custOrder.ProjectId, custOrder.ProductFamilyId)
		if err != nil {
			return result, grmi.NewBusinessError("查询派生下总成数据失败, 错误:" + err.Error())
		}
		//if len(productLi) == 0 {
		//	return result, grmi.NewBusinessError("派生下未关联需要生产的座椅总成, 派生ID:" + custOrder.ProductFamilyId)
		//}
		productFamilyRelateMap[custOrder.ProductFamilyId] = make(map[string]interface{}, len(productLi))
		for _, product := range productLi {
			productFamilyRelateMap[custOrder.ProductFamilyId][product.ProductId] = nil
		}
	}
	timeModelList := impl.InitDayModelList(dayModel, dayStartTime)
	workLineDao := baseDal.NewWorkLineDAO(session, user.PlantNr, user.UserId)
	workLineLi, err := workLineDao.Select([]grmi.Predicate{baseMeta.WorkLine_GroupLineId.NewPredicate(grmi.Equal, workLineId)}, nil)
	if err != nil {
		return result, grmi.NewBusinessError("查询工厂产线数据失败, error:" + err.Error())
	}
	if len(workLineLi) == 0 {
		return result, grmi.NewBusinessError("未查询到实体产线")
	}
	var workLineIds []string
	for _, v := range workLineLi {
		workLineIds = append(workLineIds, v.WorkLineid)
	}
	packOrderLi, err := packOrderDao.SelectClosedPackOrder(startTime.Format(grmi.DateTimeOutFormat), endTime.Format(grmi.DateTimeOutFormat), workLineIds)
	if err != nil {
		return result, grmi.NewBusinessError("查询包装单数据失败")
	}
	usefulPackOrderLi := make([]jitModel.PackOrder, 0)
	for _, packOrder := range packOrderLi {
		var custOrderId string
		var notOneCustOrder bool
		if len(packOrder.PackOrderItemLstLi) == 0 {
			continue
		}
		for index, packOrderItem := range packOrder.PackOrderItemLstLi {
			if index == 0 {
				custOrderId = packOrderItem.SerialOrder.CustOrderId
				continue
			}
			if custOrderId != packOrderItem.SerialOrder.CustOrderId {
				notOneCustOrder = true
				break
			}
		}
		if !notOneCustOrder {
			usefulPackOrderLi = append(usefulPackOrderLi, packOrder)
		}
	}
	//sort.Slice(usefulPackOrderLi, func(i, j int) bool {
	//	return usefulPackOrderLi[i].CloseTime.Restore().Unix() < usefulPackOrderLi[j].CloseTime.Restore().Unix()
	//})
	custOrderMap := make(map[string]model.CustOrder)
	displayHourMap := make(map[int]interface{})
	displayHourLi := make([]int, 0, 24)
	displayHourProductMap := make(map[int][]model.HourProductData)
	for _, packOrder := range packOrderLi {
		hour := packOrder.CloseTime.Restore().Hour()
		_, exist := displayHourMap[hour]
		if !exist {
			displayHourMap[hour] = nil
			displayHourProductMap[hour] = make([]model.HourProductData, 0)
		}
		producedLi := displayHourProductMap[hour]
		if packOrder.PackOrderItemLstLi[0].SerialOrder.CustOrderId == "" {
			continue
		}
		custOrder, exist := custOrderMap[packOrder.PackOrderItemLstLi[0].SerialOrder.CustOrderId]
		if !exist {
			custOrderP, err := custOrderDao.SelectOne(packOrder.PackOrderItemLstLi[0].SerialOrder.CustOrderId)
			if err != nil {
				return result, grmi.NewBusinessError("查询客户订单失败, 错误:" + err.Error())
			}
			if custOrderP == nil {
				return result, grmi.NewBusinessError("客户订单不存在, 客户订单ID:" + packOrder.PackOrderItemLstLi[0].SerialOrder.CustOrderId)
			}
			custOrderMap[packOrder.PackOrderItemLstLi[0].SerialOrder.CustOrderId] = *custOrderP
			custOrder = *custOrderP
			productFamily, err := productFamilyDao.SelectOne(custOrder.ProductFamilyId)
			if err != nil {
				return result, grmi.NewBusinessError("查询派生数据失败, 错误:" + err.Error())
			}
			if productFamily == nil {
				return result, grmi.NewBusinessError("客户订单派生不存在, 派生ID:" + custOrder.ProductFamilyId)
			}
			if productFamily.Jph == 0 {
				return result, grmi.NewBusinessError("派生JPH为0 派生ID:" + custOrder.ProductFamilyId)
			}
			currentMeter = int(float64(3600) / productFamily.Jph)
			productFamilyMeterMap[custOrder.ProductFamilyId] = float64(3600) / productFamily.Jph
		}
		productFamilyId := custOrder.ProductFamilyId
		if len(producedLi) == 0 {
			produceData := model.HourProductData{
				ProductFamilyId: productFamilyId,
				Qty:             1,
				LastDoneTime:    packOrder.CloseTime.Restore(),
			}
			producedLi = append(producedLi, produceData)
		} else {
			lastProduceData := producedLi[len(producedLi)-1]
			if lastProduceData.ProductFamilyId == productFamilyId {
				lastProduceData.Qty += 1
				if lastProduceData.LastDoneTime.Before(packOrder.CloseTime.Restore()) {
					lastProduceData.LastDoneTime = packOrder.CloseTime.Restore()
				}
				producedLi[len(producedLi)-1] = lastProduceData
			} else {
				produceData := model.HourProductData{
					ProductFamilyId: productFamilyId,
					Qty:             1,
					LastDoneTime:    packOrder.CloseTime.Restore(),
				}
				producedLi = append(producedLi, produceData)
			}
		}
		displayHourProductMap[hour] = producedLi
	}
	result = model.CutLine1{
		Standard:           1.0,
		ProductFamilyMeter: currentMeter,
		ProduceData:        make([]float64, 0, len(displayHourLi)),
	}
	for hour := 0; hour < 48; hour++ {
		hourData, exist := displayHourProductMap[hour]
		if !exist {
			continue
		}
		result.TimeLi = append(result.TimeLi, strconv.Itoa(hour)+"时")
		var hourPercent float64
		productDataLi := make([]model.HourProductData, 0, len(hourData))
		for _, data := range hourData {
			productDataLi = append(productDataLi, data)
		}
		//impl.QuickSortProductData(productDataLi)
		currentTime := dayStartTime.Add(time.Hour * time.Duration(hour))
		var hourAllUsedTime int
		for _, productFamilyProductData := range productDataLi {
			usedTime := impl.GetUsefulTime(currentTime, productFamilyProductData.LastDoneTime, timeModelList)
			currentTime = productFamilyProductData.LastDoneTime
			hourAllUsedTime += usedTime
		}
		currentTime = dayStartTime.Add(time.Hour * time.Duration(hour))
		for _, productFamilyProductData := range productDataLi {

			usedTime := impl.GetUsefulTime(currentTime, productFamilyProductData.LastDoneTime, timeModelList)
			currentTime = productFamilyProductData.LastDoneTime
			meter := productFamilyMeterMap[productFamilyProductData.ProductFamilyId]
			var percent float64
			if productFamilyProductData.Qty != 0 && usedTime != 0 && hourAllUsedTime != 0 && meter != 0 {
				percent = (meter / (float64(usedTime) / float64(productFamilyProductData.Qty))) * (float64(usedTime) / float64(hourAllUsedTime))
			}

			hourPercent += percent
		}
		result.ProduceData = append(result.ProduceData, utils.Decimal(hourPercent, 2))
	}
	return result, nil
}

func (impl *CustOrderServiceImplement) GetUsefulTime(start, end time.Time, timeLine *list.List) int {
	var duration int
	for element := timeLine.Front(); element != nil; element = element.Next() {
		listElement := element.Value.(model.ListElement)
		//比较开始时间listElement.Start.Before(start)
		if listElement.Start.Unix() <= start.Unix() {

			/*
				     /----------------------/  start ---- end

				/----------------------/       listElement

			*/
			//if listElement.End.After(start) && listElement.End.Before(end) {
			if listElement.End.Unix() >= start.Unix() && listElement.End.Unix() <= end.Unix() {

				duration += int(listElement.End.Sub(start).Seconds())
			}

			/*
				     /----------------------/  start ---- end

				/---------------------------------/       listElement

			*/
			//if listElement.End.After(end) {
			if listElement.End.Unix() >= end.Unix() {
				duration += int(end.Sub(start).Seconds())
			}
			//listElement.Start.After(start) && listElement.Start.Before(end)
		} else if listElement.Start.Unix() >= start.Unix() && listElement.Start.Unix() <= end.Unix() {
			/*
			   /----------------------/  start ---- end

			          /-----------/       listElement

			*/

			//if listElement.End.Before(end) {
			if listElement.End.Unix() <= end.Unix() {
				duration += int(listElement.End.Sub(listElement.Start).Seconds())
			}

			/*
			   /----------------------/  start ---- end

			          /----------------------/       listElement

			*/
			//if listElement.End.After(end) {
			if listElement.End.Unix() >= end.Unix() {
				duration += int(end.Sub(listElement.Start).Seconds())
			}
		}
	}
	return duration
}

//图例3
func (impl *CustOrderServiceImplement) SelectPic3(user *global.User, DayModelHours []string, dayModel *baseModel.DayModel, jPHPercent float64, startDayTime time.Time, endDayTime time.Time, timeBeat float64, attendancePersonNum int, workLineId string, dayStartTime time.Time) (model.PerDashboardData, error) {
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	custOrderDao := dal.NewCustOrderDAO(session, user.PlantNr, user.UserId)
	productFamilyDao := meDal.NewProductFamilyDAO(session, user.PlantNr, user.UserId)
	packOrderDao := jitDal.NewPackOrderDAO(session, user.PlantNr, user.UserId)

	productFamilyMeterMap := make(map[string]float64)
	//now := time.Now()
	//todayStart := now.Format(grmi.DateOutFormat) + " 00:00:00"
	//todayStartTime, _ := time.ParseInLocation(grmi.DateTimeOutFormat, todayStart, utils.TimezoneLocation)
	packDao := jitDal.NewPackOrderDAO(session, user.PlantNr, user.UserId)
	//图例003
	DashboardData3 := model.PerDashboardData{}
	//图例001
	workLineDao := baseDal.NewWorkLineDAO(session, user.PlantNr, user.UserId)
	workLineLi, err := workLineDao.Select([]grmi.Predicate{baseMeta.WorkLine_GroupLineId.NewPredicate(grmi.Equal, workLineId)}, nil)
	if err != nil {
		return DashboardData3, grmi.NewBusinessError("查询工厂产线数据失败, error:" + err.Error())
	}
	if len(workLineLi) == 0 {
		return DashboardData3, grmi.NewBusinessError("未查询到实体产线")
	}
	var workLineIds []string
	for _, v := range workLineLi {
		workLineIds = append(workLineIds, v.WorkLineid)
	}

	timeModelList := impl.InitDayModelList(dayModel, dayStartTime)

	packOrderLi, err := packOrderDao.SelectClosedPackOrder(utils.TimeFormat(startDayTime, ""), utils.TimeFormat(endDayTime, ""), workLineIds)
	if err != nil {
		return DashboardData3, grmi.NewBusinessError("查询包装单数据失败")
	}
	usefulPackOrderLi := make([]jitModel.PackOrder, 0)
	for _, packOrder := range packOrderLi {
		var custOrderId string
		var notOneCustOrder bool
		if len(packOrder.PackOrderItemLstLi) == 0 {
			continue
		}
		for index, packOrderItem := range packOrder.PackOrderItemLstLi {
			if index == 0 {
				custOrderId = packOrderItem.SerialOrder.CustOrderId
				continue
			}
			if custOrderId != packOrderItem.SerialOrder.CustOrderId {
				notOneCustOrder = true
				break
			}
		}
		if !notOneCustOrder {
			usefulPackOrderLi = append(usefulPackOrderLi, packOrder)
		}
	}

	custOrderMap := make(map[string]model.CustOrder)
	displayHourMap := make(map[int]interface{})
	displayHourProductMap := make(map[int][]model.HourProductData)
	for _, packOrder := range packOrderLi {
		hour := packOrder.CloseTime.Restore().Hour()
		_, exist := displayHourMap[hour]
		if !exist {
			displayHourMap[hour] = nil
			displayHourProductMap[hour] = make([]model.HourProductData, 0)
		}
		producedLi := displayHourProductMap[hour]
		if packOrder.PackOrderItemLstLi[0].SerialOrder.CustOrderId == "" {
			continue
		}
		custOrder, exist := custOrderMap[packOrder.PackOrderItemLstLi[0].SerialOrder.CustOrderId]
		if !exist {
			custOrderP, err := custOrderDao.SelectOne(packOrder.PackOrderItemLstLi[0].SerialOrder.CustOrderId)
			if err != nil {
				return DashboardData3, grmi.NewBusinessError("查询客户订单失败, 错误:" + err.Error())
			}
			if custOrderP == nil {
				return DashboardData3, grmi.NewBusinessError("客户订单不存在, 客户订单ID:" + packOrder.PackOrderItemLstLi[0].SerialOrder.CustOrderId)
			}
			custOrderMap[packOrder.PackOrderItemLstLi[0].SerialOrder.CustOrderId] = *custOrderP
			custOrder = *custOrderP
			productFamily, err := productFamilyDao.SelectOne(custOrder.ProductFamilyId)
			if err != nil {
				return DashboardData3, grmi.NewBusinessError("查询派生数据失败, 错误:" + err.Error())
			}
			if productFamily == nil {
				return DashboardData3, grmi.NewBusinessError("客户订单派生不存在, 派生ID:" + custOrder.ProductFamilyId)
			}
			if productFamily.Jph == 0 {
				return DashboardData3, grmi.NewBusinessError("派生JPH为0 派生ID:" + custOrder.ProductFamilyId)
			}
			productFamilyMeterMap[custOrder.ProductFamilyId] = float64(3600) / productFamily.Jph
		}
		productFamilyId := custOrder.ProductFamilyId
		if len(producedLi) == 0 {
			produceData := model.HourProductData{
				ProductFamilyId: productFamilyId,
				Qty:             1,
				LastDoneTime:    packOrder.CloseTime.Restore(),
			}
			producedLi = append(producedLi, produceData)
		} else {
			lastProduceData := producedLi[len(producedLi)-1]
			if lastProduceData.ProductFamilyId == productFamilyId {
				lastProduceData.Qty += 1
				if lastProduceData.LastDoneTime.Before(packOrder.CloseTime.Restore()) {
					lastProduceData.LastDoneTime = packOrder.CloseTime.Restore()
				}
				producedLi[len(producedLi)-1] = lastProduceData
			} else {
				produceData := model.HourProductData{
					ProductFamilyId: productFamilyId,
					Qty:             1,
					LastDoneTime:    packOrder.CloseTime.Restore(),
				}
				producedLi = append(producedLi, produceData)
			}
		}
		displayHourProductMap[hour] = producedLi
	}

	//定义动态JPH
	jphMap := make(map[string]float64)
	hours, err := utils.GetHoursData(startDayTime, endDayTime)
	if err != nil {
		return DashboardData3, err
	}
	if len(hours) == 0 {
		return DashboardData3, err
	}
	for hour := 0; hour < len(hours); hour++ {
		key := utils.TimeFormat(dayStartTime.Add(time.Duration(hour)*time.Hour), "yyyy-MM-dd HH:00")
		hourData, exist := displayHourProductMap[hour]
		if !exist {
			continue
		}
		var hourPercent float64
		productDataLi := make([]model.HourProductData, 0, len(hourData))
		for _, data := range hourData {
			productDataLi = append(productDataLi, data)
		}
		currentTime := dayStartTime.Add(time.Hour * time.Duration(hour))
		var hourAllUsedTime int
		for _, productFamilyProductData := range productDataLi {
			usedTime := impl.GetUsefulTime(currentTime, productFamilyProductData.LastDoneTime, timeModelList)
			currentTime = productFamilyProductData.LastDoneTime
			hourAllUsedTime += usedTime
		}
		currentTime = dayStartTime.Add(time.Hour * time.Duration(hour))
		for _, productFamilyProductData := range productDataLi {
			usedTime := impl.GetUsefulTime(currentTime, productFamilyProductData.LastDoneTime, timeModelList)
			currentTime = productFamilyProductData.LastDoneTime
			meter := productFamilyMeterMap[productFamilyProductData.ProductFamilyId]
			var percent float64
			if productFamilyProductData.Qty != 0 && usedTime != 0 && hourAllUsedTime != 0 && meter != 0 {
				percent = (meter * (float64(usedTime) / float64(hourAllUsedTime)))
				//percent = meter / (float64(usedTime) / float64(productFamilyProductData.Qty)) * (float64(usedTime) / float64(hourAllUsedTime))
			}
			hourPercent += percent
		}
		jphMap[key] = hourPercent

		DashboardData3.CompleteNumber += utils.ValueToInt(hourPercent, 0)

	}

	//查询当天所有的包装单情况
	jitData, err := packDao.SelectUserClosePackOrder(utils.TimeFormat(startDayTime, "yyyy-MM-dd HH:mm:ss"), utils.TimeFormat(endDayTime, "yyyy-MM-dd HH:mm:ss"), workLineIds)
	if err != nil {
		return DashboardData3, err
	}
	jitMap := make(map[string]int)

	for _, v := range jitData {

		key := utils.TimeFormat(v.CloseTime.Restore(), "yyyy-MM-dd HH:00")

		_, ok := jitMap[key]
		if ok {
			jitMap[key] += 1
		} else {
			jitMap[key] = 1
		}
	}
	for i := 0; i < len(DayModelHours); i++ {
		key := DayModelHours[i]
		val, ok := jitMap[key]
		one := model.DashboardData{}
		one.Date = DayModelHours[i]
		if ok {
			one.Val = utils.Decimal((float64(val) / float64(attendancePersonNum)), 2)
		} else {
			one.Val = 0
		}
		DashboardData3.DashboardData1 = append(DashboardData3.DashboardData1, one)

		two := model.DashboardData{}
		two.Date = DayModelHours[i]
		timeBeat, ok := jphMap[key]
		if !ok {
			two.Val = 0
		} else {
			if timeBeat > 0 {
				two.Val = utils.Decimal(float64((3600/timeBeat)/float64(attendancePersonNum)), 2)
			} else {
				two.Val = 0
			}

		}
		DashboardData3.DashboardData2 = append(DashboardData3.DashboardData2, two)
	}
	glog.InfoExtln("DashboardData3", "DashboardData3.CompleteNumber:", DashboardData3.CompleteNumber)
	glog.InfoExtln("DashboardData3", "jPHPercent:", jPHPercent)
	DashboardData3.CompleteNumber = utils.ValueToInt(utils.ValueToFloat(DashboardData3.CompleteNumber, 0.0)*jPHPercent, 0)
	glog.InfoExtln("DashboardData3", "DashboardData3.CompleteNumber2:", DashboardData3.CompleteNumber)
	return DashboardData3, nil
}

//查询状态
func (impl *CustOrderServiceImplement) SelectAndonInfo(user *global.User) (int, error) {
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	andonDao := baseDal.NewAndonInfoDAO(session, user.UserId)
	andonInfo, err := andonDao.Select([]grmi.Predicate{baseMeta.AndonInfo_IsActive.NewPredicate(grmi.Equal, 1)}, nil)
	if err != nil {
		return 0, err
	}
	if len(andonInfo) > 0 {
		return 1, nil
	}
	return 0, nil
}

// SelectDefectPackOrder 查询打包不良数
func (impl *CustOrderServiceImplement) SelectDefectPackOrder(user *global.User, workLineId string) (int, error) {
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	//var num int
	//packOrderDao := jitDal.NewPackOrderDAO(session, user.PlantNr, user.UserId)
	//packOrderStatusDao := jitDal.NewPackOrderStatusDAO(session, user.PlantNr, user.UserId)
	//custOrderDao := dal.NewCustOrderDAO(session, user.PlantNr, user.UserId)
	today := time.Now().Format(grmi.DateOutFormat) + " 00:00:00"
	//packOrderLi, err := packOrderDao.Select([]grmi.Predicate{
	//	jitMeta.PackOrder_CloseTime.NewPredicate(grmi.GreaterOrEqual, today),
	//	jitMeta.PackOrder_CtrlStr1.NewPredicate(grmi.Equal, jitModel.PACK_QUALITY_NG),
	//}, nil)
	//if err != nil {
	//	return 0, grmi.NewBusinessError("查询打包数据失败, 错误:" + err.Error())
	//}
	//packOrderIdLi := make([]interface{}, 0)
	//for _, packOrder := range packOrderLi {
	//	packOrderIdLi = append(packOrderIdLi, packOrder.PackOrderId)
	//}
	//statusLi, err := packOrderStatusDao.Select([]grmi.Predicate{
	//	jitMeta.PackOrderStatus_PackOrderId.NewPredicate(grmi.Include, packOrderIdLi...),
	//	jitMeta.PackOrderStatus_Status.NewPredicate(grmi.Equal, jitModel.PACK_STATUS_CLOSED),
	//}, nil)
	//if err != nil {
	//	return 0, grmi.NewBusinessError("查询打包数据失败, 错误:" + err.Error())
	//}
	//packOrderIdLi = make([]interface{}, 0)
	//for _, status := range statusLi {
	//	packOrderIdLi = append(packOrderIdLi, status.PackOrderId)
	//}
	packOrderItemDao := jitDal.NewPackOrderItemLstDAO(session, user.PlantNr, user.UserId)
	//itemLi, err := packOrderItemDao.Select([]grmi.Predicate{
	//	jitMeta.PackOrderItemLst_PackOrderId.NewPredicate(grmi.Include, packOrderIdLi...),
	//	jitMeta.PackOrderItemLst_Status.NewPredicate(grmi.Equal, jitModel.PACK_STATUS_CLOSED),
	//}, []grmi.Field{jitMeta.PackOrderItemLst_PackOrderId})
	//packOrderSerialOrderMap := make(map[string][]interface{})
	//for _, item := range itemLi {
	//	if item.SerialOrderId != "" {
	//		continue
	//	}
	//	packOrderSerialOrderMap[item.PackOrderId] = append(packOrderSerialOrderMap[item.PackOrderId], item.SerialOrderId)
	//}
	//serialOrderDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	//for _, serialOrderIdLi := range packOrderSerialOrderMap {
	//	serialOrderLi, err := serialOrderDao.Select([]grmi.Predicate{
	//		omMeta.SerialOrder_SerialOrderId.NewPredicate(grmi.Include, serialOrderIdLi...),
	//		omMeta.SerialOrder_OrderType.NewPredicate(grmi.Equal, baseModel.ORDER_TYPE_SEQ),
	//	}, nil)
	//	if err != nil {
	//		return 0, grmi.NewBusinessError("查询工单失败，错误：" + err.Error())
	//	}
	//	custOrderIdMap := make(map[string]interface{})
	//	for _, serialOrder := range serialOrderLi {
	//		custOrderIdMap[serialOrder.CustOrderId] = nil
	//	}
	//	custOrderIdLi := make([]interface{}, 0, len(custOrderIdMap))
	//	for custOrderId := range custOrderIdMap {
	//		custOrderIdLi = append(custOrderIdLi, custOrderId)
	//	}
	//	custOrderLi, err := custOrderDao.Select([]grmi.Predicate{
	//		meta.CustOrder_CustOrderId.NewPredicate(grmi.Include, custOrderIdLi...),
	//		meta.CustOrder_WorkLineId.NewPredicate(grmi.Equal, workLineId),
	//	}, nil)
	//	if err != nil {
	//		return 0, grmi.NewBusinessError("查询订单失败，错误：" + err.Error())
	//	}
	//	if len(custOrderLi) != 0 {
	//		num++
	//	}
	//}
	itemLi, err := packOrderItemDao.SelectNGPackOrderItem(workLineId, today)
	if err != nil {
		return 0, grmi.NewBusinessError("查询打包数据失败, 错误:" + err.Error())
	}
	packOrderMap := make(map[string]interface{})
	for _, item := range itemLi {
		packOrderMap[item.PackOrderId] = nil
	}
	return len(packOrderMap), nil
}
