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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/om"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/om"
	model "LAPP_LF_MOM_BACKEND/models/om"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : SerialOrderStepStatusRecLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-11 10:00:53
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderStepStatusRecLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.SerialOrderStepStatusRecLst_SerialOrderId.UrlParameterName:        grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_SerialOrderId, grmi.Equal, true),
		meta.SerialOrderStepStatusRecLst_OperationId.UrlParameterName:          grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_OperationId, grmi.Equal, true),
		meta.SerialOrderStepStatusRecLst_Pos.UrlParameterName:                  grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_Pos, grmi.Equal, true),
		meta.SerialOrderStepStatusRecLst_PO.UrlParameterName:                   grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_PO, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_StepNo.UrlParameterName:               grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_StepNo, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_PrevStatus.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_PrevStatus, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_Status.UrlParameterName:               grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_Status, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_TriggerEvent.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_TriggerEvent, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_TriggerObjectId.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_TriggerObjectId, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_TriggerSubObjectId.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_TriggerSubObjectId, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_OutputStatus.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_OutputStatus, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_OutputEvent.UrlParameterName:          grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_OutputEvent, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_OutputEventMessageId.UrlParameterName: grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_OutputEventMessageId, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_OutputObjectId.UrlParameterName:       grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_OutputObjectId, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_OutputSubObjectId.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_OutputSubObjectId, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_OutputErrNum.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_OutputErrNum, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_Remark1.UrlParameterName:              grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_Remark1, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_Remark2.UrlParameterName:              grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_Remark2, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_LastModify, grmi.Approximate, false),
		meta.SerialOrderStepStatusRecLst_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_LastUser, grmi.Equal, false),
		meta.SerialOrderStepStatusRecLst_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderStepStatusRecLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : SerialOrderStepStatusRecLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-11 10:00:53
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderStepStatusRecLstAndPaging = grmi.NewCondition(
	DefaultConditionOfSerialOrderStepStatusRecLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : SerialOrderStepStatusRecLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : SerialOrderStepStatusRecLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-11 10:00:53
 *
 ******************************************************************************/
type SerialOrderStepStatusRecLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewSerialOrderStepStatusRecLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个SerialOrderStepStatusRecLstDAOImplement实例
 *
 * @Return Value             : SerialOrderStepStatusRecLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-11 10:00:53
 *
 ******************************************************************************/
func NewSerialOrderStepStatusRecLstServiceImplement() *SerialOrderStepStatusRecLstServiceImplement {
	return &SerialOrderStepStatusRecLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/SerialOrderStepStatusRecLstService.InsertOne
 *
 ******************************************************************************/
func (impl *SerialOrderStepStatusRecLstServiceImplement) InsertOne(user *models.Usertab, entity *model.SerialOrderStepStatusRecLst) error {

	grmi.Log(user, "/services/om/implments/SerialOrderStepStatusRecLst.service.impl.go", "InsertOneSerialOrderStepStatusRecLst", "插入一个SerialOrderStepStatusRecLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderStepStatusRecLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.SerialOrderId, entity.OperationId, 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_LF_MOM_BACKEND/services/om/SerialOrderStepStatusRecLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *SerialOrderStepStatusRecLstServiceImplement) DeleteOne(user *models.Usertab, serialOrderId string, operationId int, pos int) error {

	grmi.Log(user, "/services/om/implments/SerialOrderStepStatusRecLst.service.impl.go", "DeleteOneSerialOrderStepStatusRecLst", "删除一个SerialOrderStepStatusRecLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/SerialOrderStepStatusRecLstService.SelectOne
 *
 ******************************************************************************/
func (impl *SerialOrderStepStatusRecLstServiceImplement) SelectOne(user *models.Usertab, serialOrderId string, operationId int, pos int) (*model.SerialOrderStepStatusRecLst, error) {

	grmi.Log(user, "/services/om/implments/SerialOrderStepStatusRecLst.service.impl.go", "SelectOneSerialOrderStepStatusRecLst", "查询一个SerialOrderStepStatusRecLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/SerialOrderStepStatusRecLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *SerialOrderStepStatusRecLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.SerialOrderStepStatusRecLst) error {

	grmi.Log(user, "/services/om/implments/SerialOrderStepStatusRecLst.service.impl.go", "UpdateOneSerialOrderStepStatusRecLst", "修改一个SerialOrderStepStatusRecLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderStepStatusRecLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.SerialOrderId, entity.OperationId, 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_LF_MOM_BACKEND/services/om/SerialOrderStepStatusRecLstService.Insert
 *
 ******************************************************************************/
func (impl *SerialOrderStepStatusRecLstServiceImplement) Insert(user *models.Usertab, entities *[]model.SerialOrderStepStatusRecLst) error {

	grmi.Log(user, "/services/om/implments/SerialOrderStepStatusRecLst.service.impl.go", "InsertSerialOrderStepStatusRecLst", "插入多个SerialOrderStepStatusRecLst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.SerialOrderId, entity.OperationId, 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_LF_MOM_BACKEND/services/om/SerialOrderStepStatusRecLstService.Delete
 *
 ******************************************************************************/
func (impl *SerialOrderStepStatusRecLstServiceImplement) Delete(user *models.Usertab, entities *[]model.SerialOrderStepStatusRecLst) error {

	grmi.Log(user, "/services/om/implments/SerialOrderStepStatusRecLst.service.impl.go", "DeleteSerialOrderStepStatusRecLst", "删除多个SerialOrderStepStatusRecLst")

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

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

	grmi.Log(user, "/services/om/implments/SerialOrderStepStatusRecLst.service.impl.go", "SelectSerialOrderStepStatusRecLst", "查询SerialOrderStepStatusRecLst")

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

	grmi.Log(user, "/services/om/implments/SerialOrderStepStatusRecLst.service.impl.go", "UpdateSerialOrderStepStatusRecLst", "修改多个SerialOrderStepStatusRecLst")

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

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

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