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

package implments

import (
	baseDal "LAPP_ACURA_MOM_BACKEND/dao/base"
	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              : AssetType的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-08 13:20:42
 *
 ******************************************************************************/
var DefaultConditionOfAssetType = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.AssetType_AssetType.UrlParameterName:  grmi.NewConditionItem(meta.AssetType_AssetType, grmi.Equal, true),
		meta.AssetType_Descr.UrlParameterName:      grmi.NewConditionItem(meta.AssetType_Descr, grmi.Equal, false),
		meta.AssetType_Bitmap.UrlParameterName:     grmi.NewConditionItem(meta.AssetType_Bitmap, grmi.Equal, false),
		meta.AssetType_AstypPara1.UrlParameterName: grmi.NewConditionItem(meta.AssetType_AstypPara1, grmi.Equal, false),
		meta.AssetType_AstypPara2.UrlParameterName: grmi.NewConditionItem(meta.AssetType_AstypPara2, grmi.Equal, false),
		meta.AssetType_AstypPara3.UrlParameterName: grmi.NewConditionItem(meta.AssetType_AstypPara3, grmi.Equal, false),
		meta.AssetType_AstypPara4.UrlParameterName: grmi.NewConditionItem(meta.AssetType_AstypPara4, grmi.Equal, false),
		meta.AssetType_LastModify.UrlParameterName: grmi.NewConditionItem(meta.AssetType_LastModify, grmi.Approximate, false),
		meta.AssetType_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.AssetType_LastUser, grmi.Equal, false),
		meta.AssetType_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.AssetType_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pm/AssetTypeService.InsertOne
 *
 ******************************************************************************/
func (impl *AssetTypeServiceImplement) InsertOne(user *global.User, entity *model.AssetType) error {

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

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

	snrDao := baseDal.NewSnrDAO(session, user.PlantNr, user.UserId)
	Assettypeid, err := snrDao.GetNextSnr("Assettypeid")
	if err != nil {
		return err
	}
	entity.AssetType = Assettypeid

	record, err := dao.SelectOne(entity.AssetType)
	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/AssetTypeService.DeleteOne
 *
 ******************************************************************************/
func (impl *AssetTypeServiceImplement) DeleteOne(user *global.User, assetType string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pm/AssetTypeService.SelectOne
 *
 ******************************************************************************/
func (impl *AssetTypeServiceImplement) SelectOne(user *global.User, assetType string) (*model.AssetType, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pm/AssetTypeService.UpdateOne
 *
 ******************************************************************************/
func (impl *AssetTypeServiceImplement) UpdateOne(user *global.User, entity *model.AssetType) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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