// 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              : WorkPlaceStatusRecLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-21 10:27:00
 *
 ******************************************************************************/
var DefaultConditionOfWorkPlaceStatusRecLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkPlaceStatusRecLst_WorkPlaceNr.UrlParameterName:          grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_WorkPlaceNr, grmi.Equal, true),
		meta.WorkPlaceStatusRecLst_Pos.UrlParameterName:                  grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_Pos, grmi.Equal, true),
		meta.WorkPlaceStatusRecLst_PrevStatus.UrlParameterName:           grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_PrevStatus, grmi.Equal, false),
		meta.WorkPlaceStatusRecLst_Status.UrlParameterName:               grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_Status, grmi.Equal, false),
		meta.WorkPlaceStatusRecLst_StartTime.UrlParameterName:            grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_StartTime, grmi.Approximate, false),
		meta.WorkPlaceStatusRecLst_EndTime.UrlParameterName:              grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_EndTime, grmi.Approximate, false),
		meta.WorkPlaceStatusRecLst_TriggerEvent.UrlParameterName:         grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_TriggerEvent, grmi.Equal, false),
		meta.WorkPlaceStatusRecLst_TriggerObjectId.UrlParameterName:      grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_TriggerObjectId, grmi.Equal, false),
		meta.WorkPlaceStatusRecLst_TriggerSubObjectId.UrlParameterName:   grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_TriggerSubObjectId, grmi.Equal, false),
		meta.WorkPlaceStatusRecLst_OutputStatus.UrlParameterName:         grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_OutputStatus, grmi.Equal, false),
		meta.WorkPlaceStatusRecLst_OutputEvent.UrlParameterName:          grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_OutputEvent, grmi.Equal, false),
		meta.WorkPlaceStatusRecLst_OutputEventMessageId.UrlParameterName: grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_OutputEventMessageId, grmi.Equal, false),
		meta.WorkPlaceStatusRecLst_OutputObjectId.UrlParameterName:       grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_OutputObjectId, grmi.Equal, false),
		meta.WorkPlaceStatusRecLst_OutputSubObjectId.UrlParameterName:    grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_OutputSubObjectId, grmi.Equal, false),
		meta.WorkPlaceStatusRecLst_Remark1.UrlParameterName:              grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_Remark1, grmi.Equal, false),
		meta.WorkPlaceStatusRecLst_Remark2.UrlParameterName:              grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_Remark2, grmi.Equal, false),
		meta.WorkPlaceStatusRecLst_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_LastModify, grmi.Approximate, false),
		meta.WorkPlaceStatusRecLst_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_LastUser, grmi.Equal, false),
		meta.WorkPlaceStatusRecLst_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.WorkPlaceStatusRecLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlaceStatusRecLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkPlaceStatusRecLstServiceImplement) InsertOne(user *global.User, entity *model.WorkPlaceStatusRecLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkPlaceStatusRecLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.WorkPlaceNr, entity.Pos)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlaceStatusRecLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkPlaceStatusRecLstServiceImplement) DeleteOne(user *global.User, workPlaceNr int, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlaceStatusRecLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkPlaceStatusRecLstServiceImplement) SelectOne(user *global.User, workPlaceNr int, pos int) (*model.WorkPlaceStatusRecLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlaceStatusRecLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkPlaceStatusRecLstServiceImplement) UpdateOne(user *global.User, entity *model.WorkPlaceStatusRecLst) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlaceStatusRecLstService.Insert
 *
 ******************************************************************************/
func (impl *WorkPlaceStatusRecLstServiceImplement) Insert(user *global.User, entities *[]model.WorkPlaceStatusRecLst) error {

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

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

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

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

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

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

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

	condition := DefaultConditionOfWorkPlaceStatusRecLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewWorkPlaceStatusRecLstDAO(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/WorkPlaceStatusRecLstService.Update
 *
 ******************************************************************************/
func (impl *WorkPlaceStatusRecLstServiceImplement) Update(user *global.User, entities *[]model.WorkPlaceStatusRecLst) error {

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

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

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

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