// 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              : WorkPlace的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-21 10:27:00
 *
 ******************************************************************************/
var DefaultConditionOfWorkPlace = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkPlace_WorkPlaceNr.UrlParameterName:       grmi.NewConditionItem(meta.WorkPlace_WorkPlaceNr, grmi.Equal, true),
		meta.WorkPlace_WorkPlaceId.UrlParameterName:       grmi.NewConditionItem(meta.WorkPlace_WorkPlaceId, grmi.Equal, false),
		meta.WorkPlace_Descr.UrlParameterName:             grmi.NewConditionItem(meta.WorkPlace_Descr, grmi.Equal, false),
		meta.WorkPlace_WorkPlaceGrpNr.UrlParameterName:    grmi.NewConditionItem(meta.WorkPlace_WorkPlaceGrpNr, grmi.Equal, false),
		meta.WorkPlace_WorkPlaceType.UrlParameterName:     grmi.NewConditionItem(meta.WorkPlace_WorkPlaceType, grmi.Equal, false),
		meta.WorkPlace_CapacityType.UrlParameterName:      grmi.NewConditionItem(meta.WorkPlace_CapacityType, grmi.Equal, false),
		meta.WorkPlace_CapacityFactor.UrlParameterName:    grmi.NewConditionItem(meta.WorkPlace_CapacityFactor, grmi.Equal, false),
		meta.WorkPlace_PlanEfficiency.UrlParameterName:    grmi.NewConditionItem(meta.WorkPlace_PlanEfficiency, grmi.Equal, false),
		meta.WorkPlace_CostRate.UrlParameterName:          grmi.NewConditionItem(meta.WorkPlace_CostRate, grmi.Equal, false),
		meta.WorkPlace_WeekModelNr.UrlParameterName:       grmi.NewConditionItem(meta.WorkPlace_WeekModelNr, grmi.Equal, false),
		meta.WorkPlace_WorkCalendarNr.UrlParameterName:    grmi.NewConditionItem(meta.WorkPlace_WorkCalendarNr, grmi.Equal, false),
		meta.WorkPlace_CostCenterId.UrlParameterName:      grmi.NewConditionItem(meta.WorkPlace_CostCenterId, grmi.Equal, false),
		meta.WorkPlace_LocationId.UrlParameterName:        grmi.NewConditionItem(meta.WorkPlace_LocationId, grmi.Equal, false),
		meta.WorkPlace_RequireSkillRank1.UrlParameterName: grmi.NewConditionItem(meta.WorkPlace_RequireSkillRank1, grmi.Equal, false),
		meta.WorkPlace_RequireSkillRank2.UrlParameterName: grmi.NewConditionItem(meta.WorkPlace_RequireSkillRank2, grmi.Equal, false),
		meta.WorkPlace_RequireSkillRank3.UrlParameterName: grmi.NewConditionItem(meta.WorkPlace_RequireSkillRank3, grmi.Equal, false),
		meta.WorkPlace_RequireSkillRank4.UrlParameterName: grmi.NewConditionItem(meta.WorkPlace_RequireSkillRank4, grmi.Equal, false),
		meta.WorkPlace_TriggerFrom.UrlParameterName:       grmi.NewConditionItem(meta.WorkPlace_TriggerFrom, grmi.Equal, false),
		meta.WorkPlace_TriggerDependOn.UrlParameterName:   grmi.NewConditionItem(meta.WorkPlace_TriggerDependOn, grmi.Equal, false),
		meta.WorkPlace_MonitorPalletInPlace.Name:          grmi.NewConditionItem(meta.WorkPlace_MonitorPalletInPlace, grmi.Equal, false),
		meta.WorkPlace_MonitorManualRelease.Name:          grmi.NewConditionItem(meta.WorkPlace_MonitorManualRelease, grmi.Equal, false),
		meta.WorkPlace_MonitorProblemPieceRelease.Name:    grmi.NewConditionItem(meta.WorkPlace_MonitorProblemPieceRelease, grmi.Equal, false),
		meta.WorkPlace_LastModify.UrlParameterName:        grmi.NewConditionItem(meta.WorkPlace_LastModify, grmi.Approximate, false),
		meta.WorkPlace_LastUser.UrlParameterName:          grmi.NewConditionItem(meta.WorkPlace_LastUser, grmi.Equal, false),
		meta.WorkPlace_CreateTime.UrlParameterName:        grmi.NewConditionItem(meta.WorkPlace_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkPlace的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-21 10:27:00
 *
 ******************************************************************************/
var DefaultConditionOfWorkPlaceAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkPlace.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WorkPlaceServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WorkPlace的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-21 10:27:00
 *
 ******************************************************************************/
type WorkPlaceServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkPlaceDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkPlaceDAOImplement实例
 *
 * @Return Value             : WorkPlaceDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-21 10:27:00
 *
 ******************************************************************************/
func NewWorkPlaceServiceImplement() *WorkPlaceServiceImplement {
	return &WorkPlaceServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkPlaceService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkPlaceServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkPlace) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkPlaceDAO(session, user.Pid, user.Userid)
	err := dao.InsertOne(entity)
	if err != nil {
		return err
	}
	if entity.TriggerFrom == model.TRIGGER_FROM_BARCODE {
		entity.TriggerDependOn = model.TRIGGER_DEPEND_ON_UNITSN
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkPlaceService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkPlaceServiceImplement) DeleteOne(user *models.Usertab, workPlaceNr int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkPlaceService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkPlaceServiceImplement) SelectOne(user *models.Usertab, workPlaceNr int) (*model.WorkPlace, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkPlaceService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkPlaceServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkPlace) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkPlaceService.Insert
 *
 ******************************************************************************/
func (impl *WorkPlaceServiceImplement) Insert(user *models.Usertab, entities *[]model.WorkPlace) error {

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

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

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

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

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

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

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

	condition := DefaultConditionOfWorkPlaceAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewWorkPlaceDAO(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/WorkPlaceService.Update
 *
 ******************************************************************************/
func (impl *WorkPlaceServiceImplement) Update(user *models.Usertab, entities *[]model.WorkPlace) error {

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

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

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

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