// 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              : PackOrderItemLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
var DefaultConditionOfPackOrderItemLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PackOrderItemLst_PackOrderId.UrlParameterName:   grmi.NewConditionItem(meta.PackOrderItemLst_PackOrderId, grmi.Equal, true),
		meta.PackOrderItemLst_Pos.UrlParameterName:           grmi.NewConditionItem(meta.PackOrderItemLst_Pos, grmi.Equal, true),
		meta.PackOrderItemLst_PosCode.UrlParameterName:       grmi.NewConditionItem(meta.PackOrderItemLst_PosCode, grmi.Equal, false),
		meta.PackOrderItemLst_OemSeq.UrlParameterName:        grmi.NewConditionItem(meta.PackOrderItemLst_OemSeq, grmi.Equal, false),
		meta.PackOrderItemLst_Seq.UrlParameterName:           grmi.NewConditionItem(meta.PackOrderItemLst_Seq, grmi.Equal, false),
		meta.PackOrderItemLst_PartId.UrlParameterName:        grmi.NewConditionItem(meta.PackOrderItemLst_PartId, grmi.Equal, false),
		meta.PackOrderItemLst_SerialOrderId.UrlParameterName: grmi.NewConditionItem(meta.PackOrderItemLst_SerialOrderId, grmi.Equal, false),
		meta.PackOrderItemLst_Status.UrlParameterName:        grmi.NewConditionItem(meta.PackOrderItemLst_Status, grmi.Equal, false),
		meta.PackOrderItemLst_PlanQty.UrlParameterName:       grmi.NewConditionItem(meta.PackOrderItemLst_PlanQty, grmi.Equal, false),
		meta.PackOrderItemLst_ActQty.UrlParameterName:        grmi.NewConditionItem(meta.PackOrderItemLst_ActQty, grmi.Equal, false),
		meta.PackOrderItemLst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.PackOrderItemLst_LastModify, grmi.Approximate, false),
		meta.PackOrderItemLst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.PackOrderItemLst_LastUser, grmi.Equal, false),
		meta.PackOrderItemLst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.PackOrderItemLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackOrderItemLstService.InsertOne
 *
 ******************************************************************************/
func (impl *PackOrderItemLstServiceImplement) InsertOne(user *global.User, entity *model.PackOrderItemLst) error {

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

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackOrderItemLstDAO(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/PackOrderItemLstService.SelectOne
 *
 ******************************************************************************/
func (impl *PackOrderItemLstServiceImplement) SelectOne(user *global.User, packOrderId string, pos int) (*model.PackOrderItemLst, error) {

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

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

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

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

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

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

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

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

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

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

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

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

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

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