// 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              : StepTypeRelLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
var DefaultConditionOfStepTypeRelLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.StepTypeRelLst_StepTypeName.UrlParameterName: grmi.NewConditionItem(meta.StepTypeRelLst_StepTypeName, grmi.Equal, true),
		meta.StepTypeRelLst_PID.UrlParameterName:          grmi.NewConditionItem(meta.StepTypeRelLst_PID, grmi.Equal, true),
		meta.StepTypeRelLst_OriginID.UrlParameterName:     grmi.NewConditionItem(meta.StepTypeRelLst_OriginID, grmi.Equal, false),
		meta.StepTypeRelLst_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.StepTypeRelLst_LastModify, grmi.Approximate, false),
		meta.StepTypeRelLst_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.StepTypeRelLst_LastUser, grmi.Equal, false),
		meta.StepTypeRelLst_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.StepTypeRelLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : StepTypeRelLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
var DefaultConditionOfStepTypeRelLstAndPaging = grmi.NewCondition(
	DefaultConditionOfStepTypeRelLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : StepTypeRelLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : StepTypeRelLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
type StepTypeRelLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewStepTypeRelLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个StepTypeRelLstDAOImplement实例
 *
 * @Return Value             : StepTypeRelLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
func NewStepTypeRelLstServiceImplement() *StepTypeRelLstServiceImplement {
	return &StepTypeRelLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/StepTypeRelLstService.InsertOne
 *
 ******************************************************************************/
func (impl *StepTypeRelLstServiceImplement) InsertOne(user *global.User, entity *model.StepTypeRelLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStepTypeRelLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.StepTypeName, entity.PID)
	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/StepTypeRelLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *StepTypeRelLstServiceImplement) DeleteOne(user *global.User, stepTypeName string, pID string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/StepTypeRelLstService.SelectOne
 *
 ******************************************************************************/
func (impl *StepTypeRelLstServiceImplement) SelectOne(user *global.User, stepTypeName string, pID string) (*model.StepTypeRelLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/StepTypeRelLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *StepTypeRelLstServiceImplement) UpdateOne(user *global.User, entity *model.StepTypeRelLst) error {

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

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()

	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewStepTypeRelLstDAO(session, user.PlantNr, user.UserId)
	li := *entities
	if len(li) == 0 {
		_ = session.Rollback()
		return grmi.NewBusinessError("传入数据为空")
	}
	pid := li[0].PID
	originId := li[0].OriginID

	for _, entity := range *entities {
		if entity.PID != pid {
			_ = session.Rollback()
			return grmi.NewBusinessError("父级ID不一致")
		}
		if entity.OriginID != originId {
			_ = session.Rollback()
			return grmi.NewBusinessError("最上级ID不一致")
		}
	}
	err := dao.DeleteWhere([]grmi.Predicate{meta.StepTypeRelLst_PID.NewPredicate(grmi.Equal, pid)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = dao.Insert(entities)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/StepTypeRelLstService.Delete
 *
 ******************************************************************************/
func (impl *StepTypeRelLstServiceImplement) Delete(user *global.User, entities *[]model.StepTypeRelLst) error {

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

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

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

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

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

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

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

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

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