// 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              : LabelParserHead的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-20 11:11:05
 *
 ******************************************************************************/
var DefaultConditionOfLabelParserHead = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.LabelParserHead_ParserId.UrlParameterName:   grmi.NewConditionItem(meta.LabelParserHead_ParserId, grmi.Equal, true),
		meta.LabelParserHead_Length.UrlParameterName:     grmi.NewConditionItem(meta.LabelParserHead_Length, grmi.Equal, false),
		meta.LabelParserHead_ParserType.UrlParameterName: grmi.NewConditionItem(meta.LabelParserHead_ParserType, grmi.Equal, false),
		meta.LabelParserHead_Descr.UrlParameterName:      grmi.NewConditionItem(meta.LabelParserHead_Descr, grmi.Equal, false),
		meta.LabelParserHead_Example.UrlParameterName:    grmi.NewConditionItem(meta.LabelParserHead_Example, grmi.Equal, false),
		meta.LabelParserHead_Split.UrlParameterName:      grmi.NewConditionItem(meta.LabelParserHead_Split, grmi.Equal, false),
		meta.LabelParserHead_LastModify.UrlParameterName: grmi.NewConditionItem(meta.LabelParserHead_LastModify, grmi.Approximate, false),
		meta.LabelParserHead_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.LabelParserHead_LastUser, grmi.Equal, false),
		meta.LabelParserHead_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.LabelParserHead_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : LabelParserHead的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-20 11:11:05
 *
 ******************************************************************************/
var DefaultConditionOfLabelParserHeadAndPaging = grmi.NewCondition(
	DefaultConditionOfLabelParserHead.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : LabelParserHeadServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : LabelParserHead的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-20 11:11:05
 *
 ******************************************************************************/
type LabelParserHeadServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewLabelParserHeadDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个LabelParserHeadDAOImplement实例
 *
 * @Return Value             : LabelParserHeadDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-20 11:11:05
 *
 ******************************************************************************/
func NewLabelParserHeadServiceImplement() *LabelParserHeadServiceImplement {
	return &LabelParserHeadServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/LabelParserHeadService.InsertOne
 *
 ******************************************************************************/
func (impl *LabelParserHeadServiceImplement) InsertOne(user *global.User, entity *model.LabelParserHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewLabelParserHeadDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ParserId)
	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/LabelParserHeadService.DeleteOne
 *
 ******************************************************************************/
func (impl *LabelParserHeadServiceImplement) DeleteOne(user *global.User, parserId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/LabelParserHeadService.SelectOne
 *
 ******************************************************************************/
func (impl *LabelParserHeadServiceImplement) SelectOne(user *global.User, parserId string) (*model.LabelParserHead, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/LabelParserHeadService.UpdateOne
 *
 ******************************************************************************/
func (impl *LabelParserHeadServiceImplement) UpdateOne(user *global.User, entity *model.LabelParserHead) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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