// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/base"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/base"
	model "LAPP_ACURA_MOM_BACKEND/models/base"
)

/******************************************************************************
 *
 * @Description              : LabelDetail的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
var DefaultConditionOfLabelDetail = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.LabelDetail_LabelId.UrlParameterName:       grmi.NewConditionItem(meta.LabelDetail_LabelId, grmi.Equal, true),
		meta.LabelDetail_Pos.UrlParameterName:           grmi.NewConditionItem(meta.LabelDetail_Pos, grmi.Equal, true),
		meta.LabelDetail_VariableName.UrlParameterName:  grmi.NewConditionItem(meta.LabelDetail_VariableName, grmi.Equal, false),
		meta.LabelDetail_VariableValue.UrlParameterName: grmi.NewConditionItem(meta.LabelDetail_VariableValue, grmi.Equal, false),
		meta.LabelDetail_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.LabelDetail_LastModify, grmi.Approximate, false),
		meta.LabelDetail_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.LabelDetail_LastUser, grmi.Equal, false),
		meta.LabelDetail_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.LabelDetail_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : LabelDetail的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
var DefaultConditionOfLabelDetailAndPaging = grmi.NewCondition(
	DefaultConditionOfLabelDetail.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : LabelDetailServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : LabelDetail的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
type LabelDetailServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewLabelDetailDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个LabelDetailDAOImplement实例
 *
 * @Return Value             : LabelDetailDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
func NewLabelDetailServiceImplement() *LabelDetailServiceImplement {
	return &LabelDetailServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/LabelDetailService.InsertOne
 *
 ******************************************************************************/
func (impl *LabelDetailServiceImplement) InsertOne(user *global.User, entity *model.LabelDetail) error {

	grmi.Log(user, "/services/base/implments/LabelDetail.service.impl.go", "InsertOneLabelDetail", "插入一个LabelDetail")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewLabelDetailDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.LabelId, 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/base/LabelDetailService.DeleteOne
 *
 ******************************************************************************/
func (impl *LabelDetailServiceImplement) DeleteOne(user *global.User, labelId string, pos int) error {

	grmi.Log(user, "/services/base/implments/LabelDetail.service.impl.go", "DeleteOneLabelDetail", "删除一个LabelDetail")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/LabelDetailService.SelectOne
 *
 ******************************************************************************/
func (impl *LabelDetailServiceImplement) SelectOne(user *global.User, labelId string, pos int) (*model.LabelDetail, error) {

	grmi.Log(user, "/services/base/implments/LabelDetail.service.impl.go", "SelectOneLabelDetail", "查询一个LabelDetail")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/LabelDetailService.UpdateOne
 *
 ******************************************************************************/
func (impl *LabelDetailServiceImplement) UpdateOne(user *global.User, entity *model.LabelDetail) error {

	grmi.Log(user, "/services/base/implments/LabelDetail.service.impl.go", "UpdateOneLabelDetail", "修改一个LabelDetail")

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

	grmi.Log(user, "/services/base/implments/LabelDetail.service.impl.go", "InsertLabelDetail", "插入多个LabelDetail")

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

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

	grmi.Log(user, "/services/base/implments/LabelDetail.service.impl.go", "DeleteLabelDetail", "删除多个LabelDetail")

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

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

	grmi.Log(user, "/services/base/implments/LabelDetail.service.impl.go", "SelectLabelDetail", "查询LabelDetail")

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

	grmi.Log(user, "/services/base/implments/LabelDetail.service.impl.go", "SelectLabelDetail", "查询LabelDetail")

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

	grmi.Log(user, "/services/base/implments/LabelDetail.service.impl.go", "UpdateLabelDetail", "修改多个LabelDetail")

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

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

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