// 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              : RPDashBoardLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-06 14:56:42
 *
 ******************************************************************************/
var DefaultConditionOfRPDashBoardLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.RPDashBoardLst_DashboardId.UrlParameterName: grmi.NewConditionItem(meta.RPDashBoardLst_DashboardId, grmi.Equal, true),
		meta.RPDashBoardLst_Pos.UrlParameterName:         grmi.NewConditionItem(meta.RPDashBoardLst_Pos, grmi.Equal, true),
		meta.RPDashBoardLst_ParamName.UrlParameterName:   grmi.NewConditionItem(meta.RPDashBoardLst_ParamName, grmi.Equal, false),
		meta.RPDashBoardLst_Descr.UrlParameterName:       grmi.NewConditionItem(meta.RPDashBoardLst_Descr, grmi.Equal, false),
		meta.RPDashBoardLst_IntParam1.UrlParameterName:   grmi.NewConditionItem(meta.RPDashBoardLst_IntParam1, grmi.Equal, false),
		meta.RPDashBoardLst_IntParam2.UrlParameterName:   grmi.NewConditionItem(meta.RPDashBoardLst_IntParam2, grmi.Equal, false),
		meta.RPDashBoardLst_FloatParam1.UrlParameterName: grmi.NewConditionItem(meta.RPDashBoardLst_FloatParam1, grmi.Equal, false),
		meta.RPDashBoardLst_FloatParam2.UrlParameterName: grmi.NewConditionItem(meta.RPDashBoardLst_FloatParam2, grmi.Equal, false),
		meta.RPDashBoardLst_CharParam1.UrlParameterName:  grmi.NewConditionItem(meta.RPDashBoardLst_CharParam1, grmi.Equal, false),
		meta.RPDashBoardLst_CharParam2.UrlParameterName:  grmi.NewConditionItem(meta.RPDashBoardLst_CharParam2, grmi.Equal, false),
		meta.RPDashBoardLst_CharParam3.UrlParameterName:  grmi.NewConditionItem(meta.RPDashBoardLst_CharParam3, grmi.Equal, false),
		meta.RPDashBoardLst_CharParam4.UrlParameterName:  grmi.NewConditionItem(meta.RPDashBoardLst_CharParam4, grmi.Equal, false),
		meta.RPDashBoardLst_IntParam3.UrlParameterName:   grmi.NewConditionItem(meta.RPDashBoardLst_IntParam3, grmi.Equal, false),
		meta.RPDashBoardLst_IntParam4.UrlParameterName:   grmi.NewConditionItem(meta.RPDashBoardLst_IntParam4, grmi.Equal, false),
		meta.RPDashBoardLst_IntParam5.UrlParameterName:   grmi.NewConditionItem(meta.RPDashBoardLst_IntParam5, grmi.Equal, false),
		meta.RPDashBoardLst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.RPDashBoardLst_LastModify, grmi.Approximate, false),
		meta.RPDashBoardLst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.RPDashBoardLst_LastUser, grmi.Equal, false),
		meta.RPDashBoardLst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.RPDashBoardLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : RPDashBoardLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-06 14:56:42
 *
 ******************************************************************************/
var DefaultConditionOfRPDashBoardLstAndPaging = grmi.NewCondition(
	DefaultConditionOfRPDashBoardLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : RPDashBoardLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : RPDashBoardLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-06 14:56:42
 *
 ******************************************************************************/
type RPDashBoardLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewRPDashBoardLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个RPDashBoardLstDAOImplement实例
 *
 * @Return Value             : RPDashBoardLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-06 14:56:42
 *
 ******************************************************************************/
func NewRPDashBoardLstServiceImplement() *RPDashBoardLstServiceImplement {
	return &RPDashBoardLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardLstService.InsertOne
 *
 ******************************************************************************/
func (impl *RPDashBoardLstServiceImplement) InsertOne(user *global.User, entity *model.RPDashBoardLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRPDashBoardLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.DashboardId, 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 leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *RPDashBoardLstServiceImplement) DeleteOne(user *global.User, dashboardId int, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardLstService.SelectOne
 *
 ******************************************************************************/
func (impl *RPDashBoardLstServiceImplement) SelectOne(user *global.User, dashboardId int, pos int) (*model.RPDashBoardLst, error) {

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

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

/******************************************************************************
 *
 * @Reference leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *RPDashBoardLstServiceImplement) UpdateOne(user *global.User, entity *model.RPDashBoardLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRPDashBoardLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.DashboardId, 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 leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardLstService.Insert
 *
 ******************************************************************************/
func (impl *RPDashBoardLstServiceImplement) Insert(user *global.User, entities *[]model.RPDashBoardLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DashboardId, 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 leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardLstService.Delete
 *
 ******************************************************************************/
func (impl *RPDashBoardLstServiceImplement) Delete(user *global.User, entities *[]model.RPDashBoardLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DashboardId, 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 leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardLstService.Select
 *
 ******************************************************************************/
func (impl *RPDashBoardLstServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.RPDashBoardLst, error) {

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

	condition := DefaultConditionOfRPDashBoardLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewRPDashBoardLstDAO(session, user.PlantNr, user.UserId)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardLstService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *RPDashBoardLstServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

	condition := DefaultConditionOfRPDashBoardLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewRPDashBoardLstDAO(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 leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardLstService.Update
 *
 ******************************************************************************/
func (impl *RPDashBoardLstServiceImplement) Update(user *global.User, entities *[]model.RPDashBoardLst) error {

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

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

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

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