// 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              : SerialOrderStatusRecLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-12 14:25:19
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderStatusRecLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.SerialOrderStatusRecLst_SerialOrderId.UrlParameterName:        grmi.NewConditionItem(meta.SerialOrderStatusRecLst_SerialOrderId, grmi.Equal, true),
		meta.SerialOrderStatusRecLst_Pos.UrlParameterName:                  grmi.NewConditionItem(meta.SerialOrderStatusRecLst_Pos, grmi.Equal, true),
		meta.SerialOrderStatusRecLst_PrevStatus.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderStatusRecLst_PrevStatus, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_Status.UrlParameterName:               grmi.NewConditionItem(meta.SerialOrderStatusRecLst_Status, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_StartTime.UrlParameterName:            grmi.NewConditionItem(meta.SerialOrderStatusRecLst_StartTime, grmi.Approximate, false),
		meta.SerialOrderStatusRecLst_EndTime.UrlParameterName:              grmi.NewConditionItem(meta.SerialOrderStatusRecLst_EndTime, grmi.Approximate, false),
		meta.SerialOrderStatusRecLst_TriggerEvent.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderStatusRecLst_TriggerEvent, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_TriggerObjectId.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderStatusRecLst_TriggerObjectId, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_TriggerSubObjectId.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderStatusRecLst_TriggerSubObjectId, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_OutputStatus.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderStatusRecLst_OutputStatus, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_OutputEvent.UrlParameterName:          grmi.NewConditionItem(meta.SerialOrderStatusRecLst_OutputEvent, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_OutputEventMessageId.UrlParameterName: grmi.NewConditionItem(meta.SerialOrderStatusRecLst_OutputEventMessageId, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_OutputObjectId.UrlParameterName:       grmi.NewConditionItem(meta.SerialOrderStatusRecLst_OutputObjectId, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_OutputSubObjectId.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderStatusRecLst_OutputSubObjectId, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_Remark1.UrlParameterName:              grmi.NewConditionItem(meta.SerialOrderStatusRecLst_Remark1, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_Remark2.UrlParameterName:              grmi.NewConditionItem(meta.SerialOrderStatusRecLst_Remark2, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_OutputErrNum.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderStatusRecLst_OutputErrNum, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderStatusRecLst_LastModify, grmi.Approximate, false),
		meta.SerialOrderStatusRecLst_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.SerialOrderStatusRecLst_LastUser, grmi.Equal, false),
		meta.SerialOrderStatusRecLst_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderStatusRecLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : SerialOrderStatusRecLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-20 13:07:32
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderStatusRecLstAndPaging = grmi.NewCondition(
	DefaultConditionOfSerialOrderStatusRecLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : SerialOrderStatusRecLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : SerialOrderStatusRecLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-20 13:07:32
 *
 ******************************************************************************/
type SerialOrderStatusRecLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewSerialOrderStatusRecLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个SerialOrderStatusRecLstDAOImplement实例
 *
 * @Return Value             : SerialOrderStatusRecLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-20 13:07:32
 *
 ******************************************************************************/
func NewSerialOrderStatusRecLstServiceImplement() *SerialOrderStatusRecLstServiceImplement {
	return &SerialOrderStatusRecLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/SerialOrderStatusRecLstService.InsertOne
 *
 ******************************************************************************/
func (impl *SerialOrderStatusRecLstServiceImplement) InsertOne(user *models.Usertab, entity *model.SerialOrderStatusRecLst) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/SerialOrderStatusRecLstService.SelectOne
 *
 ******************************************************************************/
func (impl *SerialOrderStatusRecLstServiceImplement) SelectOne(user *models.Usertab, serialOrderId string, pos int) (*model.SerialOrderStatusRecLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/SerialOrderStatusRecLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *SerialOrderStatusRecLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.SerialOrderStatusRecLst) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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