// 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              : ShipOrderItemLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
var DefaultConditionOfShipOrderItemLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ShipOrderItemLst_ShipOrderId.UrlParameterName:     grmi.NewConditionItem(meta.ShipOrderItemLst_ShipOrderId, grmi.Equal, true),
		meta.ShipOrderItemLst_Pos.UrlParameterName:             grmi.NewConditionItem(meta.ShipOrderItemLst_Pos, grmi.Equal, true),
		meta.ShipOrderItemLst_PackTemplateId.UrlParameterName:  grmi.NewConditionItem(meta.ShipOrderItemLst_PackTemplateId, grmi.Equal, false),
		meta.ShipOrderItemLst_PackTypeId.UrlParameterName:      grmi.NewConditionItem(meta.ShipOrderItemLst_PackTypeId, grmi.Equal, false),
		meta.ShipOrderItemLst_Status.UrlParameterName:          grmi.NewConditionItem(meta.ShipOrderItemLst_Status, grmi.Equal, false),
		meta.ShipOrderItemLst_FillQty.UrlParameterName:         grmi.NewConditionItem(meta.ShipOrderItemLst_FillQty, grmi.Equal, false),
		meta.ShipOrderItemLst_PlanQty.UrlParameterName:         grmi.NewConditionItem(meta.ShipOrderItemLst_PlanQty, grmi.Equal, false),
		meta.ShipOrderItemLst_ActQty.UrlParameterName:          grmi.NewConditionItem(meta.ShipOrderItemLst_ActQty, grmi.Equal, false),
		meta.ShipOrderItemLst_ShortShipToggle.UrlParameterName: grmi.NewConditionItem(meta.ShipOrderItemLst_ShortShipToggle, grmi.Equal, false),
		meta.ShipOrderItemLst_ShortShipQty.UrlParameterName:    grmi.NewConditionItem(meta.ShipOrderItemLst_ShortShipQty, grmi.Equal, false),
		meta.ShipOrderItemLst_ShortBy.UrlParameterName:         grmi.NewConditionItem(meta.ShipOrderItemLst_ShortBy, grmi.Equal, false),
		meta.ShipOrderItemLst_ShortTime.UrlParameterName:       grmi.NewConditionItem(meta.ShipOrderItemLst_ShortTime, grmi.Approximate, false),
		meta.ShipOrderItemLst_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.ShipOrderItemLst_LastModify, grmi.Approximate, false),
		meta.ShipOrderItemLst_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.ShipOrderItemLst_LastUser, grmi.Equal, false),
		meta.ShipOrderItemLst_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.ShipOrderItemLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipOrderItemLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ShipOrderItemLstServiceImplement) InsertOne(user *global.User, entity *model.ShipOrderItemLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewShipOrderItemLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ShipOrderId, 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/ShipOrderItemLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ShipOrderItemLstServiceImplement) DeleteOne(user *global.User, shipOrderId string, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipOrderItemLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ShipOrderItemLstServiceImplement) SelectOne(user *global.User, shipOrderId string, pos int) (*model.ShipOrderItemLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipOrderItemLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ShipOrderItemLstServiceImplement) UpdateOne(user *global.User, entity *model.ShipOrderItemLst) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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