// 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              : ArtRouteStepOdsLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
var DefaultConditionOfArtRouteStepOdsLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArtRouteStepOdsLst_ArtId.UrlParameterName:         grmi.NewConditionItem(meta.ArtRouteStepOdsLst_ArtId, grmi.Equal, true),
		meta.ArtRouteStepOdsLst_OperationNr.UrlParameterName:   grmi.NewConditionItem(meta.ArtRouteStepOdsLst_OperationNr, grmi.Equal, true),
		meta.ArtRouteStepOdsLst_Step.UrlParameterName:          grmi.NewConditionItem(meta.ArtRouteStepOdsLst_Step, grmi.Equal, true),
		meta.ArtRouteStepOdsLst_Pos.UrlParameterName:           grmi.NewConditionItem(meta.ArtRouteStepOdsLst_Pos, grmi.Equal, true),
		meta.ArtRouteStepOdsLst_Descr.UrlParameterName:         grmi.NewConditionItem(meta.ArtRouteStepOdsLst_Descr, grmi.Equal, false),
		meta.ArtRouteStepOdsLst_OdsId.UrlParameterName:         grmi.NewConditionItem(meta.ArtRouteStepOdsLst_OdsId, grmi.Equal, false),
		meta.ArtRouteStepOdsLst_OdsTypeId.UrlParameterName:     grmi.NewConditionItem(meta.ArtRouteStepOdsLst_OdsTypeId, grmi.Equal, false),
		meta.ArtRouteStepOdsLst_OdsContentIdx.UrlParameterName: grmi.NewConditionItem(meta.ArtRouteStepOdsLst_OdsContentIdx, grmi.Equal, false),
		meta.ArtRouteStepOdsLst_CtrlPara1.UrlParameterName:     grmi.NewConditionItem(meta.ArtRouteStepOdsLst_CtrlPara1, grmi.Equal, false),
		meta.ArtRouteStepOdsLst_CtrlPara2.UrlParameterName:     grmi.NewConditionItem(meta.ArtRouteStepOdsLst_CtrlPara2, grmi.Equal, false),
		meta.ArtRouteStepOdsLst_CtrlPara3.UrlParameterName:     grmi.NewConditionItem(meta.ArtRouteStepOdsLst_CtrlPara3, grmi.Equal, false),
		meta.ArtRouteStepOdsLst_CtrlPara4.UrlParameterName:     grmi.NewConditionItem(meta.ArtRouteStepOdsLst_CtrlPara4, grmi.Equal, false),
		meta.ArtRouteStepOdsLst_WorkPlaceNr.UrlParameterName:   grmi.NewConditionItem(meta.ArtRouteStepOdsLst_WorkPlaceNr, grmi.Equal, false),
		meta.ArtRouteStepOdsLst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.ArtRouteStepOdsLst_LastModify, grmi.Approximate, false),
		meta.ArtRouteStepOdsLst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.ArtRouteStepOdsLst_LastUser, grmi.Equal, false),
		meta.ArtRouteStepOdsLst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.ArtRouteStepOdsLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArtRouteStepOdsLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
var DefaultConditionOfArtRouteStepOdsLstAndPaging = grmi.NewCondition(
	DefaultConditionOfArtRouteStepOdsLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArtRouteStepOdsLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ArtRouteStepOdsLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
type ArtRouteStepOdsLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArtRouteStepOdsLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArtRouteStepOdsLstDAOImplement实例
 *
 * @Return Value             : ArtRouteStepOdsLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
func NewArtRouteStepOdsLstServiceImplement() *ArtRouteStepOdsLstServiceImplement {
	return &ArtRouteStepOdsLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/ods/ArtRouteStepOdsLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ArtRouteStepOdsLstServiceImplement) InsertOne(user *global.User, entity *model.ArtRouteStepOdsLst) error {

	grmi.Log(user, "/services/ods/implments/ArtRouteStepOdsLst.service.impl.go", "InsertOneArtRouteStepOdsLst", "插入一个ArtRouteStepOdsLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtRouteStepOdsLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ArtId, entity.OperationNr, entity.Step, 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/ArtRouteStepOdsLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArtRouteStepOdsLstServiceImplement) DeleteOne(user *global.User, artId string, operationNr int, step int, pos int) error {

	grmi.Log(user, "/services/ods/implments/ArtRouteStepOdsLst.service.impl.go", "DeleteOneArtRouteStepOdsLst", "删除一个ArtRouteStepOdsLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/ods/ArtRouteStepOdsLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ArtRouteStepOdsLstServiceImplement) SelectOne(user *global.User, artId string, operationNr int, step int, pos int) (*model.ArtRouteStepOdsLst, error) {

	grmi.Log(user, "/services/ods/implments/ArtRouteStepOdsLst.service.impl.go", "SelectOneArtRouteStepOdsLst", "查询一个ArtRouteStepOdsLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/ods/ArtRouteStepOdsLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArtRouteStepOdsLstServiceImplement) UpdateOne(user *global.User, entity *model.ArtRouteStepOdsLst) error {

	grmi.Log(user, "/services/ods/implments/ArtRouteStepOdsLst.service.impl.go", "UpdateOneArtRouteStepOdsLst", "修改一个ArtRouteStepOdsLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtRouteStepOdsLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ArtId, entity.OperationNr, entity.Step, 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/ArtRouteStepOdsLstService.Insert
 *
 ******************************************************************************/
func (impl *ArtRouteStepOdsLstServiceImplement) Insert(user *global.User, entities *[]model.ArtRouteStepOdsLst) error {

	grmi.Log(user, "/services/ods/implments/ArtRouteStepOdsLst.service.impl.go", "InsertArtRouteStepOdsLst", "插入多个ArtRouteStepOdsLst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ArtId, entity.OperationNr, entity.Step, 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/ArtRouteStepOdsLstService.Delete
 *
 ******************************************************************************/
func (impl *ArtRouteStepOdsLstServiceImplement) Delete(user *global.User, entities *[]model.ArtRouteStepOdsLst) error {

	grmi.Log(user, "/services/ods/implments/ArtRouteStepOdsLst.service.impl.go", "DeleteArtRouteStepOdsLst", "删除多个ArtRouteStepOdsLst")

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

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

	grmi.Log(user, "/services/ods/implments/ArtRouteStepOdsLst.service.impl.go", "SelectArtRouteStepOdsLst", "查询ArtRouteStepOdsLst")

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

	grmi.Log(user, "/services/ods/implments/ArtRouteStepOdsLst.service.impl.go", "UpdateArtRouteStepOdsLst", "修改多个ArtRouteStepOdsLst")

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

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

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