// 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              : PackOrderStatus的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
var DefaultConditionOfPackOrderStatus = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PackOrderStatus_PackOrderId.UrlParameterName:       grmi.NewConditionItem(meta.PackOrderStatus_PackOrderId, grmi.Equal, true),
		meta.PackOrderStatus_Status.UrlParameterName:            grmi.NewConditionItem(meta.PackOrderStatus_Status, grmi.Equal, false),
		meta.PackOrderStatus_LoadStatus.UrlParameterName:        grmi.NewConditionItem(meta.PackOrderStatus_LoadStatus, grmi.Equal, false),
		meta.PackOrderStatus_ShipStatus.UrlParameterName:        grmi.NewConditionItem(meta.PackOrderStatus_ShipStatus, grmi.Equal, false),
		meta.PackOrderStatus_BackflushedToggle.UrlParameterName: grmi.NewConditionItem(meta.PackOrderStatus_BackflushedToggle, grmi.Equal, false),
		meta.PackOrderStatus_Status1.UrlParameterName:           grmi.NewConditionItem(meta.PackOrderStatus_Status1, grmi.Equal, false),
		meta.PackOrderStatus_Status2.UrlParameterName:           grmi.NewConditionItem(meta.PackOrderStatus_Status2, grmi.Equal, false),
		meta.PackOrderStatus_LastModify.UrlParameterName:        grmi.NewConditionItem(meta.PackOrderStatus_LastModify, grmi.Approximate, false),
		meta.PackOrderStatus_LastUser.UrlParameterName:          grmi.NewConditionItem(meta.PackOrderStatus_LastUser, grmi.Equal, false),
		meta.PackOrderStatus_CreateTime.UrlParameterName:        grmi.NewConditionItem(meta.PackOrderStatus_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackOrderStatusService.InsertOne
 *
 ******************************************************************************/
func (impl *PackOrderStatusServiceImplement) InsertOne(user *global.User, entity *model.PackOrderStatus) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackOrderStatusDAO(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/PackOrderStatusService.DeleteOne
 *
 ******************************************************************************/
func (impl *PackOrderStatusServiceImplement) DeleteOne(user *global.User, packOrderId string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackOrderStatusDAO(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/PackOrderStatusService.SelectOne
 *
 ******************************************************************************/
func (impl *PackOrderStatusServiceImplement) SelectOne(user *global.User, packOrderId string) (*model.PackOrderStatus, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackOrderStatusDAO(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/PackOrderStatusService.UpdateOne
 *
 ******************************************************************************/
func (impl *PackOrderStatusServiceImplement) UpdateOne(user *global.User, entity *model.PackOrderStatus) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackOrderStatusDAO(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/PackOrderStatusService.Insert
 *
 ******************************************************************************/
func (impl *PackOrderStatusServiceImplement) Insert(user *global.User, entities *[]model.PackOrderStatus) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackOrderStatusDAO(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/PackOrderStatusService.Delete
 *
 ******************************************************************************/
func (impl *PackOrderStatusServiceImplement) Delete(user *global.User, entities *[]model.PackOrderStatus) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackOrderStatusDAO(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/PackOrderStatusService.Select
 *
 ******************************************************************************/
func (impl *PackOrderStatusServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.PackOrderStatus, error) {

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

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

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

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackOrderStatusDAO(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
}
