// 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              : LabelHead的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
var DefaultConditionOfLabelHead = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.LabelHead_LabelId.UrlParameterName:         grmi.NewConditionItem(meta.LabelHead_LabelId, grmi.Equal, true),
		meta.LableHead_OperationId.UrlParameterName:     grmi.NewConditionItem(meta.LableHead_OperationId, grmi.Equal, false),
		meta.LabelHead_LabelTemplateId.UrlParameterName: grmi.NewConditionItem(meta.LabelHead_LabelTemplateId, grmi.Equal, false),
		meta.LabelHead_PrinterId.UrlParameterName:       grmi.NewConditionItem(meta.LabelHead_PrinterId, grmi.Equal, false),
		meta.LabelHead_BusinessObjType.UrlParameterName: grmi.NewConditionItem(meta.LabelHead_BusinessObjType, grmi.Equal, false),
		meta.LabelHead_BusinessObjId.UrlParameterName:   grmi.NewConditionItem(meta.LabelHead_BusinessObjId, grmi.Equal, false),
		meta.LabelHead_Status.UrlParameterName:          grmi.NewConditionItem(meta.LabelHead_Status, grmi.Equal, false),
		meta.LabelHead_PrintQty.UrlParameterName:        grmi.NewConditionItem(meta.LabelHead_PrintQty, grmi.Equal, false),
		meta.LabelHead_PrintTime.UrlParameterName:       grmi.NewConditionItem(meta.LabelHead_PrintTime, grmi.Approximate, false),
		meta.LabelHead_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.LabelHead_LastModify, grmi.Approximate, false),
		meta.LabelHead_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.LabelHead_LastUser, grmi.Equal, false),
		meta.LabelHead_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.LabelHead_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/LabelHeadService.InsertOne
 *
 ******************************************************************************/
func (impl *LabelHeadServiceImplement) InsertOne(user *global.User, entity *model.LabelHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewLabelHeadDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.LabelId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/base/LabelHeadService.DeleteOne
 *
 ******************************************************************************/
func (impl *LabelHeadServiceImplement) DeleteOne(user *global.User, labelId string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()

	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewLabelHeadDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(labelId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(labelId)
	if err != nil {
		session.Rollback()
		return err
	}
	//删除对应的记录
	lstdao := dal.NewLabelDetailDAO(session, user.PlantNr, user.UserId)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.LabelDetail_LabelId.NewPredicate(grmi.Equal, labelId),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/LabelHeadService.SelectOne
 *
 ******************************************************************************/
func (impl *LabelHeadServiceImplement) SelectOne(user *global.User, labelId string) (*model.LabelHead, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/LabelHeadService.UpdateOne
 *
 ******************************************************************************/
func (impl *LabelHeadServiceImplement) UpdateOne(user *global.User, entity *model.LabelHead) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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