// 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              : StepType的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
var DefaultConditionOfStepType = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.StepType_StepTypeName.UrlParameterName: grmi.NewConditionItem(meta.StepType_StepTypeName, grmi.Equal, true),
		meta.StepType_StepTypeDesc.UrlParameterName: grmi.NewConditionItem(meta.StepType_StepTypeDesc, grmi.Equal, false),
		meta.StepType_STCtrlPara1.UrlParameterName:  grmi.NewConditionItem(meta.StepType_STCtrlPara1, grmi.Equal, false),
		meta.StepType_STCtrlPara2.UrlParameterName:  grmi.NewConditionItem(meta.StepType_STCtrlPara2, grmi.Equal, false),
		meta.StepType_STCtrlPara3.UrlParameterName:  grmi.NewConditionItem(meta.StepType_STCtrlPara3, grmi.Equal, false),
		meta.StepType_STCtrlPara4.UrlParameterName:  grmi.NewConditionItem(meta.StepType_STCtrlPara4, grmi.Equal, false),
		meta.StepType_STCtrlPara5.UrlParameterName:  grmi.NewConditionItem(meta.StepType_STCtrlPara5, grmi.Equal, false),
		meta.StepType_STCtrlPara6.UrlParameterName:  grmi.NewConditionItem(meta.StepType_STCtrlPara6, grmi.Equal, false),
		meta.StepType_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.StepType_LastModify, grmi.Approximate, false),
		meta.StepType_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.StepType_LastUser, grmi.Equal, false),
		meta.StepType_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.StepType_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/StepTypeService.InsertOne
 *
 ******************************************************************************/
func (impl *StepTypeServiceImplement) InsertOne(user *models.Usertab, entity *model.StepType) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStepTypeDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.StepTypeName)
	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/StepTypeService.DeleteOne
 *
 ******************************************************************************/
func (impl *StepTypeServiceImplement) DeleteOne(user *models.Usertab, stepTypeName string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/StepTypeService.SelectOne
 *
 ******************************************************************************/
func (impl *StepTypeServiceImplement) SelectOne(user *models.Usertab, stepTypeName string) (*model.StepType, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/StepTypeService.UpdateOne
 *
 ******************************************************************************/
func (impl *StepTypeServiceImplement) UpdateOne(user *models.Usertab, entity *model.StepType) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStepTypeDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.StepTypeName)
	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/StepTypeService.Insert
 *
 ******************************************************************************/
func (impl *StepTypeServiceImplement) Insert(user *models.Usertab, entities *[]model.StepType) error {

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

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

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

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

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

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

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

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

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

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

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

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