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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/pm"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/pm"
	model "LAPP_LF_MOM_BACKEND/models/pm"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : AssetTemplateLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfAssetTemplateLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.AssetTemplateLst_AssetNr.UrlParameterName:        grmi.NewConditionItem(meta.AssetTemplateLst_AssetNr, grmi.Equal, true),
		meta.AssetTemplateLst_MainTemplateNr.UrlParameterName: grmi.NewConditionItem(meta.AssetTemplateLst_MainTemplateNr, grmi.Equal, true),
		meta.AssetTemplateLst_Pos.UrlParameterName:            grmi.NewConditionItem(meta.AssetTemplateLst_Pos, grmi.Equal, false),
		meta.AssetTemplateLst_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.AssetTemplateLst_LastModify, grmi.Approximate, false),
		meta.AssetTemplateLst_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.AssetTemplateLst_LastUser, grmi.Equal, false),
		meta.AssetTemplateLst_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.AssetTemplateLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : AssetTemplateLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfAssetTemplateLstAndPaging = grmi.NewCondition(
	DefaultConditionOfAssetTemplateLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : AssetTemplateLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : AssetTemplateLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
type AssetTemplateLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewAssetTemplateLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个AssetTemplateLstDAOImplement实例
 *
 * @Return Value             : AssetTemplateLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
func NewAssetTemplateLstServiceImplement() *AssetTemplateLstServiceImplement {
	return &AssetTemplateLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetTemplateLstService.InsertOne
 *
 ******************************************************************************/
func (impl *AssetTemplateLstServiceImplement) InsertOne(user *models.Usertab, entity *model.AssetTemplateLst) error {

	grmi.Log(user, "/services/pm/implments/PM_AssetTemplateLst.service.impl.go", "InsertOneAssetTemplateLst", "插入一个AssetTemplateLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetTemplateLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *AssetTemplateLstServiceImplement) DeleteOne(user *models.Usertab, assetNr int, mainTemplateNr int) error {

	grmi.Log(user, "/services/pm/implments/PM_AssetTemplateLst.service.impl.go", "DeleteOneAssetTemplateLst", "删除一个AssetTemplateLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetTemplateLstService.SelectOne
 *
 ******************************************************************************/
func (impl *AssetTemplateLstServiceImplement) SelectOne(user *models.Usertab, assetNr int, mainTemplateNr int) (*model.AssetTemplateLst, error) {

	grmi.Log(user, "/services/pm/implments/PM_AssetTemplateLst.service.impl.go", "SelectOneAssetTemplateLst", "查询一个AssetTemplateLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAssetTemplateLstDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(assetNr, mainTemplateNr)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetTemplateLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *AssetTemplateLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.AssetTemplateLst) error {

	grmi.Log(user, "/services/pm/implments/PM_AssetTemplateLst.service.impl.go", "UpdateOneAssetTemplateLst", "修改一个AssetTemplateLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAssetTemplateLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AssetNr, entity.MainTemplateNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetTemplateLstService.Insert
 *
 ******************************************************************************/
func (impl *AssetTemplateLstServiceImplement) Insert(user *models.Usertab, entities *[]model.AssetTemplateLst) error {

	grmi.Log(user, "/services/pm/implments/PM_AssetTemplateLst.service.impl.go", "InsertAssetTemplateLst", "插入多个AssetTemplateLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAssetTemplateLstDAO(session, user.Pid, user.Userid)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetTemplateLstService.Delete
 *
 ******************************************************************************/
func (impl *AssetTemplateLstServiceImplement) Delete(user *models.Usertab, entities *[]model.AssetTemplateLst) error {

	grmi.Log(user, "/services/pm/implments/PM_AssetTemplateLst.service.impl.go", "DeleteAssetTemplateLst", "删除多个AssetTemplateLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAssetTemplateLstDAO(session, user.Pid, user.Userid)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetTemplateLstService.Select
 *
 ******************************************************************************/
func (impl *AssetTemplateLstServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/pm/implments/PM_AssetTemplateLst.service.impl.go", "SelectAssetTemplateLst", "查询AssetTemplateLst")

	condition := DefaultConditionOfAssetTemplateLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewAssetTemplateLstDAO(session, user.Pid, user.Userid)
	if condition.Fill(urlParameters) {
		result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	} else {
		result, err := dao.Select(predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetTemplateLstService.Update
 *
 ******************************************************************************/
func (impl *AssetTemplateLstServiceImplement) Update(user *models.Usertab, entities *[]model.AssetTemplateLst) error {

	grmi.Log(user, "/services/pm/implments/PM_AssetTemplateLst.service.impl.go", "UpdateAssetTemplateLst", "修改多个AssetTemplateLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAssetTemplateLstDAO(session, user.Pid, user.Userid)

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

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