// 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              : WorkShiftEffLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfWorkShiftEffLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkShiftEffLst_WorkShiftNr.UrlParameterName:      grmi.NewConditionItem(meta.WorkShiftEffLst_WorkShiftNr, grmi.Equal, true),
		meta.WorkShiftEffLst_LevelId.UrlParameterName:          grmi.NewConditionItem(meta.WorkShiftEffLst_LevelId, grmi.Equal, true),
		meta.WorkShiftEffLst_Descr.UrlParameterName:            grmi.NewConditionItem(meta.WorkShiftEffLst_Descr, grmi.Equal, false),
		meta.WorkShiftEffLst_PlanPersonUclQty.UrlParameterName: grmi.NewConditionItem(meta.WorkShiftEffLst_PlanPersonUclQty, grmi.Equal, false),
		meta.WorkShiftEffLst_PlanPersonLclQty.UrlParameterName: grmi.NewConditionItem(meta.WorkShiftEffLst_PlanPersonLclQty, grmi.Equal, false),
		meta.WorkShiftEffLst_PlanEfficiency.UrlParameterName:   grmi.NewConditionItem(meta.WorkShiftEffLst_PlanEfficiency, grmi.Equal, false),
		meta.WorkShiftEffLst_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.WorkShiftEffLst_LastModify, grmi.Approximate, false),
		meta.WorkShiftEffLst_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.WorkShiftEffLst_LastUser, grmi.Equal, false),
		meta.WorkShiftEffLst_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.WorkShiftEffLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkShiftEffLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkShiftEffLstServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkShiftEffLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkShiftEffLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkShiftNr, entity.LevelId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	dependOnDao := dal.NewWorkShiftDAO(session, user.Pid, user.Userid)
	workShift, err := dependOnDao.SelectOne(entity.WorkShiftNr)
	if err != nil {
		return err
	}
	if workShift == nil {
		return grmi.NewBusinessError("不存在对应的人员班组， 请检查")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkShiftEffLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkShiftEffLstServiceImplement) DeleteOne(user *models.Usertab, workShiftNr int, levelId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkShiftEffLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkShiftEffLstServiceImplement) SelectOne(user *models.Usertab, workShiftNr int, levelId string) (*model.WorkShiftEffLst, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkShiftEffLstDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(workShiftNr, levelId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkShiftEffLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkShiftEffLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkShiftEffLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkShiftEffLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkShiftNr, entity.LevelId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	entity.WorkShiftNr = record.WorkShiftNr
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkShiftEffLstService.Insert
 *
 ******************************************************************************/
func (impl *WorkShiftEffLstServiceImplement) Insert(user *models.Usertab, entities *[]model.WorkShiftEffLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkShiftEffLstDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WorkShiftNr, entity.LevelId)
		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/WorkShiftEffLstService.Delete
 *
 ******************************************************************************/
func (impl *WorkShiftEffLstServiceImplement) Delete(user *models.Usertab, entities *[]model.WorkShiftEffLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkShiftEffLstDAO(session, user.Pid, user.Userid)

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

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

	condition := DefaultConditionOfWorkShiftEffLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewWorkShiftEffLstDAO(session, user.Pid, 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_LF_MOM_BACKEND/services/base/WorkShiftEffLstService.Update
 *
 ******************************************************************************/
func (impl *WorkShiftEffLstServiceImplement) Update(user *models.Usertab, entities *[]model.WorkShiftEffLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkShiftEffLstDAO(session, user.Pid, user.Userid)

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

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