// 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              : ShipTemplateItemLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
var DefaultConditionOfShipTemplateItemLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ShipTemplateItemLst_ShipTemplateId.UrlParameterName: grmi.NewConditionItem(meta.ShipTemplateItemLst_ShipTemplateId, grmi.Equal, true),
		meta.ShipTemplateItemLst_Pos.UrlParameterName:            grmi.NewConditionItem(meta.ShipTemplateItemLst_Pos, grmi.Equal, true),
		meta.ShipTemplateItemLst_PackTypeId.UrlParameterName:     grmi.NewConditionItem(meta.ShipTemplateItemLst_PackTypeId, grmi.Equal, false),
		meta.ShipTemplateItemLst_PackTemplateId.UrlParameterName:   grmi.NewConditionItem(meta.ShipTemplateItemLst_PackTemplateId, grmi.Equal, false),
		meta.ShipTemplateItemLst_PlanQty.UrlParameterName:        grmi.NewConditionItem(meta.ShipTemplateItemLst_PlanQty, grmi.Equal, false),
		meta.ShipTemplateItemLst_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.ShipTemplateItemLst_LastModify, grmi.Approximate, false),
		meta.ShipTemplateItemLst_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.ShipTemplateItemLst_LastUser, grmi.Equal, false),
		meta.ShipTemplateItemLst_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.ShipTemplateItemLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipTemplateItemLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ShipTemplateItemLstServiceImplement) InsertOne(user *global.User, entity *model.ShipTemplateItemLst) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipTemplateItemLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ShipTemplateItemLstServiceImplement) SelectOne(user *global.User, shipTemplateId string, pos int) (*model.ShipTemplateItemLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipTemplateItemLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ShipTemplateItemLstServiceImplement) UpdateOne(user *global.User, entity *model.ShipTemplateItemLst) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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