// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/ods"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/ods"
	model "LAPP_ACURA_MOM_BACKEND/models/ods"
)

/******************************************************************************
 *
 * @Description              : OdsStepLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfOdsStepLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.OdsStepLst_OdsId.UrlParameterName:         grmi.NewConditionItem(meta.OdsStepLst_OdsId, grmi.Equal, true),
		meta.OdsStepLst_Pos.UrlParameterName:           grmi.NewConditionItem(meta.OdsStepLst_Pos, grmi.Equal, true),
		meta.OdsStepLst_Descr.UrlParameterName:         grmi.NewConditionItem(meta.OdsStepLst_Descr, grmi.Equal, false),
		meta.OdsStepLst_OdsTypeId.UrlParameterName:     grmi.NewConditionItem(meta.OdsStepLst_OdsTypeId, grmi.Equal, false),
		meta.OdsStepLst_OdsContentIdx.UrlParameterName: grmi.NewConditionItem(meta.OdsStepLst_OdsContentIdx, grmi.Equal, false),
		meta.OdsStepLst_CtrlPara1.UrlParameterName:     grmi.NewConditionItem(meta.OdsStepLst_CtrlPara1, grmi.Equal, false),
		meta.OdsStepLst_CtrlPara2.UrlParameterName:     grmi.NewConditionItem(meta.OdsStepLst_CtrlPara2, grmi.Equal, false),
		meta.OdsStepLst_CtrlPara3.UrlParameterName:     grmi.NewConditionItem(meta.OdsStepLst_CtrlPara3, grmi.Equal, false),
		meta.OdsStepLst_CtrlPara4.UrlParameterName:     grmi.NewConditionItem(meta.OdsStepLst_CtrlPara4, grmi.Equal, false),
		meta.OdsStepLst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.OdsStepLst_LastModify, grmi.Approximate, false),
		meta.OdsStepLst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.OdsStepLst_LastUser, grmi.Equal, false),
		meta.OdsStepLst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.OdsStepLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : OdsStepLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfOdsStepLstAndPaging = grmi.NewCondition(
	DefaultConditionOfOdsStepLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : OdsStepLstServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : OdsStepLst的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-23 10:43:27
*
******************************************************************************/
type OdsStepLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewOdsStepLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个OdsStepLstDAOImplement实例
 *
 * @Return Value             : OdsStepLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewOdsStepLstServiceImplement() *OdsStepLstServiceImplement {
	return &OdsStepLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/ods/OdsStepLstService.InsertOne
 *
 ******************************************************************************/
func (impl *OdsStepLstServiceImplement) InsertOne(user *global.User, entity *model.OdsStepLst) error {

	grmi.Log(user, "/services/ods/implments/ODS_OdsStepLst.service.impl.go", "InsertOneOdsStepLst", "插入一个OdsStepLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOdsStepLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.OdsId, 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/ods/OdsStepLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *OdsStepLstServiceImplement) DeleteOne(user *global.User, odsId string, pos int) error {

	grmi.Log(user, "/services/ods/implments/ODS_OdsStepLst.service.impl.go", "DeleteOneOdsStepLst", "删除一个OdsStepLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/ods/OdsStepLstService.SelectOne
 *
 ******************************************************************************/
func (impl *OdsStepLstServiceImplement) SelectOne(user *global.User, odsId string, pos int) (*model.OdsStepLst, error) {

	grmi.Log(user, "/services/ods/implments/ODS_OdsStepLst.service.impl.go", "SelectOneOdsStepLst", "查询一个OdsStepLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/ods/OdsStepLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *OdsStepLstServiceImplement) UpdateOne(user *global.User, entity *model.OdsStepLst) error {

	grmi.Log(user, "/services/ods/implments/ODS_OdsStepLst.service.impl.go", "UpdateOneOdsStepLst", "修改一个OdsStepLst")

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

	grmi.Log(user, "/services/ods/implments/ODS_OdsStepLst.service.impl.go", "InsertOdsStepLst", "插入多个OdsStepLst")

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

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

	grmi.Log(user, "/services/ods/implments/ODS_OdsStepLst.service.impl.go", "DeleteOdsStepLst", "删除多个OdsStepLst")

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

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

	grmi.Log(user, "/services/ods/implments/ODS_OdsStepLst.service.impl.go", "SelectOdsStepLst", "查询OdsStepLst")

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

	grmi.Log(user, "/services/ods/implments/ODS_OdsStepLst.service.impl.go", "UpdateOdsStepLst", "修改多个OdsStepLst")

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

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

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