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

package implments

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

/******************************************************************************
 *
 * @Description              : TemplateServiceLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-08 13:20:42
 *
 ******************************************************************************/
var DefaultConditionOfTemplateServiceLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.TemplateServiceLst_MainTemplateNr.UrlParameterName: grmi.NewConditionItem(meta.TemplateServiceLst_MainTemplateNr, grmi.Equal, true),
		meta.TemplateServiceLst_MainServiceNr.UrlParameterName:  grmi.NewConditionItem(meta.TemplateServiceLst_MainServiceNr, grmi.Equal, true),
		meta.TemplateServiceLst_Pos.UrlParameterName:            grmi.NewConditionItem(meta.TemplateServiceLst_Pos, grmi.Equal, false),
		meta.TemplateServiceLst_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.TemplateServiceLst_LastModify, grmi.Approximate, false),
		meta.TemplateServiceLst_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.TemplateServiceLst_LastUser, grmi.Equal, false),
		meta.TemplateServiceLst_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.TemplateServiceLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : TemplateServiceLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-08 13:20:42
 *
 ******************************************************************************/
var DefaultConditionOfTemplateServiceLstAndPaging = grmi.NewCondition(
	DefaultConditionOfTemplateServiceLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : TemplateServiceLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : TemplateServiceLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-08 13:20:42
 *
 ******************************************************************************/
type TemplateServiceLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewTemplateServiceLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个TemplateServiceLstDAOImplement实例
 *
 * @Return Value             : TemplateServiceLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-08 13:20:42
 *
 ******************************************************************************/
func NewTemplateServiceLstServiceImplement() *TemplateServiceLstServiceImplement {
	return &TemplateServiceLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pm/TemplateServiceLstService.InsertOne
 *
 ******************************************************************************/
func (impl *TemplateServiceLstServiceImplement) InsertOne(user *global.User, entity *model.TemplateServiceLst) error {

	grmi.Log(user, "/services/pm/implments/TemplateServiceLst.service.impl.go", "InsertOneTemplateServiceLst", "插入一个TemplateServiceLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewTemplateServiceLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.MainTemplateNr, entity.MainServiceNr)
	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/pm/TemplateServiceLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *TemplateServiceLstServiceImplement) DeleteOne(user *global.User, mainTemplateNr int, mainServiceNr int) error {

	grmi.Log(user, "/services/pm/implments/TemplateServiceLst.service.impl.go", "DeleteOneTemplateServiceLst", "删除一个TemplateServiceLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pm/TemplateServiceLstService.SelectOne
 *
 ******************************************************************************/
func (impl *TemplateServiceLstServiceImplement) SelectOne(user *global.User, mainTemplateNr int, mainServiceNr int) (*model.TemplateServiceLst, error) {

	grmi.Log(user, "/services/pm/implments/TemplateServiceLst.service.impl.go", "SelectOneTemplateServiceLst", "查询一个TemplateServiceLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pm/TemplateServiceLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *TemplateServiceLstServiceImplement) UpdateOne(user *global.User, entity *model.TemplateServiceLst) error {

	grmi.Log(user, "/services/pm/implments/TemplateServiceLst.service.impl.go", "UpdateOneTemplateServiceLst", "修改一个TemplateServiceLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewTemplateServiceLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.MainTemplateNr, entity.MainServiceNr)
	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/pm/TemplateServiceLstService.Insert
 *
 ******************************************************************************/
func (impl *TemplateServiceLstServiceImplement) Insert(user *global.User, entities *[]model.TemplateServiceLst) error {

	grmi.Log(user, "/services/pm/implments/TemplateServiceLst.service.impl.go", "InsertTemplateServiceLst", "插入多个TemplateServiceLst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.MainTemplateNr, entity.MainServiceNr)
		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/pm/TemplateServiceLstService.Delete
 *
 ******************************************************************************/
func (impl *TemplateServiceLstServiceImplement) Delete(user *global.User, entities *[]model.TemplateServiceLst) error {

	grmi.Log(user, "/services/pm/implments/TemplateServiceLst.service.impl.go", "DeleteTemplateServiceLst", "删除多个TemplateServiceLst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.MainTemplateNr, entity.MainServiceNr)
		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/pm/TemplateServiceLstService.Select
 *
 ******************************************************************************/
func (impl *TemplateServiceLstServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.TemplateServiceLst, error) {

	grmi.Log(user, "/services/pm/implments/TemplateServiceLst.service.impl.go", "SelectTemplateServiceLst", "查询TemplateServiceLst")

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

	grmi.Log(user, "/services/pm/implments/TemplateServiceLst.service.impl.go", "SelectTemplateServiceLst", "查询TemplateServiceLst")

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

	grmi.Log(user, "/services/pm/implments/TemplateServiceLst.service.impl.go", "UpdateTemplateServiceLst", "修改多个TemplateServiceLst")

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

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

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