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

package implments

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

/******************************************************************************
 *
 * @Description              : AttributeGroupValst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfAttributeGroupValst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.AttributeGroupValst_AttrCode.UrlParameterName:    grmi.NewConditionItem(meta.AttributeGroupValst_AttrCode, grmi.Equal, true),
		meta.AttributeGroupValst_AttrGrpCode.UrlParameterName: grmi.NewConditionItem(meta.AttributeGroupValst_AttrGrpCode, grmi.Equal, true),
		meta.AttributeGroupValst_Pos.UrlParameterName:         grmi.NewConditionItem(meta.AttributeGroupValst_Pos, grmi.Equal, false),
		meta.AttributeGroupValst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.AttributeGroupValst_LastModify, grmi.Approximate, false),
		meta.AttributeGroupValst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.AttributeGroupValst_LastUser, grmi.Equal, false),
		meta.AttributeGroupValst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.AttributeGroupValst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/AttributeGroupValstService.InsertOne
 *
 ******************************************************************************/
func (impl *AttributeGroupValstServiceImplement) InsertOne(user *models.Usertab, entity *model.AttributeGroupValst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAttributeGroupValstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AttrCode, entity.AttrGrpCode)
	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/base/AttributeGroupValstService.DeleteOne
 *
 ******************************************************************************/
func (impl *AttributeGroupValstServiceImplement) DeleteOne(user *models.Usertab, attrCode int, attrGrpCode int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/AttributeGroupValstService.SelectOne
 *
 ******************************************************************************/
func (impl *AttributeGroupValstServiceImplement) SelectOne(user *models.Usertab, attrCode int, attrGrpCode int) (*model.AttributeGroupValst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/AttributeGroupValstService.UpdateOne
 *
 ******************************************************************************/
func (impl *AttributeGroupValstServiceImplement) UpdateOne(user *models.Usertab, entity *model.AttributeGroupValst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAttributeGroupValstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AttrCode, entity.AttrGrpCode)
	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/base/AttributeGroupValstService.Insert
 *
 ******************************************************************************/
func (impl *AttributeGroupValstServiceImplement) Insert(user *models.Usertab, entities *[]model.AttributeGroupValst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AttrCode, entity.AttrGrpCode)
		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/base/AttributeGroupValstService.Delete
 *
 ******************************************************************************/
func (impl *AttributeGroupValstServiceImplement) Delete(user *models.Usertab, entities *[]model.AttributeGroupValst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AttrCode, entity.AttrGrpCode)
		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/base/AttributeGroupValstService.Select
 *
 ******************************************************************************/
func (impl *AttributeGroupValstServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfAttributeGroupValstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewAttributeGroupValstDAO(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/base/AttributeGroupValstService.Update
 *
 ******************************************************************************/
func (impl *AttributeGroupValstServiceImplement) Update(user *models.Usertab, entities *[]model.AttributeGroupValst) error {

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

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

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

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