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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/base"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/base"
	model "LAPP_LF_MOM_BACKEND/models/base"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @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_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_LF_MOM_BACKEND/services/base/WorkShiftService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) InsertOne(user *models.Usertab, 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.Pid, 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
	}
	entity.SLen = duration
	err := dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkShiftService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) DeleteOne(user *models.Usertab, 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.Pid, user.Userid)
	record, err := dao.SelectOne(workShiftNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	wslDao := dal.NewWorkShiftEffLstDAO(session, user.Pid, 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_LF_MOM_BACKEND/services/base/WorkShiftService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) SelectOne(user *models.Usertab, 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.Pid, user.Userid)
	result, err := dao.SelectOne(workShiftNr)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkShiftService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) UpdateOne(user *models.Usertab, 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.Pid, 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
	}
	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_LF_MOM_BACKEND/services/base/WorkShiftService.Insert
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) Insert(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/base/WorkShiftService.Delete
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) Delete(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/base/WorkShiftService.Select
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) Select(user *models.Usertab, 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.Pid, user.Userid)
	workShiftLstDao := dal.NewWorkShiftEffLstDAO(session, user.Pid, 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_LF_MOM_BACKEND/services/base/WorkShiftService.Update
 *
 ******************************************************************************/
func (impl *WorkShiftServiceImplement) Update(user *models.Usertab, 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.Pid, 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
}
