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

package implments

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

/******************************************************************************
 *
 * @Description              : PackOrderPlcError的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-10 14:47:04
 *
 ******************************************************************************/
var DefaultConditionOfPackOrderPlcError = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PackOrderPlcError_PackOrderId.UrlParameterName: grmi.NewConditionItem(meta.PackOrderPlcError_PackOrderId, grmi.Equal, true),
		meta.PackOrderPlcError_Status.UrlParameterName:      grmi.NewConditionItem(meta.PackOrderPlcError_Status, grmi.Equal, false),
		meta.PackOrderPlcError_Step.UrlParameterName:        grmi.NewConditionItem(meta.PackOrderPlcError_Step, grmi.Equal, false),
		meta.PackOrderPlcError_ErrorInfo.UrlParameterName:   grmi.NewConditionItem(meta.PackOrderPlcError_ErrorInfo, grmi.Equal, false),
		meta.PackOrderPlcError_ErrorType.UrlParameterName:   grmi.NewConditionItem(meta.PackOrderPlcError_ErrorType, grmi.Equal, false),
		meta.PackOrderPlcError_Location.UrlParameterName:    grmi.NewConditionItem(meta.PackOrderPlcError_Location, grmi.Equal, false),
		meta.PackOrderPlcError_SeatData.UrlParameterName:    grmi.NewConditionItem(meta.PackOrderPlcError_SeatData, grmi.Equal, false),
		meta.PackOrderPlcError_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.PackOrderPlcError_LastModify, grmi.Approximate, false),
		meta.PackOrderPlcError_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.PackOrderPlcError_LastUser, grmi.Equal, false),
		meta.PackOrderPlcError_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.PackOrderPlcError_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : PackOrderPlcError的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-10 14:47:04
 *
 ******************************************************************************/
var DefaultConditionOfPackOrderPlcErrorAndPaging = grmi.NewCondition(
	DefaultConditionOfPackOrderPlcError.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : PackOrderPlcErrorServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : PackOrderPlcError的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-10 14:47:04
 *
 ******************************************************************************/
type PackOrderPlcErrorServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewPackOrderPlcErrorDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个PackOrderPlcErrorDAOImplement实例
 *
 * @Return Value             : PackOrderPlcErrorDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-10 14:47:04
 *
 ******************************************************************************/
func NewPackOrderPlcErrorServiceImplement() *PackOrderPlcErrorServiceImplement {
	return &PackOrderPlcErrorServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackOrderPlcErrorService.InsertOne
 *
 ******************************************************************************/
func (impl *PackOrderPlcErrorServiceImplement) InsertOne(user *global.User, entity *model.PackOrderPlcError) error {

	grmi.Log(user, "/services/jit/implments/PackOrderPlcError.service.impl.go", "InsertOnePackOrderPlcError", "插入一个PackOrderPlcError")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackOrderPlcErrorDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.PackOrderId)
	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/jit/PackOrderPlcErrorService.DeleteOne
 *
 ******************************************************************************/
func (impl *PackOrderPlcErrorServiceImplement) DeleteOne(user *global.User, packOrderId string) error {

	grmi.Log(user, "/services/jit/implments/PackOrderPlcError.service.impl.go", "DeleteOnePackOrderPlcError", "删除一个PackOrderPlcError")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackOrderPlcErrorService.SelectOne
 *
 ******************************************************************************/
func (impl *PackOrderPlcErrorServiceImplement) SelectOne(user *global.User, packOrderId string) (*model.PackOrderPlcError, error) {

	grmi.Log(user, "/services/jit/implments/PackOrderPlcError.service.impl.go", "SelectOnePackOrderPlcError", "查询一个PackOrderPlcError")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackOrderPlcErrorService.UpdateOne
 *
 ******************************************************************************/
func (impl *PackOrderPlcErrorServiceImplement) UpdateOne(user *global.User, entity *model.PackOrderPlcError) error {

	grmi.Log(user, "/services/jit/implments/PackOrderPlcError.service.impl.go", "UpdateOnePackOrderPlcError", "修改一个PackOrderPlcError")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackOrderPlcErrorDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.PackOrderId)
	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/jit/PackOrderPlcErrorService.Insert
 *
 ******************************************************************************/
func (impl *PackOrderPlcErrorServiceImplement) Insert(user *global.User, entities *[]model.PackOrderPlcError) error {

	grmi.Log(user, "/services/jit/implments/PackOrderPlcError.service.impl.go", "InsertPackOrderPlcError", "插入多个PackOrderPlcError")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.PackOrderId)
		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/jit/PackOrderPlcErrorService.Delete
 *
 ******************************************************************************/
func (impl *PackOrderPlcErrorServiceImplement) Delete(user *global.User, entities *[]model.PackOrderPlcError) error {

	grmi.Log(user, "/services/jit/implments/PackOrderPlcError.service.impl.go", "DeletePackOrderPlcError", "删除多个PackOrderPlcError")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.PackOrderId)
		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/jit/PackOrderPlcErrorService.Select
 *
 ******************************************************************************/
func (impl *PackOrderPlcErrorServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.PackOrderPlcError, error) {

	grmi.Log(user, "/services/jit/implments/PackOrderPlcError.service.impl.go", "SelectPackOrderPlcError", "查询PackOrderPlcError")

	condition := DefaultConditionOfPackOrderPlcErrorAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewPackOrderPlcErrorDAO(session, user.PlantNr, user.UserId)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackOrderPlcErrorService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *PackOrderPlcErrorServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

	grmi.Log(user, "/services/jit/implments/PackOrderPlcError.service.impl.go", "SelectPackOrderPlcError", "查询PackOrderPlcError")

	condition := DefaultConditionOfPackOrderPlcErrorAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewPackOrderPlcErrorDAO(session, user.PlantNr, user.UserId)
	condition.Fill(urlParameters)
	result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackOrderPlcErrorService.Update
 *
 ******************************************************************************/
func (impl *PackOrderPlcErrorServiceImplement) Update(user *global.User, entities *[]model.PackOrderPlcError) error {

	grmi.Log(user, "/services/jit/implments/PackOrderPlcError.service.impl.go", "UpdatePackOrderPlcError", "修改多个PackOrderPlcError")

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

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

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