// 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              : PackOrderStatusHistory的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
var DefaultConditionOfPackOrderStatusHistory = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PackOrderStatusHistory_PackOrderId.UrlParameterName:      grmi.NewConditionItem(meta.PackOrderStatusHistory_PackOrderId, grmi.Equal, true),
		meta.PackOrderStatusHistory_Pos.UrlParameterName:              grmi.NewConditionItem(meta.PackOrderStatusHistory_Pos, grmi.Equal, true),
		meta.PackOrderStatusHistory_StatusChangeType.UrlParameterName: grmi.NewConditionItem(meta.PackOrderStatusHistory_StatusChangeType, grmi.Equal, false),
		meta.PackOrderStatusHistory_FromStatus.UrlParameterName:       grmi.NewConditionItem(meta.PackOrderStatusHistory_FromStatus, grmi.Equal, false),
		meta.PackOrderStatusHistory_ToStatus.UrlParameterName:         grmi.NewConditionItem(meta.PackOrderStatusHistory_ToStatus, grmi.Equal, false),
		meta.PackOrderStatusHistory_TriggerEvent.UrlParameterName:     grmi.NewConditionItem(meta.PackOrderStatusHistory_TriggerEvent, grmi.Equal, false),
		meta.PackOrderStatusHistory_TriggerObjId.UrlParameterName:     grmi.NewConditionItem(meta.PackOrderStatusHistory_TriggerObjId, grmi.Equal, false),
		meta.PackOrderStatusHistory_ChangeTime.UrlParameterName:       grmi.NewConditionItem(meta.PackOrderStatusHistory_ChangeTime, grmi.Approximate, false),
		meta.PackOrderStatusHistory_TriggerPara1.UrlParameterName:     grmi.NewConditionItem(meta.PackOrderStatusHistory_TriggerPara1, grmi.Equal, false),
		meta.PackOrderStatusHistory_TriggerPara2.UrlParameterName:     grmi.NewConditionItem(meta.PackOrderStatusHistory_TriggerPara2, grmi.Equal, false),
		meta.PackOrderStatusHistory_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.PackOrderStatusHistory_LastModify, grmi.Approximate, false),
		meta.PackOrderStatusHistory_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.PackOrderStatusHistory_LastUser, grmi.Equal, false),
		meta.PackOrderStatusHistory_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.PackOrderStatusHistory_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : PackOrderStatusHistory的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
var DefaultConditionOfPackOrderStatusHistoryAndPaging = grmi.NewCondition(
	DefaultConditionOfPackOrderStatusHistory.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : PackOrderStatusHistoryServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : PackOrderStatusHistory的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
type PackOrderStatusHistoryServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewPackOrderStatusHistoryDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个PackOrderStatusHistoryDAOImplement实例
 *
 * @Return Value             : PackOrderStatusHistoryDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
func NewPackOrderStatusHistoryServiceImplement() *PackOrderStatusHistoryServiceImplement {
	return &PackOrderStatusHistoryServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackOrderStatusHistoryService.InsertOne
 *
 ******************************************************************************/
func (impl *PackOrderStatusHistoryServiceImplement) InsertOne(user *global.User, entity *model.PackOrderStatusHistory) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackOrderStatusHistoryService.SelectOne
 *
 ******************************************************************************/
func (impl *PackOrderStatusHistoryServiceImplement) SelectOne(user *global.User, packOrderId string, pos int) (*model.PackOrderStatusHistory, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackOrderStatusHistoryService.UpdateOne
 *
 ******************************************************************************/
func (impl *PackOrderStatusHistoryServiceImplement) UpdateOne(user *global.User, entity *model.PackOrderStatusHistory) error {

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

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

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

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

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

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

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

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

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

	condition := DefaultConditionOfPackOrderStatusHistoryAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewPackOrderStatusHistoryDAO(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/PackOrderStatusHistoryService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *PackOrderStatusHistoryServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

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

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

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

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

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