// 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              : AttributeValst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfAttributeValst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.AttributeValst_AttrCode.UrlParameterName:   grmi.NewConditionItem(meta.AttributeValst_AttrCode, grmi.Equal, true),
		meta.AttributeValst_Pos.UrlParameterName:        grmi.NewConditionItem(meta.AttributeValst_Pos, grmi.Equal, true),
		meta.AttributeValst_AttrValue.UrlParameterName:  grmi.NewConditionItem(meta.AttributeValst_AttrValue, grmi.Equal, false),
		meta.AttributeValst_Descr.UrlParameterName:      grmi.NewConditionItem(meta.AttributeValst_Descr, grmi.Equal, false),
		meta.AttributeValst_ShortCode.UrlParameterName:  grmi.NewConditionItem(meta.AttributeValst_ShortCode, grmi.Equal, false),
		meta.AttributeValst_Valformat.UrlParameterName:  grmi.NewConditionItem(meta.AttributeValst_Valformat, grmi.Equal, false),
		meta.AttributeValst_LastModify.UrlParameterName: grmi.NewConditionItem(meta.AttributeValst_LastModify, grmi.Approximate, false),
		meta.AttributeValst_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.AttributeValst_LastUser, grmi.Equal, false),
		meta.AttributeValst_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.AttributeValst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : AttributeValst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfAttributeValstAndPaging = grmi.NewCondition(
	DefaultConditionOfAttributeValst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : AttributeValstServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : AttributeValst的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-23 10:43:27
*
******************************************************************************/
type AttributeValstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewAttributeValstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个AttributeValstDAOImplement实例
 *
 * @Return Value             : AttributeValstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewAttributeValstServiceImplement() *AttributeValstServiceImplement {
	return &AttributeValstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/AttributeValstService.InsertOne
 *
 ******************************************************************************/
func (impl *AttributeValstServiceImplement) InsertOne(user *global.User, entity *model.AttributeValst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAttributeValstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.AttrCode, entity.Pos)
	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/base/AttributeValstService.DeleteOne
 *
 ******************************************************************************/
func (impl *AttributeValstServiceImplement) DeleteOne(user *global.User, attrCode int, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/AttributeValstService.SelectOne
 *
 ******************************************************************************/
func (impl *AttributeValstServiceImplement) SelectOne(user *global.User, attrCode int, pos int) (*model.AttributeValst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/AttributeValstService.UpdateOne
 *
 ******************************************************************************/
func (impl *AttributeValstServiceImplement) UpdateOne(user *global.User, entity *model.AttributeValst) error {

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

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

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

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

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

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AttrCode, entity.Pos)
		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/AttributeValstService.Select
 *
 ******************************************************************************/
func (impl *AttributeValstServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfAttributeValstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewAttributeValstDAO(session, user.PlantNr, 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_ACURA_MOM_BACKEND/services/base/AttributeValstService.Update
 *
 ******************************************************************************/
func (impl *AttributeValstServiceImplement) Update(user *global.User, entities *[]model.AttributeValst) error {

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

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

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

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