// 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              : ProdOrderStatusRecLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfProdOrderStatusRecLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ProdOrderStatusRecLst_ProdOrderId.UrlParameterName:          grmi.NewConditionItem(meta.ProdOrderStatusRecLst_ProdOrderId, grmi.Equal, true),
		meta.ProdOrderStatusRecLst_Pos.UrlParameterName:                  grmi.NewConditionItem(meta.ProdOrderStatusRecLst_Pos, grmi.Equal, true),
		meta.ProdOrderStatusRecLst_PrevStatus.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrderStatusRecLst_PrevStatus, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_Status.UrlParameterName:               grmi.NewConditionItem(meta.ProdOrderStatusRecLst_Status, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_StartTime.UrlParameterName:            grmi.NewConditionItem(meta.ProdOrderStatusRecLst_StartTime, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_EndTime.UrlParameterName:              grmi.NewConditionItem(meta.ProdOrderStatusRecLst_EndTime, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_TriggerEvent.UrlParameterName:         grmi.NewConditionItem(meta.ProdOrderStatusRecLst_TriggerEvent, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_TriggerObjectId.UrlParameterName:      grmi.NewConditionItem(meta.ProdOrderStatusRecLst_TriggerObjectId, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_TriggerSubObjectId.UrlParameterName:   grmi.NewConditionItem(meta.ProdOrderStatusRecLst_TriggerSubObjectId, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_OutputEvent.UrlParameterName:          grmi.NewConditionItem(meta.ProdOrderStatusRecLst_OutputEvent, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_OutputEventMessageId.UrlParameterName: grmi.NewConditionItem(meta.ProdOrderStatusRecLst_OutputEventMessageId, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_OutputObjectId.UrlParameterName:       grmi.NewConditionItem(meta.ProdOrderStatusRecLst_OutputObjectId, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_OutputSubObjectId.UrlParameterName:    grmi.NewConditionItem(meta.ProdOrderStatusRecLst_OutputSubObjectId, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_Remark1.UrlParameterName:              grmi.NewConditionItem(meta.ProdOrderStatusRecLst_Remark1, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_Remark2.UrlParameterName:              grmi.NewConditionItem(meta.ProdOrderStatusRecLst_Remark2, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrderStatusRecLst_LastModify, grmi.Approximate, false),
		meta.ProdOrderStatusRecLst_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.ProdOrderStatusRecLst_LastUser, grmi.Equal, false),
		meta.ProdOrderStatusRecLst_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrderStatusRecLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ProdOrderStatusRecLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfProdOrderStatusRecLstAndPaging = grmi.NewCondition(
	DefaultConditionOfProdOrderStatusRecLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : ProdOrderStatusRecLstServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : ProdOrderStatusRecLst的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-23 10:43:27
*
******************************************************************************/
type ProdOrderStatusRecLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewProdOrderStatusRecLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ProdOrderStatusRecLstDAOImplement实例
 *
 * @Return Value             : ProdOrderStatusRecLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewProdOrderStatusRecLstServiceImplement() *ProdOrderStatusRecLstServiceImplement {
	return &ProdOrderStatusRecLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderStatusRecLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ProdOrderStatusRecLstServiceImplement) InsertOne(user *models.Usertab, entity *model.ProdOrderStatusRecLst) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatusRecLst.service.impl.go", "InsertOneProdOrderStatusRecLst", "插入一个ProdOrderStatusRecLst")

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

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatusRecLst.service.impl.go", "DeleteOneProdOrderStatusRecLst", "删除一个ProdOrderStatusRecLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderStatusRecLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ProdOrderStatusRecLstServiceImplement) SelectOne(user *models.Usertab, prodOrderId string, pos int) (*model.ProdOrderStatusRecLst, error) {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatusRecLst.service.impl.go", "SelectOneProdOrderStatusRecLst", "查询一个ProdOrderStatusRecLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderStatusRecLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProdOrderStatusRecLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.ProdOrderStatusRecLst) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatusRecLst.service.impl.go", "UpdateOneProdOrderStatusRecLst", "修改一个ProdOrderStatusRecLst")

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

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatusRecLst.service.impl.go", "InsertProdOrderStatusRecLst", "插入多个ProdOrderStatusRecLst")

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

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

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatusRecLst.service.impl.go", "DeleteProdOrderStatusRecLst", "删除多个ProdOrderStatusRecLst")

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

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

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatusRecLst.service.impl.go", "SelectProdOrderStatusRecLst", "查询ProdOrderStatusRecLst")

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

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatusRecLst.service.impl.go", "UpdateProdOrderStatusRecLst", "修改多个ProdOrderStatusRecLst")

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

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

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