// 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              : LabelTemplateHead的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
var DefaultConditionOfLabelTemplateHead = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.LabelTemplateHead_LabelTemplateId.UrlParameterName:         grmi.NewConditionItem(meta.LabelTemplateHead_LabelTemplateId, grmi.Equal, true),
		meta.LabelTemplateHead_Descr.UrlParameterName:                   grmi.NewConditionItem(meta.LabelTemplateHead_Descr, grmi.Equal, false),
		meta.LabelTemplateHead_LabelTemplateFile.UrlParameterName:       grmi.NewConditionItem(meta.LabelTemplateHead_LabelTemplateFile, grmi.Equal, false),
		meta.LabelTemplateHead_PictureTemplateToggle.UrlParameterName:   grmi.NewConditionItem(meta.LabelTemplateHead_PictureTemplateToggle, grmi.Equal, false),
		meta.LabelTemplateHead_Height.UrlParameterName:                  grmi.NewConditionItem(meta.LabelTemplateHead_Height, grmi.Equal, false),
		meta.LabelTemplateHead_Width.UrlParameterName:                   grmi.NewConditionItem(meta.LabelTemplateHead_Width, grmi.Equal, false),
		meta.LabelTemplateHead_BackgroundColorToggle.UrlParameterName:   grmi.NewConditionItem(meta.LabelTemplateHead_BackgroundColorToggle, grmi.Equal, false),
		meta.LabelTemplateHead_BackgroundColor.UrlParameterName:         grmi.NewConditionItem(meta.LabelTemplateHead_BackgroundColor, grmi.Equal, false),
		meta.LabelTemplateHead_BackgroundPictureToggle.UrlParameterName: grmi.NewConditionItem(meta.LabelTemplateHead_BackgroundPictureToggle, grmi.Equal, false),
		meta.LabelTemplateHead_BackgroundPicture.UrlParameterName:       grmi.NewConditionItem(meta.LabelTemplateHead_BackgroundPicture, grmi.Equal, false),
		meta.LabelTemplateHead_DPI.UrlParameterName:                     grmi.NewConditionItem(meta.LabelTemplateHead_DPI, grmi.Equal, false),
		meta.LabelTemplateHead_CtrlPara1.UrlParameterName:               grmi.NewConditionItem(meta.LabelTemplateHead_CtrlPara1, grmi.Equal, false),
		meta.LabelTemplateHead_CtrlPara2.UrlParameterName:               grmi.NewConditionItem(meta.LabelTemplateHead_CtrlPara2, grmi.Equal, false),
		meta.LabelTemplateHead_CtrlStr1.UrlParameterName:                grmi.NewConditionItem(meta.LabelTemplateHead_CtrlStr1, grmi.Equal, false),
		meta.LabelTemplateHead_CtrlStr2.UrlParameterName:                grmi.NewConditionItem(meta.LabelTemplateHead_CtrlStr2, grmi.Equal, false),
		meta.LabelTemplateHead_LastModify.UrlParameterName:              grmi.NewConditionItem(meta.LabelTemplateHead_LastModify, grmi.Approximate, false),
		meta.LabelTemplateHead_LastUser.UrlParameterName:                grmi.NewConditionItem(meta.LabelTemplateHead_LastUser, grmi.Equal, false),
		meta.LabelTemplateHead_CreateTime.UrlParameterName:              grmi.NewConditionItem(meta.LabelTemplateHead_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/base/LabelTemplateHeadService.InsertOne
 *
 ******************************************************************************/
func (impl *LabelTemplateHeadServiceImplement) InsertOne(user *global.User, entity *model.LabelTemplateHead) error {

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

	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.NewLabelTemplateHeadDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.LabelTemplateId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}
	/**处理数据**/
	for k, _ := range entity.LabelTemplateDetail {
		entity.LabelTemplateDetail[k].LabelTemplateId = entity.LabelTemplateId
		entity.LabelTemplateDetail[k].Pos = k + 1
	}
	lstdao := dal.NewLabelTemplateDetailDAO(session, user.PlantNr, user.UserId)
	err = lstdao.Insert(&entity.LabelTemplateDetail)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()

	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/base/LabelTemplateHeadService.DeleteOne
 *
 ******************************************************************************/
func (impl *LabelTemplateHeadServiceImplement) DeleteOne(user *global.User, labelTemplateId string) error {

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

	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.NewLabelTemplateHeadDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(labelTemplateId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(labelTemplateId)
	if err != nil {
		session.Rollback()
		return err
	}
	lstdao := dal.NewLabelTemplateDetailDAO(session, user.PlantNr, user.UserId)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.LabelTemplateDetail_LabelTemplateId.NewPredicate(grmi.Equal, labelTemplateId),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/base/LabelTemplateHeadService.SelectOne
 *
 ******************************************************************************/
func (impl *LabelTemplateHeadServiceImplement) SelectOne(user *global.User, labelTemplateId string) (*model.LabelTemplateHead, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewLabelTemplateHeadDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(labelTemplateId)
	if err != nil {
		return nil, err
	}
	lstdao := dal.NewLabelTemplateDetailDAO(session, user.PlantNr, user.UserId)
	list, err := lstdao.Select([]grmi.Predicate{
		meta.LabelTemplateDetail_LabelTemplateId.NewPredicate(grmi.Equal, labelTemplateId),
	}, nil)
	if err != nil {
		return nil, err
	}
	result.LabelTemplateDetail = list
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/base/LabelTemplateHeadService.UpdateOne
 *
 ******************************************************************************/
func (impl *LabelTemplateHeadServiceImplement) UpdateOne(user *global.User, entity *model.LabelTemplateHead) error {

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

	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.NewLabelTemplateHeadDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.LabelTemplateId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}
	/**处理数据**/
	for k, _ := range entity.LabelTemplateDetail {
		entity.LabelTemplateDetail[k].LabelTemplateId = entity.LabelTemplateId
		entity.LabelTemplateDetail[k].Pos = k + 1
	}
	lstdao := dal.NewLabelTemplateDetailDAO(session, user.PlantNr, user.UserId)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.LabelTemplateDetail_LabelTemplateId.NewPredicate(grmi.Equal, entity.LabelTemplateId),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	err = lstdao.Insert(&entity.LabelTemplateDetail)
	if err != nil {
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/base/LabelTemplateHeadService.Insert
 *
 ******************************************************************************/
func (impl *LabelTemplateHeadServiceImplement) Insert(user *global.User, entities *[]model.LabelTemplateHead) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.LabelTemplateId)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

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

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/base/LabelTemplateHeadService.Delete
 *
 ******************************************************************************/
func (impl *LabelTemplateHeadServiceImplement) Delete(user *global.User, entities *[]model.LabelTemplateHead) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/base/LabelTemplateHeadService.Select
 *
 ******************************************************************************/
func (impl *LabelTemplateHeadServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.LabelTemplateHead, error) {

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

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

	grmi.Log(user, "/services/base/implments/LabelTemplateHead.service.impl.go", "SelectAndPaging", "SelectAndPaging")

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

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

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

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

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