// 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              : SerialOrderStepLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-11 10:00:53
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderStepLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.SerialOrderStepLst_SerialOrderId.UrlParameterName: grmi.NewConditionItem(meta.SerialOrderStepLst_SerialOrderId, grmi.Equal, true),
		meta.SerialOrderStepLst_OperationId.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderStepLst_OperationId, grmi.Equal, true),
		meta.SerialOrderStepLst_PO.UrlParameterName:            grmi.NewConditionItem(meta.SerialOrderStepLst_PO, grmi.Equal, false),
		meta.SerialOrderStepLst_StepNo.UrlParameterName:        grmi.NewConditionItem(meta.SerialOrderStepLst_StepNo, grmi.Equal, false),
		meta.SerialOrderStepLst_Pos.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderStepLst_Pos, grmi.Equal, false),
		meta.SerialOrderStepLst_Status.UrlParameterName:        grmi.NewConditionItem(meta.SerialOrderStepLst_Status, grmi.Equal, false),
		meta.SerialOrderStepLst_StepType.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderStepLst_StepType, grmi.Equal, false),
		meta.SerialOrderStepLst_StepDesc.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderStepLst_StepDesc, grmi.Equal, false),
		meta.SerialOrderStepLst_SubArtId.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderStepLst_SubArtId, grmi.Equal, false),
		meta.SerialOrderStepLst_AttrCode.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderStepLst_AttrCode, grmi.Equal, false),
		meta.SerialOrderStepLst_AttrValue.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderStepLst_AttrValue, grmi.Equal, false),
		meta.SerialOrderStepLst_SSCtrlPara1.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderStepLst_SSCtrlPara1, grmi.Equal, false),
		meta.SerialOrderStepLst_SSCtrlPara2.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderStepLst_SSCtrlPara2, grmi.Equal, false),
		meta.SerialOrderStepLst_SSCtrlPara3.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderStepLst_SSCtrlPara3, grmi.Equal, false),
		meta.SerialOrderStepLst_SSCtrlPara4.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderStepLst_SSCtrlPara4, grmi.Equal, false),
		meta.SerialOrderStepLst_SSCtrlPara5.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderStepLst_SSCtrlPara5, grmi.Equal, false),
		meta.SerialOrderStepLst_SSCtrlPara6.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderStepLst_SSCtrlPara6, grmi.Equal, false),
		meta.SerialOrderStepLst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderStepLst_LastModify, grmi.Approximate, false),
		meta.SerialOrderStepLst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderStepLst_LastUser, grmi.Equal, false),
		meta.SerialOrderStepLst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderStepLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/SerialOrderStepLstService.InsertOne
 *
 ******************************************************************************/
func (impl *SerialOrderStepLstServiceImplement) InsertOne(user *models.Usertab, entity *model.SerialOrderStepLst) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/SerialOrderStepLstService.SelectOne
 *
 ******************************************************************************/
func (impl *SerialOrderStepLstServiceImplement) SelectOne(user *models.Usertab, serialOrderId string, operationId int) (*model.SerialOrderStepLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/SerialOrderStepLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *SerialOrderStepLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.SerialOrderStepLst) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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