// 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              : AttributeValst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfAttributeValst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.AttributeValst_AttrNr.UrlParameterName:     grmi.NewConditionItem(meta.AttributeValst_AttrNr, 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_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-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfAttributeValstAndPaging = grmi.NewCondition(
	DefaultConditionOfAttributeValst.Items,
	grmi.NewPaging(),
)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AttributeValstService.InsertOne
 *
 ******************************************************************************/
func (impl *AttributeValstServiceImplement) InsertOne(user *models.Usertab, entity *model.AttributeValst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAttributeValstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AttrNr, 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_LF_MOM_BACKEND/services/pm/AttributeValstService.DeleteOne
 *
 ******************************************************************************/
func (impl *AttributeValstServiceImplement) DeleteOne(user *models.Usertab, attrNr int, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AttributeValstService.SelectOne
 *
 ******************************************************************************/
func (impl *AttributeValstServiceImplement) SelectOne(user *models.Usertab, attrNr int, pos int) (*model.AttributeValst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AttributeValstService.UpdateOne
 *
 ******************************************************************************/
func (impl *AttributeValstServiceImplement) UpdateOne(user *models.Usertab, entity *model.AttributeValst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAttributeValstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AttrNr, 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_LF_MOM_BACKEND/services/pm/AttributeValstService.Insert
 *
 ******************************************************************************/
func (impl *AttributeValstServiceImplement) Insert(user *models.Usertab, entities *[]model.AttributeValst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AttrNr, 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_LF_MOM_BACKEND/services/pm/AttributeValstService.Delete
 *
 ******************************************************************************/
func (impl *AttributeValstServiceImplement) Delete(user *models.Usertab, entities *[]model.AttributeValst) error {

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

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

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

	grmi.Log(user, "/services/pm/implments/PM_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.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/AttributeValstService.Update
 *
 ******************************************************************************/
func (impl *AttributeValstServiceImplement) Update(user *models.Usertab, entities *[]model.AttributeValst) error {

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

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

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

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