// 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              : WorkPlaceStatus的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-21 10:27:00
 *
 ******************************************************************************/
var DefaultConditionOfWorkPlaceStatus = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkPlaceStatus_WorkPlaceNr.UrlParameterName:        grmi.NewConditionItem(meta.WorkPlaceStatus_WorkPlaceNr, grmi.Equal, true),
		meta.WorkPlaceStatus_Status.UrlParameterName:             grmi.NewConditionItem(meta.WorkPlaceStatus_Status, grmi.Equal, false),
		meta.WorkPlaceStatus_TriggerEvent.UrlParameterName:       grmi.NewConditionItem(meta.WorkPlaceStatus_TriggerEvent, grmi.Equal, false),
		meta.WorkPlaceStatus_TriggerObjectId.UrlParameterName:    grmi.NewConditionItem(meta.WorkPlaceStatus_TriggerObjectId, grmi.Equal, false),
		meta.WorkPlaceStatus_TriggerSubObjectId.UrlParameterName: grmi.NewConditionItem(meta.WorkPlaceStatus_TriggerSubObjectId, grmi.Equal, false),
		meta.WorkPlaceStatus_LastModify.UrlParameterName:         grmi.NewConditionItem(meta.WorkPlaceStatus_LastModify, grmi.Approximate, false),
		meta.WorkPlaceStatus_LastUser.UrlParameterName:           grmi.NewConditionItem(meta.WorkPlaceStatus_LastUser, grmi.Equal, false),
		meta.WorkPlaceStatus_CreateTime.UrlParameterName:         grmi.NewConditionItem(meta.WorkPlaceStatus_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkPlaceStatusService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkPlaceStatusServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkPlaceStatus) error {

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

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

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkPlaceStatusDAO(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/WorkPlaceStatusService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkPlaceStatusServiceImplement) SelectOne(user *models.Usertab, workPlaceNr int) (*model.WorkPlaceStatus, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkPlaceStatusDAO(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/WorkPlaceStatusService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkPlaceStatusServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkPlaceStatus) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkPlaceStatusService.Insert
 *
 ******************************************************************************/
func (impl *WorkPlaceStatusServiceImplement) Insert(user *models.Usertab, entities *[]model.WorkPlaceStatus) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkPlaceStatusDAO(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/WorkPlaceStatusService.Delete
 *
 ******************************************************************************/
func (impl *WorkPlaceStatusServiceImplement) Delete(user *models.Usertab, entities *[]model.WorkPlaceStatus) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkPlaceStatusDAO(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/WorkPlaceStatusService.Select
 *
 ******************************************************************************/
func (impl *WorkPlaceStatusServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkPlaceStatusDAO(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
}
