// 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              : SerialOrderOPDetail的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-11 10:00:53
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderOPDetail = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.SerialOrderOPDetail_DetailId.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderOPDetail_DetailId, grmi.Equal, true),
		meta.SerialOrderOPDetail_SerialOrderId.UrlParameterName: grmi.NewConditionItem(meta.SerialOrderOPDetail_SerialOrderId, grmi.Equal, false),
		meta.SerialOrderOPDetail_OperationId.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderOPDetail_OperationId, grmi.Equal, false),
		meta.SerialOrderOPDetail_PO.UrlParameterName:            grmi.NewConditionItem(meta.SerialOrderOPDetail_PO, grmi.Equal, false),
		meta.SerialOrderOPDetail_SubArtId.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderOPDetail_SubArtId, grmi.Equal, false),
		meta.SerialOrderOPDetail_StepNo.UrlParameterName:        grmi.NewConditionItem(meta.SerialOrderOPDetail_StepNo, grmi.Equal, false),
		meta.SerialOrderOPDetail_Pos.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderOPDetail_Pos, grmi.Equal, false),
		meta.SerialOrderOPDetail_Status.UrlParameterName:        grmi.NewConditionItem(meta.SerialOrderOPDetail_Status, grmi.Equal, false),
		meta.SerialOrderOPDetail_StepType.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderOPDetail_StepType, grmi.Equal, false),
		meta.SerialOrderOPDetail_CharPara1.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderOPDetail_CharPara1, grmi.Equal, false),
		meta.SerialOrderOPDetail_CharPara2.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderOPDetail_CharPara2, grmi.Equal, false),
		meta.SerialOrderOPDetail_CharPara3.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderOPDetail_CharPara3, grmi.Equal, false),
		meta.SerialOrderOPDetail_CharPara4.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderOPDetail_CharPara4, grmi.Equal, false),
		meta.SerialOrderOPDetail_CharPara5.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderOPDetail_CharPara5, grmi.Equal, false),
		meta.SerialOrderOPDetail_CharPara6.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderOPDetail_CharPara6, grmi.Equal, false),
		meta.SerialOrderOPDetail_CharPara7.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderOPDetail_CharPara7, grmi.Equal, false),
		meta.SerialOrderOPDetail_CharPara8.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderOPDetail_CharPara8, grmi.Equal, false),
		meta.SerialOrderOPDetail_CharPara9.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderOPDetail_CharPara9, grmi.Equal, false),
		meta.SerialOrderOPDetail_CharPara10.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderOPDetail_CharPara10, grmi.Equal, false),
		meta.SerialOrderOPDetail_IntPara1.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderOPDetail_IntPara1, grmi.Equal, false),
		meta.SerialOrderOPDetail_IntPara2.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderOPDetail_IntPara2, grmi.Equal, false),
		meta.SerialOrderOPDetail_IntPara3.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderOPDetail_IntPara3, grmi.Equal, false),
		meta.SerialOrderOPDetail_IntPara4.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderOPDetail_IntPara4, grmi.Equal, false),
		meta.SerialOrderOPDetail_IntPara5.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderOPDetail_IntPara5, grmi.Equal, false),
		meta.SerialOrderOPDetail_FloatPara1.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderOPDetail_FloatPara1, grmi.Equal, false),
		meta.SerialOrderOPDetail_FloatPara2.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderOPDetail_FloatPara2, grmi.Equal, false),
		meta.SerialOrderOPDetail_FloatPara3.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderOPDetail_FloatPara3, grmi.Equal, false),
		meta.SerialOrderOPDetail_FloatPara4.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderOPDetail_FloatPara4, grmi.Equal, false),
		meta.SerialOrderOPDetail_FloatPara5.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderOPDetail_FloatPara5, grmi.Equal, false),
		meta.SerialOrderOPDetail_FlagPara1.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderOPDetail_FlagPara1, grmi.Equal, false),
		meta.SerialOrderOPDetail_FlagPara2.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderOPDetail_FlagPara2, grmi.Equal, false),
		meta.SerialOrderOPDetail_FlagPara3.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderOPDetail_FlagPara3, grmi.Equal, false),
		meta.SerialOrderOPDetail_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderOPDetail_LastModify, grmi.Approximate, false),
		meta.SerialOrderOPDetail_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderOPDetail_LastUser, grmi.Equal, false),
		meta.SerialOrderOPDetail_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderOPDetail_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/SerialOrderOPDetailService.InsertOne
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailServiceImplement) InsertOne(user *models.Usertab, entity *model.SerialOrderOPDetail) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderOPDetailDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.DetailId)
	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/SerialOrderOPDetailService.DeleteOne
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailServiceImplement) DeleteOne(user *models.Usertab, detailId int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/SerialOrderOPDetailService.SelectOne
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailServiceImplement) SelectOne(user *models.Usertab, detailId int) (*model.SerialOrderOPDetail, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/SerialOrderOPDetailService.UpdateOne
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailServiceImplement) UpdateOne(user *models.Usertab, entity *model.SerialOrderOPDetail) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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