// 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              : SnrOccupy的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-06-22 11:26:46
 *
 ******************************************************************************/
var DefaultConditionOfSnrOccupy = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.SnrOccupy_OccupyNr.UrlParameterName:        grmi.NewConditionItem(meta.SnrOccupy_OccupyNr, grmi.Equal, true),
		meta.SnrOccupy_SnrId.UrlParameterName:           grmi.NewConditionItem(meta.SnrOccupy_SnrId, grmi.Equal, false),
		meta.SnrOccupy_ItemNr.UrlParameterName:          grmi.NewConditionItem(meta.SnrOccupy_ItemNr, grmi.Equal, false),
		meta.SnrOccupy_BusinessObjType.UrlParameterName: grmi.NewConditionItem(meta.SnrOccupy_BusinessObjType, grmi.Equal, false),
		meta.SnrOccupy_BusinessObjId.UrlParameterName:   grmi.NewConditionItem(meta.SnrOccupy_BusinessObjId, grmi.Equal, false),
		meta.SnrOccupy_SnrData.UrlParameterName:         grmi.NewConditionItem(meta.SnrOccupy_SnrData, grmi.Equal, false),
		meta.SnrOccupy_Status.UrlParameterName:          grmi.NewConditionItem(meta.SnrOccupy_Status, grmi.Equal, false),
		meta.SnrOccupy_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.SnrOccupy_LastModify, grmi.Approximate, false),
		meta.SnrOccupy_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.SnrOccupy_LastUser, grmi.Equal, false),
		meta.SnrOccupy_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.SnrOccupy_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : SnrOccupy的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-06-22 11:26:46
 *
 ******************************************************************************/
var DefaultConditionOfSnrOccupyAndPaging = grmi.NewCondition(
	DefaultConditionOfSnrOccupy.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : SnrOccupyServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : SnrOccupy的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-06-22 11:26:46
 *
 ******************************************************************************/
type SnrOccupyServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewSnrOccupyDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个SnrOccupyDAOImplement实例
 *
 * @Return Value             : SnrOccupyDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-06-22 11:26:46
 *
 ******************************************************************************/
func NewSnrOccupyServiceImplement() *SnrOccupyServiceImplement {
	return &SnrOccupyServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/SnrOccupyService.InsertOne
 *
 ******************************************************************************/
func (impl *SnrOccupyServiceImplement) InsertOne(user *global.User, entity *model.SnrOccupy) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSnrOccupyDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.OccupyNr)
	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/SnrOccupyService.DeleteOne
 *
 ******************************************************************************/
func (impl *SnrOccupyServiceImplement) DeleteOne(user *global.User, occupyNr int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/SnrOccupyService.SelectOne
 *
 ******************************************************************************/
func (impl *SnrOccupyServiceImplement) SelectOne(user *global.User, occupyNr int) (*model.SnrOccupy, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/SnrOccupyService.UpdateOne
 *
 ******************************************************************************/
func (impl *SnrOccupyServiceImplement) UpdateOne(user *global.User, entity *model.SnrOccupy) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSnrOccupyDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.OccupyNr)
	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/SnrOccupyService.Insert
 *
 ******************************************************************************/
func (impl *SnrOccupyServiceImplement) Insert(user *global.User, entities *[]model.SnrOccupy) error {

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

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

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

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.OccupyNr)
		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/SnrOccupyService.Select
 *
 ******************************************************************************/
func (impl *SnrOccupyServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.SnrOccupy, error) {

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

	condition := DefaultConditionOfSnrOccupyAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewSnrOccupyDAO(session, user.PlantNr, user.UserId)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/SnrOccupyService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *SnrOccupyServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

	condition := DefaultConditionOfSnrOccupyAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewSnrOccupyDAO(session, user.PlantNr, user.UserId)
	condition.Fill(urlParameters)
	result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/SnrOccupyService.Update
 *
 ******************************************************************************/
func (impl *SnrOccupyServiceImplement) Update(user *global.User, entities *[]model.SnrOccupy) error {

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

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

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

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