// 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              : SerialOrderPOStatusRecLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-29 17:19:37
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderPOStatusRecLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.SerialOrderPOStatusRecLst_SerialOrderId.UrlParameterName:        grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_SerialOrderId, grmi.Equal, true),
		meta.SerialOrderPOStatusRecLst_PO.UrlParameterName:                   grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_PO, grmi.Equal, true),
		meta.SerialOrderPOStatusRecLst_Pos.UrlParameterName:                  grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_Pos, grmi.Equal, true),
		meta.SerialOrderPOStatusRecLst_WorkPlaceNr.UrlParameterName:          grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_WorkPlaceNr, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_PrevStatus.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_PrevStatus, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_Status.UrlParameterName:               grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_Status, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_TriggerEvent.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_TriggerEvent, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_TriggerObjectId.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_TriggerObjectId, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_TriggerSubObjectId.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_TriggerSubObjectId, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_OutputStatus.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_OutputStatus, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_OutputEvent.UrlParameterName:          grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_OutputEvent, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_OutputEventMessageId.UrlParameterName: grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_OutputEventMessageId, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_OutputObjectId.UrlParameterName:       grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_OutputObjectId, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_OutputSubObjectId.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_OutputSubObjectId, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_OutputErrNum.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_OutputErrNum, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_Remark1.UrlParameterName:              grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_Remark1, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_Remark2.UrlParameterName:              grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_Remark2, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_LastModify, grmi.Approximate, false),
		meta.SerialOrderPOStatusRecLst_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_LastUser, grmi.Equal, false),
		meta.SerialOrderPOStatusRecLst_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderPOStatusRecLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : SerialOrderPOStatusRecLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-29 17:19:37
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderPOStatusRecLstAndPaging = grmi.NewCondition(
	DefaultConditionOfSerialOrderPOStatusRecLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : SerialOrderPOStatusRecLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : SerialOrderPOStatusRecLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-29 17:19:37
 *
 ******************************************************************************/
type SerialOrderPOStatusRecLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewSerialOrderPOStatusRecLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个SerialOrderPOStatusRecLstDAOImplement实例
 *
 * @Return Value             : SerialOrderPOStatusRecLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-29 17:19:37
 *
 ******************************************************************************/
func NewSerialOrderPOStatusRecLstServiceImplement() *SerialOrderPOStatusRecLstServiceImplement {
	return &SerialOrderPOStatusRecLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/SerialOrderPOStatusRecLstService.InsertOne
 *
 ******************************************************************************/
func (impl *SerialOrderPOStatusRecLstServiceImplement) InsertOne(user *global.User, entity *model.SerialOrderPOStatusRecLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderPOStatusRecLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.SerialOrderId, entity.PO, 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/SerialOrderPOStatusRecLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *SerialOrderPOStatusRecLstServiceImplement) DeleteOne(user *global.User, serialOrderId string, pO string, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/SerialOrderPOStatusRecLstService.SelectOne
 *
 ******************************************************************************/
func (impl *SerialOrderPOStatusRecLstServiceImplement) SelectOne(user *global.User, serialOrderId string, pO string, pos int) (*model.SerialOrderPOStatusRecLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/SerialOrderPOStatusRecLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *SerialOrderPOStatusRecLstServiceImplement) UpdateOne(user *global.User, entity *model.SerialOrderPOStatusRecLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderPOStatusRecLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.SerialOrderId, entity.PO, 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/SerialOrderPOStatusRecLstService.Insert
 *
 ******************************************************************************/
func (impl *SerialOrderPOStatusRecLstServiceImplement) Insert(user *global.User, entities *[]model.SerialOrderPOStatusRecLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.SerialOrderId, entity.PO, 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/SerialOrderPOStatusRecLstService.Delete
 *
 ******************************************************************************/
func (impl *SerialOrderPOStatusRecLstServiceImplement) Delete(user *global.User, entities *[]model.SerialOrderPOStatusRecLst) error {

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

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

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

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

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

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

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

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

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