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

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/base"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/base"
	model "LAPP_ACURA_MOM_BACKEND/models/base"
)

/******************************************************************************
 *
 * @Description              : DayModel的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfDayModel = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.DayModel_DayModelNr.UrlParameterName:      grmi.NewConditionItem(meta.DayModel_DayModelNr, grmi.Equal, true),
		meta.DayModel_Descr.UrlParameterName:           grmi.NewConditionItem(meta.DayModel_Descr, grmi.Equal, false),
		meta.DayModel_Abrev.UrlParameterName:           grmi.NewConditionItem(meta.DayModel_Abrev, grmi.Equal, false),
		meta.DayModel_HexColor.UrlParameterName:        grmi.NewConditionItem(meta.DayModel_HexColor, grmi.Equal, false),
		meta.DayModel_S1Toggle.UrlParameterName:        grmi.NewConditionItem(meta.DayModel_S1Toggle, grmi.Equal, false),
		meta.DayModel_S2Toggle.UrlParameterName:        grmi.NewConditionItem(meta.DayModel_S2Toggle, grmi.Equal, false),
		meta.DayModel_S3Toggle.UrlParameterName:        grmi.NewConditionItem(meta.DayModel_S3Toggle, grmi.Equal, false),
		meta.DayModel_WorkShiftToggle.UrlParameterName: grmi.NewConditionItem(meta.DayModel_WorkShiftToggle, grmi.Equal, false),
		meta.DayModel_S1WorkShiftNr.UrlParameterName:   grmi.NewConditionItem(meta.DayModel_S1WorkShiftNr, grmi.Equal, false),
		meta.DayModel_S2WorkShiftNr.UrlParameterName:   grmi.NewConditionItem(meta.DayModel_S2WorkShiftNr, grmi.Equal, false),
		meta.DayModel_S3WorkShiftNr.UrlParameterName:   grmi.NewConditionItem(meta.DayModel_S3WorkShiftNr, grmi.Equal, false),
		meta.DayModel_S1Beg.UrlParameterName:           grmi.NewConditionItem(meta.DayModel_S1Beg, grmi.Equal, false),
		meta.DayModel_S1End.UrlParameterName:           grmi.NewConditionItem(meta.DayModel_S1End, grmi.Equal, false),
		meta.DayModel_S1Len.UrlParameterName:           grmi.NewConditionItem(meta.DayModel_S1Len, grmi.Equal, false),
		meta.DayModel_S2Beg.UrlParameterName:           grmi.NewConditionItem(meta.DayModel_S2Beg, grmi.Equal, false),
		meta.DayModel_S2End.UrlParameterName:           grmi.NewConditionItem(meta.DayModel_S2End, grmi.Equal, false),
		meta.DayModel_S2Len.UrlParameterName:           grmi.NewConditionItem(meta.DayModel_S2Len, grmi.Equal, false),
		meta.DayModel_S3Beg.UrlParameterName:           grmi.NewConditionItem(meta.DayModel_S3Beg, grmi.Equal, false),
		meta.DayModel_S3End.UrlParameterName:           grmi.NewConditionItem(meta.DayModel_S3End, grmi.Equal, false),
		meta.DayModel_S3Len.UrlParameterName:           grmi.NewConditionItem(meta.DayModel_S3Len, grmi.Equal, false),
		meta.DayModel_DmLen.UrlParameterName:           grmi.NewConditionItem(meta.DayModel_DmLen, grmi.Equal, false),
		meta.DayModel_S1B1Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S1B1Beg, grmi.Equal, false),
		meta.DayModel_S1B1End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S1B1End, grmi.Equal, false),
		meta.DayModel_S1B2Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S1B2Beg, grmi.Equal, false),
		meta.DayModel_S1B2End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S1B2End, grmi.Equal, false),
		meta.DayModel_S1B3Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S1B3Beg, grmi.Equal, false),
		meta.DayModel_S1B3End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S1B3End, grmi.Equal, false),
		meta.DayModel_S1B4Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S1B4Beg, grmi.Equal, false),
		meta.DayModel_S1B4End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S1B4End, grmi.Equal, false),
		meta.DayModel_S1B5Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S1B5Beg, grmi.Equal, false),
		meta.DayModel_S1B5End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S1B5End, grmi.Equal, false),
		meta.DayModel_S2B1Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S2B1Beg, grmi.Equal, false),
		meta.DayModel_S2B1End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S2B1End, grmi.Equal, false),
		meta.DayModel_S2B2Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S2B2Beg, grmi.Equal, false),
		meta.DayModel_S2B2End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S2B2End, grmi.Equal, false),
		meta.DayModel_S2B3Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S2B3Beg, grmi.Equal, false),
		meta.DayModel_S2B3End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S2B3End, grmi.Equal, false),
		meta.DayModel_S2B4Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S2B4Beg, grmi.Equal, false),
		meta.DayModel_S2B4End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S2B4End, grmi.Equal, false),
		meta.DayModel_S2B5Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S2B5Beg, grmi.Equal, false),
		meta.DayModel_S2B5End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S2B5End, grmi.Equal, false),
		meta.DayModel_S3B1Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S3B1Beg, grmi.Equal, false),
		meta.DayModel_S3B1End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S3B1End, grmi.Equal, false),
		meta.DayModel_S3B2Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S3B2Beg, grmi.Equal, false),
		meta.DayModel_S3B2End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S3B2End, grmi.Equal, false),
		meta.DayModel_S3B3Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S3B3Beg, grmi.Equal, false),
		meta.DayModel_S3B3End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S3B3End, grmi.Equal, false),
		meta.DayModel_S3B4Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S3B4Beg, grmi.Equal, false),
		meta.DayModel_S3B4End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S3B4End, grmi.Equal, false),
		meta.DayModel_S3B5Beg.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S3B5Beg, grmi.Equal, false),
		meta.DayModel_S3B5End.UrlParameterName:         grmi.NewConditionItem(meta.DayModel_S3B5End, grmi.Equal, false),
		meta.DayModel_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.DayModel_LastModify, grmi.Approximate, false),
		meta.DayModel_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.DayModel_LastUser, grmi.Equal, false),
		meta.DayModel_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.DayModel_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : DayModel的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfDayModelAndPaging = grmi.NewCondition(
	DefaultConditionOfDayModel.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : DayModelServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : DayModel的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-23 10:43:27
*
******************************************************************************/
type DayModelServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewDayModelDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个DayModelDAOImplement实例
 *
 * @Return Value             : DayModelDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewDayModelServiceImplement() *DayModelServiceImplement {
	return &DayModelServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/DayModelService.InsertOne
 *
 ******************************************************************************/
func (impl *DayModelServiceImplement) InsertOne(user *global.User, entity *model.DayModel) error {

	grmi.Log(user, "/services/base/implments/DayModel.service.impl.go", "InsertOneDayModel", "插入一个DayModel")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDayModelDAO(session, user.PlantNr, user.UserId)
	var s1WorkShift *model.WorkShift
	var s2WorkShift *model.WorkShift
	var s3WorkShift *model.WorkShift
	dependOnDao := dal.NewWorkShiftDAO(session, user.PlantNr, user.UserId)
	if entity.WorkShiftToggle {
		if entity.S1WorkShiftNr != 0 {
			workShift, err := dependOnDao.SelectOne(entity.S1WorkShiftNr)
			if err != nil {
				return err
			}
			if workShift == nil {
				return grmi.NewBusinessError("S1WorkShift不存在对应的人员班组信息，请检查")
			}
			s1WorkShift = workShift
		}
		if entity.S2WorkShiftNr != 0 {
			workShift, err := dependOnDao.SelectOne(entity.S2WorkShiftNr)
			if err != nil {
				return err
			}
			if workShift == nil {
				return grmi.NewBusinessError("S2WorkShift不存在对应的人员班组信息，请检查")
			}
			s2WorkShift = workShift
			if s1WorkShift.SEnd > s2WorkShift.SBeg {
				return grmi.NewBusinessError("S2WorkShift的开始时间早于S1WorkShift的结束时间")
			}
		}
		if entity.S3WorkShiftNr != 0 {
			workShift, err := dependOnDao.SelectOne(entity.S3WorkShiftNr)
			if err != nil {
				return err
			}
			if workShift == nil {
				return grmi.NewBusinessError("S3WorkShiftNr不存在对应的人员班组信息，请检查")
			}
			s3WorkShift = workShift
			if s2WorkShift.SEnd > s3WorkShift.SBeg {
				return grmi.NewBusinessError("S3WorkShift的开始时间早于S2WorkShift的结束时间")
			}
		}
	} else {
		if entity.S1Toggle {
			if entity.S1End <= entity.S1Beg {
				return grmi.NewBusinessError("参数错误，班次1的结束时间早于开始时间")
			}
			duration := entity.S1End - entity.S1Beg
			if entity.S1B1End > entity.S1B1Beg {
				duration -= entity.S1B1End - entity.S1B1Beg
			}
			if entity.S1B2End > entity.S1B2Beg {
				duration -= entity.S1B2End - entity.S1B2Beg
			}
			if entity.S1B3End > entity.S1B3Beg {
				duration -= entity.S1B3End - entity.S1B3Beg
			}
			if entity.S1B4End > entity.S1B4Beg {
				duration -= entity.S1B4End - entity.S1B4Beg
			}
			if entity.S1B5End > entity.S1B5Beg {
				duration -= entity.S1B5End - entity.S1B5Beg
			}
			entity.S1Len = duration
		}
		if entity.S2Toggle {
			if entity.S2End <= entity.S2Beg {
				return grmi.NewBusinessError("参数错误，班次2的结束时间早于开始时间")
			}
			if entity.S2Beg < entity.S1End {
				return grmi.NewBusinessError("参数错误, 班次2的开始时间早于班次1的结束时间")
			}
			duration := entity.S2End - entity.S2Beg
			if entity.S2B1End > entity.S2B1Beg {
				duration -= entity.S2B1End - entity.S2B1Beg
			}
			if entity.S2B2End > entity.S2B2Beg {
				duration -= entity.S2B2End - entity.S2B2Beg
			}
			if entity.S2B3End > entity.S2B3Beg {
				duration -= entity.S2B3End - entity.S2B3Beg
			}
			if entity.S2B4End > entity.S2B4Beg {
				duration -= entity.S2B4End - entity.S2B4Beg
			}
			if entity.S2B5End > entity.S2B5Beg {
				duration -= entity.S2B5End - entity.S2B5Beg
			}
			entity.S2Len = duration
		}
		if entity.S3Toggle {
			if entity.S3End <= entity.S3Beg {
				return grmi.NewBusinessError("参数错误，班次3的结束时间早于开始时间")
			}
			if entity.S3Beg < entity.S2End {
				return grmi.NewBusinessError("参数错误, 班次3的开始时间早于班次2的结束时间")
			}
			duration := entity.S3End - entity.S3Beg
			if entity.S3B1End > entity.S3B1Beg {
				duration -= entity.S3B1End - entity.S3B1Beg
			}
			if entity.S3B2End > entity.S3B2Beg {
				duration -= entity.S3B2End - entity.S3B2Beg
			}
			if entity.S3B3End > entity.S3B3Beg {
				duration -= entity.S3B3End - entity.S3B3Beg
			}
			if entity.S3B4End > entity.S3B4Beg {
				duration -= entity.S3B4End - entity.S3B4Beg
			}
			if entity.S3B5End > entity.S3B5Beg {
				duration -= entity.S3B5End - entity.S3B5Beg
			}
			entity.S3Len = duration
		}
	}

	err := dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/DayModelService.DeleteOne
 *
 ******************************************************************************/
func (impl *DayModelServiceImplement) DeleteOne(user *global.User, dayModelNr int) error {

	grmi.Log(user, "/services/base/implments/DayModel.service.impl.go", "DeleteOneDayModel", "删除一个DayModel")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDayModelDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(dayModelNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(dayModelNr)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/DayModelService.SelectOne
 *
 ******************************************************************************/
func (impl *DayModelServiceImplement) SelectOne(user *global.User, dayModelNr int) (*model.DayModel, error) {

	grmi.Log(user, "/services/base/implments/DayModel.service.impl.go", "SelectOneDayModel", "查询一个DayModel")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDayModelDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(dayModelNr)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/DayModelService.UpdateOne
 *
 ******************************************************************************/
func (impl *DayModelServiceImplement) UpdateOne(user *global.User, entity *model.DayModel) error {

	grmi.Log(user, "/services/base/implments/DayModel.service.impl.go", "UpdateOneDayModel", "修改一个DayModel")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDayModelDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.DayModelNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	var s1WorkShift *model.WorkShift
	var s2WorkShift *model.WorkShift
	var s3WorkShift *model.WorkShift
	dependOnDao := dal.NewWorkShiftDAO(session, user.PlantNr, user.UserId)
	if entity.WorkShiftToggle {
		if entity.S1WorkShiftNr != 0 {
			workShift, err := dependOnDao.SelectOne(entity.S1WorkShiftNr)
			if err != nil {
				return err
			}
			if workShift == nil {
				return grmi.NewBusinessError("S1WorkShift不存在对应的人员班组信息，请检查")
			}
			s1WorkShift = workShift
		}
		if entity.S2WorkShiftNr != 0 {
			workShift, err := dependOnDao.SelectOne(entity.S2WorkShiftNr)
			if err != nil {
				return err
			}
			if workShift == nil {
				return grmi.NewBusinessError("S2WorkShift不存在对应的人员班组信息，请检查")
			}
			s2WorkShift = workShift
			if s1WorkShift.SEnd > s2WorkShift.SBeg {
				return grmi.NewBusinessError("S2WorkShift的开始时间早于S1WorkShift的结束时间")
			}
		}
		if entity.S3WorkShiftNr != 0 {
			workShift, err := dependOnDao.SelectOne(entity.S3WorkShiftNr)
			if err != nil {
				return err
			}
			if workShift == nil {
				return grmi.NewBusinessError("S3WorkShiftNr不存在对应的人员班组信息，请检查")
			}
			s3WorkShift = workShift
			if s2WorkShift.SEnd > s3WorkShift.SBeg {
				return grmi.NewBusinessError("S3WorkShift的开始时间早于S2WorkShift的结束时间")
			}
		}
	} else {
		if entity.S1Toggle {
			if entity.S1End <= entity.S1Beg {
				return grmi.NewBusinessError("参数错误，班次1的结束时间早于开始时间")
			}
			duration := entity.S1End - entity.S1Beg
			if entity.S1B1End > entity.S1B1Beg {
				duration -= entity.S1B1End - entity.S1B1Beg
			}
			if entity.S1B2End > entity.S1B2Beg {
				duration -= entity.S1B2End - entity.S1B2Beg
			}
			if entity.S1B3End > entity.S1B3Beg {
				duration -= entity.S1B3End - entity.S1B3Beg
			}
			if entity.S1B4End > entity.S1B4Beg {
				duration -= entity.S1B4End - entity.S1B4Beg
			}
			if entity.S1B5End > entity.S1B5Beg {
				duration -= entity.S1B5End - entity.S1B5Beg
			}
			entity.S1Len = duration
		}
		if entity.S2Toggle {
			if entity.S2End <= entity.S2Beg {
				return grmi.NewBusinessError("参数错误，班次2的结束时间早于开始时间")
			}
			if entity.S2Beg < entity.S1End {
				return grmi.NewBusinessError("参数错误, 班次2的开始时间早于班次1的结束时间")
			}
			duration := entity.S2End - entity.S2Beg
			if entity.S2B1End > entity.S2B1Beg {
				duration -= entity.S2B1End - entity.S2B1Beg
			}
			if entity.S2B2End > entity.S2B2Beg {
				duration -= entity.S2B2End - entity.S2B2Beg
			}
			if entity.S2B3End > entity.S2B3Beg {
				duration -= entity.S2B3End - entity.S2B3Beg
			}
			if entity.S2B4End > entity.S2B4Beg {
				duration -= entity.S2B4End - entity.S2B4Beg
			}
			if entity.S2B5End > entity.S2B5Beg {
				duration -= entity.S2B5End - entity.S2B5Beg
			}
			entity.S2Len = duration
		}
		if entity.S3Toggle {
			if entity.S3End <= entity.S3Beg {
				return grmi.NewBusinessError("参数错误，班次3的结束时间早于开始时间")
			}
			if entity.S3Beg < entity.S2End {
				return grmi.NewBusinessError("参数错误, 班次3的开始时间早于班次2的结束时间")
			}
			duration := entity.S3End - entity.S3Beg
			if entity.S3B1End > entity.S3B1Beg {
				duration -= entity.S3B1End - entity.S3B1Beg
			}
			if entity.S3B2End > entity.S3B2Beg {
				duration -= entity.S3B2End - entity.S3B2Beg
			}
			if entity.S3B3End > entity.S3B3Beg {
				duration -= entity.S3B3End - entity.S3B3Beg
			}
			if entity.S3B4End > entity.S3B4Beg {
				duration -= entity.S3B4End - entity.S3B4Beg
			}
			if entity.S3B5End > entity.S3B5Beg {
				duration -= entity.S3B5End - entity.S3B5Beg
			}
			entity.S3Len = duration
		}
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/DayModelService.Insert
 *
 ******************************************************************************/
func (impl *DayModelServiceImplement) Insert(user *global.User, entities *[]model.DayModel) error {

	grmi.Log(user, "/services/base/implments/DayModel.service.impl.go", "InsertDayModel", "插入多个DayModel")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDayModelDAO(session, user.PlantNr, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DayModelNr)
		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/base/DayModelService.Delete
 *
 ******************************************************************************/
func (impl *DayModelServiceImplement) Delete(user *global.User, entities *[]model.DayModel) error {

	grmi.Log(user, "/services/base/implments/DayModel.service.impl.go", "DeleteDayModel", "删除多个DayModel")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDayModelDAO(session, user.PlantNr, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DayModelNr)
		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/base/DayModelService.Select
 *
 ******************************************************************************/
func (impl *DayModelServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/base/implments/DayModel.service.impl.go", "SelectDayModel", "查询DayModel")

	condition := DefaultConditionOfDayModelAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewDayModelDAO(session, user.PlantNr, user.UserId)
	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/base/DayModelService.Update
 *
 ******************************************************************************/
func (impl *DayModelServiceImplement) Update(user *global.User, entities *[]model.DayModel) error {

	grmi.Log(user, "/services/base/implments/DayModel.service.impl.go", "UpdateDayModel", "修改多个DayModel")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDayModelDAO(session, user.PlantNr, user.UserId)

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

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