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

package implments

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

/******************************************************************************
 *
 * @Description              : SerialOrderOPDetailStatusRecLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-11 10:00:53
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderOPDetailStatusRecLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.SerialOrderOPDetailStatusRecLst_DetailId.UrlParameterName:             grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_DetailId, grmi.Equal, true),
		meta.SerialOrderOPDetailStatusRecLst_Pos.UrlParameterName:                  grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_Pos, grmi.Equal, true),
		meta.SerialOrderOPDetailStatusRecLst_SerialOrderId.UrlParameterName:        grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_SerialOrderId, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_PO.UrlParameterName:                   grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_PO, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_SubArtId.UrlParameterName:             grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_SubArtId, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_StepNo.UrlParameterName:               grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_StepNo, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_OriginPos.UrlParameterName:            grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_OriginPos, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_StepType.UrlParameterName:             grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_StepType, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_PrevStatus.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_PrevStatus, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_Status.UrlParameterName:               grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_Status, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_TriggerEvent.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_TriggerEvent, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_TriggerObjectId.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_TriggerObjectId, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_TriggerSubObjectId.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_TriggerSubObjectId, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_OutputStatus.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_OutputStatus, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_OutputEvent.UrlParameterName:          grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_OutputEvent, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_OutputEventMessageId.UrlParameterName: grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_OutputEventMessageId, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_OutputObjectId.UrlParameterName:       grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_OutputObjectId, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_OutputSubObjectId.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_OutputSubObjectId, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_OutputErrNum.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_OutputErrNum, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_Remark1.UrlParameterName:              grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_Remark1, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_Remark2.UrlParameterName:              grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_Remark2, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_LastModify, grmi.Approximate, false),
		meta.SerialOrderOPDetailStatusRecLst_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_LastUser, grmi.Equal, false),
		meta.SerialOrderOPDetailStatusRecLst_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderOPDetailStatusRecLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/SerialOrderOPDetailStatusRecLstService.InsertOne
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailStatusRecLstServiceImplement) InsertOne(user *global.User, entity *model.SerialOrderOPDetailStatusRecLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderOPDetailStatusRecLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.DetailId, 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/om/SerialOrderOPDetailStatusRecLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailStatusRecLstServiceImplement) DeleteOne(user *global.User, detailId int, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/SerialOrderOPDetailStatusRecLstService.SelectOne
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailStatusRecLstServiceImplement) SelectOne(user *global.User, detailId int, pos int) (*model.SerialOrderOPDetailStatusRecLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/SerialOrderOPDetailStatusRecLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailStatusRecLstServiceImplement) UpdateOne(user *global.User, entity *model.SerialOrderOPDetailStatusRecLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderOPDetailStatusRecLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.DetailId, 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/om/SerialOrderOPDetailStatusRecLstService.Insert
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailStatusRecLstServiceImplement) Insert(user *global.User, entities *[]model.SerialOrderOPDetailStatusRecLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DetailId, 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/om/SerialOrderOPDetailStatusRecLstService.Delete
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailStatusRecLstServiceImplement) Delete(user *global.User, entities *[]model.SerialOrderOPDetailStatusRecLst) error {

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

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

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

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

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

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

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

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

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