// 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              : LabelTemplateDetail的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
var DefaultConditionOfLabelTemplateDetail = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.LabelTemplateDetail_LabelTemplateId.UrlParameterName:  grmi.NewConditionItem(meta.LabelTemplateDetail_LabelTemplateId, grmi.Equal, true),
		meta.LabelTemplateDetail_Pos.UrlParameterName:              grmi.NewConditionItem(meta.LabelTemplateDetail_Pos, grmi.Equal, true),
		meta.LabelTemplateDetail_X.UrlParameterName:                grmi.NewConditionItem(meta.LabelTemplateDetail_X, grmi.Equal, false),
		meta.LabelTemplateDetail_Y.UrlParameterName:                grmi.NewConditionItem(meta.LabelTemplateDetail_Y, grmi.Equal, false),
		meta.LabelTemplateDetail_Height.UrlParameterName:           grmi.NewConditionItem(meta.LabelTemplateDetail_Height, grmi.Equal, false),
		meta.LabelTemplateDetail_Width.UrlParameterName:            grmi.NewConditionItem(meta.LabelTemplateDetail_Width, grmi.Equal, false),
		meta.LabelTemplateDetail_LabelItemType.UrlParameterName:    grmi.NewConditionItem(meta.LabelTemplateDetail_LabelItemType, grmi.Equal, false),
		meta.LabelTemplateDetail_ValueParseToggle.UrlParameterName: grmi.NewConditionItem(meta.LabelTemplateDetail_ValueParseToggle, grmi.Equal, false),
		meta.LabelTemplateDetail_LabelItemValue.UrlParameterName:   grmi.NewConditionItem(meta.LabelTemplateDetail_LabelItemValue, grmi.Equal, false),
		meta.LabelTemplateDetail_LabelItemPen.UrlParameterName:     grmi.NewConditionItem(meta.LabelTemplateDetail_LabelItemPen, grmi.Equal, false),
		meta.LabelTemplateDetail_LabelItemFilled.UrlParameterName:  grmi.NewConditionItem(meta.LabelTemplateDetail_LabelItemFilled, grmi.Equal, false),
		meta.LabelTemplateDetail_LabelItemFont.UrlParameterName:    grmi.NewConditionItem(meta.LabelTemplateDetail_LabelItemFont, grmi.Equal, false),
		meta.LabelTemplateDetail_ShowBarcodeText.UrlParameterName:  grmi.NewConditionItem(meta.LabelTemplateDetail_ShowBarcodeText, grmi.Equal, false),
		meta.LabelTemplateDetail_PreviewValue.UrlParameterName:     grmi.NewConditionItem(meta.LabelTemplateDetail_PreviewValue, grmi.Equal, false),
		meta.LabelTemplateDetail_LabelItemPara1.UrlParameterName:   grmi.NewConditionItem(meta.LabelTemplateDetail_LabelItemPara1, grmi.Equal, false),
		meta.LabelTemplateDetail_LabelItemPara2.UrlParameterName:   grmi.NewConditionItem(meta.LabelTemplateDetail_LabelItemPara2, grmi.Equal, false),
		meta.LabelTemplateDetail_LabelItemPara3.UrlParameterName:   grmi.NewConditionItem(meta.LabelTemplateDetail_LabelItemPara3, grmi.Equal, false),
		meta.LabelTemplateDetail_LabelItemPara4.UrlParameterName:   grmi.NewConditionItem(meta.LabelTemplateDetail_LabelItemPara4, grmi.Equal, false),
		meta.LabelTemplateDetail_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.LabelTemplateDetail_LastModify, grmi.Approximate, false),
		meta.LabelTemplateDetail_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.LabelTemplateDetail_LastUser, grmi.Equal, false),
		meta.LabelTemplateDetail_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.LabelTemplateDetail_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/LabelTemplateDetailService.InsertOne
 *
 ******************************************************************************/
func (impl *LabelTemplateDetailServiceImplement) InsertOne(user *global.User, entity *model.LabelTemplateDetail) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/LabelTemplateDetailService.SelectOne
 *
 ******************************************************************************/
func (impl *LabelTemplateDetailServiceImplement) SelectOne(user *global.User, labelTemplateId string, pos int) (*model.LabelTemplateDetail, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/LabelTemplateDetailService.UpdateOne
 *
 ******************************************************************************/
func (impl *LabelTemplateDetailServiceImplement) UpdateOne(user *global.User, entity *model.LabelTemplateDetail) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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