// 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              : WorkShift的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfWorkShift = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkShift_WorkShiftNr.UrlParameterName:          grmi.NewConditionItem(meta.WorkShift_WorkShiftNr, grmi.Equal, true),
		meta.WorkShift_Descr.UrlParameterName:                grmi.NewConditionItem(meta.WorkShift_Descr, grmi.Equal, false),
		meta.WorkShift_Abrev.UrlParameterName:                grmi.NewConditionItem(meta.WorkShift_Abrev, grmi.Equal, false),
		meta.WorkShift_HexColor.UrlParameterName:             grmi.NewConditionItem(meta.WorkShift_HexColor, grmi.Equal, false),
		meta.WorkShift_PlanPersonQty.UrlParameterName:        grmi.NewConditionItem(meta.WorkShift_PlanPersonQty, grmi.Equal, false),
		meta.WorkShift_PlanEfficiency.UrlParameterName:       grmi.NewConditionItem(meta.WorkShift_PlanEfficiency, grmi.Equal, false),
		meta.WorkShift_SBeg.UrlParameterName:                 grmi.NewConditionItem(meta.WorkShift_SBeg, grmi.Equal, false),
		meta.WorkShift_SEnd.UrlParameterName:                 grmi.NewConditionItem(meta.WorkShift_SEnd, grmi.Equal, false),
		meta.WorkShift_SLen.UrlParameterName:                 grmi.NewConditionItem(meta.WorkShift_SLen, grmi.Equal, false),
		meta.WorkShift_SB1Beg.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB1Beg, grmi.Equal, false),
		meta.WorkShift_SB1End.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB1End, grmi.Equal, false),
		meta.WorkShift_SB2Beg.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB2Beg, grmi.Equal, false),
		meta.WorkShift_SB2End.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB2End, grmi.Equal, false),
		meta.WorkShift_SB3Beg.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB3Beg, grmi.Equal, false),
		meta.WorkShift_SB3End.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB3End, grmi.Equal, false),
		meta.WorkShift_SB4Beg.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB4Beg, grmi.Equal, false),
		meta.WorkShift_SB4End.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB4End, grmi.Equal, false),
		meta.WorkShift_SB5Beg.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB5Beg, grmi.Equal, false),
		meta.WorkShift_SB5End.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB5End, grmi.Equal, false),
		meta.WorkShift_SB6Beg.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB6Beg, grmi.Equal, false),
		meta.WorkShift_SB6End.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB6End, grmi.Equal, false),
		meta.WorkShift_SB7Beg.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB7Beg, grmi.Equal, false),
		meta.WorkShift_SB7End.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB7End, grmi.Equal, false),
		meta.WorkShift_SB8Beg.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB8Beg, grmi.Equal, false),
		meta.WorkShift_SB8End.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB8End, grmi.Equal, false),
		meta.WorkShift_SB9Beg.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB9Beg, grmi.Equal, false),
		meta.WorkShift_SB9End.UrlParameterName:               grmi.NewConditionItem(meta.WorkShift_SB9End, grmi.Equal, false),
		meta.WorkShift_SB10Beg.UrlParameterName:              grmi.NewConditionItem(meta.WorkShift_SB10Beg, grmi.Equal, false),
		meta.WorkShift_SB10End.UrlParameterName:              grmi.NewConditionItem(meta.WorkShift_SB10End, grmi.Equal, false),
		meta.WorkShift_EnableCrossDayToggle.UrlParameterName: grmi.NewConditionItem(meta.WorkShift_EnableCrossDayToggle, grmi.Equal, false),
		meta.WorkShift_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.WorkShift_LastModify, grmi.Approximate, false),
		meta.WorkShift_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.WorkShift_LastUser, grmi.Equal, false),
		meta.WorkShift_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.WorkShift_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkShiftService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) InsertOne(user *global.User, entity *model.WorkShift) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkShiftDAO(session, user.PlantNr, user.UserId)
	//record, err := dao.SelectOne(entity.WorkShiftNr)
	//if err != nil {
	//	return err
	//}
	//if record != nil {
	//	return grmi.NewBusinessError("已经存在相同主键的记录!")
	//}
	if entity.SEnd <= entity.SBeg {
		return grmi.NewBusinessError("参数错误，SEnd <= SBeg")
	}
	duration := entity.SEnd - entity.SBeg
	if entity.SB1End > entity.SB1Beg {
		duration -= entity.SB1End - entity.SB1Beg
	}
	if entity.SB2End > entity.SB2Beg {
		duration -= entity.SB2End - entity.SB2Beg
	}
	if entity.SB3End > entity.SB3Beg {
		duration -= entity.SB3End - entity.SB3Beg
	}
	if entity.SB4End > entity.SB4Beg {
		duration -= entity.SB4End - entity.SB4Beg
	}
	if entity.SB5End > entity.SB5Beg {
		duration -= entity.SB5End - entity.SB5Beg
	}
	if entity.SB6End > entity.SB6Beg {
		duration -= entity.SB6End - entity.SB6Beg
	}
	if entity.SB7End > entity.SB7Beg {
		duration -= entity.SB7End - entity.SB7Beg
	}
	if entity.SB8End > entity.SB8Beg {
		duration -= entity.SB8End - entity.SB8Beg
	}
	if entity.SB9End > entity.SB9Beg {
		duration -= entity.SB9End - entity.SB9Beg
	}
	if entity.SB10End > entity.SB10Beg {
		duration -= entity.SB10End - entity.SB10Beg
	}
	entity.SLen = duration
	err := dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkShiftService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) DeleteOne(user *global.User, workShiftNr int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewWorkShiftDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(workShiftNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	wslDao := dal.NewWorkShiftEffLstDAO(session, user.PlantNr, user.UserId)
	err = wslDao.DeleteWhere([]grmi.Predicate{meta.WorkShiftEffLst_WorkShiftNr.NewPredicate(grmi.Equal, workShiftNr)})
	if err != nil {
		session.Rollback()
		return err
	}
	err = dao.DeleteOne(workShiftNr)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkShiftService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) SelectOne(user *global.User, workShiftNr int) (*model.WorkShift, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkShiftService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) UpdateOne(user *global.User, entity *model.WorkShift) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkShiftDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.WorkShiftNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	duration := entity.SEnd - entity.SBeg
	if entity.SB1End > entity.SB1Beg {
		duration -= entity.SB1End - entity.SB1Beg
	}
	if entity.SB2End > entity.SB2Beg {
		duration -= entity.SB2End - entity.SB2Beg
	}
	if entity.SB3End > entity.SB3Beg {
		duration -= entity.SB3End - entity.SB3Beg
	}
	if entity.SB4End > entity.SB4Beg {
		duration -= entity.SB4End - entity.SB4Beg
	}
	if entity.SB5End > entity.SB5Beg {
		duration -= entity.SB5End - entity.SB5Beg
	}
	if entity.SB6End > entity.SB6Beg {
		duration -= entity.SB6End - entity.SB6Beg
	}
	if entity.SB7End > entity.SB7Beg {
		duration -= entity.SB7End - entity.SB7Beg
	}
	if entity.SB8End > entity.SB8Beg {
		duration -= entity.SB8End - entity.SB8Beg
	}
	if entity.SB9End > entity.SB9Beg {
		duration -= entity.SB9End - entity.SB9Beg
	}
	if entity.SB10End > entity.SB10Beg {
		duration -= entity.SB10End - entity.SB10Beg
	}
	entity.SLen = duration
	entity.WorkShiftNr = record.WorkShiftNr
	err = dao.UpdateOne(entity)
	if entity.SEnd <= entity.SBeg {
		return grmi.NewBusinessError("参数错误，SEnd <= SBeg")
	}
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkShiftService.Insert
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) Insert(user *global.User, entities *[]model.WorkShift) error {

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

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

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

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

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

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

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

	condition := DefaultConditionOfWorkShiftAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewWorkShiftDAO(session, user.PlantNr, user.UserId)
	workShiftLstDao := dal.NewWorkShiftEffLstDAO(session, user.PlantNr, user.UserId)
	lstCondition := DefaultConditionOfWorkShiftEffLst
	if condition.Fill(urlParameters) {
		result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		li := result.Records.([]model.WorkShift)
		for index, workShift := range li {
			newWorkShift := workShift
			workShiftEffLstLi, err := workShiftLstDao.Select([]grmi.Predicate{meta.WorkShiftEffLst_WorkShiftNr.NewPredicate(grmi.Equal, workShift.WorkShiftNr)}, lstCondition.OrderByFields)
			if err != nil {
				return nil, err
			}
			newWorkShift.WorkShiftEffLst = workShiftEffLstLi
			li[index] = newWorkShift
		}
		result.Records = li
		return result, nil
	} else {
		result, err := dao.Select(predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		for index, workShift := range result {
			newWorkShift := workShift
			workShiftEffLstLi, err := workShiftLstDao.Select([]grmi.Predicate{meta.WorkShiftEffLst_WorkShiftNr.NewPredicate(grmi.Equal, workShift.WorkShiftNr)}, lstCondition.OrderByFields)
			if err != nil {
				return nil, err
			}
			newWorkShift.WorkShiftEffLst = workShiftEffLstLi
			result[index] = newWorkShift
		}
		return result, nil
	}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkShiftService.Update
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) Update(user *global.User, entities *[]model.WorkShift) error {

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

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

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

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