// 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              : ServiceAttrLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfServiceAttrLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ServiceAttrLst_MainServiceNr.UrlParameterName: grmi.NewConditionItem(meta.ServiceAttrLst_MainServiceNr, grmi.Equal, true),
		meta.ServiceAttrLst_Pos.UrlParameterName:           grmi.NewConditionItem(meta.ServiceAttrLst_Pos, grmi.Equal, true),
		meta.ServiceAttrLst_Descr.UrlParameterName:         grmi.NewConditionItem(meta.ServiceAttrLst_Descr, grmi.Equal, false),
		meta.ServiceAttrLst_AttrNr.UrlParameterName:        grmi.NewConditionItem(meta.ServiceAttrLst_AttrNr, grmi.Equal, false),
		meta.ServiceAttrLst_Parameter.UrlParameterName:     grmi.NewConditionItem(meta.ServiceAttrLst_Parameter, grmi.Equal, false),
		meta.ServiceAttrLst_ParaValue.UrlParameterName:     grmi.NewConditionItem(meta.ServiceAttrLst_ParaValue, grmi.Equal, false),
		meta.ServiceAttrLst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.ServiceAttrLst_LastModify, grmi.Approximate, false),
		meta.ServiceAttrLst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.ServiceAttrLst_LastUser, grmi.Equal, false),
		meta.ServiceAttrLst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.ServiceAttrLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/ServiceAttrLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ServiceAttrLstServiceImplement) InsertOne(user *models.Usertab, entity *model.ServiceAttrLst) error {

	grmi.Log(user, "/services/pm/implments/PM_ServiceAttrLst.service.impl.go", "InsertOneServiceAttrLst", "插入一个ServiceAttrLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewServiceAttrLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.MainServiceNr, 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/ServiceAttrLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ServiceAttrLstServiceImplement) DeleteOne(user *models.Usertab, mainServiceNr int, pos int) error {

	grmi.Log(user, "/services/pm/implments/PM_ServiceAttrLst.service.impl.go", "DeleteOneServiceAttrLst", "删除一个ServiceAttrLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/ServiceAttrLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ServiceAttrLstServiceImplement) SelectOne(user *models.Usertab, mainServiceNr int, pos int) (*model.ServiceAttrLst, error) {

	grmi.Log(user, "/services/pm/implments/PM_ServiceAttrLst.service.impl.go", "SelectOneServiceAttrLst", "查询一个ServiceAttrLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/ServiceAttrLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ServiceAttrLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.ServiceAttrLst) error {

	grmi.Log(user, "/services/pm/implments/PM_ServiceAttrLst.service.impl.go", "UpdateOneServiceAttrLst", "修改一个ServiceAttrLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewServiceAttrLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.MainServiceNr, 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/ServiceAttrLstService.Insert
 *
 ******************************************************************************/
func (impl *ServiceAttrLstServiceImplement) Insert(user *models.Usertab, entities *[]model.ServiceAttrLst) error {

	grmi.Log(user, "/services/pm/implments/PM_ServiceAttrLst.service.impl.go", "InsertServiceAttrLst", "插入多个ServiceAttrLst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.MainServiceNr, 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/ServiceAttrLstService.Delete
 *
 ******************************************************************************/
func (impl *ServiceAttrLstServiceImplement) Delete(user *models.Usertab, entities *[]model.ServiceAttrLst) error {

	grmi.Log(user, "/services/pm/implments/PM_ServiceAttrLst.service.impl.go", "DeleteServiceAttrLst", "删除多个ServiceAttrLst")

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

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

	grmi.Log(user, "/services/pm/implments/PM_ServiceAttrLst.service.impl.go", "SelectServiceAttrLst", "查询ServiceAttrLst")

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

	grmi.Log(user, "/services/pm/implments/PM_ServiceAttrLst.service.impl.go", "UpdateServiceAttrLst", "修改多个ServiceAttrLst")

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

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

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